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