phecda-core 3.1.1 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +32 -57
- package/dist/index.d.ts +32 -57
- package/dist/index.js +182 -409
- package/dist/index.mjs +169 -380
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ var src_exports = {};
|
|
|
30
30
|
__export(src_exports, {
|
|
31
31
|
Assign: () => Assign,
|
|
32
32
|
Base: () => Base,
|
|
33
|
-
|
|
33
|
+
CLEAR_KEY: () => CLEAR_KEY,
|
|
34
34
|
Clear: () => Clear,
|
|
35
35
|
DataMap: () => DataMap,
|
|
36
36
|
Effect: () => Effect,
|
|
@@ -39,7 +39,6 @@ __export(src_exports, {
|
|
|
39
39
|
Expose: () => Expose,
|
|
40
40
|
Global: () => Global,
|
|
41
41
|
If: () => If,
|
|
42
|
-
Ignore: () => Ignore,
|
|
43
42
|
Init: () => Init,
|
|
44
43
|
Inject: () => Inject,
|
|
45
44
|
Injectable: () => Injectable,
|
|
@@ -47,55 +46,41 @@ __export(src_exports, {
|
|
|
47
46
|
PHECDA_KEY: () => PHECDA_KEY,
|
|
48
47
|
Pipeline: () => Pipeline,
|
|
49
48
|
Provide: () => Provide,
|
|
50
|
-
Rule: () => Rule,
|
|
51
49
|
SHARE_KEY: () => SHARE_KEY,
|
|
52
50
|
Storage: () => Storage,
|
|
53
51
|
Tag: () => Tag,
|
|
54
|
-
To: () => To,
|
|
55
52
|
Unique: () => Unique,
|
|
56
53
|
Unmount: () => Unmount,
|
|
57
54
|
Watcher: () => Watcher,
|
|
58
55
|
activeInstance: () => activeInstance,
|
|
59
56
|
addDecoToClass: () => addDecoToClass,
|
|
60
|
-
classToPlain: () => classToPlain,
|
|
61
57
|
get: () => get,
|
|
62
|
-
getBind: () => getBind,
|
|
63
|
-
getExposeKey: () => getExposeKey,
|
|
64
|
-
getHandler: () => getHandler,
|
|
65
58
|
getInject: () => getInject,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
getMergedMeta: () => getMergedMeta,
|
|
60
|
+
getMeta: () => getMeta,
|
|
61
|
+
getMetaKey: () => getMetaKey,
|
|
62
|
+
getMetaParams: () => getMetaParams,
|
|
63
|
+
getOwnMeta: () => getOwnMeta,
|
|
64
|
+
getOwnMetaKey: () => getOwnMetaKey,
|
|
65
|
+
getOwnMetaParams: () => getOwnMetaParams,
|
|
71
66
|
getPhecdaFromTarget: () => getPhecdaFromTarget,
|
|
72
|
-
getShareState: () => getShareState,
|
|
73
|
-
getState: () => getState,
|
|
74
|
-
getStateKey: () => getStateKey,
|
|
75
67
|
getTag: () => getTag,
|
|
76
68
|
init: () => init,
|
|
77
|
-
|
|
69
|
+
invoke: () => invoke,
|
|
70
|
+
invokeInit: () => invokeInit,
|
|
71
|
+
invokeUnmount: () => invokeUnmount,
|
|
78
72
|
isAsyncFunc: () => isAsyncFunc,
|
|
79
73
|
isPhecda: () => isPhecda,
|
|
80
|
-
plainToClass: () => plainToClass,
|
|
81
74
|
set: () => set,
|
|
82
|
-
setExposeKey: () => setExposeKey,
|
|
83
|
-
setHandler: () => setHandler,
|
|
84
|
-
setIgnoreKey: () => setIgnoreKey,
|
|
85
75
|
setInject: () => setInject,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
setStateKey: () => setStateKey,
|
|
89
|
-
snapShot: () => snapShot,
|
|
90
|
-
transformInstance: () => transformInstance,
|
|
91
|
-
transformInstanceAsync: () => transformInstanceAsync,
|
|
92
|
-
transformProperty: () => transformProperty,
|
|
93
|
-
transformPropertyAsync: () => transformPropertyAsync
|
|
76
|
+
setMeta: () => setMeta,
|
|
77
|
+
wait: () => wait
|
|
94
78
|
});
|
|
95
79
|
module.exports = __toCommonJS(src_exports);
|
|
96
80
|
|
|
97
81
|
// src/core.ts
|
|
98
|
-
var SHARE_KEY = Symbol("phecda");
|
|
82
|
+
var SHARE_KEY = Symbol("phecda[share]");
|
|
83
|
+
var CLEAR_KEY = Symbol("phecda[clear]");
|
|
99
84
|
var PHECDA_KEY = Symbol("phecda");
|
|
100
85
|
function isPhecda(model) {
|
|
101
86
|
if (typeof model === "function")
|
|
@@ -108,33 +93,9 @@ function init(proto) {
|
|
|
108
93
|
if (!proto.hasOwnProperty(PHECDA_KEY)) {
|
|
109
94
|
proto[PHECDA_KEY] = {
|
|
110
95
|
/**
|
|
111
|
-
*
|
|
112
|
-
* 只要属性上存在至少一个装饰器,该属性就会被捕捉到
|
|
96
|
+
* 元数据
|
|
113
97
|
*/
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* @Ignore 绑定的属性,
|
|
117
|
-
* 某属性即使被捕捉,可被强行忽略,优先级最高
|
|
118
|
-
*/
|
|
119
|
-
__IGNORE_KEY: /* @__PURE__ */ new Set(),
|
|
120
|
-
/**
|
|
121
|
-
* @Clear 绑定的属性,
|
|
122
|
-
* 消除父类在该key上的state/handler, 但export key 和 state
|
|
123
|
-
*/
|
|
124
|
-
__CLEAR_KEY: /* @__PURE__ */ new Set(),
|
|
125
|
-
/**
|
|
126
|
-
* 存在状态的变量
|
|
127
|
-
* @deprecated
|
|
128
|
-
*/
|
|
129
|
-
__STATE_KEY: /* @__PURE__ */ new Set(),
|
|
130
|
-
/**
|
|
131
|
-
* 状态变量的处理器
|
|
132
|
-
*/
|
|
133
|
-
__STATE_HANDLER__: /* @__PURE__ */ new Map(),
|
|
134
|
-
/**
|
|
135
|
-
* 状态变量的共有状态
|
|
136
|
-
*/
|
|
137
|
-
__STATE_NAMESPACE__: /* @__PURE__ */ new Map()
|
|
98
|
+
__META__: /* @__PURE__ */ new Map()
|
|
138
99
|
};
|
|
139
100
|
}
|
|
140
101
|
}
|
|
@@ -145,183 +106,127 @@ function getPhecdaFromTarget(target) {
|
|
|
145
106
|
return target;
|
|
146
107
|
return Object.getPrototypeOf(target);
|
|
147
108
|
}
|
|
148
|
-
function
|
|
149
|
-
if (!
|
|
150
|
-
|
|
151
|
-
proto = proto.prototype;
|
|
152
|
-
}
|
|
153
|
-
init(proto);
|
|
154
|
-
proto[PHECDA_KEY].__STATE_KEY.add(key);
|
|
155
|
-
setExposeKey(proto, key);
|
|
156
|
-
}
|
|
157
|
-
function setExposeKey(proto, key) {
|
|
158
|
-
if (!key) {
|
|
159
|
-
key = SHARE_KEY;
|
|
109
|
+
function setMeta(proto, property, index, meta) {
|
|
110
|
+
if (!property) {
|
|
111
|
+
property = SHARE_KEY;
|
|
160
112
|
proto = proto.prototype;
|
|
161
113
|
}
|
|
162
114
|
init(proto);
|
|
163
|
-
proto[PHECDA_KEY].
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
function setHandler(proto, key, handler) {
|
|
174
|
-
if (!key) {
|
|
175
|
-
key = SHARE_KEY;
|
|
176
|
-
proto = proto.prototype;
|
|
177
|
-
}
|
|
178
|
-
init(proto);
|
|
179
|
-
if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key))
|
|
180
|
-
proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [handler]);
|
|
181
|
-
else
|
|
182
|
-
proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
|
|
183
|
-
}
|
|
184
|
-
function setState(proto, key, state) {
|
|
185
|
-
if (!key) {
|
|
186
|
-
key = SHARE_KEY;
|
|
187
|
-
proto = proto.prototype;
|
|
188
|
-
}
|
|
189
|
-
init(proto);
|
|
190
|
-
const namespace = proto[PHECDA_KEY].__STATE_NAMESPACE__;
|
|
191
|
-
namespace.set(key, state);
|
|
192
|
-
}
|
|
193
|
-
function getOwnStateKey(target) {
|
|
194
|
-
const proto = getPhecdaFromTarget(target);
|
|
195
|
-
return [...proto[PHECDA_KEY].__STATE_KEY];
|
|
196
|
-
}
|
|
197
|
-
function getStateKey(target) {
|
|
198
|
-
let proto = getPhecdaFromTarget(target);
|
|
199
|
-
const set2 = /* @__PURE__ */ new Set();
|
|
200
|
-
while (proto?.[PHECDA_KEY]) {
|
|
201
|
-
if (proto.hasOwnProperty(PHECDA_KEY))
|
|
202
|
-
proto[PHECDA_KEY].__STATE_KEY.forEach((item) => set2.add(item));
|
|
203
|
-
proto = Object.getPrototypeOf(proto);
|
|
115
|
+
if (!proto[PHECDA_KEY].__META__.has(property))
|
|
116
|
+
proto[PHECDA_KEY].__META__.set(property, { data: [], params: /* @__PURE__ */ new Map() });
|
|
117
|
+
const oldMeta = proto[PHECDA_KEY].__META__.get(property);
|
|
118
|
+
if (typeof index === "number") {
|
|
119
|
+
if (!oldMeta.params.has(index))
|
|
120
|
+
oldMeta.params.set(index, []);
|
|
121
|
+
const paramsMeta = oldMeta.params.get(index);
|
|
122
|
+
paramsMeta.push(meta);
|
|
123
|
+
} else {
|
|
124
|
+
oldMeta.data.push(meta);
|
|
204
125
|
}
|
|
205
|
-
|
|
126
|
+
proto[PHECDA_KEY].__META__.set(property, oldMeta);
|
|
206
127
|
}
|
|
207
|
-
function
|
|
128
|
+
function getOwnMetaKey(target) {
|
|
208
129
|
const proto = getPhecdaFromTarget(target);
|
|
209
|
-
return [...proto[PHECDA_KEY].
|
|
130
|
+
return [...proto[PHECDA_KEY].__META__.keys()];
|
|
210
131
|
}
|
|
211
|
-
function
|
|
132
|
+
function getMetaKey(target) {
|
|
212
133
|
let proto = getPhecdaFromTarget(target);
|
|
213
134
|
const set2 = /* @__PURE__ */ new Set();
|
|
214
|
-
const origin = proto;
|
|
215
135
|
while (proto?.[PHECDA_KEY]) {
|
|
216
|
-
if (proto.hasOwnProperty(PHECDA_KEY))
|
|
217
|
-
|
|
136
|
+
if (proto.hasOwnProperty(PHECDA_KEY)) {
|
|
137
|
+
for (const property of proto[PHECDA_KEY].__META__.keys())
|
|
138
|
+
set2.add(property);
|
|
139
|
+
}
|
|
218
140
|
proto = Object.getPrototypeOf(proto);
|
|
219
141
|
}
|
|
220
142
|
return [...set2];
|
|
221
143
|
}
|
|
222
|
-
function
|
|
223
|
-
const proto = getPhecdaFromTarget(target);
|
|
224
|
-
return [...proto[PHECDA_KEY]?.__IGNORE_KEY];
|
|
225
|
-
}
|
|
226
|
-
function getOwnHandler(target, key) {
|
|
144
|
+
function getOwnMetaParams(target, key = SHARE_KEY) {
|
|
227
145
|
const proto = getPhecdaFromTarget(target);
|
|
228
|
-
|
|
146
|
+
const { params } = proto[PHECDA_KEY].__META__.get(key);
|
|
147
|
+
return [...params.keys()];
|
|
229
148
|
}
|
|
230
|
-
function
|
|
149
|
+
function getMetaParams(target, key = SHARE_KEY) {
|
|
231
150
|
let proto = getPhecdaFromTarget(target);
|
|
232
151
|
const set2 = /* @__PURE__ */ new Set();
|
|
233
152
|
while (proto?.[PHECDA_KEY]) {
|
|
234
153
|
if (proto.hasOwnProperty(PHECDA_KEY)) {
|
|
235
|
-
proto[PHECDA_KEY].
|
|
236
|
-
if (
|
|
237
|
-
|
|
154
|
+
const meta = proto[PHECDA_KEY].__META__.get(key);
|
|
155
|
+
if (meta) {
|
|
156
|
+
for (const index of meta.params.keys())
|
|
157
|
+
set2.add(index);
|
|
158
|
+
}
|
|
238
159
|
}
|
|
239
160
|
proto = Object.getPrototypeOf(proto);
|
|
240
161
|
}
|
|
241
|
-
return [...set2];
|
|
162
|
+
return [...set2].sort((a, b) => a - b);
|
|
242
163
|
}
|
|
243
|
-
function
|
|
164
|
+
function getMeta(target, property = SHARE_KEY, index) {
|
|
244
165
|
let proto = getPhecdaFromTarget(target);
|
|
245
|
-
|
|
166
|
+
const ret = [];
|
|
246
167
|
while (proto?.[PHECDA_KEY]) {
|
|
247
168
|
if (proto.hasOwnProperty(PHECDA_KEY)) {
|
|
248
|
-
const
|
|
249
|
-
if (
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
169
|
+
const meta = proto[PHECDA_KEY].__META__.get(property);
|
|
170
|
+
if (meta) {
|
|
171
|
+
if (typeof index === "number") {
|
|
172
|
+
const paramMeta = meta.params.get(index);
|
|
173
|
+
if (paramMeta) {
|
|
174
|
+
const index2 = paramMeta.findIndex((item) => item[CLEAR_KEY]);
|
|
175
|
+
ret.unshift(...paramMeta.slice(index2 + 1));
|
|
176
|
+
if (index2 > -1)
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
const index2 = meta.data.findIndex((item) => item[CLEAR_KEY]);
|
|
181
|
+
ret.unshift(...meta.data.slice(index2 + 1));
|
|
182
|
+
if (index2 > -1)
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
253
186
|
}
|
|
254
187
|
proto = Object.getPrototypeOf(proto);
|
|
255
188
|
}
|
|
256
189
|
return ret;
|
|
257
190
|
}
|
|
258
|
-
function
|
|
191
|
+
function getOwnMeta(target, property = SHARE_KEY, index) {
|
|
259
192
|
const proto = getPhecdaFromTarget(target);
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
function invokeHandler(event, module2) {
|
|
263
|
-
const stateVars = getExposeKey(module2);
|
|
264
|
-
const initHandlers = stateVars.map((item) => {
|
|
265
|
-
return getHandler(module2, item).filter((h) => !!h[event]).map((h) => h[event](module2));
|
|
266
|
-
}).flat();
|
|
267
|
-
return module2.__PROMISE_SYMBOL__ = Promise.all(initHandlers);
|
|
193
|
+
const meta = proto[PHECDA_KEY].__META__.get(property);
|
|
194
|
+
return typeof index === "number" ? meta.params.get(index) : meta.data;
|
|
268
195
|
}
|
|
269
|
-
function set(proto,
|
|
196
|
+
function set(proto, property, value) {
|
|
270
197
|
init(proto);
|
|
271
|
-
proto[`__${
|
|
198
|
+
proto[`__${property.toUpperCase()}__`] = value;
|
|
272
199
|
}
|
|
273
|
-
function get(proto,
|
|
274
|
-
return proto[`__${
|
|
200
|
+
function get(proto, property) {
|
|
201
|
+
return proto[`__${property.toUpperCase()}__`];
|
|
275
202
|
}
|
|
276
203
|
|
|
277
204
|
// src/decorators/core.ts
|
|
278
|
-
function Init(proto,
|
|
279
|
-
|
|
280
|
-
setHandler(proto, key, {
|
|
205
|
+
function Init(proto, property) {
|
|
206
|
+
setMeta(proto, property, void 0, {
|
|
281
207
|
async init(instance) {
|
|
282
|
-
return instance[
|
|
208
|
+
return instance[property]();
|
|
283
209
|
}
|
|
284
210
|
});
|
|
285
211
|
}
|
|
286
|
-
function Unmount(proto,
|
|
287
|
-
|
|
288
|
-
setHandler(proto, key, {
|
|
212
|
+
function Unmount(proto, property) {
|
|
213
|
+
setMeta(proto, property, void 0, {
|
|
289
214
|
async unmount(instance) {
|
|
290
|
-
return instance[
|
|
215
|
+
return instance[property]();
|
|
291
216
|
}
|
|
292
217
|
});
|
|
293
218
|
}
|
|
294
|
-
function
|
|
295
|
-
|
|
296
|
-
setStateKey(proto, k);
|
|
297
|
-
setState(proto, k, {
|
|
298
|
-
value
|
|
299
|
-
});
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
function Ignore(proto, key) {
|
|
303
|
-
if (!key) {
|
|
304
|
-
proto = proto.prototype;
|
|
305
|
-
key = SHARE_KEY;
|
|
306
|
-
}
|
|
307
|
-
;
|
|
308
|
-
setIgnoreKey(proto, key);
|
|
309
|
-
}
|
|
310
|
-
function Clear(proto, key) {
|
|
311
|
-
if (!key) {
|
|
312
|
-
proto = proto.prototype;
|
|
313
|
-
key = SHARE_KEY;
|
|
314
|
-
}
|
|
315
|
-
;
|
|
316
|
-
init(proto);
|
|
317
|
-
proto[PHECDA_KEY].__CLEAR_KEY.add(key);
|
|
318
|
-
}
|
|
319
|
-
function Expose(proto, key) {
|
|
320
|
-
setExposeKey(proto, key);
|
|
219
|
+
function Expose(proto, property, index) {
|
|
220
|
+
setMeta(proto, property, index, {});
|
|
321
221
|
}
|
|
322
222
|
function Empty(model) {
|
|
323
223
|
init(model.prototype);
|
|
324
224
|
}
|
|
225
|
+
function Clear(proto, property, index) {
|
|
226
|
+
setMeta(proto, property, index, {
|
|
227
|
+
[CLEAR_KEY]: true
|
|
228
|
+
});
|
|
229
|
+
}
|
|
325
230
|
function Injectable() {
|
|
326
231
|
return (target) => Empty(target);
|
|
327
232
|
}
|
|
@@ -332,117 +237,6 @@ function getTag(moduleOrInstance) {
|
|
|
332
237
|
moduleOrInstance = moduleOrInstance.constructor;
|
|
333
238
|
return get(moduleOrInstance.prototype, "tag") || moduleOrInstance.name;
|
|
334
239
|
}
|
|
335
|
-
function getBind(model) {
|
|
336
|
-
const instance = new model();
|
|
337
|
-
const keys = getStateKey(instance);
|
|
338
|
-
const ret = {};
|
|
339
|
-
for (const item of keys) {
|
|
340
|
-
const state = getState(instance, item);
|
|
341
|
-
if (state.value)
|
|
342
|
-
ret[item] = state.value;
|
|
343
|
-
}
|
|
344
|
-
return ret;
|
|
345
|
-
}
|
|
346
|
-
function plainToClass(model, input) {
|
|
347
|
-
const instance = new model();
|
|
348
|
-
const keys = getExposeKey(instance);
|
|
349
|
-
for (const item of keys)
|
|
350
|
-
instance[item] = input[item];
|
|
351
|
-
return instance;
|
|
352
|
-
}
|
|
353
|
-
function transformInstance(instance, force = false) {
|
|
354
|
-
const err = [];
|
|
355
|
-
const keys = getExposeKey(instance);
|
|
356
|
-
const addError = err.push.bind(err);
|
|
357
|
-
for (const item of keys) {
|
|
358
|
-
const handlers = getHandler(instance, item);
|
|
359
|
-
if (handlers) {
|
|
360
|
-
for (const handler of handlers) {
|
|
361
|
-
const pipe = handler.pipe;
|
|
362
|
-
if (!pipe)
|
|
363
|
-
continue;
|
|
364
|
-
pipe(instance, addError);
|
|
365
|
-
if (err.length && !force)
|
|
366
|
-
return err;
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
return err;
|
|
371
|
-
}
|
|
372
|
-
async function transformInstanceAsync(instance, force = false) {
|
|
373
|
-
const err = [];
|
|
374
|
-
const keys = getExposeKey(instance);
|
|
375
|
-
const addError = err.push.bind(err);
|
|
376
|
-
for (const item of keys) {
|
|
377
|
-
const handlers = getHandler(instance, item);
|
|
378
|
-
if (handlers) {
|
|
379
|
-
for (const handler of handlers) {
|
|
380
|
-
const pipe = handler.pipe;
|
|
381
|
-
if (!pipe)
|
|
382
|
-
continue;
|
|
383
|
-
await pipe(instance, addError);
|
|
384
|
-
if (err.length && !force)
|
|
385
|
-
return err;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
return err;
|
|
390
|
-
}
|
|
391
|
-
function transformProperty(instance, property, force = false) {
|
|
392
|
-
const err = [];
|
|
393
|
-
const handlers = getHandler(instance, property);
|
|
394
|
-
const addError = err.push.bind(err);
|
|
395
|
-
if (handlers) {
|
|
396
|
-
for (const handler of handlers) {
|
|
397
|
-
const pipe = handler.pipe;
|
|
398
|
-
if (!pipe)
|
|
399
|
-
continue;
|
|
400
|
-
pipe(instance, addError);
|
|
401
|
-
if (err.length && !force)
|
|
402
|
-
return err;
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
return err;
|
|
406
|
-
}
|
|
407
|
-
async function transformPropertyAsync(instance, property, force = false) {
|
|
408
|
-
const err = [];
|
|
409
|
-
const handlers = getHandler(instance, property);
|
|
410
|
-
const addError = err.push.bind(err);
|
|
411
|
-
if (handlers) {
|
|
412
|
-
for (const handler of handlers) {
|
|
413
|
-
const pipe = handler.pipe;
|
|
414
|
-
if (!pipe)
|
|
415
|
-
continue;
|
|
416
|
-
await pipe(instance, addError);
|
|
417
|
-
if (err.length && !force)
|
|
418
|
-
return err;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
return err;
|
|
422
|
-
}
|
|
423
|
-
function classToPlain(instance) {
|
|
424
|
-
const data = {};
|
|
425
|
-
const exposeVars = getExposeKey(instance);
|
|
426
|
-
for (const item of exposeVars)
|
|
427
|
-
data[item] = instance[item];
|
|
428
|
-
return JSON.parse(JSON.stringify(data));
|
|
429
|
-
}
|
|
430
|
-
function snapShot(data) {
|
|
431
|
-
const snap = {};
|
|
432
|
-
for (const i in data)
|
|
433
|
-
snap[i] = data[i];
|
|
434
|
-
return {
|
|
435
|
-
data,
|
|
436
|
-
clear() {
|
|
437
|
-
for (const i in snap)
|
|
438
|
-
delete data[i];
|
|
439
|
-
},
|
|
440
|
-
apply() {
|
|
441
|
-
for (const i in snap)
|
|
442
|
-
data[i] = snap[i];
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
240
|
function addDecoToClass(c, key, handler) {
|
|
447
241
|
handler(key ? c.prototype : c, key);
|
|
448
242
|
}
|
|
@@ -455,15 +249,64 @@ function Pipeline(...decos) {
|
|
|
455
249
|
function isAsyncFunc(fn) {
|
|
456
250
|
return fn[Symbol.toStringTag] === "AsyncFunction";
|
|
457
251
|
}
|
|
458
|
-
function
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
252
|
+
function invoke(instance, key, ...params) {
|
|
253
|
+
const metaKeys = getMetaKey(instance);
|
|
254
|
+
return Promise.allSettled(metaKeys.map((k) => {
|
|
255
|
+
return getMeta(instance, k);
|
|
256
|
+
}).flat().filter((item) => typeof item[key] === "function").map((item) => item[key](instance, ...params)));
|
|
257
|
+
}
|
|
258
|
+
function invokeInit(instance) {
|
|
259
|
+
return instance.__PROMISE_SYMBOL__ = invoke(instance, "init");
|
|
260
|
+
}
|
|
261
|
+
function invokeUnmount(instance) {
|
|
262
|
+
return instance.__PROMISE_SYMBOL__ = invoke(instance, "unmount");
|
|
263
|
+
}
|
|
264
|
+
function If(value, ...decorators) {
|
|
265
|
+
if (value) {
|
|
266
|
+
return (...args) => {
|
|
267
|
+
decorators.forEach((d) => d(...args));
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
return () => {
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function getMergedMeta(target, property, index, merger = defaultMerger) {
|
|
274
|
+
const meta = getMeta(target, property, index);
|
|
275
|
+
return meta.reduce((p, c) => {
|
|
276
|
+
return merger(p, c);
|
|
277
|
+
}, {});
|
|
278
|
+
}
|
|
279
|
+
function defaultMerger(prev, cur) {
|
|
280
|
+
const newMeta = {};
|
|
281
|
+
for (const key in prev) {
|
|
282
|
+
if (key === CLEAR_KEY)
|
|
283
|
+
continue;
|
|
284
|
+
newMeta[key] = prev[key];
|
|
285
|
+
}
|
|
286
|
+
for (const key in cur) {
|
|
287
|
+
if (key === CLEAR_KEY)
|
|
288
|
+
continue;
|
|
289
|
+
if (newMeta[key] && cur[key]) {
|
|
290
|
+
if (Array.isArray(newMeta[key]) && Array.isArray(cur[key])) {
|
|
291
|
+
const set2 = new Set(newMeta[key]);
|
|
292
|
+
cur[key].forEach((item) => {
|
|
293
|
+
set2.delete(item);
|
|
294
|
+
set2.add(item);
|
|
295
|
+
});
|
|
296
|
+
newMeta[key] = [...set2];
|
|
297
|
+
} else if (typeof newMeta[key] === "object" && typeof cur[key] === "object") {
|
|
298
|
+
newMeta[key] = defaultMerger(newMeta[key], cur[key]);
|
|
299
|
+
} else {
|
|
300
|
+
newMeta[key] = cur[key];
|
|
301
|
+
}
|
|
302
|
+
} else {
|
|
303
|
+
newMeta[key] = cur[key];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return newMeta;
|
|
463
307
|
}
|
|
464
|
-
function
|
|
465
|
-
|
|
466
|
-
return getter(state);
|
|
308
|
+
function wait(...instances) {
|
|
309
|
+
return Promise.all(instances.map((i) => i.__PROMISE_SYMBOL__));
|
|
467
310
|
}
|
|
468
311
|
|
|
469
312
|
// src/di.ts
|
|
@@ -499,8 +342,7 @@ function Unique(desc) {
|
|
|
499
342
|
}
|
|
500
343
|
function Assign(cb) {
|
|
501
344
|
return (model) => {
|
|
502
|
-
|
|
503
|
-
setHandler(model, void 0, {
|
|
345
|
+
setMeta(model, void 0, void 0, {
|
|
504
346
|
init: async (instance) => {
|
|
505
347
|
const value = await cb(instance);
|
|
506
348
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
@@ -512,8 +354,7 @@ function Assign(cb) {
|
|
|
512
354
|
};
|
|
513
355
|
}
|
|
514
356
|
function Global(model) {
|
|
515
|
-
|
|
516
|
-
setHandler(model, void 0, {
|
|
357
|
+
setMeta(model, void 0, void 0, {
|
|
517
358
|
init: async (instance) => {
|
|
518
359
|
const tag = getTag(instance);
|
|
519
360
|
if (!globalThis.__PHECDA__)
|
|
@@ -522,68 +363,28 @@ function Global(model) {
|
|
|
522
363
|
}
|
|
523
364
|
});
|
|
524
365
|
}
|
|
525
|
-
function To(...callbacks) {
|
|
526
|
-
return (proto, key) => {
|
|
527
|
-
setStateKey(proto, key);
|
|
528
|
-
setHandler(proto, key, {
|
|
529
|
-
async pipe(instance, addError) {
|
|
530
|
-
for (const cb of callbacks) {
|
|
531
|
-
try {
|
|
532
|
-
if (isAsyncFunc(cb))
|
|
533
|
-
instance[key] = await cb(instance[key], instance, key);
|
|
534
|
-
else
|
|
535
|
-
instance[key] = cb(instance[key], instance, key);
|
|
536
|
-
} catch (e) {
|
|
537
|
-
addError(e.message);
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
});
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
function Rule(cb, info) {
|
|
545
|
-
return (proto, key) => {
|
|
546
|
-
setStateKey(proto, key);
|
|
547
|
-
setHandler(proto, key, {
|
|
548
|
-
async pipe(instance, addError) {
|
|
549
|
-
let ret;
|
|
550
|
-
if (isAsyncFunc(cb))
|
|
551
|
-
ret = await cb(instance[key]);
|
|
552
|
-
else
|
|
553
|
-
ret = cb(instance[key]);
|
|
554
|
-
if (!ret) {
|
|
555
|
-
if (typeof info === "string")
|
|
556
|
-
addError(info);
|
|
557
|
-
else
|
|
558
|
-
addError(info());
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
});
|
|
562
|
-
};
|
|
563
|
-
}
|
|
564
366
|
function Err(cb, isCatch = false) {
|
|
565
|
-
return (proto,
|
|
566
|
-
|
|
567
|
-
setHandler(proto, key, {
|
|
367
|
+
return (proto, property) => {
|
|
368
|
+
setMeta(proto, property, void 0, {
|
|
568
369
|
init: (instance) => {
|
|
569
|
-
if (typeof instance[
|
|
570
|
-
const oldFn = instance[
|
|
370
|
+
if (typeof instance[property] === "function") {
|
|
371
|
+
const oldFn = instance[property].bind(instance);
|
|
571
372
|
if (isAsyncFunc(oldFn)) {
|
|
572
|
-
instance[
|
|
373
|
+
instance[property] = async (...args) => {
|
|
573
374
|
try {
|
|
574
375
|
await oldFn(...args);
|
|
575
376
|
} catch (e) {
|
|
576
|
-
cb(e, instance,
|
|
377
|
+
cb(e, instance, property);
|
|
577
378
|
if (!isCatch)
|
|
578
379
|
throw e;
|
|
579
380
|
}
|
|
580
381
|
};
|
|
581
382
|
} else {
|
|
582
|
-
instance[
|
|
383
|
+
instance[property] = (...args) => {
|
|
583
384
|
try {
|
|
584
385
|
oldFn(...args);
|
|
585
386
|
} catch (e) {
|
|
586
|
-
cb(e, instance,
|
|
387
|
+
cb(e, instance, property);
|
|
587
388
|
if (!isCatch)
|
|
588
389
|
throw e;
|
|
589
390
|
}
|
|
@@ -596,11 +397,10 @@ function Err(cb, isCatch = false) {
|
|
|
596
397
|
}
|
|
597
398
|
function Watcher(eventName, options) {
|
|
598
399
|
let cb;
|
|
599
|
-
return (proto,
|
|
600
|
-
|
|
601
|
-
setHandler(proto, key, {
|
|
400
|
+
return (proto, property) => {
|
|
401
|
+
setMeta(proto, property, void 0, {
|
|
602
402
|
init(instance) {
|
|
603
|
-
return cb = getInject("watcher")?.({ eventName, instance,
|
|
403
|
+
return cb = getInject("watcher")?.({ eventName, instance, property, options });
|
|
604
404
|
},
|
|
605
405
|
unmount() {
|
|
606
406
|
return cb?.();
|
|
@@ -609,18 +409,17 @@ function Watcher(eventName, options) {
|
|
|
609
409
|
};
|
|
610
410
|
}
|
|
611
411
|
function Effect(cb) {
|
|
612
|
-
return (proto,
|
|
613
|
-
|
|
614
|
-
setHandler(proto, key, {
|
|
412
|
+
return (proto, property) => {
|
|
413
|
+
setMeta(proto, property, void 0, {
|
|
615
414
|
init(instance) {
|
|
616
|
-
instance[`$_${
|
|
617
|
-
Object.defineProperty(instance,
|
|
415
|
+
instance[`$_${property}`] = instance[property];
|
|
416
|
+
Object.defineProperty(instance, property, {
|
|
618
417
|
get() {
|
|
619
|
-
return instance[`$_${
|
|
418
|
+
return instance[`$_${property}`];
|
|
620
419
|
},
|
|
621
420
|
set(v) {
|
|
622
|
-
instance[`$_${
|
|
623
|
-
cb(v, instance,
|
|
421
|
+
instance[`$_${property}`] = v;
|
|
422
|
+
cb(v, instance, property);
|
|
624
423
|
return true;
|
|
625
424
|
}
|
|
626
425
|
});
|
|
@@ -628,31 +427,21 @@ function Effect(cb) {
|
|
|
628
427
|
});
|
|
629
428
|
};
|
|
630
429
|
}
|
|
631
|
-
function Storage({ key
|
|
430
|
+
function Storage({ key, json, stringify } = {}) {
|
|
632
431
|
if (!json)
|
|
633
432
|
json = (v) => JSON.parse(v);
|
|
634
433
|
if (!stringify)
|
|
635
434
|
stringify = (v) => JSON.stringify(v);
|
|
636
|
-
return (proto,
|
|
637
|
-
const tag =
|
|
435
|
+
return (proto, property) => {
|
|
436
|
+
const tag = key || getTag(proto);
|
|
638
437
|
init(proto);
|
|
639
|
-
|
|
640
|
-
setHandler(proto, key, {
|
|
438
|
+
setMeta(proto, property, void 0, {
|
|
641
439
|
init: (instance) => {
|
|
642
|
-
return getInject("storage")?.({ instance,
|
|
440
|
+
return getInject("storage")?.({ instance, property, tag, toJSON: json, toString: stringify });
|
|
643
441
|
}
|
|
644
442
|
});
|
|
645
443
|
};
|
|
646
444
|
}
|
|
647
|
-
function If(value, ...decorators) {
|
|
648
|
-
if (value) {
|
|
649
|
-
return (...args) => {
|
|
650
|
-
decorators.forEach((d) => d(...args));
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
return () => {
|
|
654
|
-
};
|
|
655
|
-
}
|
|
656
445
|
|
|
657
446
|
// src/base.ts
|
|
658
447
|
var Base = class {
|
|
@@ -693,7 +482,7 @@ Base = __decorateClass([
|
|
|
693
482
|
0 && (module.exports = {
|
|
694
483
|
Assign,
|
|
695
484
|
Base,
|
|
696
|
-
|
|
485
|
+
CLEAR_KEY,
|
|
697
486
|
Clear,
|
|
698
487
|
DataMap,
|
|
699
488
|
Effect,
|
|
@@ -702,7 +491,6 @@ Base = __decorateClass([
|
|
|
702
491
|
Expose,
|
|
703
492
|
Global,
|
|
704
493
|
If,
|
|
705
|
-
Ignore,
|
|
706
494
|
Init,
|
|
707
495
|
Inject,
|
|
708
496
|
Injectable,
|
|
@@ -710,48 +498,33 @@ Base = __decorateClass([
|
|
|
710
498
|
PHECDA_KEY,
|
|
711
499
|
Pipeline,
|
|
712
500
|
Provide,
|
|
713
|
-
Rule,
|
|
714
501
|
SHARE_KEY,
|
|
715
502
|
Storage,
|
|
716
503
|
Tag,
|
|
717
|
-
To,
|
|
718
504
|
Unique,
|
|
719
505
|
Unmount,
|
|
720
506
|
Watcher,
|
|
721
507
|
activeInstance,
|
|
722
508
|
addDecoToClass,
|
|
723
|
-
classToPlain,
|
|
724
509
|
get,
|
|
725
|
-
getBind,
|
|
726
|
-
getExposeKey,
|
|
727
|
-
getHandler,
|
|
728
510
|
getInject,
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
511
|
+
getMergedMeta,
|
|
512
|
+
getMeta,
|
|
513
|
+
getMetaKey,
|
|
514
|
+
getMetaParams,
|
|
515
|
+
getOwnMeta,
|
|
516
|
+
getOwnMetaKey,
|
|
517
|
+
getOwnMetaParams,
|
|
734
518
|
getPhecdaFromTarget,
|
|
735
|
-
getShareState,
|
|
736
|
-
getState,
|
|
737
|
-
getStateKey,
|
|
738
519
|
getTag,
|
|
739
520
|
init,
|
|
740
|
-
|
|
521
|
+
invoke,
|
|
522
|
+
invokeInit,
|
|
523
|
+
invokeUnmount,
|
|
741
524
|
isAsyncFunc,
|
|
742
525
|
isPhecda,
|
|
743
|
-
plainToClass,
|
|
744
526
|
set,
|
|
745
|
-
setExposeKey,
|
|
746
|
-
setHandler,
|
|
747
|
-
setIgnoreKey,
|
|
748
527
|
setInject,
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
setStateKey,
|
|
752
|
-
snapShot,
|
|
753
|
-
transformInstance,
|
|
754
|
-
transformInstanceAsync,
|
|
755
|
-
transformProperty,
|
|
756
|
-
transformPropertyAsync
|
|
528
|
+
setMeta,
|
|
529
|
+
wait
|
|
757
530
|
});
|