@wix/editor-application 1.392.0 → 1.394.0

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 (49) hide show
  1. package/dist/cjs/index.js +23 -605
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/esm/index.js +23 -602
  4. package/dist/esm/index.js.map +1 -1
  5. package/dist/statics/index.js +26 -609
  6. package/dist/statics/index.js.map +1 -1
  7. package/dist/types/index.d.ts +18 -192
  8. package/dist/types/index.d.ts.map +1 -1
  9. package/package.json +10 -35
  10. package/dist/cjs/environment-api/index.js +0 -634
  11. package/dist/cjs/environment-api/index.js.map +0 -1
  12. package/dist/cjs/platform-frame/index.js +0 -403
  13. package/dist/cjs/platform-frame/index.js.map +0 -1
  14. package/dist/cjs/platform-frame-api/index.js +0 -377
  15. package/dist/cjs/platform-frame-api/index.js.map +0 -1
  16. package/dist/cjs/platform-worker/index.js +0 -580
  17. package/dist/cjs/platform-worker/index.js.map +0 -1
  18. package/dist/cjs/platform-worker-api/index.js +0 -579
  19. package/dist/cjs/platform-worker-api/index.js.map +0 -1
  20. package/dist/esm/environment-api/index.js +0 -630
  21. package/dist/esm/environment-api/index.js.map +0 -1
  22. package/dist/esm/platform-frame/index.js +0 -401
  23. package/dist/esm/platform-frame/index.js.map +0 -1
  24. package/dist/esm/platform-frame-api/index.js +0 -375
  25. package/dist/esm/platform-frame-api/index.js.map +0 -1
  26. package/dist/esm/platform-worker/index.js +0 -578
  27. package/dist/esm/platform-worker/index.js.map +0 -1
  28. package/dist/esm/platform-worker-api/index.js +0 -577
  29. package/dist/esm/platform-worker-api/index.js.map +0 -1
  30. package/dist/statics/environment-api/index.js +0 -636
  31. package/dist/statics/environment-api/index.js.map +0 -1
  32. package/dist/statics/platform-frame/index.js +0 -404
  33. package/dist/statics/platform-frame/index.js.map +0 -1
  34. package/dist/statics/platform-frame-api/index.js +0 -380
  35. package/dist/statics/platform-frame-api/index.js.map +0 -1
  36. package/dist/statics/platform-worker/index.js +0 -581
  37. package/dist/statics/platform-worker/index.js.map +0 -1
  38. package/dist/statics/platform-worker-api/index.js +0 -581
  39. package/dist/statics/platform-worker-api/index.js.map +0 -1
  40. package/dist/types/environment-api/index.d.ts +0 -59
  41. package/dist/types/environment-api/index.d.ts.map +0 -1
  42. package/dist/types/platform-frame/index.d.ts +0 -59
  43. package/dist/types/platform-frame/index.d.ts.map +0 -1
  44. package/dist/types/platform-frame-api/index.d.ts +0 -59
  45. package/dist/types/platform-frame-api/index.d.ts.map +0 -1
  46. package/dist/types/platform-worker/index.d.ts +0 -59
  47. package/dist/types/platform-worker/index.d.ts.map +0 -1
  48. package/dist/types/platform-worker-api/index.d.ts +0 -59
  49. package/dist/types/platform-worker-api/index.d.ts.map +0 -1
package/dist/cjs/index.js CHANGED
@@ -3,6 +3,24 @@
3
3
  var publicEditorPlatformEvents = require('@wix/public-editor-platform-events');
4
4
  var publicEditorPlatformInterfaces = require('@wix/public-editor-platform-interfaces');
5
5
  var publicEditorPlatformErrors = require('@wix/public-editor-platform-errors');
6
+ var editorPlatformContexts = require('@wix/editor-platform-contexts');
7
+
8
+ function hash() {
9
+ const _performance = globalThis["performance"];
10
+ if (_performance && _performance.now) {
11
+ return _performance.now();
12
+ }
13
+ let result = "";
14
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
15
+ const charactersLength = characters.length;
16
+ let counter = 0;
17
+ const length = 10;
18
+ while (counter < length) {
19
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
20
+ counter += 1;
21
+ }
22
+ return result;
23
+ }
6
24
 
7
25
  class ApplicationLifecycle {
8
26
  constructor(events) {
@@ -22,7 +40,7 @@ class ApplicationLifecycle {
22
40
  if (!this.callbacks[event]) {
23
41
  this.callbacks[event] = [];
24
42
  }
25
- const id = `${performance.now()}`;
43
+ const id = `${hash()}`;
26
44
  this.callbacks[event].push({
27
45
  id,
28
46
  fn
@@ -45,21 +63,12 @@ class ApplicationLifecycle {
45
63
 
46
64
  var EditorPlatformApplicationErrorCode = /* @__PURE__ */ ((EditorPlatformApplicationErrorCode2) => {
47
65
  EditorPlatformApplicationErrorCode2["ApplicationRuntimeError"] = "ApplicationRuntimeError";
48
- EditorPlatformApplicationErrorCode2["ApplicationCreationError"] = "ApplicationCreationError";
49
- EditorPlatformApplicationErrorCode2["ApplicationLoadError"] = "ApplicationLoadError";
50
- EditorPlatformApplicationErrorCode2["ApplicationFetchError"] = "ApplicationFetchError";
51
- EditorPlatformApplicationErrorCode2["ApplicationExecuteError"] = "ApplicationExecuteError";
52
- EditorPlatformApplicationErrorCode2["ApplicationWasRemoved"] = "ApplicationWasRemoved";
53
- EditorPlatformApplicationErrorCode2["UndefinedApiMethod"] = "UndefinedApiMethod";
54
- EditorPlatformApplicationErrorCode2["ApplicationIsNotMutable"] = "ApplicationIsNotMutable";
55
- EditorPlatformApplicationErrorCode2["FailedToGetPrivateAPI"] = "FailedToGetPrivateAPI";
56
- EditorPlatformApplicationErrorCode2["ClientAuthError"] = "ClientAuthError";
57
66
  return EditorPlatformApplicationErrorCode2;
58
67
  })(EditorPlatformApplicationErrorCode || {});
59
68
  class EditorPlatformApplicationError extends publicEditorPlatformErrors.BaseError {
60
69
  state = {};
61
70
  constructor(message, code) {
62
- super(message, code, "Editor Platform Application Error");
71
+ super(message, code, "EP Application Error");
63
72
  }
64
73
  withUrl(url) {
65
74
  this.state = { ...this.state, url };
@@ -97,8 +106,7 @@ class EditorPlatformApplication {
97
106
  constructor(type, context) {
98
107
  this.type = type;
99
108
  this.#context = context;
100
- const bindings = this.#context.getBindings();
101
- this.appDefinitionId = bindings.appDefinitionId;
109
+ this.appDefinitionId = this.#context.getAppDefinitionId();
102
110
  const events = this.#context.getEvents();
103
111
  this.lifecycle = new ApplicationLifecycle(events);
104
112
  events.addEventListener(publicEditorPlatformInterfaces.EventType.removeAppCompleted, (e) => {
@@ -156,277 +164,6 @@ class EditorPlatformApplication {
156
164
  }
157
165
  }
158
166
 
159
- var EditorPlatformApplicationContextErrorCode = /* @__PURE__ */ ((EditorPlatformApplicationContextErrorCode2) => {
160
- EditorPlatformApplicationContextErrorCode2["IncorrectEnvironment"] = "IncorrectEnvironment";
161
- EditorPlatformApplicationContextErrorCode2["ClientAuthError"] = "ClientAuthError";
162
- return EditorPlatformApplicationContextErrorCode2;
163
- })(EditorPlatformApplicationContextErrorCode || {});
164
- class EditorPlatformApplicationContextError extends publicEditorPlatformErrors.BaseError {
165
- state = {};
166
- constructor(message, code) {
167
- super(message, code, "Editor Platform Application Context Error");
168
- }
169
- withUrl(url) {
170
- this.state = { ...this.state, url };
171
- return this;
172
- }
173
- withAppDefinitionId(appDefinitionId) {
174
- this.state = { ...this.state, appDefinitionId };
175
- return this;
176
- }
177
- }
178
- const createEditorPlatformApplicationContextError = publicEditorPlatformErrors.createErrorBuilder(EditorPlatformApplicationContextError);
179
- async function transformEventPayload(eventPayload, privateAPI) {
180
- if (!eventPayload?.type) {
181
- return eventPayload;
182
- }
183
- switch (eventPayload.type) {
184
- case "componentSelectionChanged":
185
- const componentRefs = eventPayload.componentRefs || [];
186
- const components = await Promise.all(
187
- componentRefs.map((ref) => {
188
- return privateAPI.components.getComponent(ref);
189
- })
190
- );
191
- return {
192
- type: eventPayload.type,
193
- components
194
- };
195
- default:
196
- return eventPayload;
197
- }
198
- }
199
- class ApplicationBoundEvents {
200
- constructor(appDefinitionId, events, privateAPI) {
201
- this.appDefinitionId = appDefinitionId;
202
- this.privateAPI = privateAPI;
203
- this.events = events;
204
- this.subscribe = events.subscribe.bind(events);
205
- this.commit = events.commit.bind(events);
206
- this.startTransaction = events.startTransaction.bind(events);
207
- this.silent = events.silent.bind(events);
208
- }
209
- events;
210
- subscribe;
211
- commit;
212
- startTransaction;
213
- silent;
214
- notify(event) {
215
- this.events.notify({
216
- type: event.type,
217
- payload: event.payload,
218
- meta: {
219
- appDefinitionId: this.appDefinitionId
220
- }
221
- });
222
- }
223
- notifyCustomEvent(type, payload) {
224
- this.notify({
225
- type: publicEditorPlatformEvents.PlatformAppEvent.CustomEvent,
226
- payload: {
227
- ...payload,
228
- type
229
- }
230
- });
231
- }
232
- /**
233
- * TODO: we should use same interface for all events
234
- * (subscribe vs addEventListener)
235
- */
236
- addEventListener(eventType, fn) {
237
- return this.events.subscribe(async (event) => {
238
- const isAppMatch = event.meta?.appDefinitionId === this.appDefinitionId || event.meta?.appDefinitionId === null;
239
- const transformPayload = () => transformEventPayload(event.payload, this.privateAPI);
240
- if (eventType === "*") {
241
- fn(await transformPayload());
242
- } else if (event.type === publicEditorPlatformEvents.PlatformAppEvent.CustomEvent) {
243
- if (eventType === event.payload?.type && !isAppMatch) {
244
- fn(await transformPayload());
245
- }
246
- } else if (event.type === publicEditorPlatformEvents.PlatformAppEvent.HostEvent) {
247
- if (eventType === event.payload?.type && isAppMatch) {
248
- fn(await transformPayload());
249
- }
250
- }
251
- });
252
- }
253
- }
254
- const WAIT_INJECTED_TIMEOUT = 200;
255
- const WAIT_INJECTED_RETRY_COUNT = 50;
256
- class ContextInjectionStatus {
257
- _resolveContextInjected = () => {
258
- };
259
- _isInjected = false;
260
- key;
261
- constructor(uuid) {
262
- this.key = `__${uuid}_CONTEXT_INJECTION_STATUS_KEY`;
263
- if (!globalThis[this.key]) {
264
- globalThis[this.key] = new Promise((resolve) => {
265
- this._resolveContextInjected = () => {
266
- this._isInjected = true;
267
- resolve();
268
- };
269
- });
270
- }
271
- }
272
- isInjected() {
273
- return !!this._isInjected;
274
- }
275
- resolveInjected() {
276
- this._resolveContextInjected?.();
277
- }
278
- waitInjected() {
279
- return new Promise((resolve, reject) => {
280
- let injected = false;
281
- let timeoutId;
282
- let retryCount = 0;
283
- const timeout = () => {
284
- if (injected) {
285
- return;
286
- }
287
- timeoutId = setTimeout(() => {
288
- retryCount++;
289
- if (retryCount < WAIT_INJECTED_RETRY_COUNT) {
290
- if (retryCount % 10 === 0) {
291
- console.log(
292
- createEditorPlatformApplicationContextError(
293
- EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
294
- "contexts are not resolved, still re-trying"
295
- ).withMessage(`try number ${retryCount}`).message
296
- );
297
- }
298
- timeout();
299
- return;
300
- }
301
- if (!injected) {
302
- const error = createEditorPlatformApplicationContextError(
303
- EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
304
- "contexts are not resolved, threw by timeout"
305
- );
306
- reject(error);
307
- }
308
- }, WAIT_INJECTED_TIMEOUT);
309
- };
310
- timeout();
311
- const _waitContextInjectedPromise = globalThis[this.key];
312
- _waitContextInjectedPromise.then(() => {
313
- injected = true;
314
- clearTimeout(timeoutId);
315
- resolve();
316
- });
317
- });
318
- }
319
- }
320
- const ENVIRONMENT_CONTEXT_KEY = "__ENVIRONMENT_CONTEXT_KEY";
321
- var PlatformEnvironment = /* @__PURE__ */ ((PlatformEnvironment2) => {
322
- PlatformEnvironment2["Worker"] = "Worker";
323
- PlatformEnvironment2["Frame"] = "Frame";
324
- PlatformEnvironment2["ComponentPanel"] = "ComponentPanel";
325
- return PlatformEnvironment2;
326
- })(PlatformEnvironment || {});
327
- class EnvironmentContext {
328
- constructor(environmentContext) {
329
- this.environmentContext = environmentContext;
330
- }
331
- static status = new ContextInjectionStatus("environment");
332
- static async inject(context) {
333
- if (globalThis[ENVIRONMENT_CONTEXT_KEY]) {
334
- throw createEditorPlatformApplicationContextError(
335
- EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
336
- "Environment context already exists and should not be overridden"
337
- );
338
- }
339
- globalThis[ENVIRONMENT_CONTEXT_KEY] = new EnvironmentContext(context);
340
- this.status.resolveInjected();
341
- }
342
- static async getInstance() {
343
- await this.status.waitInjected();
344
- return globalThis[ENVIRONMENT_CONTEXT_KEY];
345
- }
346
- getPrivateAPI() {
347
- return this.environmentContext.privateApi;
348
- }
349
- getEvents() {
350
- return this.environmentContext.events;
351
- }
352
- getApplicationAPIs() {
353
- return this.environmentContext.applicationAPIs ?? {};
354
- }
355
- getEnvironment() {
356
- return this.environmentContext.environment;
357
- }
358
- }
359
- const APPLICATION_CONTEXT_KEY = "__APPLICATION_CONTEXT_KEY";
360
- class ApplicationContext {
361
- constructor(applicationContext, environment) {
362
- this.applicationContext = applicationContext;
363
- this.environment = environment;
364
- this.events = new ApplicationBoundEvents(
365
- this.applicationContext.appDefinitionId,
366
- this.environment.getEvents(),
367
- this.environment.getPrivateAPI()
368
- );
369
- }
370
- static status = new ContextInjectionStatus("application");
371
- /**
372
- * TODO: use generics for context type
373
- * - application
374
- * - editor
375
- */
376
- static async inject(context) {
377
- const environment = await EnvironmentContext.getInstance();
378
- if (environment.getEnvironment() !== PlatformEnvironment.Frame) {
379
- throw createEditorPlatformApplicationContextError(
380
- EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
381
- "Application context can be injected only in frame environment"
382
- );
383
- }
384
- if (globalThis[APPLICATION_CONTEXT_KEY]) {
385
- throw createEditorPlatformApplicationContextError(
386
- EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
387
- "Application context already exists and should not be overridden"
388
- );
389
- }
390
- globalThis[APPLICATION_CONTEXT_KEY] = new ApplicationContext(
391
- context,
392
- await EnvironmentContext.getInstance()
393
- );
394
- this.status.resolveInjected();
395
- }
396
- static async getInstance() {
397
- const environment = await EnvironmentContext.getInstance();
398
- if (environment.getEnvironment() === PlatformEnvironment.Frame) {
399
- await this.status.waitInjected();
400
- return globalThis[APPLICATION_CONTEXT_KEY];
401
- } else {
402
- return __APPLICATION_CONTEXT_KEY;
403
- }
404
- }
405
- events;
406
- getAppDefinitionId() {
407
- return this.applicationContext.appDefinitionId;
408
- }
409
- getBindings() {
410
- return this.applicationContext;
411
- }
412
- getEvents() {
413
- return this.events;
414
- }
415
- getPrivateAPI() {
416
- return this.environment.getPrivateAPI();
417
- }
418
- getPrivateApplicationAPI() {
419
- const appDefinitionId = this.getAppDefinitionId();
420
- if (!appDefinitionId) {
421
- throw createEditorPlatformApplicationContextError(
422
- EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
423
- "appDefinitionId is not available"
424
- );
425
- }
426
- return this.environment.getApplicationAPIs()[appDefinitionId];
427
- }
428
- }
429
-
430
167
  var ApplicationType = /* @__PURE__ */ ((ApplicationType2) => {
431
168
  ApplicationType2["EditorAddon"] = "EDITOR_ADDON";
432
169
  ApplicationType2["Platform"] = "PLATFORM";
@@ -436,7 +173,7 @@ var ApplicationType = /* @__PURE__ */ ((ApplicationType2) => {
436
173
  class WixEditorPlatformApplication extends EditorPlatformApplication {
437
174
  static async create() {
438
175
  return new WixEditorPlatformApplication(
439
- await ApplicationContext.getInstance()
176
+ await editorPlatformContexts.ApplicationContext.getInstance()
440
177
  );
441
178
  }
442
179
  constructor(context) {
@@ -446,7 +183,7 @@ class WixEditorPlatformApplication extends EditorPlatformApplication {
446
183
 
447
184
  class WixEditorPlatformAddon extends EditorPlatformApplication {
448
185
  static async create() {
449
- const context = await ApplicationContext.getInstance();
186
+ const context = await editorPlatformContexts.ApplicationContext.getInstance();
450
187
  return new WixEditorPlatformAddon(context);
451
188
  }
452
189
  constructor(context) {
@@ -454,327 +191,8 @@ class WixEditorPlatformAddon extends EditorPlatformApplication {
454
191
  }
455
192
  }
456
193
 
457
- class WorkerEventsBridge {
458
- constructor(platformAppEvents) {
459
- this.platformAppEvents = platformAppEvents;
460
- }
461
- /**
462
- * Notify by event from Worker Manager (platform infrastructure)
463
- */
464
- notify(event) {
465
- switch (event.type) {
466
- case publicEditorPlatformEvents.PlatformLifecycleEvent.EditorReady:
467
- this.platformAppEvents.notify({
468
- ...event,
469
- // @ts-expect-error TODO: fix me
470
- type: publicEditorPlatformEvents.PlatformAppEvent.EditorReady
471
- });
472
- break;
473
- case publicEditorPlatformEvents.PlatformPrivateEvent.HostEvent:
474
- this.platformAppEvents.notify({
475
- ...event,
476
- type: publicEditorPlatformEvents.PlatformAppEvent.HostEvent
477
- });
478
- break;
479
- }
480
- }
481
- /**
482
- * Subscribe to Worker (Application) event
483
- */
484
- subscribe(cb) {
485
- this.platformAppEvents.subscribe((event) => {
486
- cb(event);
487
- });
488
- }
489
- }
490
-
491
- var PlatformConsumerEnvironmentAPIType = /* @__PURE__ */ ((PlatformConsumerEnvironmentAPIType2) => {
492
- PlatformConsumerEnvironmentAPIType2["Frame"] = "PLATFORM_FRAME_API";
493
- PlatformConsumerEnvironmentAPIType2["Worker"] = "PLATFORM_WORKER_API";
494
- return PlatformConsumerEnvironmentAPIType2;
495
- })(PlatformConsumerEnvironmentAPIType || {});
496
- class AbstractEnvironmentAPI {
497
- constructor(env) {
498
- this.env = env;
499
- this.create();
500
- }
501
- }
502
-
503
- const DESIGN_SYSTEM_STYLES_MAP = {
504
- classic: "https://www.unpkg.com/@wix/design-system/dist/statics/tokens-default.global.css",
505
- studio: "https://www.unpkg.com/@wix/design-system/dist/statics/tokens-studio.global.css"
506
- };
507
- class PlatformFrameAPI extends AbstractEnvironmentAPI {
508
- type = PlatformConsumerEnvironmentAPIType.Frame;
509
- #events = new publicEditorPlatformEvents.PlatformAppEventEmitter();
510
- #eventsBridge = new WorkerEventsBridge(this.#events);
511
- #privateAPI;
512
- #applicationPrivateAPI;
513
- constructor() {
514
- super(PlatformConsumerEnvironmentAPIType.Frame);
515
- }
516
- create() {
517
- if (typeof globalThis?.document?.head?.prepend === "function") {
518
- const params = new URL(globalThis.location.href).searchParams;
519
- const host = params.get("editorType") === "CLASSIC" ? "classic" : "studio";
520
- const url = DESIGN_SYSTEM_STYLES_MAP[host];
521
- const isAlreadyLoaded = url && !!document.querySelectorAll(`link[type="text/css"][href="${url}"]`)?.length;
522
- if (url && !isAlreadyLoaded) {
523
- const link = document.createElement("link");
524
- link.setAttribute("rel", "stylesheet");
525
- link.setAttribute("type", "text/css");
526
- link.setAttribute("href", url);
527
- globalThis.document.head.prepend(link);
528
- }
529
- }
530
- }
531
- async initEnvironment(props) {
532
- const { applicationPrivateAPI, privateAPI, appDefinitionId } = props;
533
- this.#applicationPrivateAPI = applicationPrivateAPI;
534
- this.#privateAPI = privateAPI;
535
- await EnvironmentContext.inject({
536
- environment: PlatformEnvironment.Frame,
537
- privateApi: privateAPI,
538
- events: this.#events,
539
- applicationAPIs: {
540
- [appDefinitionId]: this.#applicationPrivateAPI
541
- }
542
- });
543
- await ApplicationContext.inject({
544
- appDefinitionId,
545
- appDefinitionName: ""
546
- });
547
- }
548
- notify(event) {
549
- this.#eventsBridge.notify(event);
550
- }
551
- subscribe(cb) {
552
- this.#eventsBridge.subscribe(cb);
553
- }
554
- }
555
-
556
- const APPLICATION_REGISTRY_KEY = "__APPLICATION_REGISTRY_KEY";
557
- async function executeApplication(events, spec, bundle) {
558
- const executable = new Function(
559
- APPLICATION_CONTEXT_KEY,
560
- APPLICATION_REGISTRY_KEY,
561
- bundle
562
- );
563
- let instance;
564
- const applicationRegistryCallback = (_instance) => {
565
- if (instance) {
566
- throw createEditorPlatformApplicationError(
567
- EditorPlatformApplicationErrorCode.ApplicationExecuteError,
568
- "Application registry called more than once"
569
- ).withAppDefinitionId(spec.appDefinitionId);
570
- }
571
- if (_instance.type !== spec.type) {
572
- throw createEditorPlatformApplicationError(
573
- EditorPlatformApplicationErrorCode.ApplicationExecuteError,
574
- "Application has different type"
575
- ).withMessage("expected type", spec.type).withMessage("received type", _instance.type);
576
- }
577
- instance = _instance;
578
- };
579
- try {
580
- const context = { ...spec };
581
- executable.call(
582
- null,
583
- new ApplicationContext(context, await EnvironmentContext.getInstance()),
584
- applicationRegistryCallback
585
- );
586
- } catch (e) {
587
- throw createEditorPlatformApplicationError(
588
- EditorPlatformApplicationErrorCode.ApplicationExecuteError,
589
- e.message
590
- ).withAppDefinitionId(spec.appDefinitionId).withParentError(e);
591
- }
592
- return new Promise((resolve, reject) => {
593
- const unsubscribe = events.subscribe((event) => {
594
- const timeoutId = setTimeout(() => {
595
- clearTimeout(timeoutId);
596
- unsubscribe();
597
- if (!instance) {
598
- reject(
599
- createEditorPlatformApplicationError(
600
- EditorPlatformApplicationErrorCode.ApplicationExecuteError,
601
- "Application registry was not called, threw by timeout"
602
- ).withAppDefinitionId(spec.appDefinitionId)
603
- );
604
- }
605
- }, 5e3);
606
- if (event.type === publicEditorPlatformEvents.PlatformAppEvent.ApplicationInit && event.meta.appDefinitionId === spec.appDefinitionId) {
607
- clearTimeout(timeoutId);
608
- unsubscribe();
609
- if (!instance) {
610
- reject(
611
- createEditorPlatformApplicationError(
612
- EditorPlatformApplicationErrorCode.ApplicationExecuteError,
613
- "Application registry was not called"
614
- ).withAppDefinitionId(spec.appDefinitionId)
615
- );
616
- }
617
- resolve({ instance });
618
- }
619
- });
620
- });
621
- }
622
-
623
- class PlatformApplicationContainer {
624
- #apps = {};
625
- #privateAPI;
626
- #events;
627
- constructor(privateApi, events) {
628
- this.#privateAPI = privateApi;
629
- this.#events = events;
630
- this.#events.subscribe((event) => {
631
- switch (event.type) {
632
- case publicEditorPlatformEvents.PlatformAppEvent.HostEvent: {
633
- if (event.payload.type === publicEditorPlatformInterfaces.EventType.removeAppCompleted) {
634
- void this.#events.withEvent(
635
- this.#events.factories.createApplicationRemovedEvent(
636
- event.payload.appDefinitionId
637
- ),
638
- () => {
639
- return this.removeApplication(event.payload.appDefinitionId);
640
- }
641
- );
642
- }
643
- break;
644
- }
645
- }
646
- });
647
- }
648
- async runApplication(appSpec) {
649
- const bundle = await this.loadApplication(appSpec);
650
- const instance = await this.executeApplication(appSpec, bundle);
651
- this.setApplication(appSpec.appDefinitionId, instance);
652
- return instance;
653
- }
654
- setApplication(appDefId, instance) {
655
- this.#apps[appDefId] = instance;
656
- void this.#events.withEvent(
657
- this.#events.factories.createApplicationApiInitEvent(
658
- appDefId,
659
- // TODO: both types are set here...
660
- // @ts-expect-error TODO: fix me
661
- instance?.api?.private ? "private" : "public"
662
- ),
663
- () => {
664
- this.#privateAPI.applicationManager.setApplication(instance);
665
- }
666
- );
667
- }
668
- getApplication(appDefId) {
669
- return this.#apps[appDefId];
670
- }
671
- getAppDefinitionIds() {
672
- return Object.keys(this.#apps);
673
- }
674
- removeApplication(appDefinitionId) {
675
- delete this.#apps[appDefinitionId];
676
- }
677
- async loadApplication(appSpec) {
678
- const url = appSpec.url;
679
- return this.#events.withEvent(
680
- this.#events.factories.createApplicationLoadEvent(appSpec, url),
681
- async () => {
682
- try {
683
- return await this.loadApplicationBundle(url);
684
- } catch (e) {
685
- throw createEditorPlatformApplicationError(
686
- EditorPlatformApplicationErrorCode.ApplicationLoadError
687
- ).withUrl(url).withAppDefinitionId(appSpec.appDefinitionId).withParentError(e);
688
- }
689
- }
690
- );
691
- }
692
- async loadApplicationBundle(url) {
693
- const res = await fetch(url, {
694
- method: "GET"
695
- });
696
- const isSuccessfulResponse = res.status >= 200 && res.status <= 299;
697
- if (!isSuccessfulResponse) {
698
- throw createEditorPlatformApplicationError(
699
- EditorPlatformApplicationErrorCode.ApplicationFetchError
700
- ).withUrl(url);
701
- }
702
- return res.text();
703
- }
704
- async executeApplication(appSpec, bundle) {
705
- return this.#events.withEvent(
706
- this.#events.factories.createApplicationExecuteEvent(
707
- appSpec,
708
- appSpec.url
709
- ),
710
- async () => {
711
- const { instance } = await executeApplication(
712
- this.#events,
713
- appSpec,
714
- bundle
715
- );
716
- return instance;
717
- }
718
- );
719
- }
720
- }
721
-
722
- class PlatformWorkerAPI extends AbstractEnvironmentAPI {
723
- type = PlatformConsumerEnvironmentAPIType.Worker;
724
- #events = new publicEditorPlatformEvents.PlatformAppEventEmitter();
725
- #eventsBridge = new WorkerEventsBridge(this.#events);
726
- #container = null;
727
- #privateAPI;
728
- #pendingWaiters = [];
729
- constructor() {
730
- super(PlatformConsumerEnvironmentAPIType.Worker);
731
- }
732
- create() {
733
- }
734
- async initEnvironment(props) {
735
- const { buildPrivateAPI } = props;
736
- this.#privateAPI = await buildPrivateAPI({
737
- // TODO: should be per application (within the container before app execution)
738
- type: "EDITOR_ADDON"
739
- });
740
- await EnvironmentContext.inject({
741
- environment: PlatformEnvironment.Worker,
742
- events: this.#events,
743
- privateApi: this.#privateAPI,
744
- applicationAPIs: {}
745
- });
746
- this.#container = new PlatformApplicationContainer(
747
- this.#privateAPI,
748
- this.#events
749
- );
750
- this.#pendingWaiters.forEach((res) => res(this));
751
- }
752
- async notify(event) {
753
- await this.waitReady();
754
- this.#eventsBridge.notify(event);
755
- }
756
- subscribe(cb) {
757
- this.#eventsBridge.subscribe(cb);
758
- }
759
- async runApplication(app) {
760
- await this.waitReady();
761
- await this.#container.runApplication(app);
762
- }
763
- waitReady() {
764
- return new Promise((res) => {
765
- if (this.#privateAPI) {
766
- return res(this);
767
- }
768
- this.#pendingWaiters.push(res);
769
- });
770
- }
771
- }
772
-
773
194
  exports.ApplicationType = ApplicationType;
774
195
  exports.EditorPlatformApplication = EditorPlatformApplication;
775
- exports.PlatformConsumerEnvironmentAPIType = PlatformConsumerEnvironmentAPIType;
776
- exports.PlatformFrameAPI = PlatformFrameAPI;
777
- exports.PlatformWorkerAPI = PlatformWorkerAPI;
778
196
  exports.WixEditorPlatformAddon = WixEditorPlatformAddon;
779
197
  exports.WixEditorPlatformApplication = WixEditorPlatformApplication;
780
198
  //# sourceMappingURL=index.js.map