phecda-web 1.0.1-alpha.6 → 1.0.1-alpha.8

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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # phecda-web
2
+
3
+ Fulfill `phecda-core` standard (`Storage/Watcher`);
4
+
5
+ Work for `phecda-vue`/`phecda-react`
package/dist/index.d.ts CHANGED
@@ -3,10 +3,8 @@ export * from 'phecda-core';
3
3
 
4
4
  interface ActiveInstance {
5
5
  state: Record<string | symbol, any>;
6
- _v: WeakMap<any, any>;
7
- _r: WeakMap<any, any>;
8
- _f: WeakMap<any, any>;
9
- _c: WeakMap<any, any>;
6
+ origin: WeakMap<any, any>;
7
+ cache: WeakMap<any, any>;
10
8
  [key: string]: any;
11
9
  }
12
10
  interface PhecdaEmitter {
@@ -22,9 +20,7 @@ declare function waitUntilInit(...instances: InstanceType<Construct>[]): Promise
22
20
  declare function resetActiveInstance(instance?: ActiveInstance): void;
23
21
  declare function getActiveInstance(): ActiveInstance;
24
22
  declare function serializeState(): any;
23
+ declare function isModuleLoad(module: Construct): boolean;
25
24
  declare function unmountModule(module: Construct | PropertyKey): Promise<void>;
26
25
 
27
- declare function wrapError(target: any, key: PropertyKey, errorHandler: Function): (...args: any) => any;
28
- declare function isAsyncFunc(fn: Function): boolean;
29
-
30
- export { ActiveInstance, PhecdaEmitter, defaultWebInject, emitter, getActiveInstance, isAsyncFunc, resetActiveInstance, serializeState, unmountModule, waitUntilInit, wrapError };
26
+ export { ActiveInstance, PhecdaEmitter, defaultWebInject, emitter, getActiveInstance, isModuleLoad, resetActiveInstance, serializeState, unmountModule, waitUntilInit };
package/dist/index.js CHANGED
@@ -31,12 +31,11 @@ __export(src_exports, {
31
31
  defaultWebInject: () => defaultWebInject,
32
32
  emitter: () => emitter,
33
33
  getActiveInstance: () => getActiveInstance,
34
- isAsyncFunc: () => isAsyncFunc,
34
+ isModuleLoad: () => isModuleLoad,
35
35
  resetActiveInstance: () => resetActiveInstance,
36
36
  serializeState: () => serializeState,
37
37
  unmountModule: () => unmountModule,
38
- waitUntilInit: () => waitUntilInit,
39
- wrapError: () => wrapError
38
+ waitUntilInit: () => waitUntilInit
40
39
  });
41
40
  module.exports = __toCommonJS(src_exports);
42
41
  __reExport(src_exports, require("phecda-core"), module.exports);
@@ -62,12 +61,12 @@ function defaultWebInject() {
62
61
  });
63
62
  }
64
63
  if (!(0, import_phecda_core.getProperty)("storage")) {
65
- (0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance }) => {
64
+ (0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance, toJSON, toString }) => {
66
65
  if (!tag)
67
66
  return;
68
67
  const initstr = localStorage.getItem(tag);
69
68
  if (initstr) {
70
- const data = JSON.parse(initstr);
69
+ const data = toJSON(initstr);
71
70
  if (key) {
72
71
  instance[key] = data;
73
72
  } else {
@@ -78,7 +77,7 @@ function defaultWebInject() {
78
77
  }
79
78
  }
80
79
  globalThis.addEventListener("beforeunload", () => {
81
- localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
80
+ localStorage.setItem(tag, toString(key ? instance[key] : instance));
82
81
  });
83
82
  });
84
83
  }
@@ -95,11 +94,8 @@ var activeInstance;
95
94
  function resetActiveInstance(instance) {
96
95
  activeInstance = instance || {
97
96
  state: {},
98
- _o: /* @__PURE__ */ new WeakMap(),
99
- _v: /* @__PURE__ */ new WeakMap(),
100
- _r: /* @__PURE__ */ new WeakMap(),
101
- _f: /* @__PURE__ */ new WeakMap(),
102
- _c: /* @__PURE__ */ new WeakMap()
97
+ origin: /* @__PURE__ */ new WeakMap(),
98
+ cache: /* @__PURE__ */ new WeakMap()
103
99
  };
104
100
  }
105
101
  __name(resetActiveInstance, "resetActiveInstance");
@@ -111,45 +107,33 @@ function serializeState() {
111
107
  return JSON.parse(JSON.stringify(activeInstance.state));
112
108
  }
113
109
  __name(serializeState, "serializeState");
110
+ function isModuleLoad(module2) {
111
+ const { origin, state } = getActiveInstance();
112
+ const tag = (0, import_phecda_core2.getTag)(module2);
113
+ if (tag in state) {
114
+ if (origin.get(state[tag]) !== module2)
115
+ throw new Error(`Synonym module: Module taged "${String(tag)}" (but not "${module2.name}") has been loaded before`);
116
+ return true;
117
+ }
118
+ return false;
119
+ }
120
+ __name(isModuleLoad, "isModuleLoad");
114
121
  async function unmountModule(module2) {
115
122
  if (typeof module2 === "object")
116
- module2 = getTag(module2);
123
+ module2 = (0, import_phecda_core2.getTag)(module2);
117
124
  const { state } = getActiveInstance();
118
125
  await (0, import_phecda_core2.invokeHandler)("unmount", state[module2]);
119
126
  delete state[module2];
120
127
  }
121
128
  __name(unmountModule, "unmountModule");
122
-
123
- // src/utils.ts
124
- function wrapError(target, key, errorHandler) {
125
- if (isAsyncFunc(target[key])) {
126
- return (...args) => {
127
- return target[key].apply(target, args).catch(errorHandler);
128
- };
129
- } else {
130
- return (...args) => {
131
- try {
132
- return target[key].apply(target, args);
133
- } catch (e) {
134
- return errorHandler(e);
135
- }
136
- };
137
- }
138
- }
139
- __name(wrapError, "wrapError");
140
- function isAsyncFunc(fn) {
141
- return fn[Symbol.toStringTag] === "AsyncFunction";
142
- }
143
- __name(isAsyncFunc, "isAsyncFunc");
144
129
  // Annotate the CommonJS export names for ESM import in node:
145
130
  0 && (module.exports = {
146
131
  defaultWebInject,
147
132
  emitter,
148
133
  getActiveInstance,
149
- isAsyncFunc,
134
+ isModuleLoad,
150
135
  resetActiveInstance,
151
136
  serializeState,
152
137
  unmountModule,
153
- waitUntilInit,
154
- wrapError
138
+ waitUntilInit
155
139
  });
package/dist/index.mjs CHANGED
@@ -25,12 +25,12 @@ function defaultWebInject() {
25
25
  });
26
26
  }
27
27
  if (!getProperty("storage")) {
28
- injectProperty("storage", ({ tag, key, instance }) => {
28
+ injectProperty("storage", ({ tag, key, instance, toJSON, toString }) => {
29
29
  if (!tag)
30
30
  return;
31
31
  const initstr = localStorage.getItem(tag);
32
32
  if (initstr) {
33
- const data = JSON.parse(initstr);
33
+ const data = toJSON(initstr);
34
34
  if (key) {
35
35
  instance[key] = data;
36
36
  } else {
@@ -41,7 +41,7 @@ function defaultWebInject() {
41
41
  }
42
42
  }
43
43
  globalThis.addEventListener("beforeunload", () => {
44
- localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
44
+ localStorage.setItem(tag, toString(key ? instance[key] : instance));
45
45
  });
46
46
  });
47
47
  }
@@ -49,7 +49,7 @@ function defaultWebInject() {
49
49
  __name(defaultWebInject, "defaultWebInject");
50
50
 
51
51
  // src/core.ts
52
- import { invokeHandler } from "phecda-core";
52
+ import { getTag, invokeHandler } from "phecda-core";
53
53
  function waitUntilInit(...instances) {
54
54
  return Promise.all(instances.map((i) => i._promise));
55
55
  }
@@ -58,11 +58,8 @@ var activeInstance;
58
58
  function resetActiveInstance(instance) {
59
59
  activeInstance = instance || {
60
60
  state: {},
61
- _o: /* @__PURE__ */ new WeakMap(),
62
- _v: /* @__PURE__ */ new WeakMap(),
63
- _r: /* @__PURE__ */ new WeakMap(),
64
- _f: /* @__PURE__ */ new WeakMap(),
65
- _c: /* @__PURE__ */ new WeakMap()
61
+ origin: /* @__PURE__ */ new WeakMap(),
62
+ cache: /* @__PURE__ */ new WeakMap()
66
63
  };
67
64
  }
68
65
  __name(resetActiveInstance, "resetActiveInstance");
@@ -74,6 +71,17 @@ function serializeState() {
74
71
  return JSON.parse(JSON.stringify(activeInstance.state));
75
72
  }
76
73
  __name(serializeState, "serializeState");
74
+ function isModuleLoad(module) {
75
+ const { origin, state } = getActiveInstance();
76
+ const tag = getTag(module);
77
+ if (tag in state) {
78
+ if (origin.get(state[tag]) !== module)
79
+ throw new Error(`Synonym module: Module taged "${String(tag)}" (but not "${module.name}") has been loaded before`);
80
+ return true;
81
+ }
82
+ return false;
83
+ }
84
+ __name(isModuleLoad, "isModuleLoad");
77
85
  async function unmountModule(module) {
78
86
  if (typeof module === "object")
79
87
  module = getTag(module);
@@ -82,36 +90,13 @@ async function unmountModule(module) {
82
90
  delete state[module];
83
91
  }
84
92
  __name(unmountModule, "unmountModule");
85
-
86
- // src/utils.ts
87
- function wrapError(target, key, errorHandler) {
88
- if (isAsyncFunc(target[key])) {
89
- return (...args) => {
90
- return target[key].apply(target, args).catch(errorHandler);
91
- };
92
- } else {
93
- return (...args) => {
94
- try {
95
- return target[key].apply(target, args);
96
- } catch (e) {
97
- return errorHandler(e);
98
- }
99
- };
100
- }
101
- }
102
- __name(wrapError, "wrapError");
103
- function isAsyncFunc(fn) {
104
- return fn[Symbol.toStringTag] === "AsyncFunction";
105
- }
106
- __name(isAsyncFunc, "isAsyncFunc");
107
93
  export {
108
94
  defaultWebInject,
109
95
  emitter,
110
96
  getActiveInstance,
111
- isAsyncFunc,
97
+ isModuleLoad,
112
98
  resetActiveInstance,
113
99
  serializeState,
114
100
  unmountModule,
115
- waitUntilInit,
116
- wrapError
101
+ waitUntilInit
117
102
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-web",
3
- "version": "1.0.1-alpha.6",
3
+ "version": "1.0.1-alpha.8",
4
4
  "description": "provide web function for phecda",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -13,7 +13,7 @@
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
15
  "mitt": "^3.0.0",
16
- "phecda-core": "3.0.0-alpha.9"
16
+ "phecda-core": "3.0.0-alpha.11"
17
17
  },
18
18
  "devDependencies": {
19
19
  "tsup": "^6.5.0"