phecda-react 0.0.1-alpha.3 → 0.1.0-alpha.10

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
@@ -1,27 +1,13 @@
1
- import * as react from 'react';
2
- import { Events } from 'phecda-core';
3
- export * from 'phecda-core';
4
-
5
- interface PhecdaEmitter {
6
- on<N extends keyof Events>(eventName: N, cb: (args: Events[N]) => void): void;
7
- off<N extends keyof Events>(eventName: N, cb?: (args: Events[N]) => void): void;
8
- emit<N extends keyof Events>(eventName: N, param: Events[N]): void;
9
- }
10
- interface PhecdaInstance {
11
- useOMap: Map<any, any>;
12
- useVMap: WeakMap<any, any>;
13
- useRMap: WeakMap<any, any>;
14
- fnMap: WeakMap<any, any>;
15
- }
16
-
17
- declare const emitter: PhecdaEmitter;
18
- declare function setActivePhecda(phecda: PhecdaInstance): void;
19
- declare function getActivePhecda(): PhecdaInstance;
20
- declare function useO<T extends new (...args: any) => any>(module: T): any;
21
- declare function useR<T extends new (...args: any) => any>(module: T): [InstanceType<T>, InstanceType<T>];
22
- declare function createPhecdaContext(): ({ children }: any) => react.FunctionComponentElement<react.ProviderProps<null>>;
23
-
24
- declare function wrapError(target: any, key: PropertyKey, errorHandler: Function): (...args: any) => any;
25
- declare function isAsyncFunc(fn: Function): boolean;
26
-
27
- export { createPhecdaContext, emitter, getActivePhecda, isAsyncFunc, setActivePhecda, useO, useR, wrapError };
1
+ import { Construct, Events } from 'phecda-web';
2
+ export * from 'phecda-web';
3
+
4
+ declare function useO<T extends Construct>(module: T): any;
5
+ declare function useR<T extends Construct>(module: T): [InstanceType<T>, InstanceType<T>];
6
+ declare function createPhecda(): {
7
+ load(state: any): any;
8
+ unmount(): Promise<void>;
9
+ };
10
+ declare function useEvent<Key extends keyof Events>(eventName: Key, cb: (event: Events[Key]) => void): ((arg: Events[Key]) => void)[];
11
+ declare function initialize<M extends Construct>(module: M, deleteOtherProperty?: boolean): InstanceType<M> | void;
12
+
13
+ export { createPhecda, initialize, useEvent, useO, useR };
package/dist/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
7
  var __export = (target, all) => {
@@ -19,85 +17,45 @@ var __copyProps = (to, from, except, desc) => {
19
17
  return to;
20
18
  };
21
19
  var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
24
- mod
25
- ));
26
20
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
21
 
28
22
  // src/index.ts
29
23
  var src_exports = {};
30
24
  __export(src_exports, {
31
- createPhecdaContext: () => createPhecdaContext,
32
- emitter: () => emitter,
33
- getActivePhecda: () => getActivePhecda,
34
- isAsyncFunc: () => isAsyncFunc,
35
- setActivePhecda: () => setActivePhecda,
25
+ createPhecda: () => createPhecda,
26
+ initialize: () => initialize,
27
+ useEvent: () => useEvent,
36
28
  useO: () => useO,
37
- useR: () => useR,
38
- wrapError: () => wrapError
29
+ useR: () => useR
39
30
  });
40
31
  module.exports = __toCommonJS(src_exports);
41
32
 
42
- // src/composables.ts
33
+ // src/core.ts
43
34
  var import_valtio = require("valtio");
44
- var import_phecda_core = require("phecda-core");
45
35
  var import_react = require("react");
46
- var import_mitt = __toESM(require("mitt"));
47
-
48
- // src/utils.ts
49
- function wrapError(target, key, errorHandler) {
50
- if (isAsyncFunc(target[key])) {
51
- return (...args) => {
52
- return target[key].apply(target, args).catch(errorHandler);
53
- };
54
- } else {
55
- return (...args) => {
56
- try {
57
- return target[key].apply(target, args);
58
- } catch (e) {
59
- return errorHandler(e);
60
- }
61
- };
62
- }
63
- }
64
- __name(wrapError, "wrapError");
65
- function isAsyncFunc(fn) {
66
- return fn[Symbol.toStringTag] === "AsyncFunction";
67
- }
68
- __name(isAsyncFunc, "isAsyncFunc");
69
-
70
- // src/composables.ts
71
- var emitter = (0, import_mitt.default)();
72
- var activePhecda = {
73
- useOMap: /* @__PURE__ */ new Map(),
74
- useVMap: /* @__PURE__ */ new WeakMap(),
75
- useRMap: /* @__PURE__ */ new WeakMap(),
76
- fnMap: /* @__PURE__ */ new WeakMap()
77
- };
78
- function setActivePhecda(phecda) {
79
- activePhecda = phecda;
80
- }
81
- __name(setActivePhecda, "setActivePhecda");
82
- function getActivePhecda() {
83
- return activePhecda;
84
- }
85
- __name(getActivePhecda, "getActivePhecda");
36
+ var import_phecda_web = require("phecda-web");
86
37
  function useO(module2) {
87
- const { useOMap } = getActivePhecda();
88
- const tag = (0, import_phecda_core.getTag)(module2) || module2.name;
89
- if (useOMap.has(tag))
90
- return useOMap.get(tag);
38
+ const { state, _o: oMap } = (0, import_phecda_web.getActiveInstance)();
39
+ if (module2.prototype.__ISOLATE__) {
40
+ const instance2 = new module2();
41
+ return instance2;
42
+ }
43
+ const tag = (0, import_phecda_web.getTag)(module2);
44
+ if (tag in state) {
45
+ if (oMap.get(state[tag]) !== module2)
46
+ console.warn(`Synonym module: Module taged "${String(tag)}" has been loaded before, so won't load Module "${module2.name}"`);
47
+ return state[tag];
48
+ }
91
49
  const instance = new module2();
92
- useOMap.set(tag, instance);
50
+ state[tag] = instance;
93
51
  return instance;
94
52
  }
95
53
  __name(useO, "useO");
96
54
  function useR(module2) {
97
- const { useRMap, fnMap } = getActivePhecda();
55
+ const { _r: rmap, _f: fmap } = (0, import_phecda_web.getActiveInstance)();
98
56
  const instance = useO(module2);
99
- if (useRMap.has(instance)) {
100
- const proxyInstance2 = useRMap.get(instance);
57
+ if (rmap.has(instance)) {
58
+ const proxyInstance2 = rmap.get(instance);
101
59
  return [
102
60
  (0, import_valtio.useSnapshot)(proxyInstance2),
103
61
  proxyInstance2
@@ -105,14 +63,16 @@ function useR(module2) {
105
63
  }
106
64
  const proxyInstance = (0, import_valtio.proxy)(new Proxy(instance, {
107
65
  get(target, key) {
66
+ if (key === "_promise")
67
+ return target[key];
108
68
  if (typeof target[key] === "function") {
109
- if (fnMap.has(target[key]))
110
- return fnMap.get(target[key]);
111
- const errorHandler = (0, import_phecda_core.getHandler)(target, key).find((item) => item.error)?.error;
69
+ if (fmap.has(target[key]))
70
+ return fmap.get(target[key]);
71
+ const errorHandler = (0, import_phecda_web.getHandler)(target, key).find((item) => item.error)?.error;
112
72
  if (!errorHandler)
113
73
  return target[key].bind(target);
114
- const wrapper = wrapError(target, key, errorHandler);
115
- fnMap.set(target[key], wrapper);
74
+ const wrapper = (0, import_phecda_web.wrapError)(target, key, errorHandler);
75
+ fmap.set(target[key], wrapper);
116
76
  return wrapper;
117
77
  }
118
78
  return target[key];
@@ -122,85 +82,61 @@ function useR(module2) {
122
82
  return true;
123
83
  }
124
84
  }));
125
- (0, import_phecda_core.register)(proxyInstance);
126
- useRMap.set(instance, proxyInstance);
85
+ instance._promise = (0, import_phecda_web.invokeHandler)("init", proxyInstance);
86
+ rmap.set(instance, proxyInstance);
127
87
  return [
128
88
  (0, import_valtio.useSnapshot)(proxyInstance),
129
89
  proxyInstance
130
90
  ];
131
91
  }
132
92
  __name(useR, "useR");
133
- function createPhecdaContext() {
134
- const { Provider } = (0, import_react.createContext)(null);
135
- let eventRecord = [];
136
- return ({ children }) => {
137
- (0, import_react.useEffect)(() => {
138
- return () => {
139
- getActivePhecda().useOMap.clear();
140
- eventRecord.forEach(([eventName, handler]) => emitter.off(eventName, handler));
141
- eventRecord = [];
142
- };
143
- }, []);
144
- if (!(0, import_phecda_core.getProperty)("watcher")) {
145
- (0, import_phecda_core.injectProperty)("watcher", ({ eventName, instance, key, options }) => {
146
- const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
147
- if (options?.once) {
148
- const handler = /* @__PURE__ */ __name((...args) => {
149
- fn(...args);
150
- emitter.off(eventName, handler);
151
- }, "handler");
152
- emitter.on(eventName, handler);
153
- eventRecord.push([
154
- eventName,
155
- handler
156
- ]);
157
- } else {
158
- eventRecord.push([
159
- eventName,
160
- fn
161
- ]);
162
- emitter.on(eventName, fn);
163
- }
164
- });
165
- }
166
- if (!(0, import_phecda_core.getProperty)("storage")) {
167
- (0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance }) => {
168
- if (!tag)
169
- return;
170
- const initstr = localStorage.getItem(tag);
171
- if (initstr) {
172
- const data = JSON.parse(initstr);
173
- if (key) {
174
- instance[key] = data;
175
- } else {
176
- for (const i in data) {
177
- if (i)
178
- instance[i] = data[i];
179
- }
180
- }
181
- }
182
- globalThis.addEventListener("beforeunload", () => {
183
- localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
184
- });
185
- });
93
+ function createPhecda() {
94
+ (0, import_phecda_web.resetActiveInstance)();
95
+ return {
96
+ load(state) {
97
+ const instance = (0, import_phecda_web.getActiveInstance)();
98
+ instance.state = state;
99
+ return this;
100
+ },
101
+ async unmount() {
102
+ const { state } = (0, import_phecda_web.getActiveInstance)();
103
+ await Object.values(state).map((ins) => (0, import_phecda_web.invokeHandler)("unmount", ins));
104
+ (0, import_phecda_web.resetActiveInstance)();
186
105
  }
187
- return (0, import_react.createElement)(Provider, {
188
- value: null
189
- }, children);
190
106
  };
191
107
  }
192
- __name(createPhecdaContext, "createPhecdaContext");
108
+ __name(createPhecda, "createPhecda");
109
+ function useEvent(eventName, cb) {
110
+ (0, import_react.useEffect)(() => {
111
+ return () => emitter.off(eventName, cb);
112
+ });
113
+ emitter.on(eventName, cb);
114
+ return [
115
+ (arg) => emitter.emit(eventName, arg),
116
+ () => emitter.off(eventName, cb)
117
+ ];
118
+ }
119
+ __name(useEvent, "useEvent");
120
+ function initialize(module2, deleteOtherProperty = true) {
121
+ const instance = useO(module2);
122
+ const newInstance = new module2();
123
+ Object.assign(instance, newInstance);
124
+ if (deleteOtherProperty) {
125
+ for (const key in instance) {
126
+ if (!(key in newInstance))
127
+ delete instance[key];
128
+ }
129
+ }
130
+ }
131
+ __name(initialize, "initialize");
193
132
 
194
133
  // src/index.ts
195
- __reExport(src_exports, require("phecda-core"), module.exports);
134
+ __reExport(src_exports, require("phecda-web"), module.exports);
196
135
  // Annotate the CommonJS export names for ESM import in node:
197
136
  0 && (module.exports = {
198
- createPhecdaContext,
199
- emitter,
200
- getActivePhecda,
201
- isAsyncFunc,
202
- setActivePhecda,
137
+ createPhecda,
138
+ initialize,
139
+ useEvent,
203
140
  useO,
204
- useR,
205
- wrapError
141
+ useR
206
142
  });
package/dist/index.mjs CHANGED
@@ -1,65 +1,32 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
- // src/composables.ts
4
+ // src/core.ts
5
5
  import { proxy, useSnapshot } from "valtio";
6
- import { getHandler, getProperty, getTag, injectProperty, register } from "phecda-core";
7
- import { createContext, createElement, useEffect } from "react";
8
- import mitt from "mitt";
9
-
10
- // src/utils.ts
11
- function wrapError(target, key, errorHandler) {
12
- if (isAsyncFunc(target[key])) {
13
- return (...args) => {
14
- return target[key].apply(target, args).catch(errorHandler);
15
- };
16
- } else {
17
- return (...args) => {
18
- try {
19
- return target[key].apply(target, args);
20
- } catch (e) {
21
- return errorHandler(e);
22
- }
23
- };
24
- }
25
- }
26
- __name(wrapError, "wrapError");
27
- function isAsyncFunc(fn) {
28
- return fn[Symbol.toStringTag] === "AsyncFunction";
29
- }
30
- __name(isAsyncFunc, "isAsyncFunc");
31
-
32
- // src/composables.ts
33
- var emitter = mitt();
34
- var activePhecda = {
35
- useOMap: /* @__PURE__ */ new Map(),
36
- useVMap: /* @__PURE__ */ new WeakMap(),
37
- useRMap: /* @__PURE__ */ new WeakMap(),
38
- fnMap: /* @__PURE__ */ new WeakMap()
39
- };
40
- function setActivePhecda(phecda) {
41
- activePhecda = phecda;
42
- }
43
- __name(setActivePhecda, "setActivePhecda");
44
- function getActivePhecda() {
45
- return activePhecda;
46
- }
47
- __name(getActivePhecda, "getActivePhecda");
6
+ import { useEffect } from "react";
7
+ import { getActiveInstance, getHandler, getTag, invokeHandler, resetActiveInstance, wrapError } from "phecda-web";
48
8
  function useO(module) {
49
- const { useOMap } = getActivePhecda();
50
- const tag = getTag(module) || module.name;
51
- if (useOMap.has(tag))
52
- return useOMap.get(tag);
9
+ const { state, _o: oMap } = getActiveInstance();
10
+ if (module.prototype.__ISOLATE__) {
11
+ const instance2 = new module();
12
+ return instance2;
13
+ }
14
+ const tag = getTag(module);
15
+ if (tag in state) {
16
+ if (oMap.get(state[tag]) !== module)
17
+ console.warn(`Synonym module: Module taged "${String(tag)}" has been loaded before, so won't load Module "${module.name}"`);
18
+ return state[tag];
19
+ }
53
20
  const instance = new module();
54
- useOMap.set(tag, instance);
21
+ state[tag] = instance;
55
22
  return instance;
56
23
  }
57
24
  __name(useO, "useO");
58
25
  function useR(module) {
59
- const { useRMap, fnMap } = getActivePhecda();
26
+ const { _r: rmap, _f: fmap } = getActiveInstance();
60
27
  const instance = useO(module);
61
- if (useRMap.has(instance)) {
62
- const proxyInstance2 = useRMap.get(instance);
28
+ if (rmap.has(instance)) {
29
+ const proxyInstance2 = rmap.get(instance);
63
30
  return [
64
31
  useSnapshot(proxyInstance2),
65
32
  proxyInstance2
@@ -67,14 +34,16 @@ function useR(module) {
67
34
  }
68
35
  const proxyInstance = proxy(new Proxy(instance, {
69
36
  get(target, key) {
37
+ if (key === "_promise")
38
+ return target[key];
70
39
  if (typeof target[key] === "function") {
71
- if (fnMap.has(target[key]))
72
- return fnMap.get(target[key]);
40
+ if (fmap.has(target[key]))
41
+ return fmap.get(target[key]);
73
42
  const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
74
43
  if (!errorHandler)
75
44
  return target[key].bind(target);
76
45
  const wrapper = wrapError(target, key, errorHandler);
77
- fnMap.set(target[key], wrapper);
46
+ fmap.set(target[key], wrapper);
78
47
  return wrapper;
79
48
  }
80
49
  return target[key];
@@ -84,84 +53,60 @@ function useR(module) {
84
53
  return true;
85
54
  }
86
55
  }));
87
- register(proxyInstance);
88
- useRMap.set(instance, proxyInstance);
56
+ instance._promise = invokeHandler("init", proxyInstance);
57
+ rmap.set(instance, proxyInstance);
89
58
  return [
90
59
  useSnapshot(proxyInstance),
91
60
  proxyInstance
92
61
  ];
93
62
  }
94
63
  __name(useR, "useR");
95
- function createPhecdaContext() {
96
- const { Provider } = createContext(null);
97
- let eventRecord = [];
98
- return ({ children }) => {
99
- useEffect(() => {
100
- return () => {
101
- getActivePhecda().useOMap.clear();
102
- eventRecord.forEach(([eventName, handler]) => emitter.off(eventName, handler));
103
- eventRecord = [];
104
- };
105
- }, []);
106
- if (!getProperty("watcher")) {
107
- injectProperty("watcher", ({ eventName, instance, key, options }) => {
108
- const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
109
- if (options?.once) {
110
- const handler = /* @__PURE__ */ __name((...args) => {
111
- fn(...args);
112
- emitter.off(eventName, handler);
113
- }, "handler");
114
- emitter.on(eventName, handler);
115
- eventRecord.push([
116
- eventName,
117
- handler
118
- ]);
119
- } else {
120
- eventRecord.push([
121
- eventName,
122
- fn
123
- ]);
124
- emitter.on(eventName, fn);
125
- }
126
- });
127
- }
128
- if (!getProperty("storage")) {
129
- injectProperty("storage", ({ tag, key, instance }) => {
130
- if (!tag)
131
- return;
132
- const initstr = localStorage.getItem(tag);
133
- if (initstr) {
134
- const data = JSON.parse(initstr);
135
- if (key) {
136
- instance[key] = data;
137
- } else {
138
- for (const i in data) {
139
- if (i)
140
- instance[i] = data[i];
141
- }
142
- }
143
- }
144
- globalThis.addEventListener("beforeunload", () => {
145
- localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
146
- });
147
- });
64
+ function createPhecda() {
65
+ resetActiveInstance();
66
+ return {
67
+ load(state) {
68
+ const instance = getActiveInstance();
69
+ instance.state = state;
70
+ return this;
71
+ },
72
+ async unmount() {
73
+ const { state } = getActiveInstance();
74
+ await Object.values(state).map((ins) => invokeHandler("unmount", ins));
75
+ resetActiveInstance();
148
76
  }
149
- return createElement(Provider, {
150
- value: null
151
- }, children);
152
77
  };
153
78
  }
154
- __name(createPhecdaContext, "createPhecdaContext");
79
+ __name(createPhecda, "createPhecda");
80
+ function useEvent(eventName, cb) {
81
+ useEffect(() => {
82
+ return () => emitter.off(eventName, cb);
83
+ });
84
+ emitter.on(eventName, cb);
85
+ return [
86
+ (arg) => emitter.emit(eventName, arg),
87
+ () => emitter.off(eventName, cb)
88
+ ];
89
+ }
90
+ __name(useEvent, "useEvent");
91
+ function initialize(module, deleteOtherProperty = true) {
92
+ const instance = useO(module);
93
+ const newInstance = new module();
94
+ Object.assign(instance, newInstance);
95
+ if (deleteOtherProperty) {
96
+ for (const key in instance) {
97
+ if (!(key in newInstance))
98
+ delete instance[key];
99
+ }
100
+ }
101
+ }
102
+ __name(initialize, "initialize");
155
103
 
156
104
  // src/index.ts
157
- export * from "phecda-core";
105
+ export * from "phecda-web";
158
106
  export {
159
- createPhecdaContext,
160
- emitter,
161
- getActivePhecda,
162
- isAsyncFunc,
163
- setActivePhecda,
107
+ createPhecda,
108
+ initialize,
109
+ useEvent,
164
110
  useO,
165
- useR,
166
- wrapError
111
+ useR
167
112
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "phecda-react",
3
- "version": "0.0.1-alpha.3",
4
- "description": "provide store with phecda function to react",
3
+ "version": "0.1.0-alpha.10",
4
+ "description": "provide phecda function to react by valtio",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -12,10 +12,9 @@
12
12
  ],
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "mitt": "^3.0.0",
16
15
  "react": "^18.2.0",
17
16
  "valtio": "^1.13.0",
18
- "phecda-core": "3.0.0-alpha.2"
17
+ "phecda-web": "1.0.1-alpha.6"
19
18
  },
20
19
  "devDependencies": {
21
20
  "@types/react": "^18.2.48",