phecda-web 1.0.1-alpha.2 → 1.0.1-alpha.4

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 CHANGED
@@ -2,7 +2,7 @@ import { Events, Construct } from 'phecda-core';
2
2
  export * from 'phecda-core';
3
3
 
4
4
  interface ActiveInstance {
5
- state: Record<string, any>;
5
+ state: Record<string | symbol, any>;
6
6
  _v: WeakMap<any, any>;
7
7
  _r: WeakMap<any, any>;
8
8
  _f: WeakMap<any, any>;
@@ -14,21 +14,17 @@ interface PhecdaEmitter {
14
14
  off<N extends keyof Events>(eventName: N, cb?: (args: Events[N]) => void): void;
15
15
  emit<N extends keyof Events>(eventName: N, param: Events[N]): void;
16
16
  }
17
- interface Plugin {
18
- setup(instance: ActiveInstance): void;
19
- unmount?(instance: ActiveInstance): void;
20
- }
21
17
 
22
18
  declare const emitter: PhecdaEmitter;
23
- declare const storagePlugin: () => Plugin;
24
- declare const watchPlugin: () => Plugin;
19
+ declare function defaultWebInject(): void;
25
20
 
26
21
  declare function waitUntilInit(...instances: InstanceType<Construct>[]): Promise<any[]>;
27
22
  declare function resetActiveInstance(instance?: ActiveInstance): void;
28
23
  declare function getActiveInstance(): ActiveInstance;
29
24
  declare function serializeState(): any;
25
+ declare function unmountModule(module: Construct | PropertyKey): Promise<void>;
30
26
 
31
27
  declare function wrapError(target: any, key: PropertyKey, errorHandler: Function): (...args: any) => any;
32
28
  declare function isAsyncFunc(fn: Function): boolean;
33
29
 
34
- export { ActiveInstance, PhecdaEmitter, Plugin, emitter, getActiveInstance, isAsyncFunc, resetActiveInstance, serializeState, storagePlugin, waitUntilInit, watchPlugin, wrapError };
30
+ export { ActiveInstance, PhecdaEmitter, defaultWebInject, emitter, getActiveInstance, isAsyncFunc, resetActiveInstance, serializeState, unmountModule, waitUntilInit, wrapError };
package/dist/index.js CHANGED
@@ -28,14 +28,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
28
28
  // src/index.ts
29
29
  var src_exports = {};
30
30
  __export(src_exports, {
31
+ defaultWebInject: () => defaultWebInject,
31
32
  emitter: () => emitter,
32
33
  getActiveInstance: () => getActiveInstance,
33
34
  isAsyncFunc: () => isAsyncFunc,
34
35
  resetActiveInstance: () => resetActiveInstance,
35
36
  serializeState: () => serializeState,
36
- storagePlugin: () => storagePlugin,
37
+ unmountModule: () => unmountModule,
37
38
  waitUntilInit: () => waitUntilInit,
38
- watchPlugin: () => watchPlugin,
39
39
  wrapError: () => wrapError
40
40
  });
41
41
  module.exports = __toCommonJS(src_exports);
@@ -45,63 +45,48 @@ __reExport(src_exports, require("phecda-core"), module.exports);
45
45
  var import_phecda_core = require("phecda-core");
46
46
  var import_mitt = __toESM(require("mitt"));
47
47
  var emitter = (0, import_mitt.default)();
48
- var storagePlugin = /* @__PURE__ */ __name(() => {
49
- return {
50
- setup() {
51
- (0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance }) => {
52
- if (!tag)
53
- return;
54
- const initstr = localStorage.getItem(tag);
55
- if (initstr) {
56
- const data = JSON.parse(initstr);
57
- if (key) {
58
- instance[key] = data;
59
- } else {
60
- for (const i in data) {
61
- if (i)
62
- instance[i] = data[i];
63
- }
64
- }
65
- }
66
- globalThis.addEventListener("beforeunload", () => {
67
- localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
68
- });
69
- });
70
- }
71
- };
72
- }, "storagePlugin");
73
- var watchPlugin = /* @__PURE__ */ __name(() => {
74
- const eventRecord = [];
75
- return {
76
- setup() {
77
- (0, import_phecda_core.injectProperty)("watcher", ({ eventName, instance, key, options }) => {
78
- const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
79
- if (options?.once) {
80
- const handler = /* @__PURE__ */ __name((...args) => {
81
- fn(...args);
82
- emitter.off(eventName, handler);
83
- }, "handler");
84
- emitter.on(eventName, handler);
85
- eventRecord.push([
86
- eventName,
87
- handler
88
- ]);
48
+ function defaultWebInject() {
49
+ if (!(0, import_phecda_core.getProperty)("watcher")) {
50
+ (0, import_phecda_core.injectProperty)("watcher", ({ eventName, instance, key, options }) => {
51
+ const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
52
+ if (options?.once) {
53
+ const handler = /* @__PURE__ */ __name(() => {
54
+ fn();
55
+ emitter.off(eventName);
56
+ }, "handler");
57
+ emitter.on(eventName, handler);
58
+ } else {
59
+ emitter.on(eventName, fn);
60
+ }
61
+ return () => emitter.off(eventName);
62
+ });
63
+ }
64
+ if (!(0, import_phecda_core.getProperty)("storage")) {
65
+ (0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance }) => {
66
+ if (!tag)
67
+ return;
68
+ const initstr = localStorage.getItem(tag);
69
+ if (initstr) {
70
+ const data = JSON.parse(initstr);
71
+ if (key) {
72
+ instance[key] = data;
89
73
  } else {
90
- eventRecord.push([
91
- eventName,
92
- fn
93
- ]);
94
- emitter.on(eventName, fn);
74
+ for (const i in data) {
75
+ if (i)
76
+ instance[i] = data[i];
77
+ }
95
78
  }
79
+ }
80
+ globalThis.addEventListener("beforeunload", () => {
81
+ localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
96
82
  });
97
- },
98
- unmount() {
99
- eventRecord.forEach(([eventName, handler]) => emitter.off(eventName, handler));
100
- }
101
- };
102
- }, "watchPlugin");
83
+ });
84
+ }
85
+ }
86
+ __name(defaultWebInject, "defaultWebInject");
103
87
 
104
88
  // src/core.ts
89
+ var import_phecda_core2 = require("phecda-core");
105
90
  function waitUntilInit(...instances) {
106
91
  return Promise.all(instances.map((i) => i._promise));
107
92
  }
@@ -110,6 +95,7 @@ var activeInstance;
110
95
  function resetActiveInstance(instance) {
111
96
  activeInstance = instance || {
112
97
  state: {},
98
+ _o: /* @__PURE__ */ new WeakMap(),
113
99
  _v: /* @__PURE__ */ new WeakMap(),
114
100
  _r: /* @__PURE__ */ new WeakMap(),
115
101
  _f: /* @__PURE__ */ new WeakMap(),
@@ -125,6 +111,14 @@ function serializeState() {
125
111
  return JSON.parse(JSON.stringify(activeInstance.state));
126
112
  }
127
113
  __name(serializeState, "serializeState");
114
+ async function unmountModule(module2) {
115
+ if (typeof module2 === "object")
116
+ module2 = getTag(module2);
117
+ const { state } = getActiveInstance();
118
+ await (0, import_phecda_core2.unmountParallel)(state[module2]);
119
+ delete state[module2];
120
+ }
121
+ __name(unmountModule, "unmountModule");
128
122
 
129
123
  // src/utils.ts
130
124
  function wrapError(target, key, errorHandler) {
@@ -149,13 +143,13 @@ function isAsyncFunc(fn) {
149
143
  __name(isAsyncFunc, "isAsyncFunc");
150
144
  // Annotate the CommonJS export names for ESM import in node:
151
145
  0 && (module.exports = {
146
+ defaultWebInject,
152
147
  emitter,
153
148
  getActiveInstance,
154
149
  isAsyncFunc,
155
150
  resetActiveInstance,
156
151
  serializeState,
157
- storagePlugin,
152
+ unmountModule,
158
153
  waitUntilInit,
159
- watchPlugin,
160
154
  wrapError
161
155
  });
package/dist/index.mjs CHANGED
@@ -5,66 +5,51 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
5
5
  export * from "phecda-core";
6
6
 
7
7
  // src/plugin.ts
8
- import { injectProperty } from "phecda-core";
8
+ import { getProperty, injectProperty } from "phecda-core";
9
9
  import mitt from "mitt";
10
10
  var emitter = mitt();
11
- var storagePlugin = /* @__PURE__ */ __name(() => {
12
- return {
13
- setup() {
14
- injectProperty("storage", ({ tag, key, instance }) => {
15
- if (!tag)
16
- return;
17
- const initstr = localStorage.getItem(tag);
18
- if (initstr) {
19
- const data = JSON.parse(initstr);
20
- if (key) {
21
- instance[key] = data;
22
- } else {
23
- for (const i in data) {
24
- if (i)
25
- instance[i] = data[i];
26
- }
27
- }
28
- }
29
- globalThis.addEventListener("beforeunload", () => {
30
- localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
31
- });
32
- });
33
- }
34
- };
35
- }, "storagePlugin");
36
- var watchPlugin = /* @__PURE__ */ __name(() => {
37
- const eventRecord = [];
38
- return {
39
- setup() {
40
- injectProperty("watcher", ({ eventName, instance, key, options }) => {
41
- const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
42
- if (options?.once) {
43
- const handler = /* @__PURE__ */ __name((...args) => {
44
- fn(...args);
45
- emitter.off(eventName, handler);
46
- }, "handler");
47
- emitter.on(eventName, handler);
48
- eventRecord.push([
49
- eventName,
50
- handler
51
- ]);
11
+ function defaultWebInject() {
12
+ if (!getProperty("watcher")) {
13
+ injectProperty("watcher", ({ eventName, instance, key, options }) => {
14
+ const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
15
+ if (options?.once) {
16
+ const handler = /* @__PURE__ */ __name(() => {
17
+ fn();
18
+ emitter.off(eventName);
19
+ }, "handler");
20
+ emitter.on(eventName, handler);
21
+ } else {
22
+ emitter.on(eventName, fn);
23
+ }
24
+ return () => emitter.off(eventName);
25
+ });
26
+ }
27
+ if (!getProperty("storage")) {
28
+ injectProperty("storage", ({ tag, key, instance }) => {
29
+ if (!tag)
30
+ return;
31
+ const initstr = localStorage.getItem(tag);
32
+ if (initstr) {
33
+ const data = JSON.parse(initstr);
34
+ if (key) {
35
+ instance[key] = data;
52
36
  } else {
53
- eventRecord.push([
54
- eventName,
55
- fn
56
- ]);
57
- emitter.on(eventName, fn);
37
+ for (const i in data) {
38
+ if (i)
39
+ instance[i] = data[i];
40
+ }
58
41
  }
42
+ }
43
+ globalThis.addEventListener("beforeunload", () => {
44
+ localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
59
45
  });
60
- },
61
- unmount() {
62
- eventRecord.forEach(([eventName, handler]) => emitter.off(eventName, handler));
63
- }
64
- };
65
- }, "watchPlugin");
46
+ });
47
+ }
48
+ }
49
+ __name(defaultWebInject, "defaultWebInject");
66
50
 
67
51
  // src/core.ts
52
+ import { unmountParallel } from "phecda-core";
68
53
  function waitUntilInit(...instances) {
69
54
  return Promise.all(instances.map((i) => i._promise));
70
55
  }
@@ -73,6 +58,7 @@ var activeInstance;
73
58
  function resetActiveInstance(instance) {
74
59
  activeInstance = instance || {
75
60
  state: {},
61
+ _o: /* @__PURE__ */ new WeakMap(),
76
62
  _v: /* @__PURE__ */ new WeakMap(),
77
63
  _r: /* @__PURE__ */ new WeakMap(),
78
64
  _f: /* @__PURE__ */ new WeakMap(),
@@ -88,6 +74,14 @@ function serializeState() {
88
74
  return JSON.parse(JSON.stringify(activeInstance.state));
89
75
  }
90
76
  __name(serializeState, "serializeState");
77
+ async function unmountModule(module) {
78
+ if (typeof module === "object")
79
+ module = getTag(module);
80
+ const { state } = getActiveInstance();
81
+ await unmountParallel(state[module]);
82
+ delete state[module];
83
+ }
84
+ __name(unmountModule, "unmountModule");
91
85
 
92
86
  // src/utils.ts
93
87
  function wrapError(target, key, errorHandler) {
@@ -111,13 +105,13 @@ function isAsyncFunc(fn) {
111
105
  }
112
106
  __name(isAsyncFunc, "isAsyncFunc");
113
107
  export {
108
+ defaultWebInject,
114
109
  emitter,
115
110
  getActiveInstance,
116
111
  isAsyncFunc,
117
112
  resetActiveInstance,
118
113
  serializeState,
119
- storagePlugin,
114
+ unmountModule,
120
115
  waitUntilInit,
121
- watchPlugin,
122
116
  wrapError
123
117
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-web",
3
- "version": "1.0.1-alpha.2",
3
+ "version": "1.0.1-alpha.4",
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.5"
16
+ "phecda-core": "3.0.0-alpha.7"
17
17
  },
18
18
  "devDependencies": {
19
19
  "tsup": "^6.5.0"