phecda-core 3.0.0-alpha.11 → 3.0.0-alpha.12
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 +24 -22
- package/dist/index.js +140 -103
- package/dist/index.mjs +133 -98
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,26 @@ declare function Clear(proto: any, key: PropertyKey): void;
|
|
|
6
6
|
declare function Expose(proto: any, key: PropertyKey): void;
|
|
7
7
|
declare function Empty(module: any): void;
|
|
8
8
|
|
|
9
|
+
declare const SHARE_KEY: unique symbol;
|
|
10
|
+
declare const PHECDA_KEY: unique symbol;
|
|
11
|
+
declare function isPhecda(module: any): module is Construct;
|
|
12
|
+
declare function init(proto: Phecda): void;
|
|
13
|
+
declare function setStateVar(proto: Phecda, key: PropertyKey): void;
|
|
14
|
+
declare function setExposeKey(proto: Phecda, key: PropertyKey): void;
|
|
15
|
+
declare function setIgnoreKey(proto: Phecda, key: PropertyKey): void;
|
|
16
|
+
declare function setHandler(proto: Phecda, key: PropertyKey, handler: Handler): void;
|
|
17
|
+
declare function setState(proto: Phecda, key: PropertyKey, state: Record<string, any>): void;
|
|
18
|
+
declare function getOwnStateVars(target: any): string[];
|
|
19
|
+
declare function getStateVars(target: any): PropertyKey[];
|
|
20
|
+
declare function getOwnExposeKey(target: any): string[];
|
|
21
|
+
declare function getExposeKey(target: any): PropertyKey[];
|
|
22
|
+
declare function getOwnIgnoreKey(target: any): string[];
|
|
23
|
+
declare function getOwnHandler(target: any, key: PropertyKey): Handler[];
|
|
24
|
+
declare function getHandler(target: any, key: PropertyKey): any[];
|
|
25
|
+
declare function getState(target: any, key: PropertyKey): any;
|
|
26
|
+
declare function getOwnState(target: any, key: PropertyKey): Record<string, any>;
|
|
27
|
+
declare function invokeHandler(event: string, instance: Phecda): Promise<any[]>;
|
|
28
|
+
|
|
9
29
|
interface NameSpace {
|
|
10
30
|
[name: string]: Phecda;
|
|
11
31
|
}
|
|
@@ -19,7 +39,7 @@ interface Handler {
|
|
|
19
39
|
}
|
|
20
40
|
interface Phecda {
|
|
21
41
|
prototype: any;
|
|
22
|
-
|
|
42
|
+
[PHECDA_KEY]: {
|
|
23
43
|
__EXPOSE_VAR__: Set<PropertyKey>;
|
|
24
44
|
__IGNORE_VAR__: Set<PropertyKey>;
|
|
25
45
|
__STATE_VAR__: Set<PropertyKey>;
|
|
@@ -39,7 +59,7 @@ declare function Unique(desc?: string): (module: any) => void;
|
|
|
39
59
|
declare function Assign(cb: (instance?: any) => any): (module: any) => void;
|
|
40
60
|
declare function Global(module: any): void;
|
|
41
61
|
declare function To(...callbacks: ((arg: any, instance: any, key: string) => any)[]): (proto: any, key: PropertyKey) => void;
|
|
42
|
-
declare function Err(cb: (e: Error | any) => void, isCatch?: boolean): (proto: any, key: PropertyKey) => void;
|
|
62
|
+
declare function Err(cb: (e: Error | any, instance: any, key: string) => void, isCatch?: boolean): (proto: any, key: PropertyKey) => void;
|
|
43
63
|
interface StorageParam {
|
|
44
64
|
key?: string;
|
|
45
65
|
instance: any;
|
|
@@ -69,6 +89,7 @@ declare function getTag<M extends Construct | AbConstruct>(moduleOrInstance: M |
|
|
|
69
89
|
declare function getBind<M extends Construct | AbConstruct>(module: M): any;
|
|
70
90
|
declare function plainToClass<M extends Construct, Data extends Record<PropertyKey, any>>(module: M, input: Data): InstanceType<M>;
|
|
71
91
|
declare function transformClass<M extends Construct>(instance: InstanceType<M>, force?: boolean): Promise<string[]>;
|
|
92
|
+
declare function transformProperty<M extends Construct>(instance: InstanceType<M>, property: string, force?: boolean): Promise<string[]>;
|
|
72
93
|
declare function classToPlain<M>(instance: M): ClassValue<M>;
|
|
73
94
|
declare function snapShot<T extends Construct>(data: InstanceType<T>): {
|
|
74
95
|
data: InstanceType<T>;
|
|
@@ -79,25 +100,6 @@ declare function addDecoToClass<M extends Construct | AbConstruct>(c: M, key: ke
|
|
|
79
100
|
declare function Pipeline(...decos: ((...args: any) => void)[]): (...args: any) => void;
|
|
80
101
|
declare function isAsyncFunc(fn: Function): boolean;
|
|
81
102
|
|
|
82
|
-
declare function isPhecda(module: any): module is Construct;
|
|
83
|
-
declare const SHARE_KEY: unique symbol;
|
|
84
|
-
declare function init(proto: Phecda): void;
|
|
85
|
-
declare function setVar(proto: Phecda, key: PropertyKey): void;
|
|
86
|
-
declare function setExposeKey(proto: Phecda, key: PropertyKey): void;
|
|
87
|
-
declare function setIgnoreKey(proto: Phecda, key: PropertyKey): void;
|
|
88
|
-
declare function getOwnModuleState(instance: Phecda): string[];
|
|
89
|
-
declare function getModuleState(instance: Phecda): PropertyKey[];
|
|
90
|
-
declare function getOwnExposeKey(instance: Phecda): string[];
|
|
91
|
-
declare function getExposeKey(instance: Phecda): PropertyKey[];
|
|
92
|
-
declare function getOwnIgnoreKey(instance: Phecda): string[];
|
|
93
|
-
declare function regisHandler(proto: Phecda, key: PropertyKey, handler: Handler): void;
|
|
94
|
-
declare function getOwnHandler(instance: Phecda, key: PropertyKey): Handler[];
|
|
95
|
-
declare function getHandler(instance: Phecda, key: PropertyKey): any[];
|
|
96
|
-
declare function setState(proto: Phecda, key: PropertyKey, state: Record<string, any>): void;
|
|
97
|
-
declare function getOwnState(instance: Phecda, key: PropertyKey): Object;
|
|
98
|
-
declare function getState(instance: Phecda, key: PropertyKey): any;
|
|
99
|
-
declare function invokeHandler(event: string, instance: Phecda): Promise<any[]>;
|
|
100
|
-
|
|
101
103
|
declare const DataMap: InjectData;
|
|
102
104
|
declare function Provide<K extends keyof InjectData>(key: K, value: InjectData[K]): void;
|
|
103
105
|
declare function Inject<K extends keyof InjectData>(key: K): InjectData[K];
|
|
@@ -105,4 +107,4 @@ declare const activeInstance: Record<string, any>;
|
|
|
105
107
|
declare function injectProperty(key: string, value: any): Record<string, any>;
|
|
106
108
|
declare function getProperty(key: string): any;
|
|
107
109
|
|
|
108
|
-
export { AbConstruct, Assign, Bind, ClassValue, Clear, Construct, DataMap, Effect, Empty, Err, Events, Expose, Global, Handler, Ignore, Init, Inject, InjectData, Isolate, NameSpace, Phecda, Pipeline, Provide, SHARE_KEY, Storage, StorageParam, Tag, To, Unique, Unmount, Watcher, WatcherParam, activeInstance, addDecoToClass, classToPlain, getBind, getExposeKey, getHandler,
|
|
110
|
+
export { AbConstruct, Assign, Bind, ClassValue, Clear, Construct, DataMap, Effect, Empty, Err, Events, Expose, Global, Handler, Ignore, Init, Inject, InjectData, Isolate, NameSpace, PHECDA_KEY, Phecda, Pipeline, Provide, SHARE_KEY, Storage, StorageParam, Tag, To, Unique, Unmount, Watcher, WatcherParam, activeInstance, addDecoToClass, classToPlain, getBind, getExposeKey, getHandler, getOwnExposeKey, getOwnHandler, getOwnIgnoreKey, getOwnState, getOwnStateVars, getProperty, getState, getStateVars, getTag, init, injectProperty, invokeHandler, isAsyncFunc, isPhecda, plainToClass, setExposeKey, setHandler, setIgnoreKey, setState, setStateVar, snapShot, transformClass, transformProperty };
|
package/dist/index.js
CHANGED
|
@@ -34,6 +34,7 @@ __export(src_exports, {
|
|
|
34
34
|
Init: () => Init,
|
|
35
35
|
Inject: () => Inject,
|
|
36
36
|
Isolate: () => Isolate,
|
|
37
|
+
PHECDA_KEY: () => PHECDA_KEY,
|
|
37
38
|
Pipeline: () => Pipeline,
|
|
38
39
|
Provide: () => Provide,
|
|
39
40
|
SHARE_KEY: () => SHARE_KEY,
|
|
@@ -49,14 +50,14 @@ __export(src_exports, {
|
|
|
49
50
|
getBind: () => getBind,
|
|
50
51
|
getExposeKey: () => getExposeKey,
|
|
51
52
|
getHandler: () => getHandler,
|
|
52
|
-
getModuleState: () => getModuleState,
|
|
53
53
|
getOwnExposeKey: () => getOwnExposeKey,
|
|
54
54
|
getOwnHandler: () => getOwnHandler,
|
|
55
55
|
getOwnIgnoreKey: () => getOwnIgnoreKey,
|
|
56
|
-
getOwnModuleState: () => getOwnModuleState,
|
|
57
56
|
getOwnState: () => getOwnState,
|
|
57
|
+
getOwnStateVars: () => getOwnStateVars,
|
|
58
58
|
getProperty: () => getProperty,
|
|
59
59
|
getState: () => getState,
|
|
60
|
+
getStateVars: () => getStateVars,
|
|
60
61
|
getTag: () => getTag,
|
|
61
62
|
init: () => init,
|
|
62
63
|
injectProperty: () => injectProperty,
|
|
@@ -64,29 +65,31 @@ __export(src_exports, {
|
|
|
64
65
|
isAsyncFunc: () => isAsyncFunc,
|
|
65
66
|
isPhecda: () => isPhecda,
|
|
66
67
|
plainToClass: () => plainToClass,
|
|
67
|
-
regisHandler: () => regisHandler,
|
|
68
68
|
setExposeKey: () => setExposeKey,
|
|
69
|
+
setHandler: () => setHandler,
|
|
69
70
|
setIgnoreKey: () => setIgnoreKey,
|
|
70
71
|
setState: () => setState,
|
|
71
|
-
|
|
72
|
+
setStateVar: () => setStateVar,
|
|
72
73
|
snapShot: () => snapShot,
|
|
73
|
-
transformClass: () => transformClass
|
|
74
|
+
transformClass: () => transformClass,
|
|
75
|
+
transformProperty: () => transformProperty
|
|
74
76
|
});
|
|
75
77
|
module.exports = __toCommonJS(src_exports);
|
|
76
78
|
|
|
77
79
|
// src/core.ts
|
|
80
|
+
var SHARE_KEY = Symbol("phecda");
|
|
81
|
+
var PHECDA_KEY = Symbol("phecda");
|
|
78
82
|
function isPhecda(module2) {
|
|
79
83
|
if (typeof module2 === "function")
|
|
80
|
-
return !!module2.prototype?.
|
|
84
|
+
return !!module2.prototype?.[PHECDA_KEY];
|
|
81
85
|
return false;
|
|
82
86
|
}
|
|
83
87
|
__name(isPhecda, "isPhecda");
|
|
84
|
-
var SHARE_KEY = Symbol("phecda-core");
|
|
85
88
|
function init(proto) {
|
|
86
89
|
if (!proto)
|
|
87
90
|
return;
|
|
88
|
-
if (!proto.hasOwnProperty(
|
|
89
|
-
proto
|
|
91
|
+
if (!proto.hasOwnProperty(PHECDA_KEY)) {
|
|
92
|
+
proto[PHECDA_KEY] = {
|
|
90
93
|
__EXPOSE_VAR__: /* @__PURE__ */ new Set(),
|
|
91
94
|
__IGNORE_VAR__: /* @__PURE__ */ new Set(),
|
|
92
95
|
__STATE_VAR__: /* @__PURE__ */ new Set(),
|
|
@@ -96,55 +99,79 @@ function init(proto) {
|
|
|
96
99
|
}
|
|
97
100
|
}
|
|
98
101
|
__name(init, "init");
|
|
99
|
-
function
|
|
102
|
+
function getPhecdaFromTarget(target) {
|
|
103
|
+
if (typeof target === "function")
|
|
104
|
+
return target.prototype;
|
|
105
|
+
if (target.hasOwnProperty(PHECDA_KEY))
|
|
106
|
+
return target;
|
|
107
|
+
return Object.getPrototypeOf(target);
|
|
108
|
+
}
|
|
109
|
+
__name(getPhecdaFromTarget, "getPhecdaFromTarget");
|
|
110
|
+
function setStateVar(proto, key) {
|
|
100
111
|
init(proto);
|
|
101
|
-
proto.
|
|
112
|
+
proto[PHECDA_KEY].__STATE_VAR__.add(key);
|
|
102
113
|
setExposeKey(proto, key);
|
|
103
114
|
}
|
|
104
|
-
__name(
|
|
115
|
+
__name(setStateVar, "setStateVar");
|
|
105
116
|
function setExposeKey(proto, key) {
|
|
106
117
|
init(proto);
|
|
107
|
-
proto.
|
|
118
|
+
proto[PHECDA_KEY].__EXPOSE_VAR__.add(key);
|
|
108
119
|
}
|
|
109
120
|
__name(setExposeKey, "setExposeKey");
|
|
110
121
|
function setIgnoreKey(proto, key) {
|
|
111
122
|
init(proto);
|
|
112
|
-
proto.
|
|
123
|
+
proto[PHECDA_KEY].__IGNORE_VAR__.add(key);
|
|
113
124
|
}
|
|
114
125
|
__name(setIgnoreKey, "setIgnoreKey");
|
|
115
|
-
function
|
|
116
|
-
|
|
126
|
+
function setHandler(proto, key, handler) {
|
|
127
|
+
init(proto);
|
|
128
|
+
if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key))
|
|
129
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [
|
|
130
|
+
handler
|
|
131
|
+
]);
|
|
132
|
+
else
|
|
133
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
|
|
134
|
+
}
|
|
135
|
+
__name(setHandler, "setHandler");
|
|
136
|
+
function setState(proto, key, state) {
|
|
137
|
+
init(proto);
|
|
138
|
+
const namespace = proto[PHECDA_KEY].__STATE_NAMESPACE__;
|
|
139
|
+
namespace.set(key, state);
|
|
140
|
+
}
|
|
141
|
+
__name(setState, "setState");
|
|
142
|
+
function getOwnStateVars(target) {
|
|
143
|
+
const proto = getPhecdaFromTarget(target);
|
|
117
144
|
return [
|
|
118
|
-
...
|
|
145
|
+
...proto[PHECDA_KEY].__STATE_VAR__
|
|
119
146
|
];
|
|
120
147
|
}
|
|
121
|
-
__name(
|
|
122
|
-
function
|
|
123
|
-
let proto =
|
|
148
|
+
__name(getOwnStateVars, "getOwnStateVars");
|
|
149
|
+
function getStateVars(target) {
|
|
150
|
+
let proto = getPhecdaFromTarget(target);
|
|
124
151
|
const set = /* @__PURE__ */ new Set();
|
|
125
|
-
while (proto?.
|
|
126
|
-
proto.
|
|
152
|
+
while (proto?.[PHECDA_KEY]) {
|
|
153
|
+
proto[PHECDA_KEY].__STATE_VAR__.forEach((item) => set.add(item));
|
|
127
154
|
proto = Object.getPrototypeOf(proto);
|
|
128
155
|
}
|
|
129
156
|
return [
|
|
130
157
|
...set
|
|
131
158
|
];
|
|
132
159
|
}
|
|
133
|
-
__name(
|
|
134
|
-
function getOwnExposeKey(
|
|
135
|
-
|
|
160
|
+
__name(getStateVars, "getStateVars");
|
|
161
|
+
function getOwnExposeKey(target) {
|
|
162
|
+
const proto = getPhecdaFromTarget(target);
|
|
136
163
|
return [
|
|
137
|
-
...
|
|
138
|
-
].filter((item) => !
|
|
164
|
+
...proto[PHECDA_KEY].__EXPOSE_VAR__
|
|
165
|
+
].filter((item) => !proto[PHECDA_KEY].__IGNORE_VAR__.has(item));
|
|
139
166
|
}
|
|
140
167
|
__name(getOwnExposeKey, "getOwnExposeKey");
|
|
141
|
-
function getExposeKey(
|
|
142
|
-
let proto =
|
|
168
|
+
function getExposeKey(target) {
|
|
169
|
+
let proto = getPhecdaFromTarget(target);
|
|
143
170
|
const set = /* @__PURE__ */ new Set();
|
|
144
|
-
while (proto?.
|
|
171
|
+
while (proto?.[PHECDA_KEY]) {
|
|
145
172
|
[
|
|
146
|
-
...proto.
|
|
147
|
-
].forEach((item) => !proto.
|
|
173
|
+
...proto[PHECDA_KEY].__EXPOSE_VAR__
|
|
174
|
+
].forEach((item) => !proto[PHECDA_KEY].__IGNORE_VAR__.has(item) && set.add(item));
|
|
148
175
|
proto = Object.getPrototypeOf(proto);
|
|
149
176
|
}
|
|
150
177
|
return [
|
|
@@ -152,35 +179,27 @@ function getExposeKey(instance) {
|
|
|
152
179
|
];
|
|
153
180
|
}
|
|
154
181
|
__name(getExposeKey, "getExposeKey");
|
|
155
|
-
function getOwnIgnoreKey(
|
|
156
|
-
|
|
182
|
+
function getOwnIgnoreKey(target) {
|
|
183
|
+
const proto = getPhecdaFromTarget(target);
|
|
184
|
+
if (!proto?.[PHECDA_KEY])
|
|
157
185
|
return [];
|
|
158
186
|
return [
|
|
159
|
-
...
|
|
187
|
+
...proto[PHECDA_KEY].__IGNORE_VAR__
|
|
160
188
|
];
|
|
161
189
|
}
|
|
162
190
|
__name(getOwnIgnoreKey, "getOwnIgnoreKey");
|
|
163
|
-
function
|
|
164
|
-
|
|
165
|
-
if (!proto
|
|
166
|
-
proto._namespace.__STATE_HANDLER__.set(key, [
|
|
167
|
-
handler
|
|
168
|
-
]);
|
|
169
|
-
else
|
|
170
|
-
proto._namespace.__STATE_HANDLER__.get(key).push(handler);
|
|
171
|
-
}
|
|
172
|
-
__name(regisHandler, "regisHandler");
|
|
173
|
-
function getOwnHandler(instance, key) {
|
|
174
|
-
if (!instance?._namespace)
|
|
191
|
+
function getOwnHandler(target, key) {
|
|
192
|
+
const proto = getPhecdaFromTarget(target);
|
|
193
|
+
if (!proto?.[PHECDA_KEY])
|
|
175
194
|
return [];
|
|
176
|
-
return
|
|
195
|
+
return proto[PHECDA_KEY].__STATE_HANDLER__.get(key) || [];
|
|
177
196
|
}
|
|
178
197
|
__name(getOwnHandler, "getOwnHandler");
|
|
179
|
-
function getHandler(
|
|
180
|
-
let proto =
|
|
198
|
+
function getHandler(target, key) {
|
|
199
|
+
let proto = getPhecdaFromTarget(target);
|
|
181
200
|
const set = /* @__PURE__ */ new Set();
|
|
182
|
-
while (proto?.
|
|
183
|
-
proto.
|
|
201
|
+
while (proto?.[PHECDA_KEY]) {
|
|
202
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.get(key)?.forEach((item) => set.add(item));
|
|
184
203
|
proto = Object.getPrototypeOf(proto);
|
|
185
204
|
}
|
|
186
205
|
return [
|
|
@@ -188,22 +207,11 @@ function getHandler(instance, key) {
|
|
|
188
207
|
];
|
|
189
208
|
}
|
|
190
209
|
__name(getHandler, "getHandler");
|
|
191
|
-
function
|
|
192
|
-
|
|
193
|
-
const namespace = proto._namespace.__STATE_NAMESPACE__;
|
|
194
|
-
namespace.set(key, state);
|
|
195
|
-
}
|
|
196
|
-
__name(setState, "setState");
|
|
197
|
-
function getOwnState(instance, key) {
|
|
198
|
-
instance = Object.getPrototypeOf(instance);
|
|
199
|
-
return instance._namespace.__STATE_NAMESPACE__.get(key) || {};
|
|
200
|
-
}
|
|
201
|
-
__name(getOwnState, "getOwnState");
|
|
202
|
-
function getState(instance, key) {
|
|
203
|
-
let proto = Object.getPrototypeOf(instance);
|
|
210
|
+
function getState(target, key) {
|
|
211
|
+
let proto = getPhecdaFromTarget(target);
|
|
204
212
|
let ret = {};
|
|
205
|
-
while (proto?.
|
|
206
|
-
const state = proto.
|
|
213
|
+
while (proto?.[PHECDA_KEY]) {
|
|
214
|
+
const state = proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key);
|
|
207
215
|
if (state)
|
|
208
216
|
ret = {
|
|
209
217
|
...state,
|
|
@@ -214,6 +222,11 @@ function getState(instance, key) {
|
|
|
214
222
|
return ret;
|
|
215
223
|
}
|
|
216
224
|
__name(getState, "getState");
|
|
225
|
+
function getOwnState(target, key) {
|
|
226
|
+
const proto = getPhecdaFromTarget(target);
|
|
227
|
+
return proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key) || {};
|
|
228
|
+
}
|
|
229
|
+
__name(getOwnState, "getOwnState");
|
|
217
230
|
function invokeHandler(event, instance) {
|
|
218
231
|
const stateVars = getExposeKey(instance);
|
|
219
232
|
const initHandlers = stateVars.map((item) => {
|
|
@@ -225,8 +238,8 @@ __name(invokeHandler, "invokeHandler");
|
|
|
225
238
|
|
|
226
239
|
// src/decorators/core.ts
|
|
227
240
|
function Init(proto, key) {
|
|
228
|
-
|
|
229
|
-
|
|
241
|
+
setStateVar(proto, key);
|
|
242
|
+
setHandler(proto, key, {
|
|
230
243
|
async init(instance) {
|
|
231
244
|
return instance[key]();
|
|
232
245
|
}
|
|
@@ -234,8 +247,8 @@ function Init(proto, key) {
|
|
|
234
247
|
}
|
|
235
248
|
__name(Init, "Init");
|
|
236
249
|
function Unmount(proto, key) {
|
|
237
|
-
|
|
238
|
-
|
|
250
|
+
setStateVar(proto, key);
|
|
251
|
+
setHandler(proto, key, {
|
|
239
252
|
async unmount(instance) {
|
|
240
253
|
return instance[key]();
|
|
241
254
|
}
|
|
@@ -244,7 +257,7 @@ function Unmount(proto, key) {
|
|
|
244
257
|
__name(Unmount, "Unmount");
|
|
245
258
|
function Bind(value) {
|
|
246
259
|
return (proto, k) => {
|
|
247
|
-
|
|
260
|
+
setStateVar(proto, k);
|
|
248
261
|
setState(proto, k, {
|
|
249
262
|
value
|
|
250
263
|
});
|
|
@@ -257,11 +270,11 @@ function Ignore(proto, key) {
|
|
|
257
270
|
__name(Ignore, "Ignore");
|
|
258
271
|
function Clear(proto, key) {
|
|
259
272
|
init(proto);
|
|
260
|
-
proto.
|
|
261
|
-
proto.
|
|
262
|
-
proto.
|
|
263
|
-
proto.
|
|
264
|
-
proto.
|
|
273
|
+
proto[PHECDA_KEY].__EXPOSE_VAR__.delete(key);
|
|
274
|
+
proto[PHECDA_KEY].__IGNORE_VAR__.delete(key);
|
|
275
|
+
proto[PHECDA_KEY].__STATE_VAR__.delete(key);
|
|
276
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.delete(key);
|
|
277
|
+
proto[PHECDA_KEY].__STATE_NAMESPACE__.delete(key);
|
|
265
278
|
}
|
|
266
279
|
__name(Clear, "Clear");
|
|
267
280
|
function Expose(proto, key) {
|
|
@@ -282,7 +295,7 @@ function getTag(moduleOrInstance) {
|
|
|
282
295
|
__name(getTag, "getTag");
|
|
283
296
|
function getBind(module2) {
|
|
284
297
|
const instance = new module2();
|
|
285
|
-
const keys =
|
|
298
|
+
const keys = getStateVars(instance);
|
|
286
299
|
const ret = {};
|
|
287
300
|
for (const item of keys) {
|
|
288
301
|
const state = getState(instance, item);
|
|
@@ -302,12 +315,14 @@ function plainToClass(module2, input) {
|
|
|
302
315
|
__name(plainToClass, "plainToClass");
|
|
303
316
|
async function transformClass(instance, force = false) {
|
|
304
317
|
const err = [];
|
|
305
|
-
const
|
|
306
|
-
for (const item of
|
|
318
|
+
const keys = getExposeKey(instance);
|
|
319
|
+
for (const item of keys) {
|
|
307
320
|
const handlers = getHandler(instance, item);
|
|
308
321
|
if (handlers) {
|
|
309
322
|
for (const handler of handlers) {
|
|
310
323
|
const pipe = handler.pipe;
|
|
324
|
+
if (!pipe)
|
|
325
|
+
continue;
|
|
311
326
|
try {
|
|
312
327
|
await pipe(instance);
|
|
313
328
|
} catch (e) {
|
|
@@ -321,6 +336,26 @@ async function transformClass(instance, force = false) {
|
|
|
321
336
|
return err;
|
|
322
337
|
}
|
|
323
338
|
__name(transformClass, "transformClass");
|
|
339
|
+
async function transformProperty(instance, property, force = false) {
|
|
340
|
+
const err = [];
|
|
341
|
+
const handlers = getHandler(instance, property);
|
|
342
|
+
if (handlers) {
|
|
343
|
+
for (const handler of handlers) {
|
|
344
|
+
const pipe = handler.pipe;
|
|
345
|
+
if (!pipe)
|
|
346
|
+
continue;
|
|
347
|
+
try {
|
|
348
|
+
await pipe(instance);
|
|
349
|
+
} catch (e) {
|
|
350
|
+
err.push(e.message);
|
|
351
|
+
if (!force)
|
|
352
|
+
return err;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return err;
|
|
357
|
+
}
|
|
358
|
+
__name(transformProperty, "transformProperty");
|
|
324
359
|
function classToPlain(instance) {
|
|
325
360
|
const data = {};
|
|
326
361
|
const exposeVars = getExposeKey(instance);
|
|
@@ -411,8 +446,8 @@ __name(Unique, "Unique");
|
|
|
411
446
|
function Assign(cb) {
|
|
412
447
|
return (module2) => {
|
|
413
448
|
init(module2.prototype);
|
|
414
|
-
|
|
415
|
-
|
|
449
|
+
setStateVar(module2.prototype, SHARE_KEY);
|
|
450
|
+
setHandler(module2.prototype, SHARE_KEY, {
|
|
416
451
|
init: async (instance) => {
|
|
417
452
|
const value = await cb(instance);
|
|
418
453
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
@@ -426,8 +461,8 @@ function Assign(cb) {
|
|
|
426
461
|
__name(Assign, "Assign");
|
|
427
462
|
function Global(module2) {
|
|
428
463
|
init(module2.prototype);
|
|
429
|
-
|
|
430
|
-
|
|
464
|
+
setStateVar(module2.prototype, SHARE_KEY);
|
|
465
|
+
setHandler(module2.prototype, SHARE_KEY, {
|
|
431
466
|
init: async (instance) => {
|
|
432
467
|
const tag = instance.__TAG__;
|
|
433
468
|
if (!tag)
|
|
@@ -441,8 +476,8 @@ function Global(module2) {
|
|
|
441
476
|
__name(Global, "Global");
|
|
442
477
|
function To(...callbacks) {
|
|
443
478
|
return (proto, key) => {
|
|
444
|
-
|
|
445
|
-
|
|
479
|
+
setStateVar(proto, key);
|
|
480
|
+
setHandler(proto, key, {
|
|
446
481
|
async pipe(instance) {
|
|
447
482
|
for (const cb of callbacks)
|
|
448
483
|
instance[key] = await cb(instance[key], instance, key);
|
|
@@ -453,8 +488,8 @@ function To(...callbacks) {
|
|
|
453
488
|
__name(To, "To");
|
|
454
489
|
function Err(cb, isCatch = false) {
|
|
455
490
|
return (proto, key) => {
|
|
456
|
-
|
|
457
|
-
|
|
491
|
+
setStateVar(proto, key);
|
|
492
|
+
setHandler(proto, key, {
|
|
458
493
|
init: (instance) => {
|
|
459
494
|
if (typeof instance[key] === "function") {
|
|
460
495
|
const oldFn = instance[key].bind(instance);
|
|
@@ -463,7 +498,7 @@ function Err(cb, isCatch = false) {
|
|
|
463
498
|
try {
|
|
464
499
|
await oldFn(...args);
|
|
465
500
|
} catch (e) {
|
|
466
|
-
cb(e);
|
|
501
|
+
cb(e, instance, key);
|
|
467
502
|
if (!isCatch)
|
|
468
503
|
throw e;
|
|
469
504
|
}
|
|
@@ -473,7 +508,7 @@ function Err(cb, isCatch = false) {
|
|
|
473
508
|
try {
|
|
474
509
|
oldFn(...args);
|
|
475
510
|
} catch (e) {
|
|
476
|
-
cb(e);
|
|
511
|
+
cb(e, instance, key);
|
|
477
512
|
if (!isCatch)
|
|
478
513
|
throw e;
|
|
479
514
|
}
|
|
@@ -488,8 +523,8 @@ __name(Err, "Err");
|
|
|
488
523
|
function Watcher(eventName, options) {
|
|
489
524
|
let cb;
|
|
490
525
|
return (proto, key) => {
|
|
491
|
-
|
|
492
|
-
|
|
526
|
+
setStateVar(proto, key);
|
|
527
|
+
setHandler(proto, key, {
|
|
493
528
|
init(instance) {
|
|
494
529
|
return cb = getProperty("watcher")?.({
|
|
495
530
|
eventName,
|
|
@@ -507,8 +542,8 @@ function Watcher(eventName, options) {
|
|
|
507
542
|
__name(Watcher, "Watcher");
|
|
508
543
|
function Effect(eventName, options) {
|
|
509
544
|
return (proto, key) => {
|
|
510
|
-
|
|
511
|
-
|
|
545
|
+
setStateVar(proto, key);
|
|
546
|
+
setHandler(proto, key, {
|
|
512
547
|
init(instance) {
|
|
513
548
|
instance[`$_${key}`] = instance[key];
|
|
514
549
|
Object.defineProperty(instance, key, {
|
|
@@ -541,8 +576,8 @@ function Storage({ key: storeKey, toJSON, toString } = {}) {
|
|
|
541
576
|
if (key) {
|
|
542
577
|
init(proto);
|
|
543
578
|
tag = storeKey || `${getTag(proto)}_${key}`;
|
|
544
|
-
|
|
545
|
-
|
|
579
|
+
setStateVar(proto, key);
|
|
580
|
+
setHandler(proto, key, {
|
|
546
581
|
init: (instance) => {
|
|
547
582
|
return getProperty("storage")?.({
|
|
548
583
|
instance,
|
|
@@ -556,8 +591,8 @@ function Storage({ key: storeKey, toJSON, toString } = {}) {
|
|
|
556
591
|
} else {
|
|
557
592
|
init(proto.prototype);
|
|
558
593
|
tag = storeKey || getTag(proto);
|
|
559
|
-
|
|
560
|
-
|
|
594
|
+
setStateVar(proto.prototype, SHARE_KEY);
|
|
595
|
+
setHandler(proto.prototype, SHARE_KEY, {
|
|
561
596
|
init: (instance) => {
|
|
562
597
|
return getProperty("storage")?.({
|
|
563
598
|
instance,
|
|
@@ -587,6 +622,7 @@ __name(Storage, "Storage");
|
|
|
587
622
|
Init,
|
|
588
623
|
Inject,
|
|
589
624
|
Isolate,
|
|
625
|
+
PHECDA_KEY,
|
|
590
626
|
Pipeline,
|
|
591
627
|
Provide,
|
|
592
628
|
SHARE_KEY,
|
|
@@ -602,14 +638,14 @@ __name(Storage, "Storage");
|
|
|
602
638
|
getBind,
|
|
603
639
|
getExposeKey,
|
|
604
640
|
getHandler,
|
|
605
|
-
getModuleState,
|
|
606
641
|
getOwnExposeKey,
|
|
607
642
|
getOwnHandler,
|
|
608
643
|
getOwnIgnoreKey,
|
|
609
|
-
getOwnModuleState,
|
|
610
644
|
getOwnState,
|
|
645
|
+
getOwnStateVars,
|
|
611
646
|
getProperty,
|
|
612
647
|
getState,
|
|
648
|
+
getStateVars,
|
|
613
649
|
getTag,
|
|
614
650
|
init,
|
|
615
651
|
injectProperty,
|
|
@@ -617,11 +653,12 @@ __name(Storage, "Storage");
|
|
|
617
653
|
isAsyncFunc,
|
|
618
654
|
isPhecda,
|
|
619
655
|
plainToClass,
|
|
620
|
-
regisHandler,
|
|
621
656
|
setExposeKey,
|
|
657
|
+
setHandler,
|
|
622
658
|
setIgnoreKey,
|
|
623
659
|
setState,
|
|
624
|
-
|
|
660
|
+
setStateVar,
|
|
625
661
|
snapShot,
|
|
626
|
-
transformClass
|
|
662
|
+
transformClass,
|
|
663
|
+
transformProperty
|
|
627
664
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -2,18 +2,19 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// src/core.ts
|
|
5
|
+
var SHARE_KEY = Symbol("phecda");
|
|
6
|
+
var PHECDA_KEY = Symbol("phecda");
|
|
5
7
|
function isPhecda(module) {
|
|
6
8
|
if (typeof module === "function")
|
|
7
|
-
return !!module.prototype?.
|
|
9
|
+
return !!module.prototype?.[PHECDA_KEY];
|
|
8
10
|
return false;
|
|
9
11
|
}
|
|
10
12
|
__name(isPhecda, "isPhecda");
|
|
11
|
-
var SHARE_KEY = Symbol("phecda-core");
|
|
12
13
|
function init(proto) {
|
|
13
14
|
if (!proto)
|
|
14
15
|
return;
|
|
15
|
-
if (!proto.hasOwnProperty(
|
|
16
|
-
proto
|
|
16
|
+
if (!proto.hasOwnProperty(PHECDA_KEY)) {
|
|
17
|
+
proto[PHECDA_KEY] = {
|
|
17
18
|
__EXPOSE_VAR__: /* @__PURE__ */ new Set(),
|
|
18
19
|
__IGNORE_VAR__: /* @__PURE__ */ new Set(),
|
|
19
20
|
__STATE_VAR__: /* @__PURE__ */ new Set(),
|
|
@@ -23,55 +24,79 @@ function init(proto) {
|
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
26
|
__name(init, "init");
|
|
26
|
-
function
|
|
27
|
+
function getPhecdaFromTarget(target) {
|
|
28
|
+
if (typeof target === "function")
|
|
29
|
+
return target.prototype;
|
|
30
|
+
if (target.hasOwnProperty(PHECDA_KEY))
|
|
31
|
+
return target;
|
|
32
|
+
return Object.getPrototypeOf(target);
|
|
33
|
+
}
|
|
34
|
+
__name(getPhecdaFromTarget, "getPhecdaFromTarget");
|
|
35
|
+
function setStateVar(proto, key) {
|
|
27
36
|
init(proto);
|
|
28
|
-
proto.
|
|
37
|
+
proto[PHECDA_KEY].__STATE_VAR__.add(key);
|
|
29
38
|
setExposeKey(proto, key);
|
|
30
39
|
}
|
|
31
|
-
__name(
|
|
40
|
+
__name(setStateVar, "setStateVar");
|
|
32
41
|
function setExposeKey(proto, key) {
|
|
33
42
|
init(proto);
|
|
34
|
-
proto.
|
|
43
|
+
proto[PHECDA_KEY].__EXPOSE_VAR__.add(key);
|
|
35
44
|
}
|
|
36
45
|
__name(setExposeKey, "setExposeKey");
|
|
37
46
|
function setIgnoreKey(proto, key) {
|
|
38
47
|
init(proto);
|
|
39
|
-
proto.
|
|
48
|
+
proto[PHECDA_KEY].__IGNORE_VAR__.add(key);
|
|
40
49
|
}
|
|
41
50
|
__name(setIgnoreKey, "setIgnoreKey");
|
|
42
|
-
function
|
|
43
|
-
|
|
51
|
+
function setHandler(proto, key, handler) {
|
|
52
|
+
init(proto);
|
|
53
|
+
if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key))
|
|
54
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [
|
|
55
|
+
handler
|
|
56
|
+
]);
|
|
57
|
+
else
|
|
58
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
|
|
59
|
+
}
|
|
60
|
+
__name(setHandler, "setHandler");
|
|
61
|
+
function setState(proto, key, state) {
|
|
62
|
+
init(proto);
|
|
63
|
+
const namespace = proto[PHECDA_KEY].__STATE_NAMESPACE__;
|
|
64
|
+
namespace.set(key, state);
|
|
65
|
+
}
|
|
66
|
+
__name(setState, "setState");
|
|
67
|
+
function getOwnStateVars(target) {
|
|
68
|
+
const proto = getPhecdaFromTarget(target);
|
|
44
69
|
return [
|
|
45
|
-
...
|
|
70
|
+
...proto[PHECDA_KEY].__STATE_VAR__
|
|
46
71
|
];
|
|
47
72
|
}
|
|
48
|
-
__name(
|
|
49
|
-
function
|
|
50
|
-
let proto =
|
|
73
|
+
__name(getOwnStateVars, "getOwnStateVars");
|
|
74
|
+
function getStateVars(target) {
|
|
75
|
+
let proto = getPhecdaFromTarget(target);
|
|
51
76
|
const set = /* @__PURE__ */ new Set();
|
|
52
|
-
while (proto?.
|
|
53
|
-
proto.
|
|
77
|
+
while (proto?.[PHECDA_KEY]) {
|
|
78
|
+
proto[PHECDA_KEY].__STATE_VAR__.forEach((item) => set.add(item));
|
|
54
79
|
proto = Object.getPrototypeOf(proto);
|
|
55
80
|
}
|
|
56
81
|
return [
|
|
57
82
|
...set
|
|
58
83
|
];
|
|
59
84
|
}
|
|
60
|
-
__name(
|
|
61
|
-
function getOwnExposeKey(
|
|
62
|
-
|
|
85
|
+
__name(getStateVars, "getStateVars");
|
|
86
|
+
function getOwnExposeKey(target) {
|
|
87
|
+
const proto = getPhecdaFromTarget(target);
|
|
63
88
|
return [
|
|
64
|
-
...
|
|
65
|
-
].filter((item) => !
|
|
89
|
+
...proto[PHECDA_KEY].__EXPOSE_VAR__
|
|
90
|
+
].filter((item) => !proto[PHECDA_KEY].__IGNORE_VAR__.has(item));
|
|
66
91
|
}
|
|
67
92
|
__name(getOwnExposeKey, "getOwnExposeKey");
|
|
68
|
-
function getExposeKey(
|
|
69
|
-
let proto =
|
|
93
|
+
function getExposeKey(target) {
|
|
94
|
+
let proto = getPhecdaFromTarget(target);
|
|
70
95
|
const set = /* @__PURE__ */ new Set();
|
|
71
|
-
while (proto?.
|
|
96
|
+
while (proto?.[PHECDA_KEY]) {
|
|
72
97
|
[
|
|
73
|
-
...proto.
|
|
74
|
-
].forEach((item) => !proto.
|
|
98
|
+
...proto[PHECDA_KEY].__EXPOSE_VAR__
|
|
99
|
+
].forEach((item) => !proto[PHECDA_KEY].__IGNORE_VAR__.has(item) && set.add(item));
|
|
75
100
|
proto = Object.getPrototypeOf(proto);
|
|
76
101
|
}
|
|
77
102
|
return [
|
|
@@ -79,35 +104,27 @@ function getExposeKey(instance) {
|
|
|
79
104
|
];
|
|
80
105
|
}
|
|
81
106
|
__name(getExposeKey, "getExposeKey");
|
|
82
|
-
function getOwnIgnoreKey(
|
|
83
|
-
|
|
107
|
+
function getOwnIgnoreKey(target) {
|
|
108
|
+
const proto = getPhecdaFromTarget(target);
|
|
109
|
+
if (!proto?.[PHECDA_KEY])
|
|
84
110
|
return [];
|
|
85
111
|
return [
|
|
86
|
-
...
|
|
112
|
+
...proto[PHECDA_KEY].__IGNORE_VAR__
|
|
87
113
|
];
|
|
88
114
|
}
|
|
89
115
|
__name(getOwnIgnoreKey, "getOwnIgnoreKey");
|
|
90
|
-
function
|
|
91
|
-
|
|
92
|
-
if (!proto
|
|
93
|
-
proto._namespace.__STATE_HANDLER__.set(key, [
|
|
94
|
-
handler
|
|
95
|
-
]);
|
|
96
|
-
else
|
|
97
|
-
proto._namespace.__STATE_HANDLER__.get(key).push(handler);
|
|
98
|
-
}
|
|
99
|
-
__name(regisHandler, "regisHandler");
|
|
100
|
-
function getOwnHandler(instance, key) {
|
|
101
|
-
if (!instance?._namespace)
|
|
116
|
+
function getOwnHandler(target, key) {
|
|
117
|
+
const proto = getPhecdaFromTarget(target);
|
|
118
|
+
if (!proto?.[PHECDA_KEY])
|
|
102
119
|
return [];
|
|
103
|
-
return
|
|
120
|
+
return proto[PHECDA_KEY].__STATE_HANDLER__.get(key) || [];
|
|
104
121
|
}
|
|
105
122
|
__name(getOwnHandler, "getOwnHandler");
|
|
106
|
-
function getHandler(
|
|
107
|
-
let proto =
|
|
123
|
+
function getHandler(target, key) {
|
|
124
|
+
let proto = getPhecdaFromTarget(target);
|
|
108
125
|
const set = /* @__PURE__ */ new Set();
|
|
109
|
-
while (proto?.
|
|
110
|
-
proto.
|
|
126
|
+
while (proto?.[PHECDA_KEY]) {
|
|
127
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.get(key)?.forEach((item) => set.add(item));
|
|
111
128
|
proto = Object.getPrototypeOf(proto);
|
|
112
129
|
}
|
|
113
130
|
return [
|
|
@@ -115,22 +132,11 @@ function getHandler(instance, key) {
|
|
|
115
132
|
];
|
|
116
133
|
}
|
|
117
134
|
__name(getHandler, "getHandler");
|
|
118
|
-
function
|
|
119
|
-
|
|
120
|
-
const namespace = proto._namespace.__STATE_NAMESPACE__;
|
|
121
|
-
namespace.set(key, state);
|
|
122
|
-
}
|
|
123
|
-
__name(setState, "setState");
|
|
124
|
-
function getOwnState(instance, key) {
|
|
125
|
-
instance = Object.getPrototypeOf(instance);
|
|
126
|
-
return instance._namespace.__STATE_NAMESPACE__.get(key) || {};
|
|
127
|
-
}
|
|
128
|
-
__name(getOwnState, "getOwnState");
|
|
129
|
-
function getState(instance, key) {
|
|
130
|
-
let proto = Object.getPrototypeOf(instance);
|
|
135
|
+
function getState(target, key) {
|
|
136
|
+
let proto = getPhecdaFromTarget(target);
|
|
131
137
|
let ret = {};
|
|
132
|
-
while (proto?.
|
|
133
|
-
const state = proto.
|
|
138
|
+
while (proto?.[PHECDA_KEY]) {
|
|
139
|
+
const state = proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key);
|
|
134
140
|
if (state)
|
|
135
141
|
ret = {
|
|
136
142
|
...state,
|
|
@@ -141,6 +147,11 @@ function getState(instance, key) {
|
|
|
141
147
|
return ret;
|
|
142
148
|
}
|
|
143
149
|
__name(getState, "getState");
|
|
150
|
+
function getOwnState(target, key) {
|
|
151
|
+
const proto = getPhecdaFromTarget(target);
|
|
152
|
+
return proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key) || {};
|
|
153
|
+
}
|
|
154
|
+
__name(getOwnState, "getOwnState");
|
|
144
155
|
function invokeHandler(event, instance) {
|
|
145
156
|
const stateVars = getExposeKey(instance);
|
|
146
157
|
const initHandlers = stateVars.map((item) => {
|
|
@@ -152,8 +163,8 @@ __name(invokeHandler, "invokeHandler");
|
|
|
152
163
|
|
|
153
164
|
// src/decorators/core.ts
|
|
154
165
|
function Init(proto, key) {
|
|
155
|
-
|
|
156
|
-
|
|
166
|
+
setStateVar(proto, key);
|
|
167
|
+
setHandler(proto, key, {
|
|
157
168
|
async init(instance) {
|
|
158
169
|
return instance[key]();
|
|
159
170
|
}
|
|
@@ -161,8 +172,8 @@ function Init(proto, key) {
|
|
|
161
172
|
}
|
|
162
173
|
__name(Init, "Init");
|
|
163
174
|
function Unmount(proto, key) {
|
|
164
|
-
|
|
165
|
-
|
|
175
|
+
setStateVar(proto, key);
|
|
176
|
+
setHandler(proto, key, {
|
|
166
177
|
async unmount(instance) {
|
|
167
178
|
return instance[key]();
|
|
168
179
|
}
|
|
@@ -171,7 +182,7 @@ function Unmount(proto, key) {
|
|
|
171
182
|
__name(Unmount, "Unmount");
|
|
172
183
|
function Bind(value) {
|
|
173
184
|
return (proto, k) => {
|
|
174
|
-
|
|
185
|
+
setStateVar(proto, k);
|
|
175
186
|
setState(proto, k, {
|
|
176
187
|
value
|
|
177
188
|
});
|
|
@@ -184,11 +195,11 @@ function Ignore(proto, key) {
|
|
|
184
195
|
__name(Ignore, "Ignore");
|
|
185
196
|
function Clear(proto, key) {
|
|
186
197
|
init(proto);
|
|
187
|
-
proto.
|
|
188
|
-
proto.
|
|
189
|
-
proto.
|
|
190
|
-
proto.
|
|
191
|
-
proto.
|
|
198
|
+
proto[PHECDA_KEY].__EXPOSE_VAR__.delete(key);
|
|
199
|
+
proto[PHECDA_KEY].__IGNORE_VAR__.delete(key);
|
|
200
|
+
proto[PHECDA_KEY].__STATE_VAR__.delete(key);
|
|
201
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.delete(key);
|
|
202
|
+
proto[PHECDA_KEY].__STATE_NAMESPACE__.delete(key);
|
|
192
203
|
}
|
|
193
204
|
__name(Clear, "Clear");
|
|
194
205
|
function Expose(proto, key) {
|
|
@@ -209,7 +220,7 @@ function getTag(moduleOrInstance) {
|
|
|
209
220
|
__name(getTag, "getTag");
|
|
210
221
|
function getBind(module) {
|
|
211
222
|
const instance = new module();
|
|
212
|
-
const keys =
|
|
223
|
+
const keys = getStateVars(instance);
|
|
213
224
|
const ret = {};
|
|
214
225
|
for (const item of keys) {
|
|
215
226
|
const state = getState(instance, item);
|
|
@@ -229,12 +240,14 @@ function plainToClass(module, input) {
|
|
|
229
240
|
__name(plainToClass, "plainToClass");
|
|
230
241
|
async function transformClass(instance, force = false) {
|
|
231
242
|
const err = [];
|
|
232
|
-
const
|
|
233
|
-
for (const item of
|
|
243
|
+
const keys = getExposeKey(instance);
|
|
244
|
+
for (const item of keys) {
|
|
234
245
|
const handlers = getHandler(instance, item);
|
|
235
246
|
if (handlers) {
|
|
236
247
|
for (const handler of handlers) {
|
|
237
248
|
const pipe = handler.pipe;
|
|
249
|
+
if (!pipe)
|
|
250
|
+
continue;
|
|
238
251
|
try {
|
|
239
252
|
await pipe(instance);
|
|
240
253
|
} catch (e) {
|
|
@@ -248,6 +261,26 @@ async function transformClass(instance, force = false) {
|
|
|
248
261
|
return err;
|
|
249
262
|
}
|
|
250
263
|
__name(transformClass, "transformClass");
|
|
264
|
+
async function transformProperty(instance, property, force = false) {
|
|
265
|
+
const err = [];
|
|
266
|
+
const handlers = getHandler(instance, property);
|
|
267
|
+
if (handlers) {
|
|
268
|
+
for (const handler of handlers) {
|
|
269
|
+
const pipe = handler.pipe;
|
|
270
|
+
if (!pipe)
|
|
271
|
+
continue;
|
|
272
|
+
try {
|
|
273
|
+
await pipe(instance);
|
|
274
|
+
} catch (e) {
|
|
275
|
+
err.push(e.message);
|
|
276
|
+
if (!force)
|
|
277
|
+
return err;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return err;
|
|
282
|
+
}
|
|
283
|
+
__name(transformProperty, "transformProperty");
|
|
251
284
|
function classToPlain(instance) {
|
|
252
285
|
const data = {};
|
|
253
286
|
const exposeVars = getExposeKey(instance);
|
|
@@ -338,8 +371,8 @@ __name(Unique, "Unique");
|
|
|
338
371
|
function Assign(cb) {
|
|
339
372
|
return (module) => {
|
|
340
373
|
init(module.prototype);
|
|
341
|
-
|
|
342
|
-
|
|
374
|
+
setStateVar(module.prototype, SHARE_KEY);
|
|
375
|
+
setHandler(module.prototype, SHARE_KEY, {
|
|
343
376
|
init: async (instance) => {
|
|
344
377
|
const value = await cb(instance);
|
|
345
378
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
@@ -353,8 +386,8 @@ function Assign(cb) {
|
|
|
353
386
|
__name(Assign, "Assign");
|
|
354
387
|
function Global(module) {
|
|
355
388
|
init(module.prototype);
|
|
356
|
-
|
|
357
|
-
|
|
389
|
+
setStateVar(module.prototype, SHARE_KEY);
|
|
390
|
+
setHandler(module.prototype, SHARE_KEY, {
|
|
358
391
|
init: async (instance) => {
|
|
359
392
|
const tag = instance.__TAG__;
|
|
360
393
|
if (!tag)
|
|
@@ -368,8 +401,8 @@ function Global(module) {
|
|
|
368
401
|
__name(Global, "Global");
|
|
369
402
|
function To(...callbacks) {
|
|
370
403
|
return (proto, key) => {
|
|
371
|
-
|
|
372
|
-
|
|
404
|
+
setStateVar(proto, key);
|
|
405
|
+
setHandler(proto, key, {
|
|
373
406
|
async pipe(instance) {
|
|
374
407
|
for (const cb of callbacks)
|
|
375
408
|
instance[key] = await cb(instance[key], instance, key);
|
|
@@ -380,8 +413,8 @@ function To(...callbacks) {
|
|
|
380
413
|
__name(To, "To");
|
|
381
414
|
function Err(cb, isCatch = false) {
|
|
382
415
|
return (proto, key) => {
|
|
383
|
-
|
|
384
|
-
|
|
416
|
+
setStateVar(proto, key);
|
|
417
|
+
setHandler(proto, key, {
|
|
385
418
|
init: (instance) => {
|
|
386
419
|
if (typeof instance[key] === "function") {
|
|
387
420
|
const oldFn = instance[key].bind(instance);
|
|
@@ -390,7 +423,7 @@ function Err(cb, isCatch = false) {
|
|
|
390
423
|
try {
|
|
391
424
|
await oldFn(...args);
|
|
392
425
|
} catch (e) {
|
|
393
|
-
cb(e);
|
|
426
|
+
cb(e, instance, key);
|
|
394
427
|
if (!isCatch)
|
|
395
428
|
throw e;
|
|
396
429
|
}
|
|
@@ -400,7 +433,7 @@ function Err(cb, isCatch = false) {
|
|
|
400
433
|
try {
|
|
401
434
|
oldFn(...args);
|
|
402
435
|
} catch (e) {
|
|
403
|
-
cb(e);
|
|
436
|
+
cb(e, instance, key);
|
|
404
437
|
if (!isCatch)
|
|
405
438
|
throw e;
|
|
406
439
|
}
|
|
@@ -415,8 +448,8 @@ __name(Err, "Err");
|
|
|
415
448
|
function Watcher(eventName, options) {
|
|
416
449
|
let cb;
|
|
417
450
|
return (proto, key) => {
|
|
418
|
-
|
|
419
|
-
|
|
451
|
+
setStateVar(proto, key);
|
|
452
|
+
setHandler(proto, key, {
|
|
420
453
|
init(instance) {
|
|
421
454
|
return cb = getProperty("watcher")?.({
|
|
422
455
|
eventName,
|
|
@@ -434,8 +467,8 @@ function Watcher(eventName, options) {
|
|
|
434
467
|
__name(Watcher, "Watcher");
|
|
435
468
|
function Effect(eventName, options) {
|
|
436
469
|
return (proto, key) => {
|
|
437
|
-
|
|
438
|
-
|
|
470
|
+
setStateVar(proto, key);
|
|
471
|
+
setHandler(proto, key, {
|
|
439
472
|
init(instance) {
|
|
440
473
|
instance[`$_${key}`] = instance[key];
|
|
441
474
|
Object.defineProperty(instance, key, {
|
|
@@ -468,8 +501,8 @@ function Storage({ key: storeKey, toJSON, toString } = {}) {
|
|
|
468
501
|
if (key) {
|
|
469
502
|
init(proto);
|
|
470
503
|
tag = storeKey || `${getTag(proto)}_${key}`;
|
|
471
|
-
|
|
472
|
-
|
|
504
|
+
setStateVar(proto, key);
|
|
505
|
+
setHandler(proto, key, {
|
|
473
506
|
init: (instance) => {
|
|
474
507
|
return getProperty("storage")?.({
|
|
475
508
|
instance,
|
|
@@ -483,8 +516,8 @@ function Storage({ key: storeKey, toJSON, toString } = {}) {
|
|
|
483
516
|
} else {
|
|
484
517
|
init(proto.prototype);
|
|
485
518
|
tag = storeKey || getTag(proto);
|
|
486
|
-
|
|
487
|
-
|
|
519
|
+
setStateVar(proto.prototype, SHARE_KEY);
|
|
520
|
+
setHandler(proto.prototype, SHARE_KEY, {
|
|
488
521
|
init: (instance) => {
|
|
489
522
|
return getProperty("storage")?.({
|
|
490
523
|
instance,
|
|
@@ -513,6 +546,7 @@ export {
|
|
|
513
546
|
Init,
|
|
514
547
|
Inject,
|
|
515
548
|
Isolate,
|
|
549
|
+
PHECDA_KEY,
|
|
516
550
|
Pipeline,
|
|
517
551
|
Provide,
|
|
518
552
|
SHARE_KEY,
|
|
@@ -528,14 +562,14 @@ export {
|
|
|
528
562
|
getBind,
|
|
529
563
|
getExposeKey,
|
|
530
564
|
getHandler,
|
|
531
|
-
getModuleState,
|
|
532
565
|
getOwnExposeKey,
|
|
533
566
|
getOwnHandler,
|
|
534
567
|
getOwnIgnoreKey,
|
|
535
|
-
getOwnModuleState,
|
|
536
568
|
getOwnState,
|
|
569
|
+
getOwnStateVars,
|
|
537
570
|
getProperty,
|
|
538
571
|
getState,
|
|
572
|
+
getStateVars,
|
|
539
573
|
getTag,
|
|
540
574
|
init,
|
|
541
575
|
injectProperty,
|
|
@@ -543,11 +577,12 @@ export {
|
|
|
543
577
|
isAsyncFunc,
|
|
544
578
|
isPhecda,
|
|
545
579
|
plainToClass,
|
|
546
|
-
regisHandler,
|
|
547
580
|
setExposeKey,
|
|
581
|
+
setHandler,
|
|
548
582
|
setIgnoreKey,
|
|
549
583
|
setState,
|
|
550
|
-
|
|
584
|
+
setStateVar,
|
|
551
585
|
snapShot,
|
|
552
|
-
transformClass
|
|
586
|
+
transformClass,
|
|
587
|
+
transformProperty
|
|
553
588
|
};
|