phecda-core 3.0.0-alpha.9 → 3.0.0-beta.13
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 +73 -45
- package/dist/index.js +371 -245
- package/dist/index.mjs +355 -236
- package/package.json +1 -1
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,114 +24,115 @@ 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 =
|
|
51
|
-
const
|
|
52
|
-
while (proto?.
|
|
53
|
-
proto.
|
|
73
|
+
__name(getOwnStateVars, "getOwnStateVars");
|
|
74
|
+
function getStateVars(target) {
|
|
75
|
+
let proto = getPhecdaFromTarget(target);
|
|
76
|
+
const set2 = /* @__PURE__ */ new Set();
|
|
77
|
+
while (proto?.[PHECDA_KEY]) {
|
|
78
|
+
proto[PHECDA_KEY].__STATE_VAR__.forEach((item) => set2.add(item));
|
|
54
79
|
proto = Object.getPrototypeOf(proto);
|
|
55
80
|
}
|
|
56
81
|
return [
|
|
57
|
-
...
|
|
82
|
+
...set2
|
|
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 =
|
|
70
|
-
const
|
|
71
|
-
while (proto?.
|
|
93
|
+
function getExposeKey(target) {
|
|
94
|
+
let proto = getPhecdaFromTarget(target);
|
|
95
|
+
const set2 = /* @__PURE__ */ new Set();
|
|
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) && set2.add(item));
|
|
75
100
|
proto = Object.getPrototypeOf(proto);
|
|
76
101
|
}
|
|
77
102
|
return [
|
|
78
|
-
...
|
|
103
|
+
...set2
|
|
79
104
|
];
|
|
80
105
|
}
|
|
81
106
|
__name(getExposeKey, "getExposeKey");
|
|
82
|
-
function getOwnIgnoreKey(
|
|
83
|
-
|
|
84
|
-
return [];
|
|
107
|
+
function getOwnIgnoreKey(target) {
|
|
108
|
+
const proto = getPhecdaFromTarget(target);
|
|
85
109
|
return [
|
|
86
|
-
...
|
|
110
|
+
...proto[PHECDA_KEY]?.__IGNORE_VAR__
|
|
87
111
|
];
|
|
88
112
|
}
|
|
89
113
|
__name(getOwnIgnoreKey, "getOwnIgnoreKey");
|
|
90
|
-
function
|
|
91
|
-
|
|
92
|
-
|
|
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)
|
|
102
|
-
return [];
|
|
103
|
-
return instance._namespace.__STATE_HANDLER__.get(key) || [];
|
|
114
|
+
function getOwnHandler(target, key) {
|
|
115
|
+
const proto = getPhecdaFromTarget(target);
|
|
116
|
+
return proto[PHECDA_KEY]?.__STATE_HANDLER__.get(key) || [];
|
|
104
117
|
}
|
|
105
118
|
__name(getOwnHandler, "getOwnHandler");
|
|
106
|
-
function getHandler(
|
|
107
|
-
let proto =
|
|
108
|
-
const
|
|
109
|
-
while (proto?.
|
|
110
|
-
proto.
|
|
119
|
+
function getHandler(target, key) {
|
|
120
|
+
let proto = getPhecdaFromTarget(target);
|
|
121
|
+
const set2 = /* @__PURE__ */ new Set();
|
|
122
|
+
while (proto?.[PHECDA_KEY]) {
|
|
123
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.get(key)?.forEach((item) => set2.add(item));
|
|
111
124
|
proto = Object.getPrototypeOf(proto);
|
|
112
125
|
}
|
|
113
126
|
return [
|
|
114
|
-
...
|
|
127
|
+
...set2
|
|
115
128
|
];
|
|
116
129
|
}
|
|
117
130
|
__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);
|
|
131
|
+
function getState(target, key) {
|
|
132
|
+
let proto = getPhecdaFromTarget(target);
|
|
131
133
|
let ret = {};
|
|
132
|
-
while (proto?.
|
|
133
|
-
const state = proto.
|
|
134
|
+
while (proto?.[PHECDA_KEY]) {
|
|
135
|
+
const state = proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key);
|
|
134
136
|
if (state)
|
|
135
137
|
ret = {
|
|
136
138
|
...state,
|
|
@@ -141,25 +143,89 @@ function getState(instance, key) {
|
|
|
141
143
|
return ret;
|
|
142
144
|
}
|
|
143
145
|
__name(getState, "getState");
|
|
146
|
+
function getOwnState(target, key) {
|
|
147
|
+
const proto = getPhecdaFromTarget(target);
|
|
148
|
+
return proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key) || {};
|
|
149
|
+
}
|
|
150
|
+
__name(getOwnState, "getOwnState");
|
|
144
151
|
function invokeHandler(event, instance) {
|
|
145
152
|
const stateVars = getExposeKey(instance);
|
|
146
153
|
const initHandlers = stateVars.map((item) => {
|
|
147
|
-
return getHandler(instance, item).filter((h) => h[event]).map((h) => h[event](instance));
|
|
154
|
+
return getHandler(instance, item).filter((h) => !!h[event]).map((h) => h[event](instance));
|
|
148
155
|
}).flat();
|
|
149
156
|
return Promise.all(initHandlers);
|
|
150
157
|
}
|
|
151
158
|
__name(invokeHandler, "invokeHandler");
|
|
159
|
+
function set(proto, key, value) {
|
|
160
|
+
init(proto);
|
|
161
|
+
proto[PHECDA_KEY][`__${key.toUpperCase()}__`] = value;
|
|
162
|
+
}
|
|
163
|
+
__name(set, "set");
|
|
164
|
+
function get(proto, key) {
|
|
165
|
+
return proto[PHECDA_KEY]?.[`__${key.toUpperCase()}__`];
|
|
166
|
+
}
|
|
167
|
+
__name(get, "get");
|
|
168
|
+
|
|
169
|
+
// src/decorators/core.ts
|
|
170
|
+
function Init(proto, key) {
|
|
171
|
+
setStateVar(proto, key);
|
|
172
|
+
setHandler(proto, key, {
|
|
173
|
+
async init(instance) {
|
|
174
|
+
return instance[key]();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
__name(Init, "Init");
|
|
179
|
+
function Unmount(proto, key) {
|
|
180
|
+
setStateVar(proto, key);
|
|
181
|
+
setHandler(proto, key, {
|
|
182
|
+
async unmount(instance) {
|
|
183
|
+
return instance[key]();
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
__name(Unmount, "Unmount");
|
|
188
|
+
function Bind(value) {
|
|
189
|
+
return (proto, k) => {
|
|
190
|
+
setStateVar(proto, k);
|
|
191
|
+
setState(proto, k, {
|
|
192
|
+
value
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
__name(Bind, "Bind");
|
|
197
|
+
function Ignore(proto, key) {
|
|
198
|
+
setIgnoreKey(proto, key);
|
|
199
|
+
}
|
|
200
|
+
__name(Ignore, "Ignore");
|
|
201
|
+
function Clear(proto, key) {
|
|
202
|
+
init(proto);
|
|
203
|
+
proto[PHECDA_KEY].__EXPOSE_VAR__.delete(key);
|
|
204
|
+
proto[PHECDA_KEY].__IGNORE_VAR__.delete(key);
|
|
205
|
+
proto[PHECDA_KEY].__STATE_VAR__.delete(key);
|
|
206
|
+
proto[PHECDA_KEY].__STATE_HANDLER__.delete(key);
|
|
207
|
+
proto[PHECDA_KEY].__STATE_NAMESPACE__.delete(key);
|
|
208
|
+
}
|
|
209
|
+
__name(Clear, "Clear");
|
|
210
|
+
function Expose(proto, key) {
|
|
211
|
+
setExposeKey(proto, key);
|
|
212
|
+
}
|
|
213
|
+
__name(Expose, "Expose");
|
|
214
|
+
function Empty(module) {
|
|
215
|
+
init(module.prototype);
|
|
216
|
+
}
|
|
217
|
+
__name(Empty, "Empty");
|
|
152
218
|
|
|
153
219
|
// src/helper.ts
|
|
154
220
|
function getTag(moduleOrInstance) {
|
|
155
221
|
if (typeof moduleOrInstance === "object")
|
|
156
222
|
moduleOrInstance = moduleOrInstance.constructor;
|
|
157
|
-
return moduleOrInstance.prototype?.__TAG__ || moduleOrInstance.name;
|
|
223
|
+
return moduleOrInstance.prototype[PHECDA_KEY]?.__TAG__ || moduleOrInstance.name;
|
|
158
224
|
}
|
|
159
225
|
__name(getTag, "getTag");
|
|
160
226
|
function getBind(module) {
|
|
161
227
|
const instance = new module();
|
|
162
|
-
const keys =
|
|
228
|
+
const keys = getStateVars(instance);
|
|
163
229
|
const ret = {};
|
|
164
230
|
for (const item of keys) {
|
|
165
231
|
const state = getState(instance, item);
|
|
@@ -177,35 +243,88 @@ function plainToClass(module, input) {
|
|
|
177
243
|
return instance;
|
|
178
244
|
}
|
|
179
245
|
__name(plainToClass, "plainToClass");
|
|
180
|
-
|
|
246
|
+
function transformInstance(instance, force = false) {
|
|
181
247
|
const err = [];
|
|
182
|
-
const
|
|
183
|
-
|
|
248
|
+
const keys = getExposeKey(instance);
|
|
249
|
+
const addError = err.push.bind(err);
|
|
250
|
+
for (const item of keys) {
|
|
184
251
|
const handlers = getHandler(instance, item);
|
|
185
252
|
if (handlers) {
|
|
186
253
|
for (const handler of handlers) {
|
|
187
254
|
const pipe = handler.pipe;
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
return err;
|
|
194
|
-
}
|
|
255
|
+
if (!pipe)
|
|
256
|
+
continue;
|
|
257
|
+
pipe(instance, addError);
|
|
258
|
+
if (err.length && !force)
|
|
259
|
+
return err;
|
|
195
260
|
}
|
|
196
261
|
}
|
|
197
262
|
}
|
|
198
263
|
return err;
|
|
199
264
|
}
|
|
200
|
-
__name(
|
|
201
|
-
function
|
|
265
|
+
__name(transformInstance, "transformInstance");
|
|
266
|
+
async function transformInstanceAsync(instance, force = false) {
|
|
267
|
+
const err = [];
|
|
268
|
+
const keys = getExposeKey(instance);
|
|
269
|
+
const addError = err.push.bind(err);
|
|
270
|
+
for (const item of keys) {
|
|
271
|
+
const handlers = getHandler(instance, item);
|
|
272
|
+
if (handlers) {
|
|
273
|
+
for (const handler of handlers) {
|
|
274
|
+
const pipe = handler.pipe;
|
|
275
|
+
if (!pipe)
|
|
276
|
+
continue;
|
|
277
|
+
await pipe(instance, addError);
|
|
278
|
+
if (err.length && !force)
|
|
279
|
+
return err;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return err;
|
|
284
|
+
}
|
|
285
|
+
__name(transformInstanceAsync, "transformInstanceAsync");
|
|
286
|
+
function transformProperty(instance, property, force = false) {
|
|
287
|
+
const err = [];
|
|
288
|
+
const handlers = getHandler(instance, property);
|
|
289
|
+
const addError = err.push.bind(err);
|
|
290
|
+
if (handlers) {
|
|
291
|
+
for (const handler of handlers) {
|
|
292
|
+
const pipe = handler.pipe;
|
|
293
|
+
if (!pipe)
|
|
294
|
+
continue;
|
|
295
|
+
pipe(instance, addError);
|
|
296
|
+
if (err.length && !force)
|
|
297
|
+
return err;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return err;
|
|
301
|
+
}
|
|
302
|
+
__name(transformProperty, "transformProperty");
|
|
303
|
+
async function transformPropertyAsync(instance, property, force = false) {
|
|
304
|
+
const err = [];
|
|
305
|
+
const handlers = getHandler(instance, property);
|
|
306
|
+
const addError = err.push.bind(err);
|
|
307
|
+
if (handlers) {
|
|
308
|
+
for (const handler of handlers) {
|
|
309
|
+
const pipe = handler.pipe;
|
|
310
|
+
if (!pipe)
|
|
311
|
+
continue;
|
|
312
|
+
await pipe(instance, addError);
|
|
313
|
+
if (err.length && !force)
|
|
314
|
+
return err;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return err;
|
|
318
|
+
}
|
|
319
|
+
__name(transformPropertyAsync, "transformPropertyAsync");
|
|
320
|
+
function classToPlain(instance) {
|
|
202
321
|
const data = {};
|
|
203
322
|
const exposeVars = getExposeKey(instance);
|
|
204
323
|
for (const item of exposeVars)
|
|
205
324
|
data[item] = instance[item];
|
|
206
|
-
return data;
|
|
325
|
+
return JSON.parse(JSON.stringify(data));
|
|
207
326
|
}
|
|
208
|
-
__name(
|
|
327
|
+
__name(classToPlain, "classToPlain");
|
|
209
328
|
function snapShot(data) {
|
|
210
329
|
const snap = {};
|
|
211
330
|
for (const i in data)
|
|
@@ -223,8 +342,8 @@ function snapShot(data) {
|
|
|
223
342
|
};
|
|
224
343
|
}
|
|
225
344
|
__name(snapShot, "snapShot");
|
|
226
|
-
function addDecoToClass(c, key, handler
|
|
227
|
-
handler(
|
|
345
|
+
function addDecoToClass(c, key, handler) {
|
|
346
|
+
handler(key === SHARE_KEY ? c : c.prototype, key);
|
|
228
347
|
}
|
|
229
348
|
__name(addDecoToClass, "addDecoToClass");
|
|
230
349
|
function Pipeline(...decos) {
|
|
@@ -234,92 +353,55 @@ function Pipeline(...decos) {
|
|
|
234
353
|
};
|
|
235
354
|
}
|
|
236
355
|
__name(Pipeline, "Pipeline");
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
function Init(proto, key) {
|
|
240
|
-
setVar(proto, key);
|
|
241
|
-
regisHandler(proto, key, {
|
|
242
|
-
async init(instance) {
|
|
243
|
-
return instance[key]();
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
__name(Init, "Init");
|
|
248
|
-
function Unmount(proto, key) {
|
|
249
|
-
setVar(proto, key);
|
|
250
|
-
regisHandler(proto, key, {
|
|
251
|
-
async unmount(instance) {
|
|
252
|
-
return instance[key]();
|
|
253
|
-
}
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
__name(Unmount, "Unmount");
|
|
257
|
-
function Bind(value) {
|
|
258
|
-
return (proto, k) => {
|
|
259
|
-
setVar(proto, k);
|
|
260
|
-
setState(proto, k, {
|
|
261
|
-
value
|
|
262
|
-
});
|
|
263
|
-
};
|
|
356
|
+
function isAsyncFunc(fn) {
|
|
357
|
+
return fn[Symbol.toStringTag] === "AsyncFunction";
|
|
264
358
|
}
|
|
265
|
-
__name(
|
|
266
|
-
|
|
267
|
-
|
|
359
|
+
__name(isAsyncFunc, "isAsyncFunc");
|
|
360
|
+
|
|
361
|
+
// src/di.ts
|
|
362
|
+
var DataMap = {};
|
|
363
|
+
function Provide(key, value) {
|
|
364
|
+
DataMap[key] = value;
|
|
268
365
|
}
|
|
269
|
-
__name(
|
|
270
|
-
function
|
|
271
|
-
|
|
272
|
-
proto._namespace.__EXPOSE_VAR__.delete(key);
|
|
273
|
-
proto._namespace.__IGNORE_VAR__.delete(key);
|
|
274
|
-
proto._namespace.__STATE_VAR__.delete(key);
|
|
275
|
-
proto._namespace.__STATE_HANDLER__.delete(key);
|
|
276
|
-
proto._namespace.__STATE_NAMESPACE__.delete(key);
|
|
366
|
+
__name(Provide, "Provide");
|
|
367
|
+
function Inject(key) {
|
|
368
|
+
return DataMap[key];
|
|
277
369
|
}
|
|
278
|
-
__name(
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
error: cb
|
|
284
|
-
});
|
|
285
|
-
};
|
|
370
|
+
__name(Inject, "Inject");
|
|
371
|
+
var activeInstance = {};
|
|
372
|
+
function injectKey(key, value) {
|
|
373
|
+
activeInstance[key] = value;
|
|
374
|
+
return activeInstance;
|
|
286
375
|
}
|
|
287
|
-
__name(
|
|
288
|
-
function
|
|
289
|
-
|
|
376
|
+
__name(injectKey, "injectKey");
|
|
377
|
+
function getKey(key) {
|
|
378
|
+
return activeInstance[key];
|
|
290
379
|
}
|
|
291
|
-
__name(
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
for (const cb of callbacks)
|
|
298
|
-
instance[key] = await cb(instance[key], instance, key);
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
};
|
|
380
|
+
__name(getKey, "getKey");
|
|
381
|
+
|
|
382
|
+
// src/decorators/function.ts
|
|
383
|
+
function Isolate(module) {
|
|
384
|
+
set(module.prototype, "isolate", true);
|
|
385
|
+
module.prototype[PHECDA_KEY].__ISOLATE__ = true;
|
|
302
386
|
}
|
|
303
|
-
__name(
|
|
387
|
+
__name(Isolate, "Isolate");
|
|
304
388
|
function Tag(tag) {
|
|
305
389
|
return (module) => {
|
|
306
|
-
|
|
307
|
-
module.prototype.__TAG__ = tag;
|
|
390
|
+
set(module.prototype, "tag", tag);
|
|
308
391
|
};
|
|
309
392
|
}
|
|
310
393
|
__name(Tag, "Tag");
|
|
311
394
|
function Unique(desc) {
|
|
312
395
|
return (module) => {
|
|
313
|
-
|
|
314
|
-
module.prototype.__TAG__ = Symbol(desc || module.name);
|
|
396
|
+
set(module.prototype, "tag", Symbol(desc || module.name));
|
|
315
397
|
};
|
|
316
398
|
}
|
|
317
399
|
__name(Unique, "Unique");
|
|
318
400
|
function Assign(cb) {
|
|
319
401
|
return (module) => {
|
|
320
402
|
init(module.prototype);
|
|
321
|
-
|
|
322
|
-
|
|
403
|
+
setStateVar(module.prototype, SHARE_KEY);
|
|
404
|
+
setHandler(module.prototype, SHARE_KEY, {
|
|
323
405
|
init: async (instance) => {
|
|
324
406
|
const value = await cb(instance);
|
|
325
407
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
@@ -333,10 +415,10 @@ function Assign(cb) {
|
|
|
333
415
|
__name(Assign, "Assign");
|
|
334
416
|
function Global(module) {
|
|
335
417
|
init(module.prototype);
|
|
336
|
-
|
|
337
|
-
|
|
418
|
+
setStateVar(module.prototype, SHARE_KEY);
|
|
419
|
+
setHandler(module.prototype, SHARE_KEY, {
|
|
338
420
|
init: async (instance) => {
|
|
339
|
-
const tag = instance.__TAG__;
|
|
421
|
+
const tag = instance[PHECDA_KEY].__TAG__;
|
|
340
422
|
if (!tag)
|
|
341
423
|
return;
|
|
342
424
|
if (!globalThis.__PHECDA__)
|
|
@@ -346,59 +428,88 @@ function Global(module) {
|
|
|
346
428
|
});
|
|
347
429
|
}
|
|
348
430
|
__name(Global, "Global");
|
|
349
|
-
function
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
}
|
|
366
|
-
__name(Inject, "Inject");
|
|
367
|
-
function Nested(module) {
|
|
368
|
-
return To(async (property) => {
|
|
369
|
-
const instance = plainToClass(module, property);
|
|
370
|
-
const err = await transformClass(instance);
|
|
371
|
-
if (err.length > 0)
|
|
372
|
-
throw new Error(err[0]);
|
|
373
|
-
return instance;
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
__name(Nested, "Nested");
|
|
377
|
-
function Isolate() {
|
|
378
|
-
return (target) => {
|
|
379
|
-
target.prototype.__ISOLATE__ = true;
|
|
431
|
+
function To(...callbacks) {
|
|
432
|
+
return (proto, key) => {
|
|
433
|
+
setStateVar(proto, key);
|
|
434
|
+
setHandler(proto, key, {
|
|
435
|
+
async pipe(instance, addError) {
|
|
436
|
+
for (const cb of callbacks) {
|
|
437
|
+
try {
|
|
438
|
+
if (isAsyncFunc(cb))
|
|
439
|
+
instance[key] = await cb(instance[key], instance, key);
|
|
440
|
+
else
|
|
441
|
+
instance[key] = cb(instance[key], instance, key);
|
|
442
|
+
} catch (e) {
|
|
443
|
+
addError(e.message);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
});
|
|
380
448
|
};
|
|
381
449
|
}
|
|
382
|
-
__name(
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
450
|
+
__name(To, "To");
|
|
451
|
+
function Rule(cb, info) {
|
|
452
|
+
return (proto, key) => {
|
|
453
|
+
setStateVar(proto, key);
|
|
454
|
+
setHandler(proto, key, {
|
|
455
|
+
async pipe(instance, addError) {
|
|
456
|
+
let ret;
|
|
457
|
+
if (isAsyncFunc(cb))
|
|
458
|
+
ret = await cb(instance[key]);
|
|
459
|
+
else
|
|
460
|
+
ret = cb(instance[key]);
|
|
461
|
+
if (!ret) {
|
|
462
|
+
if (typeof info === "string")
|
|
463
|
+
addError(info);
|
|
464
|
+
else
|
|
465
|
+
addError(info());
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
};
|
|
389
470
|
}
|
|
390
|
-
__name(
|
|
391
|
-
function
|
|
392
|
-
return
|
|
471
|
+
__name(Rule, "Rule");
|
|
472
|
+
function Err(cb, isCatch = false) {
|
|
473
|
+
return (proto, key) => {
|
|
474
|
+
setStateVar(proto, key);
|
|
475
|
+
setHandler(proto, key, {
|
|
476
|
+
init: (instance) => {
|
|
477
|
+
if (typeof instance[key] === "function") {
|
|
478
|
+
const oldFn = instance[key].bind(instance);
|
|
479
|
+
if (isAsyncFunc(oldFn)) {
|
|
480
|
+
instance[key] = async (...args) => {
|
|
481
|
+
try {
|
|
482
|
+
await oldFn(...args);
|
|
483
|
+
} catch (e) {
|
|
484
|
+
cb(e, instance, key);
|
|
485
|
+
if (!isCatch)
|
|
486
|
+
throw e;
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
} else {
|
|
490
|
+
instance[key] = (...args) => {
|
|
491
|
+
try {
|
|
492
|
+
oldFn(...args);
|
|
493
|
+
} catch (e) {
|
|
494
|
+
cb(e, instance, key);
|
|
495
|
+
if (!isCatch)
|
|
496
|
+
throw e;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
};
|
|
393
504
|
}
|
|
394
|
-
__name(
|
|
505
|
+
__name(Err, "Err");
|
|
395
506
|
function Watcher(eventName, options) {
|
|
396
507
|
let cb;
|
|
397
508
|
return (proto, key) => {
|
|
398
|
-
|
|
399
|
-
|
|
509
|
+
setStateVar(proto, key);
|
|
510
|
+
setHandler(proto, key, {
|
|
400
511
|
init(instance) {
|
|
401
|
-
return cb =
|
|
512
|
+
return cb = getKey("watcher")?.({
|
|
402
513
|
eventName,
|
|
403
514
|
instance,
|
|
404
515
|
key,
|
|
@@ -412,10 +523,10 @@ function Watcher(eventName, options) {
|
|
|
412
523
|
};
|
|
413
524
|
}
|
|
414
525
|
__name(Watcher, "Watcher");
|
|
415
|
-
function Effect(
|
|
526
|
+
function Effect(cb) {
|
|
416
527
|
return (proto, key) => {
|
|
417
|
-
|
|
418
|
-
|
|
528
|
+
setStateVar(proto, key);
|
|
529
|
+
setHandler(proto, key, {
|
|
419
530
|
init(instance) {
|
|
420
531
|
instance[`$_${key}`] = instance[key];
|
|
421
532
|
Object.defineProperty(instance, key, {
|
|
@@ -424,12 +535,7 @@ function Effect(eventName, options) {
|
|
|
424
535
|
},
|
|
425
536
|
set(v) {
|
|
426
537
|
instance[`$_${key}`] = v;
|
|
427
|
-
|
|
428
|
-
instance,
|
|
429
|
-
key,
|
|
430
|
-
value: v,
|
|
431
|
-
options
|
|
432
|
-
});
|
|
538
|
+
cb(v, instance, key);
|
|
433
539
|
return true;
|
|
434
540
|
}
|
|
435
541
|
});
|
|
@@ -438,34 +544,40 @@ function Effect(eventName, options) {
|
|
|
438
544
|
};
|
|
439
545
|
}
|
|
440
546
|
__name(Effect, "Effect");
|
|
441
|
-
function Storage(storeKey) {
|
|
547
|
+
function Storage({ key: storeKey, toJSON, toString } = {}) {
|
|
548
|
+
if (!toJSON)
|
|
549
|
+
toJSON = /* @__PURE__ */ __name((v) => JSON.parse(v), "toJSON");
|
|
550
|
+
if (!toString)
|
|
551
|
+
toString = /* @__PURE__ */ __name((v) => JSON.stringify(v), "toString");
|
|
442
552
|
return (proto, key) => {
|
|
443
553
|
let tag;
|
|
444
554
|
if (key) {
|
|
445
555
|
init(proto);
|
|
446
|
-
tag = storeKey || proto
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
regisHandler(proto, uniTag, {
|
|
556
|
+
tag = storeKey || `${getTag(proto)}_${key}`;
|
|
557
|
+
setStateVar(proto, key);
|
|
558
|
+
setHandler(proto, key, {
|
|
450
559
|
init: (instance) => {
|
|
451
|
-
return
|
|
560
|
+
return getKey("storage")?.({
|
|
452
561
|
instance,
|
|
453
562
|
key,
|
|
454
|
-
tag
|
|
563
|
+
tag,
|
|
564
|
+
toJSON,
|
|
565
|
+
toString
|
|
455
566
|
});
|
|
456
567
|
}
|
|
457
568
|
});
|
|
458
569
|
} else {
|
|
459
570
|
init(proto.prototype);
|
|
460
|
-
tag = storeKey ||
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
regisHandler(proto.prototype, uniTag, {
|
|
571
|
+
tag = storeKey || getTag(proto);
|
|
572
|
+
setStateVar(proto.prototype, SHARE_KEY);
|
|
573
|
+
setHandler(proto.prototype, SHARE_KEY, {
|
|
464
574
|
init: (instance) => {
|
|
465
|
-
return
|
|
575
|
+
return getKey("storage")?.({
|
|
466
576
|
instance,
|
|
467
|
-
key
|
|
468
|
-
tag
|
|
577
|
+
key,
|
|
578
|
+
tag,
|
|
579
|
+
toJSON,
|
|
580
|
+
toString
|
|
469
581
|
});
|
|
470
582
|
}
|
|
471
583
|
});
|
|
@@ -487,9 +599,10 @@ export {
|
|
|
487
599
|
Init,
|
|
488
600
|
Inject,
|
|
489
601
|
Isolate,
|
|
490
|
-
|
|
602
|
+
PHECDA_KEY,
|
|
491
603
|
Pipeline,
|
|
492
604
|
Provide,
|
|
605
|
+
Rule,
|
|
493
606
|
SHARE_KEY,
|
|
494
607
|
Storage,
|
|
495
608
|
Tag,
|
|
@@ -499,29 +612,35 @@ export {
|
|
|
499
612
|
Watcher,
|
|
500
613
|
activeInstance,
|
|
501
614
|
addDecoToClass,
|
|
502
|
-
|
|
615
|
+
classToPlain,
|
|
616
|
+
get,
|
|
503
617
|
getBind,
|
|
504
618
|
getExposeKey,
|
|
505
619
|
getHandler,
|
|
506
|
-
|
|
620
|
+
getKey,
|
|
507
621
|
getOwnExposeKey,
|
|
508
622
|
getOwnHandler,
|
|
509
623
|
getOwnIgnoreKey,
|
|
510
|
-
getOwnModuleState,
|
|
511
624
|
getOwnState,
|
|
512
|
-
|
|
625
|
+
getOwnStateVars,
|
|
513
626
|
getState,
|
|
627
|
+
getStateVars,
|
|
514
628
|
getTag,
|
|
515
629
|
init,
|
|
516
|
-
|
|
630
|
+
injectKey,
|
|
517
631
|
invokeHandler,
|
|
632
|
+
isAsyncFunc,
|
|
518
633
|
isPhecda,
|
|
519
634
|
plainToClass,
|
|
520
|
-
|
|
635
|
+
set,
|
|
521
636
|
setExposeKey,
|
|
637
|
+
setHandler,
|
|
522
638
|
setIgnoreKey,
|
|
523
639
|
setState,
|
|
524
|
-
|
|
640
|
+
setStateVar,
|
|
525
641
|
snapShot,
|
|
526
|
-
|
|
642
|
+
transformInstance,
|
|
643
|
+
transformInstanceAsync,
|
|
644
|
+
transformProperty,
|
|
645
|
+
transformPropertyAsync
|
|
527
646
|
};
|