-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembedded-api.js
More file actions
165 lines (141 loc) · 3.96 KB
/
embedded-api.js
File metadata and controls
165 lines (141 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(function (globals) {
class EmbeddedNetsBloxAPI extends EventTarget {
constructor(element) {
super();
this.element = element;
this._requests = {};
this._listeners = {};
this._actionListeners = [];
const receiveMessage = (event) => {
const data = event.data;
const { type } = data;
if (type === "reply") {
const { id } = data;
const request = this._requests[id];
if (request) {
request.resolve(data);
delete this._requests[id];
}
} else if (type === "event") {
const { eventType, detail } = data;
this.dispatchEvent(new CustomEvent(eventType, { detail }));
}
};
window.addEventListener("message", receiveMessage, false);
}
async getProjectXML() {
const reqData = { type: "export-project" };
const data = await this.reqReply(reqData);
return data.xml;
}
async getUsername() {
const reqData = { type: "get-username" };
const data = await this.reqReply(reqData);
return data.username;
}
async addActionListener(fn) {
const callback = (event) => fn(event.detail);
this._actionListeners.push([fn, callback]);
await this.addEventListener("action", callback);
return callback;
}
async removeActionListener(fn) {
const index = this._actionListeners.findIndex((pair) => pair[0] === fn);
const callback =
index > -1 ? this._actionListeners.splice(index, 1).pop()[1] : null;
return this.removeEventListener("action", callback);
}
async addEventListener(type, fn) {
const listenerId = this.genUuid();
this._listeners[listenerId] = fn;
await this.reqReply({
type: "add-listener",
eventType: type,
listenerId,
});
return super.addEventListener(type, fn);
}
async removeEventListener(type, fn) {
const listenerId = Object.keys(this._listeners).find(
(id) => this._listeners[id] === fn
);
await this.reqReply({
type: "remove-listener",
eventType: type,
listenerId,
});
return super.removeEventListener(type, fn);
}
async import(name, content) {
this.call({
type: "import",
name: name,
content: content,
});
}
async saveToCloud(name) {
this.call({
type: "save-cloud",
name: name,
});
}
async publishProject(name, publish) {
this.call({
type: "publish",
name: name,
publish: publish,
});
}
async runProject() {
this.call({
type: "run-script",
});
}
async getGlobalVariables() {
const reqData = { type: "global-variables" };
const data = await this.reqReply(reqData);
return data.variables;
}
async getStageImage() {
const reqData = { type: "stage-image" };
const data = await this.reqReply(reqData);
return data.stageImage;
}
async deleteProject(name) {
this.call({
type: "delete-project",
name: name,
});
}
genUuid() {
return Date.now() + Math.floor(Math.random() * 1000);
}
reqReply(reqData) {
const id = this.genUuid();
const deferred = defer();
this._requests[id] = deferred;
reqData.id = id;
this.call(reqData);
setTimeout(() => {
const deferred = this._requests[id];
if (deferred) {
deferred.reject(new Error("Timeout Exceeded"));
delete this._requests[id];
}
}, 5000);
return deferred.promise;
}
call(msgData) {
this.element.contentWindow.postMessage(msgData, "*");
}
}
function defer() {
const deferred = {};
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
}
globals.EmbeddedNetsBloxAPI = EmbeddedNetsBloxAPI;
})(this);