phecda-react 0.1.0-alpha.9 → 0.1.0-beta.15

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,13 +1,13 @@
1
1
  import { Construct, Events } from 'phecda-web';
2
2
  export * from 'phecda-web';
3
3
 
4
- declare function useO<T extends Construct>(module: T): any;
5
- declare function useR<T extends Construct>(module: T): [InstanceType<T>, InstanceType<T>];
4
+ declare function useO<T extends Construct>(model: T): any;
5
+ declare function useR<T extends Construct>(model: T): [InstanceType<T>, InstanceType<T>];
6
6
  declare function createPhecda(): {
7
- load(state: any): any;
7
+ load(state?: any): any;
8
8
  unmount(): Promise<void>;
9
9
  };
10
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;
11
+ declare function initialize<M extends Construct>(model: M, deleteOtherProperty?: boolean): InstanceType<M> | void;
12
12
 
13
13
  export { createPhecda, initialize, useEvent, useO, useR };
package/dist/index.js CHANGED
@@ -34,56 +34,36 @@ module.exports = __toCommonJS(src_exports);
34
34
  var import_valtio = require("valtio");
35
35
  var import_react = require("react");
36
36
  var import_phecda_web = require("phecda-web");
37
- function useO(module2) {
38
- const { state, _o: oMap } = (0, import_phecda_web.getActiveInstance)();
39
- if (module2.prototype.__ISOLATE__) {
40
- const instance2 = new module2();
37
+ function useO(model) {
38
+ const { state, origin } = (0, import_phecda_web.getActiveInstance)();
39
+ if ((0, import_phecda_web.get)(model.prototype, "isolate")) {
40
+ const instance2 = new model();
41
41
  return instance2;
42
42
  }
43
- const tag = (0, import_phecda_web.getTag)(module2);
43
+ const tag = (0, import_phecda_web.getTag)(model);
44
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}"`);
45
+ if (origin.get(state[tag]) !== model)
46
+ console.warn(`Synonym module: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
47
47
  return state[tag];
48
48
  }
49
- const instance = new module2();
49
+ const instance = new model();
50
50
  state[tag] = instance;
51
51
  return instance;
52
52
  }
53
53
  __name(useO, "useO");
54
- function useR(module2) {
55
- const { _r: rmap, _f: fmap } = (0, import_phecda_web.getActiveInstance)();
56
- const instance = useO(module2);
57
- if (rmap.has(instance)) {
58
- const proxyInstance2 = rmap.get(instance);
54
+ function useR(model) {
55
+ const { cache: cacheMap } = (0, import_phecda_web.getActiveInstance)();
56
+ const instance = useO(model);
57
+ if (cacheMap.has(instance)) {
58
+ const proxyInstance2 = cacheMap.get(instance);
59
59
  return [
60
60
  (0, import_valtio.useSnapshot)(proxyInstance2),
61
61
  proxyInstance2
62
62
  ];
63
63
  }
64
- const proxyInstance = (0, import_valtio.proxy)(new Proxy(instance, {
65
- get(target, key) {
66
- if (key === "_promise")
67
- return target[key];
68
- if (typeof target[key] === "function") {
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;
72
- if (!errorHandler)
73
- return target[key].bind(target);
74
- const wrapper = (0, import_phecda_web.wrapError)(target, key, errorHandler);
75
- fmap.set(target[key], wrapper);
76
- return wrapper;
77
- }
78
- return target[key];
79
- },
80
- set(target, key, v) {
81
- target[key] = v;
82
- return true;
83
- }
84
- }));
85
- instance._promise = (0, import_phecda_web.registerSerial)(proxyInstance);
86
- rmap.set(instance, proxyInstance);
64
+ const proxyInstance = (0, import_valtio.proxy)(instance);
65
+ instance._promise = (0, import_phecda_web.invokeHandler)("init", proxyInstance);
66
+ cacheMap.set(instance, proxyInstance);
87
67
  return [
88
68
  (0, import_valtio.useSnapshot)(proxyInstance),
89
69
  proxyInstance
@@ -95,12 +75,13 @@ function createPhecda() {
95
75
  return {
96
76
  load(state) {
97
77
  const instance = (0, import_phecda_web.getActiveInstance)();
98
- instance.state = state;
78
+ if (state)
79
+ instance.state = state;
99
80
  return this;
100
81
  },
101
82
  async unmount() {
102
83
  const { state } = (0, import_phecda_web.getActiveInstance)();
103
- await Object.values(state).map((ins) => (0, import_phecda_web.unmountParallel)(ins));
84
+ await Object.values(state).map((ins) => (0, import_phecda_web.invokeHandler)("unmount", ins));
104
85
  (0, import_phecda_web.resetActiveInstance)();
105
86
  }
106
87
  };
@@ -108,18 +89,18 @@ function createPhecda() {
108
89
  __name(createPhecda, "createPhecda");
109
90
  function useEvent(eventName, cb) {
110
91
  (0, import_react.useEffect)(() => {
111
- return () => emitter.off(eventName, cb);
92
+ return () => import_phecda_web.emitter.off(eventName, cb);
112
93
  });
113
- emitter.on(eventName, cb);
94
+ import_phecda_web.emitter.on(eventName, cb);
114
95
  return [
115
- (arg) => emitter.emit(eventName, arg),
116
- () => emitter.off(eventName, cb)
96
+ (arg) => import_phecda_web.emitter.emit(eventName, arg),
97
+ () => import_phecda_web.emitter.off(eventName, cb)
117
98
  ];
118
99
  }
119
100
  __name(useEvent, "useEvent");
120
- function initialize(module2, deleteOtherProperty = true) {
121
- const instance = useO(module2);
122
- const newInstance = new module2();
101
+ function initialize(model, deleteOtherProperty = true) {
102
+ const instance = useO(model);
103
+ const newInstance = new model();
123
104
  Object.assign(instance, newInstance);
124
105
  if (deleteOtherProperty) {
125
106
  for (const key in instance) {
package/dist/index.mjs CHANGED
@@ -4,57 +4,37 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  // src/core.ts
5
5
  import { proxy, useSnapshot } from "valtio";
6
6
  import { useEffect } from "react";
7
- import { getActiveInstance, getHandler, getTag, registerSerial, resetActiveInstance, unmountParallel, wrapError } from "phecda-web";
8
- function useO(module) {
9
- const { state, _o: oMap } = getActiveInstance();
10
- if (module.prototype.__ISOLATE__) {
11
- const instance2 = new module();
7
+ import { emitter, get, getActiveInstance, getTag, invokeHandler, resetActiveInstance } from "phecda-web";
8
+ function useO(model) {
9
+ const { state, origin } = getActiveInstance();
10
+ if (get(model.prototype, "isolate")) {
11
+ const instance2 = new model();
12
12
  return instance2;
13
13
  }
14
- const tag = getTag(module);
14
+ const tag = getTag(model);
15
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}"`);
16
+ if (origin.get(state[tag]) !== model)
17
+ console.warn(`Synonym module: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
18
18
  return state[tag];
19
19
  }
20
- const instance = new module();
20
+ const instance = new model();
21
21
  state[tag] = instance;
22
22
  return instance;
23
23
  }
24
24
  __name(useO, "useO");
25
- function useR(module) {
26
- const { _r: rmap, _f: fmap } = getActiveInstance();
27
- const instance = useO(module);
28
- if (rmap.has(instance)) {
29
- const proxyInstance2 = rmap.get(instance);
25
+ function useR(model) {
26
+ const { cache: cacheMap } = getActiveInstance();
27
+ const instance = useO(model);
28
+ if (cacheMap.has(instance)) {
29
+ const proxyInstance2 = cacheMap.get(instance);
30
30
  return [
31
31
  useSnapshot(proxyInstance2),
32
32
  proxyInstance2
33
33
  ];
34
34
  }
35
- const proxyInstance = proxy(new Proxy(instance, {
36
- get(target, key) {
37
- if (key === "_promise")
38
- return target[key];
39
- if (typeof target[key] === "function") {
40
- if (fmap.has(target[key]))
41
- return fmap.get(target[key]);
42
- const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
43
- if (!errorHandler)
44
- return target[key].bind(target);
45
- const wrapper = wrapError(target, key, errorHandler);
46
- fmap.set(target[key], wrapper);
47
- return wrapper;
48
- }
49
- return target[key];
50
- },
51
- set(target, key, v) {
52
- target[key] = v;
53
- return true;
54
- }
55
- }));
56
- instance._promise = registerSerial(proxyInstance);
57
- rmap.set(instance, proxyInstance);
35
+ const proxyInstance = proxy(instance);
36
+ instance._promise = invokeHandler("init", proxyInstance);
37
+ cacheMap.set(instance, proxyInstance);
58
38
  return [
59
39
  useSnapshot(proxyInstance),
60
40
  proxyInstance
@@ -66,12 +46,13 @@ function createPhecda() {
66
46
  return {
67
47
  load(state) {
68
48
  const instance = getActiveInstance();
69
- instance.state = state;
49
+ if (state)
50
+ instance.state = state;
70
51
  return this;
71
52
  },
72
53
  async unmount() {
73
54
  const { state } = getActiveInstance();
74
- await Object.values(state).map((ins) => unmountParallel(ins));
55
+ await Object.values(state).map((ins) => invokeHandler("unmount", ins));
75
56
  resetActiveInstance();
76
57
  }
77
58
  };
@@ -88,9 +69,9 @@ function useEvent(eventName, cb) {
88
69
  ];
89
70
  }
90
71
  __name(useEvent, "useEvent");
91
- function initialize(module, deleteOtherProperty = true) {
92
- const instance = useO(module);
93
- const newInstance = new module();
72
+ function initialize(model, deleteOtherProperty = true) {
73
+ const instance = useO(model);
74
+ const newInstance = new model();
94
75
  Object.assign(instance, newInstance);
95
76
  if (deleteOtherProperty) {
96
77
  for (const key in instance) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-react",
3
- "version": "0.1.0-alpha.9",
3
+ "version": "0.1.0-beta.15",
4
4
  "description": "provide phecda function to react by valtio",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -14,7 +14,7 @@
14
14
  "dependencies": {
15
15
  "react": "^18.2.0",
16
16
  "valtio": "^1.13.0",
17
- "phecda-web": "1.0.1-alpha.5"
17
+ "phecda-web": "1.0.1-beta.11"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@types/react": "^18.2.48",