@metapages/metapage 0.13.9 → 0.13.10-alpha1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/index.mjs +2157 -0
  2. package/dist/index.mjs.map +1 -0
  3. package/dist/metapage/Metaframe.d.ts +2 -2
  4. package/dist/metapage/Metaframe.d.ts.map +1 -1
  5. package/dist/metapage/MetapageIFrameRpcClient.d.ts +5 -5
  6. package/dist/metapage/MetapageIFrameRpcClient.d.ts.map +1 -1
  7. package/package.json +10 -13
  8. package/src/metapage/Metaframe.ts +30 -21
  9. package/src/metapage/MetapageIFrameRpcClient.ts +46 -30
  10. package/dist/browser/metaframe/index.js +0 -30
  11. package/dist/browser/metaframe/index.js.map +0 -1
  12. package/dist/browser/metapage/index.js +0 -36
  13. package/dist/browser/metapage/index.js.map +0 -1
  14. package/dist/index.js +0 -8
  15. package/dist/index.js.map +0 -1
  16. package/dist/metapage/Constants.js +0 -7
  17. package/dist/metapage/Constants.js.map +0 -1
  18. package/dist/metapage/Metaframe.js +0 -346
  19. package/dist/metapage/Metaframe.js.map +0 -1
  20. package/dist/metapage/Metapage.js +0 -797
  21. package/dist/metapage/Metapage.js.map +0 -1
  22. package/dist/metapage/MetapageIFrameRpcClient.js +0 -363
  23. package/dist/metapage/MetapageIFrameRpcClient.js.map +0 -1
  24. package/dist/metapage/MetapageTools.js +0 -273
  25. package/dist/metapage/MetapageTools.js.map +0 -1
  26. package/dist/metapage/Shared.js +0 -32
  27. package/dist/metapage/Shared.js.map +0 -1
  28. package/dist/metapage/data.js +0 -112
  29. package/dist/metapage/data.js.map +0 -1
  30. package/dist/metapage/errors/MissingMetaframeJson.js +0 -7
  31. package/dist/metapage/errors/MissingMetaframeJson.js.map +0 -1
  32. package/dist/metapage/errors/RootMetapageError.js +0 -7
  33. package/dist/metapage/errors/RootMetapageError.js.map +0 -1
  34. package/dist/metapage/jsonrpc2.js +0 -25
  35. package/dist/metapage/jsonrpc2.js.map +0 -1
  36. package/dist/metapage/v0_0_1/all.js +0 -16
  37. package/dist/metapage/v0_0_1/all.js.map +0 -1
  38. package/dist/metapage/v0_1_0/all.js +0 -16
  39. package/dist/metapage/v0_1_0/all.js.map +0 -1
  40. package/dist/metapage/v0_2/all.js +0 -3
  41. package/dist/metapage/v0_2/all.js.map +0 -1
  42. package/dist/metapage/v0_3/JsonRpcMethods.js +0 -20
  43. package/dist/metapage/v0_3/JsonRpcMethods.js.map +0 -1
  44. package/dist/metapage/v0_3/all.js +0 -2
  45. package/dist/metapage/v0_3/all.js.map +0 -1
  46. package/dist/metapage/v0_4/core.js +0 -2
  47. package/dist/metapage/v0_4/core.js.map +0 -1
  48. package/dist/metapage/v0_4/events.js +0 -12
  49. package/dist/metapage/v0_4/events.js.map +0 -1
  50. package/dist/metapage/v0_4/index.js +0 -7
  51. package/dist/metapage/v0_4/index.js.map +0 -1
  52. package/dist/metapage/v0_4/jsonrpc.js +0 -20
  53. package/dist/metapage/v0_4/jsonrpc.js.map +0 -1
  54. package/dist/metapage/v0_4/metaframe.js +0 -2
  55. package/dist/metapage/v0_4/metaframe.js.map +0 -1
  56. package/dist/metapage/v0_4/metapage.js +0 -2
  57. package/dist/metapage/v0_4/metapage.js.map +0 -1
  58. package/dist/metapage/v0_4/versions.js +0 -17
  59. package/dist/metapage/v0_4/versions.js.map +0 -1
@@ -1,797 +0,0 @@
1
- import * as objectHash from "object-hash";
2
- import { VERSION_METAPAGE, METAPAGE_KEY_STATE, METAPAGE_KEY_DEFINITION, } from "./Constants";
3
- import { JsonRpcMethodsFromChild, } from "./v0_4";
4
- import { log as MetapageToolsLog, getMatchingVersion, generateMetapageId, existsAnyUrlParam, pageLoaded, isDebugFromUrlsParams, convertMetapageDefinitionToCurrentVersion, } from "./MetapageTools";
5
- import { INITIAL_NULL_METAPAGE_DEFINITION, MetapageShared } from "./Shared";
6
- import { MetapageEvents, } from "./v0_4/events";
7
- import { MetapageIFrameRpcClient } from "./MetapageIFrameRpcClient";
8
- import { deserializeInputs, serializeInputs } from "./data";
9
- export var MetapageEventStateType;
10
- (function (MetapageEventStateType) {
11
- MetapageEventStateType["all"] = "all";
12
- MetapageEventStateType["delta"] = "delta";
13
- })(MetapageEventStateType || (MetapageEventStateType = {}));
14
- const emptyState = () => {
15
- return {
16
- metaframes: {
17
- inputs: {},
18
- outputs: {},
19
- },
20
- plugins: {
21
- inputs: {},
22
- outputs: {},
23
- },
24
- };
25
- };
26
- export const getLibraryVersionMatching = (version) => {
27
- return getMatchingVersion(version);
28
- };
29
- export const matchPipe = (outputPipeName, inputPipeName) => {
30
- if (!inputPipeName || inputPipeName === "*") {
31
- return true;
32
- }
33
- if (outputPipeName === inputPipeName) {
34
- return true;
35
- }
36
- if (inputPipeName.endsWith("*")) {
37
- return outputPipeName.startsWith(inputPipeName.slice(0, -1));
38
- }
39
- if (inputPipeName.startsWith("*")) {
40
- return outputPipeName.endsWith(inputPipeName.slice(1));
41
- }
42
- return false;
43
- };
44
- const CONSOLE_BACKGROUND_COLOR_DEFAULT = "bcbcbc";
45
- export class Metapage extends MetapageShared {
46
- constructor(opts) {
47
- super();
48
- this._state = emptyState();
49
- this._metaframes = {};
50
- this._plugins = {};
51
- this._pluginOrder = [];
52
- this.debug = isDebugFromUrlsParams();
53
- this._internalReceivedMessageCounter = 0;
54
- this._cachedInputLookupMap = {};
55
- this._inputMap = {};
56
- this._id = opts && opts.id ? opts.id : generateMetapageId();
57
- this._consoleBackgroundColor =
58
- opts && opts.color ? opts.color : CONSOLE_BACKGROUND_COLOR_DEFAULT;
59
- this.addPipe = this.addPipe.bind(this);
60
- this.dispose = this.dispose.bind(this);
61
- this.addMetaframe = this.addMetaframe.bind(this);
62
- this.addPlugin = this.addPlugin.bind(this);
63
- this.getInputsFromOutput = this.getInputsFromOutput.bind(this);
64
- this.getMetaframe = this.getMetaframe.bind(this);
65
- this.getMetaframeIds = this.getMetaframeIds.bind(this);
66
- this.getMetaframeOrPlugin = this.getMetaframeOrPlugin.bind(this);
67
- this.getMetaframes = this.getMetaframes.bind(this);
68
- this.getPlugin = this.getPlugin.bind(this);
69
- this.getPluginIds = this.getPluginIds.bind(this);
70
- this.getState = this.getState.bind(this);
71
- this.getStateMetaframes = this.getStateMetaframes.bind(this);
72
- this.isValidJSONRpcMessage = this.isValidJSONRpcMessage.bind(this);
73
- this.log = this.log.bind(this);
74
- this.logInternal = this.logInternal.bind(this);
75
- this.metaframeIds = this.metaframeIds.bind(this);
76
- this.metaframes = this.metaframes.bind(this);
77
- this.onMessage = this.onMessage.bind(this);
78
- this.pluginIds = this.pluginIds.bind(this);
79
- this.plugins = this.plugins.bind(this);
80
- this.removeAll = this.removeAll.bind(this);
81
- this.removeMetaframe = this.removeMetaframe.bind(this);
82
- this.removePlugin = this.removePlugin.bind(this);
83
- this.setDebugFromUrlParams = this.setDebugFromUrlParams.bind(this);
84
- this.setDefinition = this.setDefinition.bind(this);
85
- this.setInput = this.setInput.bind(this);
86
- this.setInputs = this.setInputs.bind(this);
87
- this.setInputStateOnly = this.setInputStateOnly.bind(this);
88
- this.setMetaframeClientInputAndSentClientEvent =
89
- this.setMetaframeClientInputAndSentClientEvent.bind(this);
90
- this.setOutputStateOnly = this.setOutputStateOnly.bind(this);
91
- this.setState = this.setState.bind(this);
92
- this.isDisposed = this.isDisposed.bind(this);
93
- this.updatePluginsWithDefinition =
94
- this.updatePluginsWithDefinition.bind(this);
95
- this._emitDefinitionEvent = this._emitDefinitionEvent.bind(this);
96
- pageLoaded().then(() => {
97
- if (this.isDisposed()) {
98
- return;
99
- }
100
- window.addEventListener("message", this.onMessage);
101
- this.log("Initialized");
102
- });
103
- }
104
- static from(metaPageDef, inputs) {
105
- if (metaPageDef == null) {
106
- throw "Metapage definition cannot be null";
107
- }
108
- if (typeof metaPageDef === "string") {
109
- try {
110
- metaPageDef = JSON.parse(metaPageDef);
111
- }
112
- catch (err) {
113
- throw "Cannot parse into JSON:\n${metaPageDef}";
114
- }
115
- }
116
- var metapage = new Metapage();
117
- return metapage.setDefinition(metaPageDef);
118
- }
119
- isDisposed() {
120
- return this._metaframes === undefined;
121
- }
122
- addListenerReturnDisposer(event, listener) {
123
- super.addListener(event, listener);
124
- const disposer = () => {
125
- super.removeListener(event, listener);
126
- };
127
- return disposer;
128
- }
129
- setDebugFromUrlParams() {
130
- this.debug = existsAnyUrlParam(["MP_DEBUG", "DEBUG", "debug", "mp_debug"]);
131
- return this;
132
- }
133
- getState() {
134
- return this._state;
135
- }
136
- setState(newState) {
137
- this._state = newState;
138
- this.getMetaframeIds().forEach((metaframeId) => {
139
- this.getMetaframe(metaframeId).setInputs(this._state.metaframes.inputs[metaframeId]);
140
- this.getMetaframe(metaframeId).setOutputs(this._state.metaframes.outputs[metaframeId]);
141
- });
142
- this.getPluginIds().forEach((pluginId) => {
143
- this.getPlugin(pluginId).setInputs(this._state.plugins.inputs[pluginId]);
144
- this.getPlugin(pluginId).setOutputs(this._state.plugins.outputs[pluginId]);
145
- });
146
- this.emit(MetapageEvents.State, this._state);
147
- }
148
- getStateMetaframes() {
149
- return this._state.metaframes;
150
- }
151
- getDefinition() {
152
- return this._definition;
153
- }
154
- setDefinition(def, state) {
155
- if (!def.version) {
156
- throw "Metapage definition must have a version";
157
- }
158
- const newDefinition = convertMetapageDefinitionToCurrentVersion(def);
159
- if (newDefinition.metaframes) {
160
- Object.keys(newDefinition.metaframes).forEach((metaframeId) => {
161
- if (newDefinition.plugins &&
162
- newDefinition.plugins.includes(metaframeId)) {
163
- this.emitErrorMessage(`Plugin with url=${metaframeId} matches metaframe. Metaframe ids and plugin urls are not allowed to collide`);
164
- throw `Plugin with url=${metaframeId} matches metaframe. Metaframe ids and plugin urls are not allowed to collide`;
165
- }
166
- var metaframeDefinition = newDefinition.metaframes[metaframeId];
167
- if (typeof metaframeDefinition !== "object") {
168
- this.emitErrorMessage(`Metaframe "${metaframeId}" is not an object`);
169
- throw `Metaframe "${metaframeId}" is not an object`;
170
- }
171
- if (!metaframeDefinition.url) {
172
- this.emitErrorMessage(`Metaframe "${metaframeId}" missing field: url`);
173
- throw `Metaframe "${metaframeId}" missing field: url`;
174
- }
175
- });
176
- }
177
- const previousDefinition = this._definition;
178
- this._definition = newDefinition;
179
- Object.keys(this._metaframes).forEach((metaframeId) => {
180
- if (!newDefinition.metaframes || !newDefinition.metaframes[metaframeId]) {
181
- this.removeMetaframe(metaframeId);
182
- }
183
- });
184
- Object.keys(this._plugins).forEach((url) => {
185
- if (newDefinition.plugins && !newDefinition.plugins.includes(url)) {
186
- this.removePlugin(url);
187
- }
188
- });
189
- this._pluginOrder = newDefinition.plugins ? newDefinition.plugins : [];
190
- if (state) {
191
- this._state = state;
192
- }
193
- if (newDefinition.metaframes) {
194
- Object.keys(newDefinition.metaframes).forEach((newMetaframeId) => {
195
- if (!this._metaframes.hasOwnProperty(newMetaframeId)) {
196
- const metaframeDefinition = newDefinition.metaframes[newMetaframeId];
197
- this.addMetaframe(newMetaframeId, metaframeDefinition);
198
- }
199
- });
200
- }
201
- if (newDefinition.plugins) {
202
- newDefinition.plugins.forEach((url) => {
203
- if (!this._plugins.hasOwnProperty(url)) {
204
- this.addPlugin(url);
205
- }
206
- });
207
- }
208
- if (previousDefinition !== INITIAL_NULL_METAPAGE_DEFINITION) {
209
- window.setTimeout(() => {
210
- if (!this.isDisposed() && newDefinition === this._definition) {
211
- this._emitDefinitionEvent();
212
- if (state) {
213
- this.emit(MetapageEvents.State, this._state);
214
- }
215
- }
216
- }, 0);
217
- }
218
- return this;
219
- }
220
- _emitDefinitionEvent() {
221
- const event = {
222
- definition: this._definition,
223
- metaframes: this._metaframes,
224
- plugins: this._plugins,
225
- };
226
- this.emit(MetapageEvents.Definition, event);
227
- }
228
- addPipe(target, input) {
229
- if (!this._inputMap[target]) {
230
- this._inputMap[target] = [];
231
- }
232
- this._inputMap[target].push(input);
233
- }
234
- removeMetaframe(metaframeId) {
235
- if (!this._metaframes[metaframeId]) {
236
- return;
237
- }
238
- this._metaframes[metaframeId].dispose();
239
- delete this._metaframes[metaframeId];
240
- delete this._state.metaframes.inputs[metaframeId];
241
- delete this._state.metaframes.outputs[metaframeId];
242
- delete this._inputMap[metaframeId];
243
- Object.keys(this._inputMap).forEach((otherMetaframeId) => {
244
- const inputPipes = this._inputMap[otherMetaframeId];
245
- let index = 0;
246
- while (index <= inputPipes.length) {
247
- if (inputPipes[index] && inputPipes[index].metaframe === metaframeId) {
248
- inputPipes.splice(index, 1);
249
- }
250
- else {
251
- index++;
252
- }
253
- }
254
- });
255
- this._cachedInputLookupMap = {};
256
- }
257
- removePlugin(url) {
258
- if (!this._plugins[url]) {
259
- return;
260
- }
261
- this._plugins[url].dispose();
262
- delete this._plugins[url];
263
- delete this._state.plugins.inputs[url];
264
- delete this._state.plugins.outputs[url];
265
- }
266
- removeAll() {
267
- Object.keys(this._metaframes).forEach((id) => this._metaframes[id].dispose());
268
- Object.keys(this._plugins).forEach((id) => this._plugins[id].dispose());
269
- this._metaframes = {};
270
- this._plugins = {};
271
- this._state = emptyState();
272
- this._inputMap = {};
273
- this._cachedInputLookupMap = {};
274
- }
275
- metaframes() {
276
- return this.getMetaframes();
277
- }
278
- metaframeIds() {
279
- return this.getMetaframeIds();
280
- }
281
- getMetaframeIds() {
282
- return Object.keys(this._metaframes);
283
- }
284
- getMetaframes() {
285
- return Object.assign({}, this._metaframes);
286
- }
287
- plugins() {
288
- return Object.assign({}, this._plugins);
289
- }
290
- pluginIds() {
291
- return this.getPluginIds();
292
- }
293
- getPluginIds() {
294
- return this._pluginOrder.slice(0);
295
- }
296
- getMetaframe(id) {
297
- return this._metaframes[id];
298
- }
299
- getPlugin(url) {
300
- return this._plugins[url];
301
- }
302
- addMetaframe(metaframeId, definition) {
303
- if (!metaframeId) {
304
- throw "addMetaframe missing metaframeId";
305
- }
306
- if (!definition) {
307
- throw "addMetaframe missing definition";
308
- }
309
- if (this._metaframes[metaframeId]) {
310
- this.emitErrorMessage(`Existing metaframe for id=${metaframeId}`);
311
- throw `Existing metaframe for id=${metaframeId}`;
312
- }
313
- if (!definition.url) {
314
- this.emitErrorMessage(`Metaframe definition missing url id=${metaframeId}`);
315
- throw `Metaframe definition missing url id=${metaframeId}`;
316
- }
317
- var iframeClient = new MetapageIFrameRpcClient(this, definition.url, metaframeId, this._id, this._consoleBackgroundColor, this.debug).setMetapage(this);
318
- this._metaframes[metaframeId] = iframeClient;
319
- iframeClient.addListener(MetapageEvents.Error, (err) => {
320
- this.emit(MetapageEvents.Error, {
321
- metaframeId: iframeClient.id,
322
- metaframeUrl: iframeClient.url,
323
- error: err,
324
- });
325
- });
326
- if (definition.inputs) {
327
- definition.inputs.forEach((input) => this.addPipe(metaframeId, input));
328
- }
329
- iframeClient.setInputs(this._state.metaframes.inputs[metaframeId]);
330
- return iframeClient;
331
- }
332
- addPlugin(url) {
333
- if (!url) {
334
- throw "Plugin missing url";
335
- }
336
- const iframeClient = new MetapageIFrameRpcClient(this, url, url, this._id, this._consoleBackgroundColor, this.debug)
337
- .setInputs(this._state.plugins.inputs[url])
338
- .setMetapage(this)
339
- .setPlugin();
340
- bindPlugin(this, iframeClient);
341
- this._plugins[url] = iframeClient;
342
- return iframeClient;
343
- }
344
- dispose() {
345
- this.log("disposing");
346
- super.removeAllListeners();
347
- window.removeEventListener("message", this.onMessage);
348
- if (this._metaframes) {
349
- Object.keys(this._metaframes).forEach((metaframeId) => this._metaframes[metaframeId].dispose());
350
- }
351
- if (this._plugins) {
352
- Object.keys(this._plugins).forEach((pluginId) => this._plugins[pluginId].dispose());
353
- }
354
- this._id = undefined;
355
- this._metaframes = undefined;
356
- this._plugins = undefined;
357
- this._state = undefined;
358
- this._cachedInputLookupMap = undefined;
359
- this._inputMap = undefined;
360
- }
361
- log(o, color, backgroundColor) {
362
- if (!this.debug) {
363
- return;
364
- }
365
- this.logInternal(o, color, backgroundColor);
366
- }
367
- error(err) {
368
- this.logInternal(err, "f00", this._consoleBackgroundColor);
369
- this.emitErrorMessage(`${err}`);
370
- }
371
- emitErrorMessage(err) {
372
- this.emit(MetapageEvents.Error, err);
373
- }
374
- getInputsFromOutput(source, outputPipeId) {
375
- if (!this._cachedInputLookupMap[source]) {
376
- this._cachedInputLookupMap[source] = {};
377
- }
378
- if (!this._cachedInputLookupMap[source][outputPipeId]) {
379
- var targets = [];
380
- this._cachedInputLookupMap[source][outputPipeId] = targets;
381
- Object.keys(this._inputMap).forEach((metaframeId) => {
382
- if (metaframeId === source) {
383
- return;
384
- }
385
- this._inputMap[metaframeId].forEach((inputPipe) => {
386
- if (inputPipe.metaframe == source) {
387
- if (matchPipe(outputPipeId, inputPipe.source)) {
388
- var targetName = inputPipe.target;
389
- if (!inputPipe.target ||
390
- inputPipe.target.startsWith("*") ||
391
- inputPipe.target === "") {
392
- targetName = outputPipeId;
393
- }
394
- targets.push({ metaframe: metaframeId, pipe: targetName });
395
- }
396
- }
397
- });
398
- });
399
- }
400
- return this._cachedInputLookupMap[source][outputPipeId];
401
- }
402
- isValidJSONRpcMessage(message) {
403
- if (message.jsonrpc !== "2.0") {
404
- return false;
405
- }
406
- const method = message.method;
407
- switch (method) {
408
- case JsonRpcMethodsFromChild.SetupIframeClientRequest:
409
- return true;
410
- default:
411
- var iframeId = message.iframeId;
412
- if (iframeId &&
413
- !(message.parentId === this._id &&
414
- (this._metaframes[iframeId] || this._plugins[iframeId]))) {
415
- return false;
416
- }
417
- return true;
418
- }
419
- }
420
- setInput(iframeId, inputPipeId, value) {
421
- this.setInputStateOnly(iframeId, inputPipeId, value);
422
- this.setMetaframeClientInputAndSentClientEvent(iframeId, inputPipeId, value);
423
- this.emit(MetapageEvents.State, this._state);
424
- this.emit(MetapageEvents.Inputs, this._state);
425
- }
426
- setMetaframeClientInputAndSentClientEvent(iframeId, inputPipeId, value) {
427
- if (typeof iframeId === "object") {
428
- if (inputPipeId || value) {
429
- throw "bad arguments, see API docs";
430
- }
431
- const inputs = iframeId;
432
- Object.keys(inputs).forEach((id) => {
433
- var metaframeId = id;
434
- var metaframeInputs = inputs[metaframeId];
435
- if (typeof metaframeInputs !== "object") {
436
- throw "bad arguments, see API docs";
437
- }
438
- var iframeClient = this._metaframes[metaframeId];
439
- if (iframeClient) {
440
- iframeClient.setInputs(metaframeInputs);
441
- }
442
- else {
443
- this.error("No iframe id=$metaframeId");
444
- }
445
- });
446
- }
447
- else if (typeof iframeId === "string") {
448
- const iframeClient = this._metaframes[iframeId];
449
- if (iframeClient == null) {
450
- this.error(`No iframe id=${iframeId}`);
451
- }
452
- if (typeof inputPipeId === "string") {
453
- iframeClient.setInput(inputPipeId, value);
454
- }
455
- else if (typeof inputPipeId === "object") {
456
- iframeClient.setInputs(inputPipeId);
457
- }
458
- else {
459
- throw "bad arguments, see API docs";
460
- }
461
- }
462
- else {
463
- throw "bad arguments, see API docs";
464
- }
465
- }
466
- setInputs(iframeId, inputPipeId, value) {
467
- this.setInput(iframeId, inputPipeId, value);
468
- }
469
- setOutputStateOnly(iframeId, inputPipeId, value) {
470
- this._setStateOnly(false, iframeId, inputPipeId, value);
471
- }
472
- setInputStateOnly(iframeId, inputPipeId, value) {
473
- this._setStateOnly(true, iframeId, inputPipeId, value);
474
- }
475
- _setStateOnly(isInputs, iframeId, inputPipeId, value) {
476
- if (typeof iframeId === "object") {
477
- if (inputPipeId || value) {
478
- throw "If first argument is an object, subsequent args should be undefined";
479
- }
480
- const inputsMetaframesNew = iframeId;
481
- Object.keys(inputsMetaframesNew).forEach((metaframeId) => {
482
- var metaframeValuesNew = inputsMetaframesNew[metaframeId];
483
- if (typeof metaframeValuesNew !== "object") {
484
- throw "Object values must be objects";
485
- }
486
- const isMetaframe = this._metaframes.hasOwnProperty(metaframeId);
487
- if (!isMetaframe && !this._plugins.hasOwnProperty(metaframeId)) {
488
- throw "No metaframe or plugin: ${metaframeId}";
489
- }
490
- const inputOrOutputState = isMetaframe
491
- ? isInputs
492
- ? this._state.metaframes.inputs
493
- : this._state.metaframes.outputs
494
- : isInputs
495
- ? this._state.plugins.inputs
496
- : this._state.plugins.outputs;
497
- inputOrOutputState[metaframeId] = inputOrOutputState[metaframeId]
498
- ? inputOrOutputState[metaframeId]
499
- : {};
500
- Object.keys(metaframeValuesNew).forEach((metaframePipedId) => {
501
- if (metaframeValuesNew[metaframePipedId] === undefined) {
502
- delete inputOrOutputState[metaframeId][metaframePipedId];
503
- }
504
- else {
505
- inputOrOutputState[metaframeId][metaframePipedId] =
506
- metaframeValuesNew[metaframePipedId];
507
- }
508
- });
509
- });
510
- }
511
- else if (typeof iframeId === "string") {
512
- const metaframeId = iframeId;
513
- const isMetaframe = this._metaframes.hasOwnProperty(metaframeId);
514
- if (!isMetaframe && !this._plugins.hasOwnProperty(metaframeId)) {
515
- throw `No metaframe or plugin: ${metaframeId}`;
516
- }
517
- const inputOrOutputState = isMetaframe
518
- ? isInputs
519
- ? this._state.metaframes.inputs
520
- : this._state.metaframes.outputs
521
- : isInputs
522
- ? this._state.plugins.inputs
523
- : this._state.plugins.outputs;
524
- if (typeof inputPipeId === "string") {
525
- inputOrOutputState[metaframeId] = inputOrOutputState[metaframeId]
526
- ? inputOrOutputState[metaframeId]
527
- : {};
528
- const metaframePipeId = inputPipeId;
529
- if (value === undefined) {
530
- delete inputOrOutputState[metaframeId][metaframePipeId];
531
- }
532
- else {
533
- inputOrOutputState[metaframeId][metaframePipeId] = value;
534
- }
535
- }
536
- else if (typeof inputPipeId === "object") {
537
- inputOrOutputState[metaframeId] = inputOrOutputState[metaframeId]
538
- ? inputOrOutputState[metaframeId]
539
- : {};
540
- const metaframeValuesNew = inputPipeId;
541
- Object.keys(metaframeValuesNew).forEach((metaframePipedId) => {
542
- if (metaframeValuesNew[metaframePipedId] === undefined) {
543
- delete inputOrOutputState[metaframeId][metaframePipedId];
544
- }
545
- else {
546
- inputOrOutputState[metaframeId][metaframePipedId] =
547
- metaframeValuesNew[metaframePipedId];
548
- }
549
- });
550
- }
551
- else {
552
- throw "Second argument must be a string or an object";
553
- }
554
- }
555
- else {
556
- throw "First argument must be a string or an object";
557
- }
558
- }
559
- getMetaframeOrPlugin(key) {
560
- var val = this._metaframes[key];
561
- if (!val) {
562
- val = this._plugins[key];
563
- }
564
- return val;
565
- }
566
- onMessage(e) {
567
- if (typeof e.data === "object") {
568
- const jsonrpc = e.data;
569
- if (!this.isValidJSONRpcMessage(jsonrpc)) {
570
- return;
571
- }
572
- var method = jsonrpc.method;
573
- const metaframeId = jsonrpc.iframeId;
574
- if (!metaframeId) {
575
- return;
576
- }
577
- const metaframeOrPlugin = this.getMetaframeOrPlugin(metaframeId);
578
- const isPlugin = this._plugins[metaframeId];
579
- jsonrpc["_messageCount"] = ++this
580
- ._internalReceivedMessageCounter;
581
- if (this.debug) {
582
- this.log(`processing ${JSON.stringify(jsonrpc, null, " ").substr(0, 500)}`);
583
- }
584
- switch (method) {
585
- case JsonRpcMethodsFromChild.SetupIframeClientRequest:
586
- if (metaframeOrPlugin) {
587
- metaframeOrPlugin.register();
588
- }
589
- break;
590
- case JsonRpcMethodsFromChild.SetupIframeServerResponseAck:
591
- if (metaframeOrPlugin) {
592
- const params = jsonrpc.params;
593
- metaframeOrPlugin.registered(params.version);
594
- }
595
- break;
596
- case JsonRpcMethodsFromChild.OutputsUpdate:
597
- const outputs = jsonrpc.params;
598
- if (this._metaframes[metaframeId]) {
599
- var iframe = this._metaframes[metaframeId];
600
- this.setOutputStateOnly(metaframeId, outputs);
601
- iframe.setOutputs(outputs);
602
- this.emit(MetapageEvents.State, this._state);
603
- var modified = false;
604
- const outputKeys = Object.keys(outputs);
605
- const collectedOutputs = {};
606
- outputKeys.forEach((outputKey, i) => {
607
- const targets = this.getInputsFromOutput(metaframeId, outputKey);
608
- if (targets.length > 0) {
609
- targets.forEach((target) => {
610
- if (!collectedOutputs[target.metaframe]) {
611
- collectedOutputs[target.metaframe] = {};
612
- }
613
- collectedOutputs[target.metaframe][target.pipe] =
614
- outputs[outputKey];
615
- this.setInputStateOnly(target.metaframe, target.pipe, outputs[outputKey]);
616
- modified = true;
617
- });
618
- }
619
- });
620
- Object.keys(collectedOutputs).forEach((metaframeId) => {
621
- this._metaframes[metaframeId].setInputs(collectedOutputs[metaframeId]);
622
- });
623
- if (modified) {
624
- this.emit(MetapageEvents.State, this._state);
625
- }
626
- if (this.debug) {
627
- iframe.ack({ jsonrpc: jsonrpc, state: this._state });
628
- }
629
- }
630
- else if (this._plugins[metaframeId]) {
631
- const outputPersistanceAllowed = !outputs[METAPAGE_KEY_STATE] && !outputs[METAPAGE_KEY_DEFINITION];
632
- if (outputPersistanceAllowed) {
633
- this.setOutputStateOnly(metaframeId, outputs);
634
- }
635
- this._plugins[metaframeId].setOutputs(outputs);
636
- if (outputPersistanceAllowed) {
637
- this.emit(MetapageEvents.State, this._state);
638
- }
639
- if (this.debug) {
640
- this._plugins[metaframeId].ack({
641
- jsonrpc: jsonrpc,
642
- state: this._state,
643
- });
644
- }
645
- }
646
- else {
647
- this.error(`missing metaframe/plugin=$metaframeId`);
648
- }
649
- break;
650
- case JsonRpcMethodsFromChild.InputsUpdate:
651
- var inputs = jsonrpc.params;
652
- if (this.debug)
653
- this.log(`inputs ${JSON.stringify(inputs)} from ${metaframeId}`);
654
- if (this._metaframes[metaframeId]) {
655
- this.setInputStateOnly(metaframeId, inputs);
656
- this._metaframes[metaframeId].setInputs(inputs);
657
- this.emit(MetapageEvents.State, this._state);
658
- if (this.debug) {
659
- this._metaframes[metaframeId].ack({
660
- jsonrpc: jsonrpc,
661
- state: this._state,
662
- });
663
- }
664
- }
665
- else if (this._plugins[metaframeId]) {
666
- const inputPersistanceAllowed = !inputs[METAPAGE_KEY_STATE] && !inputs[METAPAGE_KEY_DEFINITION];
667
- if (inputPersistanceAllowed) {
668
- this.setInputStateOnly(metaframeId, inputs);
669
- }
670
- this._plugins[metaframeId].setInputs(inputs);
671
- if (inputPersistanceAllowed) {
672
- this.emit(MetapageEvents.State, this._state);
673
- }
674
- if (this.debug) {
675
- this._plugins[metaframeId].ack({
676
- jsonrpc: jsonrpc,
677
- state: this._state,
678
- });
679
- }
680
- }
681
- else {
682
- console.error(`InputsUpdate failed no metaframe or plugin id: "${metaframeId}"`);
683
- this.error(`InputsUpdate failed no metaframe or plugin id: "${metaframeId}"`);
684
- }
685
- break;
686
- case JsonRpcMethodsFromChild.PluginRequest:
687
- if (isPlugin && metaframeOrPlugin) {
688
- if (metaframeOrPlugin.hasPermissionsState()) {
689
- metaframeOrPlugin.setInput(METAPAGE_KEY_STATE, this._state);
690
- if (this.debug) {
691
- metaframeOrPlugin.ack({ jsonrpc: jsonrpc, state: this._state });
692
- }
693
- }
694
- }
695
- break;
696
- case JsonRpcMethodsFromChild.HashParamsUpdate:
697
- if (!isPlugin && metaframeOrPlugin) {
698
- const hashParamsUpdatePayload = jsonrpc.params;
699
- const url = new URL(metaframeOrPlugin.url);
700
- url.hash = hashParamsUpdatePayload.hash;
701
- metaframeOrPlugin.url = url.href;
702
- this._definition.metaframes[hashParamsUpdatePayload.metaframe].url =
703
- url.href;
704
- this._emitDefinitionEvent();
705
- }
706
- break;
707
- default:
708
- if (this.debug) {
709
- this.log(`Unknown RPC method: "${method}"`);
710
- }
711
- }
712
- this.emit(MetapageEvents.Message, jsonrpc);
713
- }
714
- }
715
- updatePluginsWithDefinition() {
716
- const currentMetapageDef = this.getDefinition();
717
- Object.values(this._plugins).forEach((plugin) => {
718
- if (plugin.hasPermissionsDefinition()) {
719
- updatePluginWithDefinition(plugin);
720
- }
721
- });
722
- }
723
- logInternal(o, color, backgroundColor) {
724
- backgroundColor = backgroundColor
725
- ? backgroundColor
726
- : this._consoleBackgroundColor;
727
- let s;
728
- if (typeof o === "string") {
729
- s = o;
730
- }
731
- else if (typeof o === "number") {
732
- s = o + "";
733
- }
734
- else {
735
- s = JSON.stringify(o);
736
- }
737
- s = this._id ? `Metapage[${this._id}] ${s}` : s;
738
- MetapageToolsLog(s, color, backgroundColor);
739
- }
740
- }
741
- Metapage.version = VERSION_METAPAGE;
742
- Metapage.DEFINITION = MetapageEvents.Definition;
743
- Metapage.DEFINITION_UPDATE_REQUEST = MetapageEvents.DefinitionUpdateRequest;
744
- Metapage.ERROR = MetapageEvents.Error;
745
- Metapage.INPUTS = MetapageEvents.Inputs;
746
- Metapage.MESSAGE = MetapageEvents.Message;
747
- Metapage.OUTPUTS = MetapageEvents.Outputs;
748
- Metapage.STATE = MetapageEvents.State;
749
- Metapage.deserializeInputs = deserializeInputs;
750
- Metapage.serializeInputs = serializeInputs;
751
- const bindPlugin = async (metapage, plugin) => {
752
- try {
753
- const metaframeDef = await plugin.getDefinition();
754
- if (!metaframeDef) {
755
- throw `${plugin.url}`;
756
- }
757
- if (plugin.isDisposed()) {
758
- return;
759
- }
760
- if (plugin.hasPermissionsDefinition()) {
761
- var disposer = metapage.addListenerReturnDisposer(MetapageEvents.Definition, (definition) => {
762
- plugin.setInput(METAPAGE_KEY_DEFINITION, definition.definition);
763
- });
764
- plugin._disposables.push(disposer);
765
- var currentMetapageDef = metapage.getDefinition();
766
- if (currentMetapageDef) {
767
- plugin.setInput(METAPAGE_KEY_DEFINITION, currentMetapageDef);
768
- if (metaframeDef.outputs) {
769
- var disposer = plugin.onOutput(METAPAGE_KEY_DEFINITION, (definition) => {
770
- if (objectHash.sha1(definition) !==
771
- objectHash.sha1(currentMetapageDef)) {
772
- metapage.emit(MetapageEvents.DefinitionUpdateRequest, definition);
773
- }
774
- });
775
- plugin._disposables.push(disposer);
776
- }
777
- }
778
- }
779
- if (plugin.hasPermissionsState()) {
780
- if (metaframeDef.outputs) {
781
- var disposer = plugin.onOutput(METAPAGE_KEY_STATE, (state) => {
782
- metapage.setState(state);
783
- });
784
- plugin._disposables.push(disposer);
785
- }
786
- }
787
- }
788
- catch (err) {
789
- console.error(err);
790
- metapage.emit(MetapageEvents.Error, `Failed to get plugin definition from "${plugin.getDefinitionUrl()}", error=${err}`);
791
- }
792
- };
793
- const updatePluginWithDefinition = (plugin) => {
794
- const currentMetapageDef = plugin._metapage.getDefinition();
795
- plugin.setInput(METAPAGE_KEY_DEFINITION, currentMetapageDef);
796
- };
797
- //# sourceMappingURL=Metapage.js.map