phecda-react 0.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 fgsreally
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,27 @@
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>(Model: T): any;
21
+ declare function useR<T extends new (...args: any) => any>(Model: 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 };
package/dist/index.js ADDED
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ 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
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+
28
+ // src/index.ts
29
+ var src_exports = {};
30
+ __export(src_exports, {
31
+ createPhecdaContext: () => createPhecdaContext,
32
+ emitter: () => emitter,
33
+ getActivePhecda: () => getActivePhecda,
34
+ isAsyncFunc: () => isAsyncFunc,
35
+ setActivePhecda: () => setActivePhecda,
36
+ useO: () => useO,
37
+ useR: () => useR,
38
+ wrapError: () => wrapError
39
+ });
40
+ module.exports = __toCommonJS(src_exports);
41
+
42
+ // src/composables.ts
43
+ var import_valtio = require("valtio");
44
+ var import_phecda_core = require("phecda-core");
45
+ 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");
86
+ function useO(Model) {
87
+ const { useOMap } = getActivePhecda();
88
+ if (useOMap.has(Model))
89
+ return useOMap.get(Model);
90
+ const instance = new Model();
91
+ useOMap.set(Model, instance);
92
+ return instance;
93
+ }
94
+ __name(useO, "useO");
95
+ function useR(Model) {
96
+ const { useRMap, fnMap } = getActivePhecda();
97
+ const instance = useO(Model);
98
+ if (useRMap.has(instance)) {
99
+ const proxyInstance2 = useRMap.get(instance);
100
+ return [
101
+ (0, import_valtio.useSnapshot)(proxyInstance2),
102
+ proxyInstance2
103
+ ];
104
+ }
105
+ const proxyInstance = (0, import_valtio.proxy)(new Proxy(instance, {
106
+ get(target, key) {
107
+ if (typeof target[key] === "function") {
108
+ if (fnMap.has(target[key]))
109
+ return fnMap.get(target[key]);
110
+ const errorHandler = (0, import_phecda_core.getHandler)(target, key).find((item) => item.error)?.error;
111
+ if (!errorHandler)
112
+ return target[key].bind(target);
113
+ const wrapper = wrapError(target, key, errorHandler);
114
+ fnMap.set(target[key], wrapper);
115
+ return wrapper;
116
+ }
117
+ return target[key];
118
+ },
119
+ set(target, key, v) {
120
+ target[key] = v;
121
+ return true;
122
+ }
123
+ }));
124
+ (0, import_phecda_core.register)(proxyInstance);
125
+ useRMap.set(instance, proxyInstance);
126
+ return [
127
+ (0, import_valtio.useSnapshot)(proxyInstance),
128
+ proxyInstance
129
+ ];
130
+ }
131
+ __name(useR, "useR");
132
+ function createPhecdaContext() {
133
+ const { Provider } = (0, import_react.createContext)(null);
134
+ let eventRecord = [];
135
+ return ({ children }) => {
136
+ (0, import_react.useEffect)(() => {
137
+ return () => {
138
+ getActivePhecda().useOMap.clear();
139
+ eventRecord.forEach(([eventName, handler]) => emitter.off(eventName, handler));
140
+ eventRecord = [];
141
+ };
142
+ }, []);
143
+ if (!(0, import_phecda_core.getProperty)("watcher")) {
144
+ (0, import_phecda_core.injectProperty)("watcher", ({ eventName, instance, key, options }) => {
145
+ const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
146
+ if (options?.once) {
147
+ const handler = /* @__PURE__ */ __name((...args) => {
148
+ fn(...args);
149
+ emitter.off(eventName, handler);
150
+ }, "handler");
151
+ emitter.on(eventName, handler);
152
+ eventRecord.push([
153
+ eventName,
154
+ handler
155
+ ]);
156
+ } else {
157
+ eventRecord.push([
158
+ eventName,
159
+ fn
160
+ ]);
161
+ emitter.on(eventName, fn);
162
+ }
163
+ });
164
+ }
165
+ if (!(0, import_phecda_core.getProperty)("storage")) {
166
+ (0, import_phecda_core.injectProperty)("storage", ({ tag, key, instance }) => {
167
+ if (!tag)
168
+ return;
169
+ const initstr = localStorage.getItem(tag);
170
+ if (initstr) {
171
+ const data = JSON.parse(initstr);
172
+ if (key) {
173
+ instance[key] = data;
174
+ } else {
175
+ for (const i in data) {
176
+ if (i)
177
+ instance[i] = data[i];
178
+ }
179
+ }
180
+ }
181
+ globalThis.addEventListener("beforeunload", () => {
182
+ localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
183
+ });
184
+ });
185
+ }
186
+ return (0, import_react.createElement)(Provider, {
187
+ value: null
188
+ }, children);
189
+ };
190
+ }
191
+ __name(createPhecdaContext, "createPhecdaContext");
192
+
193
+ // src/index.ts
194
+ __reExport(src_exports, require("phecda-core"), module.exports);
195
+ // Annotate the CommonJS export names for ESM import in node:
196
+ 0 && (module.exports = {
197
+ createPhecdaContext,
198
+ emitter,
199
+ getActivePhecda,
200
+ isAsyncFunc,
201
+ setActivePhecda,
202
+ useO,
203
+ useR,
204
+ wrapError
205
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,166 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/composables.ts
5
+ import { proxy, useSnapshot } from "valtio";
6
+ import { getHandler, getProperty, 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");
48
+ function useO(Model) {
49
+ const { useOMap } = getActivePhecda();
50
+ if (useOMap.has(Model))
51
+ return useOMap.get(Model);
52
+ const instance = new Model();
53
+ useOMap.set(Model, instance);
54
+ return instance;
55
+ }
56
+ __name(useO, "useO");
57
+ function useR(Model) {
58
+ const { useRMap, fnMap } = getActivePhecda();
59
+ const instance = useO(Model);
60
+ if (useRMap.has(instance)) {
61
+ const proxyInstance2 = useRMap.get(instance);
62
+ return [
63
+ useSnapshot(proxyInstance2),
64
+ proxyInstance2
65
+ ];
66
+ }
67
+ const proxyInstance = proxy(new Proxy(instance, {
68
+ get(target, key) {
69
+ if (typeof target[key] === "function") {
70
+ if (fnMap.has(target[key]))
71
+ return fnMap.get(target[key]);
72
+ const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
73
+ if (!errorHandler)
74
+ return target[key].bind(target);
75
+ const wrapper = wrapError(target, key, errorHandler);
76
+ fnMap.set(target[key], wrapper);
77
+ return wrapper;
78
+ }
79
+ return target[key];
80
+ },
81
+ set(target, key, v) {
82
+ target[key] = v;
83
+ return true;
84
+ }
85
+ }));
86
+ register(proxyInstance);
87
+ useRMap.set(instance, proxyInstance);
88
+ return [
89
+ useSnapshot(proxyInstance),
90
+ proxyInstance
91
+ ];
92
+ }
93
+ __name(useR, "useR");
94
+ function createPhecdaContext() {
95
+ const { Provider } = createContext(null);
96
+ let eventRecord = [];
97
+ return ({ children }) => {
98
+ useEffect(() => {
99
+ return () => {
100
+ getActivePhecda().useOMap.clear();
101
+ eventRecord.forEach(([eventName, handler]) => emitter.off(eventName, handler));
102
+ eventRecord = [];
103
+ };
104
+ }, []);
105
+ if (!getProperty("watcher")) {
106
+ injectProperty("watcher", ({ eventName, instance, key, options }) => {
107
+ const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
108
+ if (options?.once) {
109
+ const handler = /* @__PURE__ */ __name((...args) => {
110
+ fn(...args);
111
+ emitter.off(eventName, handler);
112
+ }, "handler");
113
+ emitter.on(eventName, handler);
114
+ eventRecord.push([
115
+ eventName,
116
+ handler
117
+ ]);
118
+ } else {
119
+ eventRecord.push([
120
+ eventName,
121
+ fn
122
+ ]);
123
+ emitter.on(eventName, fn);
124
+ }
125
+ });
126
+ }
127
+ if (!getProperty("storage")) {
128
+ injectProperty("storage", ({ tag, key, instance }) => {
129
+ if (!tag)
130
+ return;
131
+ const initstr = localStorage.getItem(tag);
132
+ if (initstr) {
133
+ const data = JSON.parse(initstr);
134
+ if (key) {
135
+ instance[key] = data;
136
+ } else {
137
+ for (const i in data) {
138
+ if (i)
139
+ instance[i] = data[i];
140
+ }
141
+ }
142
+ }
143
+ globalThis.addEventListener("beforeunload", () => {
144
+ localStorage.setItem(tag, JSON.stringify(key ? instance[key] : instance));
145
+ });
146
+ });
147
+ }
148
+ return createElement(Provider, {
149
+ value: null
150
+ }, children);
151
+ };
152
+ }
153
+ __name(createPhecdaContext, "createPhecdaContext");
154
+
155
+ // src/index.ts
156
+ export * from "phecda-core";
157
+ export {
158
+ createPhecdaContext,
159
+ emitter,
160
+ getActivePhecda,
161
+ isAsyncFunc,
162
+ setActivePhecda,
163
+ useO,
164
+ useR,
165
+ wrapError
166
+ };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "phecda-react",
3
+ "version": "0.0.0",
4
+ "description": "provide store with phecda function to react",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "keywords": [],
9
+ "author": "",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "license": "MIT",
14
+ "dependencies": {
15
+ "mitt": "^3.0.0",
16
+ "react": "^18.2.0",
17
+ "react-dom": "^18.2.0",
18
+ "valtio": "^1.13.0",
19
+ "phecda-core": "2.1.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/react": "^18.2.48",
23
+ "tsup": "^6.5.0"
24
+ },
25
+ "scripts": {
26
+ "build": "tsup",
27
+ "dev": "tsup --watch"
28
+ }
29
+ }