@netless/window-manager 0.4.38 → 0.4.39

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.
package/dist/typings.d.ts CHANGED
@@ -62,7 +62,9 @@ export declare type RegisterEvents<SetupResult = any> = {
62
62
  };
63
63
  export declare type RegisterParams<AppOptions = any, SetupResult = any, Attributes = any> = {
64
64
  kind: string;
65
- src: NetlessApp<Attributes, SetupResult> | string | (() => Promise<NetlessApp<Attributes, SetupResult>>);
65
+ src: NetlessApp<Attributes, SetupResult> | string | (() => Promise<NetlessApp<Attributes, SetupResult>>) | (() => Promise<{
66
+ default: NetlessApp<Attributes, SetupResult>;
67
+ }>);
66
68
  appOptions?: AppOptions | (() => AppOptions);
67
69
  addHooks?: (emitter: Emittery<RegisterEvents<SetupResult>>) => void;
68
70
  /** dynamic load app package name */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/window-manager",
3
- "version": "0.4.38",
3
+ "version": "0.4.39",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.es.js",
@@ -31,12 +31,19 @@ class AppRegister {
31
31
  this.appClassesCache.delete(params.kind);
32
32
  this.registered.set(params.kind, params);
33
33
 
34
- const srcOrAppOrFunction = params.src;
34
+ const paramSrc = params.src;
35
+ let srcOrAppOrFunction: () => Promise<NetlessApp>;
35
36
  let downloadApp: () => Promise<NetlessApp>;
36
37
 
37
- if (typeof srcOrAppOrFunction === "string") {
38
+ if (typeof paramSrc === "string") {
39
+ srcOrAppOrFunction = () => loadApp(paramSrc, params.kind, params.name);
40
+ if (this.syncRegisterApp) {
41
+ this.syncRegisterApp({ kind: params.kind, src: paramSrc, name: params.name });
42
+ }
43
+ }
44
+ if (typeof paramSrc === "function") {
38
45
  downloadApp = async () => {
39
- let appClass = (await loadApp(srcOrAppOrFunction, params.kind, params.name)) as any;
46
+ let appClass = await srcOrAppOrFunction() as any;
40
47
  if (appClass) {
41
48
  if (appClass.__esModule) {
42
49
  appClass = appClass.default;
@@ -48,15 +55,10 @@ class AppRegister {
48
55
  );
49
56
  }
50
57
  };
51
- if (this.syncRegisterApp) {
52
- this.syncRegisterApp({ kind: params.kind, src: srcOrAppOrFunction, name: params.name });
53
- }
54
- } else if (typeof srcOrAppOrFunction === "function") {
55
- downloadApp = srcOrAppOrFunction;
56
- } else {
57
- downloadApp = async () => srcOrAppOrFunction;
58
58
  }
59
-
59
+ if (typeof paramSrc === "object") {
60
+ downloadApp = async () => paramSrc;
61
+ }
60
62
  this.appClasses.set(params.kind, async () => {
61
63
  let app = this.appClassesCache.get(params.kind);
62
64
  if (!app) {
@@ -28,41 +28,45 @@ export const executeScript = (text: string, appName: string): NetlessApp => {
28
28
  return result;
29
29
  };
30
30
 
31
- export const loadApp = async (
32
- url: string,
33
- key: string,
34
- name?: string
35
- ): Promise<NetlessApp | undefined> => {
31
+ export const loadApp = async (url: string, key: string, name?: string): Promise<NetlessApp> => {
36
32
  const appName = name || Prefix + key;
37
33
  callbacks.emit("loadApp", { kind: key, status: "start" });
34
+
35
+ let text: string;
38
36
  try {
39
- const text = await getScript(url);
37
+ text = await getScript(url);
40
38
  if (!text || text.length === 0) {
41
39
  callbacks.emit("loadApp", { kind: key, status: "failed", reason: "script is empty." });
42
- return;
40
+ throw new Error("[WindowManager]: script is empty.");
43
41
  }
44
- try {
42
+ } catch (error) {
43
+ callbacks.emit("loadApp", { kind: key, status: "failed", reason: error.message });
44
+ throw error;
45
+ }
46
+ return getResult(text, appName, key);
47
+ };
48
+
49
+ const getResult = (text: string, appName: string, key: string): NetlessApp => {
50
+ try {
51
+ const result = executeScript(text, appName);
52
+ callbacks.emit("loadApp", { kind: key, status: "success" });
53
+ return result;
54
+ } catch (error: any) {
55
+ if (error.message.includes("Can only have one anonymous define call per script file")) {
56
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
57
+ // @ts-ignore
58
+ const define = window.define;
59
+ if ("function" == typeof define && define.amd) {
60
+ delete define.amd;
61
+ }
45
62
  const result = executeScript(text, appName);
46
63
  callbacks.emit("loadApp", { kind: key, status: "success" });
47
64
  return result;
48
- } catch (error: any) {
49
- if (error.message.includes("Can only have one anonymous define call per script file")) {
50
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
51
- // @ts-ignore
52
- const define = window.define;
53
- if ("function" == typeof define && define.amd) {
54
- delete define.amd;
55
- }
56
- const result = executeScript(text, appName);
57
- callbacks.emit("loadApp", { kind: key, status: "success" });
58
- return result;
59
- }
60
- callbacks.emit("loadApp", { kind: key, status: "failed", reason: error.message });
61
65
  }
62
- } catch (error: any) {
63
66
  callbacks.emit("loadApp", { kind: key, status: "failed", reason: error.message });
67
+ throw error;
64
68
  }
65
- };
69
+ }
66
70
 
67
71
  async function fetchWithTimeout(resource: string, options: RequestInit & { timeout: number }) {
68
72
  const { timeout = 10000 } = options;
package/src/typings.ts CHANGED
@@ -67,7 +67,11 @@ export type RegisterEvents<SetupResult = any> = {
67
67
 
68
68
  export type RegisterParams<AppOptions = any, SetupResult = any, Attributes = any> = {
69
69
  kind: string;
70
- src: NetlessApp<Attributes, SetupResult> | string | (() => Promise<NetlessApp<Attributes, SetupResult>>);
70
+ src:
71
+ | NetlessApp<Attributes, SetupResult>
72
+ | string
73
+ | (() => Promise<NetlessApp<Attributes, SetupResult>>)
74
+ | (() => Promise<{ default: NetlessApp<Attributes, SetupResult> }>);
71
75
  appOptions?: AppOptions | (() => AppOptions);
72
76
  addHooks?: (emitter: Emittery<RegisterEvents<SetupResult>>) => void;
73
77
  /** dynamic load app package name */