phecda-react 0.1.0 → 1.0.1

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,20 @@
1
+ # phecda-react
2
+
3
+ > it need a compile plugin to support `emitDecoratorMetadata` in `vite/webpack`
4
+
5
+ it depends on `valtio`
6
+
7
+ ## quick
8
+
9
+ ```tsx
10
+ export function Home() {
11
+ const [getter, setter] = useR(UserModel)
12
+
13
+ return (
14
+ <>
15
+ <div>{getter.name}</div>
16
+ <button onClick={() => { setter.changeName() }}></button>
17
+ </>
18
+ )
19
+ }
20
+ ```
package/dist/index.d.ts CHANGED
@@ -1,13 +1,37 @@
1
- import { Construct, Events } from 'phecda-web';
1
+ import * as react from 'react';
2
+ import { WebPhecda, Construct, Events } from 'phecda-web';
2
3
  export * from 'phecda-web';
3
4
 
4
- declare function useO<T extends Construct>(model: T): any;
5
- declare function useR<T extends Construct>(model: T): [InstanceType<T>, InstanceType<T>];
6
- declare function createPhecda(): {
7
- load(state?: any): any;
8
- unmount(): Promise<void>;
9
- };
5
+ declare const PhecdaContext: react.Context<WebPhecda | undefined>;
6
+ declare const phecdaSymbol: unique symbol;
7
+ declare function createPhecda(): WebPhecda;
8
+
9
+ declare function usePhecda(): WebPhecda;
10
+ declare function getPhecda(phecda?: WebPhecda): WebPhecda;
11
+ declare function getR<Model extends Construct>(model: Model, phecda?: WebPhecda): InstanceType<Model>;
10
12
  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>(model: M, deleteOtherProperty?: boolean): InstanceType<M> | void;
13
+ declare function useR<Model extends Construct>(model: Model): (InstanceType<Model> | (InstanceType<Model> extends infer T ? T extends InstanceType<Model> ? T extends {
14
+ $$valtioSnapshot: infer S;
15
+ } ? S : T extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T : T extends Promise<unknown> ? Awaited<T> : T extends object ? T extends infer T_1 extends object ? { readonly [K in keyof T_1]: T[K] extends infer T_2 ? T_2 extends T[K] ? T_2 extends {
16
+ $$valtioSnapshot: infer S;
17
+ } ? S : T_2 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_2 : T_2 extends Promise<unknown> ? Awaited<T_2> : T_2 extends object ? T_2 extends infer T_3 extends object ? { readonly [K_1 in keyof T_3]: T_2[K_1] extends infer T_4 ? T_4 extends T_2[K_1] ? T_4 extends {
18
+ $$valtioSnapshot: infer S;
19
+ } ? S : T_4 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_4 : T_4 extends Promise<unknown> ? Awaited<T_4> : T_4 extends object ? T_4 extends infer T_5 extends object ? { readonly [K_2 in keyof T_5]: T_4[K_2] extends infer T_6 ? T_6 extends T_4[K_2] ? T_6 extends {
20
+ $$valtioSnapshot: infer S;
21
+ } ? S : T_6 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_6 : T_6 extends Promise<unknown> ? Awaited<T_6> : T_6 extends object ? T_6 extends infer T_7 extends object ? { readonly [K_3 in keyof T_7]: T_6[K_3] extends infer T_8 ? T_8 extends T_6[K_3] ? T_8 extends {
22
+ $$valtioSnapshot: infer S;
23
+ } ? S : T_8 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_8 : T_8 extends Promise<unknown> ? Awaited<T_8> : T_8 extends object ? T_8 extends infer T_9 extends object ? { readonly [K_4 in keyof T_9]: T_8[K_4] extends infer T_10 ? T_10 extends T_8[K_4] ? T_10 extends {
24
+ $$valtioSnapshot: infer S;
25
+ } ? S : T_10 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_10 : T_10 extends Promise<unknown> ? Awaited<T_10> : T_10 extends object ? T_10 extends infer T_11 extends object ? { readonly [K_5 in keyof T_11]: T_10[K_5] extends infer T_12 ? T_12 extends T_10[K_5] ? T_12 extends {
26
+ $$valtioSnapshot: infer S;
27
+ } ? S : T_12 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_12 : T_12 extends Promise<unknown> ? Awaited<T_12> : T_12 extends object ? T_12 extends infer T_13 extends object ? { readonly [K_6 in keyof T_13]: T_12[K_6] extends infer T_14 ? T_14 extends T_12[K_6] ? T_14 extends {
28
+ $$valtioSnapshot: infer S;
29
+ } ? S : T_14 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_14 : T_14 extends Promise<unknown> ? Awaited<T_14> : T_14 extends object ? T_14 extends infer T_15 extends object ? { readonly [K_7 in keyof T_15]: T_14[K_7] extends infer T_16 ? T_16 extends T_14[K_7] ? T_16 extends {
30
+ $$valtioSnapshot: infer S;
31
+ } ? S : T_16 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_16 : T_16 extends Promise<unknown> ? Awaited<T_16> : T_16 extends object ? T_16 extends infer T_17 extends object ? { readonly [K_8 in keyof T_17]: T_16[K_8] extends infer T_18 ? T_18 extends T_16[K_8] ? T_18 extends {
32
+ $$valtioSnapshot: infer S;
33
+ } ? S : T_18 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_18 : T_18 extends Promise<unknown> ? Awaited<T_18> : T_18 extends object ? T_18 extends infer T_19 extends object ? { readonly [K_9 in keyof T_19]: T_18[K_9] extends infer T_20 ? T_20 extends T_18[K_9] ? T_20 extends {
34
+ $$valtioSnapshot: infer S;
35
+ } ? S : T_20 extends RegExp | Error | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_20 : T_20 extends Promise<unknown> ? Awaited<T_20> : T_20 extends object ? T_20 extends infer T_21 extends object ? { readonly [K_10 in keyof T_21]: any; } : never : T_20 : never : never; } : never : T_18 : never : never; } : never : T_16 : never : never; } : never : T_14 : never : never; } : never : T_12 : never : never; } : never : T_10 : never : never; } : never : T_8 : never : never; } : never : T_6 : never : never; } : never : T_4 : never : never; } : never : T_2 : never : never; } : never : T : never : never))[];
12
36
 
13
- export { createPhecda, initialize, useEvent, useO, useR };
37
+ export { PhecdaContext, createPhecda, getPhecda, getR, phecdaSymbol, useEvent, usePhecda, useR };
package/dist/index.js CHANGED
@@ -22,108 +22,88 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
22
22
  // src/index.ts
23
23
  var src_exports = {};
24
24
  __export(src_exports, {
25
+ PhecdaContext: () => PhecdaContext,
25
26
  createPhecda: () => createPhecda,
26
- initialize: () => initialize,
27
+ getPhecda: () => getPhecda,
28
+ getR: () => getR,
29
+ phecdaSymbol: () => phecdaSymbol,
27
30
  useEvent: () => useEvent,
28
- useO: () => useO,
31
+ usePhecda: () => usePhecda,
29
32
  useR: () => useR
30
33
  });
31
34
  module.exports = __toCommonJS(src_exports);
32
35
 
33
36
  // src/core.ts
34
37
  var import_valtio = require("valtio");
35
- var import_react = require("react");
36
38
  var import_phecda_web = require("phecda-web");
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
- return instance2;
42
- }
43
- const tag = (0, import_phecda_web.getTag)(model);
44
- if (tag in state) {
45
- if (process.env.NODE_ENV === "development") {
46
- if (origin.get(state[tag]) === model)
47
- return state[tag];
48
- } else {
49
- if (origin.get(state[tag]) !== model)
50
- console.warn(`Synonym model: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
51
- return state[tag];
52
- }
53
- return state[tag];
54
- }
55
- const instance = new model();
56
- state[tag] = instance;
57
- return instance;
58
- }
59
- __name(useO, "useO");
60
- function useR(model) {
61
- const { cache: cacheMap } = (0, import_phecda_web.getActiveInstance)();
62
- const instance = useO(model);
63
- if (cacheMap.has(instance)) {
64
- const proxyInstance2 = cacheMap.get(instance);
65
- return [
66
- (0, import_valtio.useSnapshot)(proxyInstance2),
67
- proxyInstance2
68
- ];
69
- }
70
- const proxyInstance = (0, import_valtio.proxy)(instance);
71
- instance._promise = (0, import_phecda_web.invokeHandler)("init", proxyInstance);
72
- cacheMap.set(instance, proxyInstance);
73
- return [
74
- (0, import_valtio.useSnapshot)(proxyInstance),
75
- proxyInstance
76
- ];
77
- }
78
- __name(useR, "useR");
39
+ var import_react = require("react");
40
+ var PhecdaContext = (0, import_react.createContext)(void 0);
41
+ var phecdaSymbol = Symbol("");
79
42
  function createPhecda() {
80
- (0, import_phecda_web.resetActiveInstance)();
81
- return {
82
- load(state) {
83
- const instance = (0, import_phecda_web.getActiveInstance)();
84
- if (state)
85
- instance.state = state;
86
- return this;
87
- },
88
- async unmount() {
89
- const { state } = (0, import_phecda_web.getActiveInstance)();
90
- await Object.values(state).map((ins) => (0, import_phecda_web.invokeHandler)("unmount", ins));
91
- (0, import_phecda_web.resetActiveInstance)();
92
- }
93
- };
43
+ return new import_phecda_web.WebPhecda((instance) => {
44
+ return (0, import_valtio.proxy)(instance);
45
+ });
94
46
  }
95
47
  __name(createPhecda, "createPhecda");
48
+
49
+ // src/index.ts
50
+ __reExport(src_exports, require("phecda-web"), module.exports);
51
+
52
+ // src/hook.ts
53
+ var import_phecda_web2 = require("phecda-web");
54
+ var import_react2 = require("react");
55
+ var import_valtio2 = require("valtio");
56
+ var cacheMap = /* @__PURE__ */ new WeakMap();
57
+ function usePhecda() {
58
+ const activePhecda = (0, import_react2.useContext)(PhecdaContext);
59
+ if (!activePhecda)
60
+ throw new Error("[phecda-react]: must under <PhecdaContext.Provider></PhecdaContext.Provider> ");
61
+ if (!cacheMap.has(activePhecda))
62
+ cacheMap.set(activePhecda, (0, import_phecda_web2.bindMethod)(activePhecda));
63
+ return cacheMap.get(activePhecda);
64
+ }
65
+ __name(usePhecda, "usePhecda");
66
+ function getPhecda(phecda) {
67
+ const activePhecda = phecda || (0, import_phecda_web2.getDefaultPhecda)();
68
+ if (!activePhecda)
69
+ throw new Error("[phecda-react] manually inject the phecda instance if there is no default phecda");
70
+ if (!cacheMap.has(activePhecda))
71
+ cacheMap.set(activePhecda, (0, import_phecda_web2.bindMethod)(activePhecda));
72
+ return cacheMap.get(activePhecda);
73
+ }
74
+ __name(getPhecda, "getPhecda");
75
+ function getR(model, phecda) {
76
+ return getPhecda(phecda).init(model);
77
+ }
78
+ __name(getR, "getR");
96
79
  function useEvent(eventName, cb) {
97
- (0, import_react.useEffect)(() => {
98
- return () => import_phecda_web.emitter.off(eventName, cb);
80
+ (0, import_react2.useEffect)(() => {
81
+ return () => import_phecda_web2.emitter.off(eventName, cb);
99
82
  });
100
- import_phecda_web.emitter.on(eventName, cb);
83
+ import_phecda_web2.emitter.on(eventName, cb);
101
84
  return [
102
- (arg) => import_phecda_web.emitter.emit(eventName, arg),
103
- () => import_phecda_web.emitter.off(eventName, cb)
85
+ (arg) => import_phecda_web2.emitter.emit(eventName, arg),
86
+ () => import_phecda_web2.emitter.off(eventName, cb)
104
87
  ];
105
88
  }
106
89
  __name(useEvent, "useEvent");
107
- function initialize(model, deleteOtherProperty = true) {
108
- const instance = useO(model);
109
- const newInstance = new model();
110
- Object.assign(instance, newInstance);
111
- if (deleteOtherProperty) {
112
- for (const key in instance) {
113
- if (!(key in newInstance))
114
- delete instance[key];
115
- }
116
- }
90
+ function useR(model) {
91
+ const proxy2 = usePhecda().init(model);
92
+ return [
93
+ (0, import_valtio2.useSnapshot)(proxy2),
94
+ (0, import_phecda_web2.bindMethod)(proxy2)
95
+ ];
117
96
  }
118
- __name(initialize, "initialize");
119
-
120
- // src/index.ts
121
- __reExport(src_exports, require("phecda-web"), module.exports);
97
+ __name(useR, "useR");
122
98
  // Annotate the CommonJS export names for ESM import in node:
123
99
  0 && (module.exports = {
100
+ PhecdaContext,
124
101
  createPhecda,
125
- initialize,
102
+ getPhecda,
103
+ getR,
104
+ phecdaSymbol,
126
105
  useEvent,
127
- useO,
128
- useR
106
+ usePhecda,
107
+ useR,
108
+ ...require("phecda-web")
129
109
  });
package/dist/index.mjs CHANGED
@@ -2,68 +2,48 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/core.ts
5
- import { proxy, useSnapshot } from "valtio";
6
- import { useEffect } from "react";
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
- return instance2;
13
- }
14
- const tag = getTag(model);
15
- if (tag in state) {
16
- if (process.env.NODE_ENV === "development") {
17
- if (origin.get(state[tag]) === model)
18
- return state[tag];
19
- } else {
20
- if (origin.get(state[tag]) !== model)
21
- console.warn(`Synonym model: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
22
- return state[tag];
23
- }
24
- return state[tag];
25
- }
26
- const instance = new model();
27
- state[tag] = instance;
28
- return instance;
29
- }
30
- __name(useO, "useO");
31
- function useR(model) {
32
- const { cache: cacheMap } = getActiveInstance();
33
- const instance = useO(model);
34
- if (cacheMap.has(instance)) {
35
- const proxyInstance2 = cacheMap.get(instance);
36
- return [
37
- useSnapshot(proxyInstance2),
38
- proxyInstance2
39
- ];
40
- }
41
- const proxyInstance = proxy(instance);
42
- instance._promise = invokeHandler("init", proxyInstance);
43
- cacheMap.set(instance, proxyInstance);
44
- return [
45
- useSnapshot(proxyInstance),
46
- proxyInstance
47
- ];
48
- }
49
- __name(useR, "useR");
5
+ import { proxy } from "valtio";
6
+ import { WebPhecda } from "phecda-web";
7
+ import { createContext } from "react";
8
+ var PhecdaContext = createContext(void 0);
9
+ var phecdaSymbol = Symbol("");
50
10
  function createPhecda() {
51
- resetActiveInstance();
52
- return {
53
- load(state) {
54
- const instance = getActiveInstance();
55
- if (state)
56
- instance.state = state;
57
- return this;
58
- },
59
- async unmount() {
60
- const { state } = getActiveInstance();
61
- await Object.values(state).map((ins) => invokeHandler("unmount", ins));
62
- resetActiveInstance();
63
- }
64
- };
11
+ return new WebPhecda((instance) => {
12
+ return proxy(instance);
13
+ });
65
14
  }
66
15
  __name(createPhecda, "createPhecda");
16
+
17
+ // src/index.ts
18
+ export * from "phecda-web";
19
+
20
+ // src/hook.ts
21
+ import { bindMethod, emitter, getDefaultPhecda } from "phecda-web";
22
+ import { useContext, useEffect } from "react";
23
+ import { useSnapshot } from "valtio";
24
+ var cacheMap = /* @__PURE__ */ new WeakMap();
25
+ function usePhecda() {
26
+ const activePhecda = useContext(PhecdaContext);
27
+ if (!activePhecda)
28
+ throw new Error("[phecda-react]: must under <PhecdaContext.Provider></PhecdaContext.Provider> ");
29
+ if (!cacheMap.has(activePhecda))
30
+ cacheMap.set(activePhecda, bindMethod(activePhecda));
31
+ return cacheMap.get(activePhecda);
32
+ }
33
+ __name(usePhecda, "usePhecda");
34
+ function getPhecda(phecda) {
35
+ const activePhecda = phecda || getDefaultPhecda();
36
+ if (!activePhecda)
37
+ throw new Error("[phecda-react] manually inject the phecda instance if there is no default phecda");
38
+ if (!cacheMap.has(activePhecda))
39
+ cacheMap.set(activePhecda, bindMethod(activePhecda));
40
+ return cacheMap.get(activePhecda);
41
+ }
42
+ __name(getPhecda, "getPhecda");
43
+ function getR(model, phecda) {
44
+ return getPhecda(phecda).init(model);
45
+ }
46
+ __name(getR, "getR");
67
47
  function useEvent(eventName, cb) {
68
48
  useEffect(() => {
69
49
  return () => emitter.off(eventName, cb);
@@ -75,25 +55,21 @@ function useEvent(eventName, cb) {
75
55
  ];
76
56
  }
77
57
  __name(useEvent, "useEvent");
78
- function initialize(model, deleteOtherProperty = true) {
79
- const instance = useO(model);
80
- const newInstance = new model();
81
- Object.assign(instance, newInstance);
82
- if (deleteOtherProperty) {
83
- for (const key in instance) {
84
- if (!(key in newInstance))
85
- delete instance[key];
86
- }
87
- }
58
+ function useR(model) {
59
+ const proxy2 = usePhecda().init(model);
60
+ return [
61
+ useSnapshot(proxy2),
62
+ bindMethod(proxy2)
63
+ ];
88
64
  }
89
- __name(initialize, "initialize");
90
-
91
- // src/index.ts
92
- export * from "phecda-web";
65
+ __name(useR, "useR");
93
66
  export {
67
+ PhecdaContext,
94
68
  createPhecda,
95
- initialize,
69
+ getPhecda,
70
+ getR,
71
+ phecdaSymbol,
96
72
  useEvent,
97
- useO,
73
+ usePhecda,
98
74
  useR
99
75
  };
package/package.json CHANGED
@@ -1,20 +1,24 @@
1
1
  {
2
2
  "name": "phecda-react",
3
- "version": "0.1.0",
3
+ "version": "1.0.1",
4
4
  "description": "provide phecda function to react by valtio",
5
+ "author": "fgsreally",
6
+ "license": "MIT",
7
+ "repository": "https://github.com/fgsreally/phecda/tree/main/packages/react",
8
+ "keywords": [
9
+ "phecda",
10
+ "react"
11
+ ],
5
12
  "main": "dist/index.js",
6
13
  "module": "dist/index.mjs",
7
14
  "types": "dist/index.d.ts",
8
- "keywords": [],
9
- "author": "",
10
15
  "files": [
11
16
  "dist"
12
17
  ],
13
- "license": "MIT",
14
18
  "dependencies": {
15
19
  "react": "^18.2.0",
16
20
  "valtio": "^1.13.0",
17
- "phecda-web": "1.0.1"
21
+ "phecda-web": "2.0.2"
18
22
  },
19
23
  "devDependencies": {
20
24
  "@types/react": "^18.2.48",