phecda-react 0.0.1-alpha.3 → 0.1.0-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
@@ -1,27 +1,14 @@
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, Plugin, 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
+ use(...plugins: Plugin[]): void;
8
+ load(state: any): any;
9
+ unmount(): void;
10
+ };
11
+ declare function useEvent<Key extends keyof Events>(eventName: Key, cb: (event: Events[Key]) => void): ((arg: Events[Key]) => any)[];
12
+ declare function initialize<M extends Construct>(module: M, deleteOtherProperty?: boolean): InstanceType<M> | void;
13
+
14
+ 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,43 @@ 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 } = (0, import_phecda_web.getActiveInstance)();
39
+ if (module2.prototype.__ISOLATE__) {
40
+ const instance2 = new module2();
41
+ instance2._promise = registerAsync(instance2);
42
+ return instance2;
43
+ }
44
+ const tag = (0, import_phecda_web.getTag)(module2) || module2.name;
45
+ if (tag in state)
46
+ return state[tag];
91
47
  const instance = new module2();
92
- useOMap.set(tag, instance);
48
+ state[tag] = instance;
93
49
  return instance;
94
50
  }
95
51
  __name(useO, "useO");
96
52
  function useR(module2) {
97
- const { useRMap, fnMap } = getActivePhecda();
53
+ const { _r: rmap, _f: fmap } = (0, import_phecda_web.getActiveInstance)();
98
54
  const instance = useO(module2);
99
- if (useRMap.has(instance)) {
100
- const proxyInstance2 = useRMap.get(instance);
55
+ if (rmap.has(instance)) {
56
+ const proxyInstance2 = rmap.get(instance);
101
57
  return [
102
58
  (0, import_valtio.useSnapshot)(proxyInstance2),
103
59
  proxyInstance2
@@ -106,13 +62,13 @@ function useR(module2) {
106
62
  const proxyInstance = (0, import_valtio.proxy)(new Proxy(instance, {
107
63
  get(target, key) {
108
64
  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;
65
+ if (fmap.has(target[key]))
66
+ return fmap.get(target[key]);
67
+ const errorHandler = (0, import_phecda_web.getHandler)(target, key).find((item) => item.error)?.error;
112
68
  if (!errorHandler)
113
69
  return target[key].bind(target);
114
- const wrapper = wrapError(target, key, errorHandler);
115
- fnMap.set(target[key], wrapper);
70
+ const wrapper = (0, import_phecda_web.wrapError)(target, key, errorHandler);
71
+ fmap.set(target[key], wrapper);
116
72
  return wrapper;
117
73
  }
118
74
  return target[key];
@@ -122,85 +78,66 @@ function useR(module2) {
122
78
  return true;
123
79
  }
124
80
  }));
125
- (0, import_phecda_core.register)(proxyInstance);
126
- useRMap.set(instance, proxyInstance);
81
+ (0, import_phecda_web.register)(proxyInstance);
82
+ rmap.set(instance, proxyInstance);
127
83
  return [
128
84
  (0, import_valtio.useSnapshot)(proxyInstance),
129
85
  proxyInstance
130
86
  ];
131
87
  }
132
88
  __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
- });
89
+ function createPhecda() {
90
+ (0, import_phecda_web.resetActiveInstance)();
91
+ const instance = (0, import_phecda_web.getActiveInstance)();
92
+ const pluginSet = [];
93
+ return {
94
+ use(...plugins) {
95
+ plugins.forEach((p) => {
96
+ p.setup(instance);
97
+ pluginSet.push(p);
185
98
  });
99
+ },
100
+ load(state) {
101
+ instance.state = state;
102
+ return this;
103
+ },
104
+ unmount() {
105
+ pluginSet.forEach((p) => p.unmount?.(instance));
186
106
  }
187
- return (0, import_react.createElement)(Provider, {
188
- value: null
189
- }, children);
190
107
  };
191
108
  }
192
- __name(createPhecdaContext, "createPhecdaContext");
109
+ __name(createPhecda, "createPhecda");
110
+ function useEvent(eventName, cb) {
111
+ (0, import_react.useEffect)(() => {
112
+ return () => emitter.off(eventName, cb);
113
+ });
114
+ emitter.on(eventName, cb);
115
+ return [
116
+ (arg) => emitter.emit(eventName, arg),
117
+ () => emitter.off(eventName, cb)
118
+ ];
119
+ }
120
+ __name(useEvent, "useEvent");
121
+ function initialize(module2, deleteOtherProperty = true) {
122
+ const instance = useO(module2);
123
+ const newInstance = new module2();
124
+ Object.assign(instance, newInstance);
125
+ if (deleteOtherProperty) {
126
+ for (const key in instance) {
127
+ if (!(key in newInstance))
128
+ delete instance[key];
129
+ }
130
+ }
131
+ }
132
+ __name(initialize, "initialize");
193
133
 
194
134
  // src/index.ts
195
- __reExport(src_exports, require("phecda-core"), module.exports);
135
+ __reExport(src_exports, require("phecda-web"), module.exports);
196
136
  // Annotate the CommonJS export names for ESM import in node:
197
137
  0 && (module.exports = {
198
- createPhecdaContext,
199
- emitter,
200
- getActivePhecda,
201
- isAsyncFunc,
202
- setActivePhecda,
138
+ createPhecda,
139
+ initialize,
140
+ useEvent,
203
141
  useO,
204
- useR,
205
- wrapError
142
+ useR
206
143
  });
package/dist/index.mjs CHANGED
@@ -1,65 +1,30 @@
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, register, resetActiveInstance, wrapError } from "phecda-web";
48
8
  function useO(module) {
49
- const { useOMap } = getActivePhecda();
9
+ const { state } = getActiveInstance();
10
+ if (module.prototype.__ISOLATE__) {
11
+ const instance2 = new module();
12
+ instance2._promise = registerAsync(instance2);
13
+ return instance2;
14
+ }
50
15
  const tag = getTag(module) || module.name;
51
- if (useOMap.has(tag))
52
- return useOMap.get(tag);
16
+ if (tag in state)
17
+ return state[tag];
53
18
  const instance = new module();
54
- useOMap.set(tag, instance);
19
+ state[tag] = instance;
55
20
  return instance;
56
21
  }
57
22
  __name(useO, "useO");
58
23
  function useR(module) {
59
- const { useRMap, fnMap } = getActivePhecda();
24
+ const { _r: rmap, _f: fmap } = getActiveInstance();
60
25
  const instance = useO(module);
61
- if (useRMap.has(instance)) {
62
- const proxyInstance2 = useRMap.get(instance);
26
+ if (rmap.has(instance)) {
27
+ const proxyInstance2 = rmap.get(instance);
63
28
  return [
64
29
  useSnapshot(proxyInstance2),
65
30
  proxyInstance2
@@ -68,13 +33,13 @@ function useR(module) {
68
33
  const proxyInstance = proxy(new Proxy(instance, {
69
34
  get(target, key) {
70
35
  if (typeof target[key] === "function") {
71
- if (fnMap.has(target[key]))
72
- return fnMap.get(target[key]);
36
+ if (fmap.has(target[key]))
37
+ return fmap.get(target[key]);
73
38
  const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
74
39
  if (!errorHandler)
75
40
  return target[key].bind(target);
76
41
  const wrapper = wrapError(target, key, errorHandler);
77
- fnMap.set(target[key], wrapper);
42
+ fmap.set(target[key], wrapper);
78
43
  return wrapper;
79
44
  }
80
45
  return target[key];
@@ -85,83 +50,64 @@ function useR(module) {
85
50
  }
86
51
  }));
87
52
  register(proxyInstance);
88
- useRMap.set(instance, proxyInstance);
53
+ rmap.set(instance, proxyInstance);
89
54
  return [
90
55
  useSnapshot(proxyInstance),
91
56
  proxyInstance
92
57
  ];
93
58
  }
94
59
  __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
- });
60
+ function createPhecda() {
61
+ resetActiveInstance();
62
+ const instance = getActiveInstance();
63
+ const pluginSet = [];
64
+ return {
65
+ use(...plugins) {
66
+ plugins.forEach((p) => {
67
+ p.setup(instance);
68
+ pluginSet.push(p);
147
69
  });
70
+ },
71
+ load(state) {
72
+ instance.state = state;
73
+ return this;
74
+ },
75
+ unmount() {
76
+ pluginSet.forEach((p) => p.unmount?.(instance));
148
77
  }
149
- return createElement(Provider, {
150
- value: null
151
- }, children);
152
78
  };
153
79
  }
154
- __name(createPhecdaContext, "createPhecdaContext");
80
+ __name(createPhecda, "createPhecda");
81
+ function useEvent(eventName, cb) {
82
+ useEffect(() => {
83
+ return () => emitter.off(eventName, cb);
84
+ });
85
+ emitter.on(eventName, cb);
86
+ return [
87
+ (arg) => emitter.emit(eventName, arg),
88
+ () => emitter.off(eventName, cb)
89
+ ];
90
+ }
91
+ __name(useEvent, "useEvent");
92
+ function initialize(module, deleteOtherProperty = true) {
93
+ const instance = useO(module);
94
+ const newInstance = new module();
95
+ Object.assign(instance, newInstance);
96
+ if (deleteOtherProperty) {
97
+ for (const key in instance) {
98
+ if (!(key in newInstance))
99
+ delete instance[key];
100
+ }
101
+ }
102
+ }
103
+ __name(initialize, "initialize");
155
104
 
156
105
  // src/index.ts
157
- export * from "phecda-core";
106
+ export * from "phecda-web";
158
107
  export {
159
- createPhecdaContext,
160
- emitter,
161
- getActivePhecda,
162
- isAsyncFunc,
163
- setActivePhecda,
108
+ createPhecda,
109
+ initialize,
110
+ useEvent,
164
111
  useO,
165
- useR,
166
- wrapError
112
+ useR
167
113
  };
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.4",
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.0"
19
18
  },
20
19
  "devDependencies": {
21
20
  "@types/react": "^18.2.48",