phecda-web 1.0.1-alpha.6 → 1.0.1-alpha.7
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/index.d.ts +4 -5
- package/dist/index.js +19 -9
- package/dist/index.mjs +18 -9
- package/package.json +2 -2
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
|
-
|
|
7
|
-
|
|
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,10 @@ 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
26
|
declare function wrapError(target: any, key: PropertyKey, errorHandler: Function): (...args: any) => any;
|
|
28
27
|
declare function isAsyncFunc(fn: Function): boolean;
|
|
29
28
|
|
|
30
|
-
export { ActiveInstance, PhecdaEmitter, defaultWebInject, emitter, getActiveInstance, isAsyncFunc, resetActiveInstance, serializeState, unmountModule, waitUntilInit, wrapError };
|
|
29
|
+
export { ActiveInstance, PhecdaEmitter, defaultWebInject, emitter, getActiveInstance, isAsyncFunc, isModuleLoad, resetActiveInstance, serializeState, unmountModule, waitUntilInit, wrapError };
|
package/dist/index.js
CHANGED
|
@@ -32,6 +32,7 @@ __export(src_exports, {
|
|
|
32
32
|
emitter: () => emitter,
|
|
33
33
|
getActiveInstance: () => getActiveInstance,
|
|
34
34
|
isAsyncFunc: () => isAsyncFunc,
|
|
35
|
+
isModuleLoad: () => isModuleLoad,
|
|
35
36
|
resetActiveInstance: () => resetActiveInstance,
|
|
36
37
|
serializeState: () => serializeState,
|
|
37
38
|
unmountModule: () => unmountModule,
|
|
@@ -62,12 +63,12 @@ function defaultWebInject() {
|
|
|
62
63
|
});
|
|
63
64
|
}
|
|
64
65
|
if (!(0, import_phecda_core.getProperty)("storage")) {
|
|
65
|
-
(0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance }) => {
|
|
66
|
+
(0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance, toJSON, toString }) => {
|
|
66
67
|
if (!tag)
|
|
67
68
|
return;
|
|
68
69
|
const initstr = localStorage.getItem(tag);
|
|
69
70
|
if (initstr) {
|
|
70
|
-
const data =
|
|
71
|
+
const data = toJSON(initstr);
|
|
71
72
|
if (key) {
|
|
72
73
|
instance[key] = data;
|
|
73
74
|
} else {
|
|
@@ -78,7 +79,7 @@ function defaultWebInject() {
|
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
globalThis.addEventListener("beforeunload", () => {
|
|
81
|
-
localStorage.setItem(tag,
|
|
82
|
+
localStorage.setItem(tag, toString(key ? instance[key] : instance));
|
|
82
83
|
});
|
|
83
84
|
});
|
|
84
85
|
}
|
|
@@ -95,11 +96,8 @@ var activeInstance;
|
|
|
95
96
|
function resetActiveInstance(instance) {
|
|
96
97
|
activeInstance = instance || {
|
|
97
98
|
state: {},
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
_r: /* @__PURE__ */ new WeakMap(),
|
|
101
|
-
_f: /* @__PURE__ */ new WeakMap(),
|
|
102
|
-
_c: /* @__PURE__ */ new WeakMap()
|
|
99
|
+
origin: /* @__PURE__ */ new WeakMap(),
|
|
100
|
+
cache: /* @__PURE__ */ new WeakMap()
|
|
103
101
|
};
|
|
104
102
|
}
|
|
105
103
|
__name(resetActiveInstance, "resetActiveInstance");
|
|
@@ -111,9 +109,20 @@ function serializeState() {
|
|
|
111
109
|
return JSON.parse(JSON.stringify(activeInstance.state));
|
|
112
110
|
}
|
|
113
111
|
__name(serializeState, "serializeState");
|
|
112
|
+
function isModuleLoad(module2) {
|
|
113
|
+
const { origin, state } = getActiveInstance();
|
|
114
|
+
const tag = (0, import_phecda_core2.getTag)(module2);
|
|
115
|
+
if (tag in state) {
|
|
116
|
+
if (origin.get(state[tag]) !== module2)
|
|
117
|
+
throw new Error(`Synonym module: Module taged "${String(tag)}" (but not "${module2.name}") has been loaded before`);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
__name(isModuleLoad, "isModuleLoad");
|
|
114
123
|
async function unmountModule(module2) {
|
|
115
124
|
if (typeof module2 === "object")
|
|
116
|
-
module2 = getTag(module2);
|
|
125
|
+
module2 = (0, import_phecda_core2.getTag)(module2);
|
|
117
126
|
const { state } = getActiveInstance();
|
|
118
127
|
await (0, import_phecda_core2.invokeHandler)("unmount", state[module2]);
|
|
119
128
|
delete state[module2];
|
|
@@ -147,6 +156,7 @@ __name(isAsyncFunc, "isAsyncFunc");
|
|
|
147
156
|
emitter,
|
|
148
157
|
getActiveInstance,
|
|
149
158
|
isAsyncFunc,
|
|
159
|
+
isModuleLoad,
|
|
150
160
|
resetActiveInstance,
|
|
151
161
|
serializeState,
|
|
152
162
|
unmountModule,
|
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 =
|
|
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,
|
|
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
|
-
|
|
62
|
-
|
|
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);
|
|
@@ -109,6 +117,7 @@ export {
|
|
|
109
117
|
emitter,
|
|
110
118
|
getActiveInstance,
|
|
111
119
|
isAsyncFunc,
|
|
120
|
+
isModuleLoad,
|
|
112
121
|
resetActiveInstance,
|
|
113
122
|
serializeState,
|
|
114
123
|
unmountModule,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phecda-web",
|
|
3
|
-
"version": "1.0.1-alpha.
|
|
3
|
+
"version": "1.0.1-alpha.7",
|
|
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.
|
|
16
|
+
"phecda-core": "3.0.0-alpha.10"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"tsup": "^6.5.0"
|