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