phecda-core 3.1.0 → 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.js CHANGED
@@ -3,7 +3,6 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
6
  var __export = (target, all) => {
8
7
  for (var name in all)
9
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -17,13 +16,21 @@ var __copyProps = (to, from, except, desc) => {
17
16
  return to;
18
17
  };
19
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __decorateClass = (decorators, target, key, kind) => {
20
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
21
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
22
+ if (decorator = decorators[i])
23
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
24
+ if (kind && result) __defProp(target, key, result);
25
+ return result;
26
+ };
20
27
 
21
28
  // src/index.ts
22
29
  var src_exports = {};
23
30
  __export(src_exports, {
24
31
  Assign: () => Assign,
25
32
  Base: () => Base,
26
- Bind: () => Bind,
33
+ CLEAR_KEY: () => CLEAR_KEY,
27
34
  Clear: () => Clear,
28
35
  DataMap: () => DataMap,
29
36
  Effect: () => Effect,
@@ -32,602 +39,364 @@ __export(src_exports, {
32
39
  Expose: () => Expose,
33
40
  Global: () => Global,
34
41
  If: () => If,
35
- Ignore: () => Ignore,
36
42
  Init: () => Init,
37
43
  Inject: () => Inject,
44
+ Injectable: () => Injectable,
38
45
  Isolate: () => Isolate,
39
46
  PHECDA_KEY: () => PHECDA_KEY,
40
47
  Pipeline: () => Pipeline,
41
48
  Provide: () => Provide,
42
- Rule: () => Rule,
43
49
  SHARE_KEY: () => SHARE_KEY,
44
50
  Storage: () => Storage,
45
51
  Tag: () => Tag,
46
- To: () => To,
47
52
  Unique: () => Unique,
48
53
  Unmount: () => Unmount,
49
54
  Watcher: () => Watcher,
50
55
  activeInstance: () => activeInstance,
51
56
  addDecoToClass: () => addDecoToClass,
52
- classToPlain: () => classToPlain,
53
57
  get: () => get,
54
- getBind: () => getBind,
55
- getExposeKey: () => getExposeKey,
56
- getHandler: () => getHandler,
57
58
  getInject: () => getInject,
58
- getOwnExposeKey: () => getOwnExposeKey,
59
- getOwnHandler: () => getOwnHandler,
60
- getOwnIgnoreKey: () => getOwnIgnoreKey,
61
- getOwnState: () => getOwnState,
62
- getOwnStateKey: () => getOwnStateKey,
59
+ getMergedMeta: () => getMergedMeta,
60
+ getMeta: () => getMeta,
61
+ getMetaKey: () => getMetaKey,
62
+ getMetaParams: () => getMetaParams,
63
+ getOwnMeta: () => getOwnMeta,
64
+ getOwnMetaKey: () => getOwnMetaKey,
65
+ getOwnMetaParams: () => getOwnMetaParams,
63
66
  getPhecdaFromTarget: () => getPhecdaFromTarget,
64
- getShareState: () => getShareState,
65
- getState: () => getState,
66
- getStateKey: () => getStateKey,
67
67
  getTag: () => getTag,
68
68
  init: () => init,
69
- invokeHandler: () => invokeHandler,
69
+ invoke: () => invoke,
70
+ invokeInit: () => invokeInit,
71
+ invokeUnmount: () => invokeUnmount,
70
72
  isAsyncFunc: () => isAsyncFunc,
71
73
  isPhecda: () => isPhecda,
72
- plainToClass: () => plainToClass,
73
74
  set: () => set,
74
- setExposeKey: () => setExposeKey,
75
- setHandler: () => setHandler,
76
- setIgnoreKey: () => setIgnoreKey,
77
75
  setInject: () => setInject,
78
- setPropertyState: () => setPropertyState,
79
- setState: () => setState,
80
- setStateKey: () => setStateKey,
81
- snapShot: () => snapShot,
82
- transformInstance: () => transformInstance,
83
- transformInstanceAsync: () => transformInstanceAsync,
84
- transformProperty: () => transformProperty,
85
- transformPropertyAsync: () => transformPropertyAsync
76
+ setMeta: () => setMeta
86
77
  });
87
78
  module.exports = __toCommonJS(src_exports);
88
79
 
89
80
  // src/core.ts
90
- var SHARE_KEY = Symbol("phecda");
81
+ var SHARE_KEY = Symbol("phecda[share]");
82
+ var CLEAR_KEY = Symbol("phecda[clear]");
91
83
  var PHECDA_KEY = Symbol("phecda");
92
84
  function isPhecda(model) {
93
- if (typeof model === "function") return !!model.prototype[PHECDA_KEY];
85
+ if (typeof model === "function")
86
+ return !!model.prototype[PHECDA_KEY];
94
87
  return false;
95
88
  }
96
- __name(isPhecda, "isPhecda");
97
89
  function init(proto) {
98
- if (!proto) return;
90
+ if (!proto)
91
+ return;
99
92
  if (!proto.hasOwnProperty(PHECDA_KEY)) {
100
93
  proto[PHECDA_KEY] = {
101
94
  /**
102
- * 暴露的变量,
103
- * 只要属性上存在至少一个装饰器,该属性就会被捕捉到
104
- */
105
- __EXPOSE_KEY: /* @__PURE__ */ new Set(),
106
- /**
107
- * @Ignore 绑定的属性,
108
- * 某属性即使被捕捉,可被强行忽略,优先级最高
109
- */
110
- __IGNORE_KEY: /* @__PURE__ */ new Set(),
111
- /**
112
- * @Clear 绑定的属性,
113
- * 消除父类在该key上的state/handler, 但export key 和 state
114
- */
115
- __CLEAR_KEY: /* @__PURE__ */ new Set(),
116
- /**
117
- * 存在状态的变量
118
- * @deprecated
119
- */
120
- __STATE_KEY: /* @__PURE__ */ new Set(),
121
- /**
122
- * 状态变量的处理器
123
- */
124
- __STATE_HANDLER__: /* @__PURE__ */ new Map(),
125
- /**
126
- * 状态变量的共有状态
127
- */
128
- __STATE_NAMESPACE__: /* @__PURE__ */ new Map()
95
+ * 元数据
96
+ */
97
+ __META__: /* @__PURE__ */ new Map()
129
98
  };
130
99
  }
131
100
  }
132
- __name(init, "init");
133
101
  function getPhecdaFromTarget(target) {
134
- if (typeof target === "function") return target.prototype;
135
- if (target.hasOwnProperty(PHECDA_KEY)) return target;
102
+ if (typeof target === "function")
103
+ return target.prototype;
104
+ if (target.hasOwnProperty(PHECDA_KEY))
105
+ return target;
136
106
  return Object.getPrototypeOf(target);
137
107
  }
138
- __name(getPhecdaFromTarget, "getPhecdaFromTarget");
139
- function setStateKey(proto, key) {
140
- if (!key) {
141
- key = SHARE_KEY;
142
- proto = proto.prototype;
143
- }
144
- init(proto);
145
- proto[PHECDA_KEY].__STATE_KEY.add(key);
146
- setExposeKey(proto, key);
147
- }
148
- __name(setStateKey, "setStateKey");
149
- function setExposeKey(proto, key) {
150
- if (!key) {
151
- key = SHARE_KEY;
152
- proto = proto.prototype;
153
- }
154
- init(proto);
155
- proto[PHECDA_KEY].__EXPOSE_KEY.add(key);
156
- }
157
- __name(setExposeKey, "setExposeKey");
158
- function setIgnoreKey(proto, key) {
159
- if (!key) {
160
- key = SHARE_KEY;
108
+ function setMeta(proto, property, index, meta) {
109
+ if (!property) {
110
+ property = SHARE_KEY;
161
111
  proto = proto.prototype;
162
112
  }
163
113
  init(proto);
164
- proto[PHECDA_KEY].__IGNORE_KEY.add(key);
165
- }
166
- __name(setIgnoreKey, "setIgnoreKey");
167
- function setHandler(proto, key, handler) {
168
- if (!key) {
169
- key = SHARE_KEY;
170
- proto = proto.prototype;
171
- }
172
- init(proto);
173
- if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key)) proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [
174
- handler
175
- ]);
176
- else proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
177
- }
178
- __name(setHandler, "setHandler");
179
- function setState(proto, key, state) {
180
- if (!key) {
181
- key = SHARE_KEY;
182
- proto = proto.prototype;
183
- }
184
- init(proto);
185
- const namespace = proto[PHECDA_KEY].__STATE_NAMESPACE__;
186
- namespace.set(key, state);
187
- }
188
- __name(setState, "setState");
189
- function getOwnStateKey(target) {
190
- const proto = getPhecdaFromTarget(target);
191
- return [
192
- ...proto[PHECDA_KEY].__STATE_KEY
193
- ];
194
- }
195
- __name(getOwnStateKey, "getOwnStateKey");
196
- function getStateKey(target) {
197
- let proto = getPhecdaFromTarget(target);
198
- const set2 = /* @__PURE__ */ new Set();
199
- while (proto?.[PHECDA_KEY]) {
200
- if (proto.hasOwnProperty(PHECDA_KEY)) proto[PHECDA_KEY].__STATE_KEY.forEach((item) => set2.add(item));
201
- 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);
202
124
  }
203
- return [
204
- ...set2
205
- ];
125
+ proto[PHECDA_KEY].__META__.set(property, oldMeta);
206
126
  }
207
- __name(getStateKey, "getStateKey");
208
- function getOwnExposeKey(target) {
127
+ function getOwnMetaKey(target) {
209
128
  const proto = getPhecdaFromTarget(target);
210
- return [
211
- ...proto[PHECDA_KEY].__EXPOSE_KEY
212
- ].filter((item) => !proto[PHECDA_KEY].__IGNORE_KEY.has(item));
129
+ return [...proto[PHECDA_KEY].__META__.keys()];
213
130
  }
214
- __name(getOwnExposeKey, "getOwnExposeKey");
215
- function getExposeKey(target) {
131
+ function getMetaKey(target) {
216
132
  let proto = getPhecdaFromTarget(target);
217
133
  const set2 = /* @__PURE__ */ new Set();
218
- const origin = proto;
219
134
  while (proto?.[PHECDA_KEY]) {
220
- if (proto.hasOwnProperty(PHECDA_KEY)) [
221
- ...proto[PHECDA_KEY].__EXPOSE_KEY
222
- ].forEach((item) => !origin[PHECDA_KEY].__IGNORE_KEY.has(item) && set2.add(item));
135
+ if (proto.hasOwnProperty(PHECDA_KEY)) {
136
+ for (const property of proto[PHECDA_KEY].__META__.keys())
137
+ set2.add(property);
138
+ }
223
139
  proto = Object.getPrototypeOf(proto);
224
140
  }
225
- return [
226
- ...set2
227
- ];
228
- }
229
- __name(getExposeKey, "getExposeKey");
230
- function getOwnIgnoreKey(target) {
231
- const proto = getPhecdaFromTarget(target);
232
- return [
233
- ...proto[PHECDA_KEY]?.__IGNORE_KEY
234
- ];
141
+ return [...set2];
235
142
  }
236
- __name(getOwnIgnoreKey, "getOwnIgnoreKey");
237
- function getOwnHandler(target, key) {
143
+ function getOwnMetaParams(target, key = SHARE_KEY) {
238
144
  const proto = getPhecdaFromTarget(target);
239
- return proto[PHECDA_KEY]?.__STATE_HANDLER__.get(key) || [];
145
+ const { params } = proto[PHECDA_KEY].__META__.get(key);
146
+ return [...params.keys()];
240
147
  }
241
- __name(getOwnHandler, "getOwnHandler");
242
- function getHandler(target, key) {
148
+ function getMetaParams(target, key = SHARE_KEY) {
243
149
  let proto = getPhecdaFromTarget(target);
244
150
  const set2 = /* @__PURE__ */ new Set();
245
151
  while (proto?.[PHECDA_KEY]) {
246
152
  if (proto.hasOwnProperty(PHECDA_KEY)) {
247
- proto[PHECDA_KEY].__STATE_HANDLER__.get(key)?.forEach((item) => set2.add(item));
248
- if (proto[PHECDA_KEY].__CLEAR_KEY.has(key)) break;
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
+ }
249
158
  }
250
159
  proto = Object.getPrototypeOf(proto);
251
160
  }
252
- return [
253
- ...set2
254
- ];
161
+ return [...set2].sort((a, b) => a - b);
255
162
  }
256
- __name(getHandler, "getHandler");
257
- function getState(target, key = SHARE_KEY) {
163
+ function getMeta(target, property = SHARE_KEY, index) {
258
164
  let proto = getPhecdaFromTarget(target);
259
- let ret = {};
165
+ const ret = [];
260
166
  while (proto?.[PHECDA_KEY]) {
261
167
  if (proto.hasOwnProperty(PHECDA_KEY)) {
262
- const state = proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key);
263
- if (state) ret = {
264
- ...state,
265
- ...ret
266
- };
267
- if (proto[PHECDA_KEY].__CLEAR_KEY.has(key)) break;
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
+ }
268
185
  }
269
186
  proto = Object.getPrototypeOf(proto);
270
187
  }
271
188
  return ret;
272
189
  }
273
- __name(getState, "getState");
274
- function getOwnState(target, key = SHARE_KEY) {
190
+ function getOwnMeta(target, property = SHARE_KEY, index) {
275
191
  const proto = getPhecdaFromTarget(target);
276
- return proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key) || {};
277
- }
278
- __name(getOwnState, "getOwnState");
279
- function invokeHandler(event, module2) {
280
- const stateVars = getExposeKey(module2);
281
- const initHandlers = stateVars.map((item) => {
282
- return getHandler(module2, item).filter((h) => !!h[event]).map((h) => h[event](module2));
283
- }).flat();
284
- return module2.__PROMISE_SYMBOL__ = Promise.all(initHandlers);
285
- }
286
- __name(invokeHandler, "invokeHandler");
287
- function set(proto, key, value) {
192
+ const meta = proto[PHECDA_KEY].__META__.get(property);
193
+ return typeof index === "number" ? meta.params.get(index) : meta.data;
194
+ }
195
+ function set(proto, property, value) {
288
196
  init(proto);
289
- proto[`__${key.toUpperCase()}__`] = value;
197
+ proto[`__${property.toUpperCase()}__`] = value;
290
198
  }
291
- __name(set, "set");
292
- function get(proto, key) {
293
- return proto[`__${key.toUpperCase()}__`];
199
+ function get(proto, property) {
200
+ return proto[`__${property.toUpperCase()}__`];
294
201
  }
295
- __name(get, "get");
296
202
 
297
203
  // src/decorators/core.ts
298
- function Init(proto, key) {
299
- setStateKey(proto, key);
300
- setHandler(proto, key, {
204
+ function Init(proto, property) {
205
+ setMeta(proto, property, void 0, {
301
206
  async init(instance) {
302
- return instance[key]();
207
+ return instance[property]();
303
208
  }
304
209
  });
305
210
  }
306
- __name(Init, "Init");
307
- function Unmount(proto, key) {
308
- setStateKey(proto, key);
309
- setHandler(proto, key, {
211
+ function Unmount(proto, property) {
212
+ setMeta(proto, property, void 0, {
310
213
  async unmount(instance) {
311
- return instance[key]();
214
+ return instance[property]();
312
215
  }
313
216
  });
314
217
  }
315
- __name(Unmount, "Unmount");
316
- function Bind(value) {
317
- return (proto, k) => {
318
- setStateKey(proto, k);
319
- setState(proto, k, {
320
- value
321
- });
322
- };
323
- }
324
- __name(Bind, "Bind");
325
- function Ignore(proto, key) {
326
- if (!key) {
327
- proto = proto.prototype;
328
- key = SHARE_KEY;
329
- }
330
- setIgnoreKey(proto, key);
331
- }
332
- __name(Ignore, "Ignore");
333
- function Clear(proto, key) {
334
- if (!key) {
335
- proto = proto.prototype;
336
- key = SHARE_KEY;
337
- }
338
- init(proto);
339
- proto[PHECDA_KEY].__CLEAR_KEY.add(key);
218
+ function Expose(proto, property, index) {
219
+ setMeta(proto, property, index, {});
340
220
  }
341
- __name(Clear, "Clear");
342
- function Expose(proto, key) {
343
- setExposeKey(proto, key);
344
- }
345
- __name(Expose, "Expose");
346
221
  function Empty(model) {
347
222
  init(model.prototype);
348
223
  }
349
- __name(Empty, "Empty");
224
+ function Clear(proto, property, index) {
225
+ setMeta(proto, property, index, {
226
+ [CLEAR_KEY]: true
227
+ });
228
+ }
229
+ function Injectable() {
230
+ return (target) => Empty(target);
231
+ }
350
232
 
351
233
  // src/helper.ts
352
234
  function getTag(moduleOrInstance) {
353
- if (typeof moduleOrInstance === "object") moduleOrInstance = moduleOrInstance.constructor;
235
+ if (typeof moduleOrInstance === "object")
236
+ moduleOrInstance = moduleOrInstance.constructor;
354
237
  return get(moduleOrInstance.prototype, "tag") || moduleOrInstance.name;
355
238
  }
356
- __name(getTag, "getTag");
357
- function getBind(model) {
358
- const instance = new model();
359
- const keys = getStateKey(instance);
360
- const ret = {};
361
- for (const item of keys) {
362
- const state = getState(instance, item);
363
- if (state.value) ret[item] = state.value;
364
- }
365
- return ret;
366
- }
367
- __name(getBind, "getBind");
368
- function plainToClass(model, input) {
369
- const instance = new model();
370
- const keys = getExposeKey(instance);
371
- for (const item of keys) instance[item] = input[item];
372
- return instance;
373
- }
374
- __name(plainToClass, "plainToClass");
375
- function transformInstance(instance, force = false) {
376
- const err = [];
377
- const keys = getExposeKey(instance);
378
- const addError = err.push.bind(err);
379
- for (const item of keys) {
380
- const handlers = getHandler(instance, item);
381
- if (handlers) {
382
- for (const handler of handlers) {
383
- const pipe = handler.pipe;
384
- if (!pipe) continue;
385
- pipe(instance, addError);
386
- if (err.length && !force) return err;
387
- }
388
- }
389
- }
390
- return err;
391
- }
392
- __name(transformInstance, "transformInstance");
393
- async function transformInstanceAsync(instance, force = false) {
394
- const err = [];
395
- const keys = getExposeKey(instance);
396
- const addError = err.push.bind(err);
397
- for (const item of keys) {
398
- const handlers = getHandler(instance, item);
399
- if (handlers) {
400
- for (const handler of handlers) {
401
- const pipe = handler.pipe;
402
- if (!pipe) continue;
403
- await pipe(instance, addError);
404
- if (err.length && !force) return err;
405
- }
406
- }
407
- }
408
- return err;
409
- }
410
- __name(transformInstanceAsync, "transformInstanceAsync");
411
- function transformProperty(instance, property, force = false) {
412
- const err = [];
413
- const handlers = getHandler(instance, property);
414
- const addError = err.push.bind(err);
415
- if (handlers) {
416
- for (const handler of handlers) {
417
- const pipe = handler.pipe;
418
- if (!pipe) continue;
419
- pipe(instance, addError);
420
- if (err.length && !force) return err;
421
- }
422
- }
423
- return err;
424
- }
425
- __name(transformProperty, "transformProperty");
426
- async function transformPropertyAsync(instance, property, force = false) {
427
- const err = [];
428
- const handlers = getHandler(instance, property);
429
- const addError = err.push.bind(err);
430
- if (handlers) {
431
- for (const handler of handlers) {
432
- const pipe = handler.pipe;
433
- if (!pipe) continue;
434
- await pipe(instance, addError);
435
- if (err.length && !force) return err;
436
- }
437
- }
438
- return err;
439
- }
440
- __name(transformPropertyAsync, "transformPropertyAsync");
441
- function classToPlain(instance) {
442
- const data = {};
443
- const exposeVars = getExposeKey(instance);
444
- for (const item of exposeVars) data[item] = instance[item];
445
- return JSON.parse(JSON.stringify(data));
446
- }
447
- __name(classToPlain, "classToPlain");
448
- function snapShot(data) {
449
- const snap = {};
450
- for (const i in data) snap[i] = data[i];
451
- return {
452
- data,
453
- clear() {
454
- for (const i in snap) delete data[i];
455
- },
456
- apply() {
457
- for (const i in snap) data[i] = snap[i];
458
- }
459
- };
460
- }
461
- __name(snapShot, "snapShot");
462
239
  function addDecoToClass(c, key, handler) {
463
240
  handler(key ? c.prototype : c, key);
464
241
  }
465
- __name(addDecoToClass, "addDecoToClass");
466
242
  function Pipeline(...decos) {
467
243
  return (...args) => {
468
- for (const d of decos) d(...args);
244
+ for (const d of decos)
245
+ d(...args);
469
246
  };
470
247
  }
471
- __name(Pipeline, "Pipeline");
472
248
  function isAsyncFunc(fn) {
473
249
  return fn[Symbol.toStringTag] === "AsyncFunction";
474
250
  }
475
- __name(isAsyncFunc, "isAsyncFunc");
476
- function setPropertyState(target, k, setter) {
477
- setStateKey(target, k);
478
- const state = getOwnState(target, k) || {};
479
- setter(state);
480
- setState(target, k, state);
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");
259
+ }
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
+ };
481
271
  }
482
- __name(setPropertyState, "setPropertyState");
483
- function getShareState(target, getter) {
484
- const state = getOwnState(target, SHARE_KEY) || {};
485
- return getter(state);
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;
486
306
  }
487
- __name(getShareState, "getShareState");
488
307
 
489
308
  // src/di.ts
490
309
  var DataMap = {};
491
310
  function Provide(key, value) {
492
311
  DataMap[key] = value;
493
312
  }
494
- __name(Provide, "Provide");
495
313
  function Inject(key) {
496
314
  return DataMap[key];
497
315
  }
498
- __name(Inject, "Inject");
499
316
  var activeInstance = {};
500
317
  function setInject(key, value) {
501
318
  activeInstance[key] = value;
502
319
  return activeInstance;
503
320
  }
504
- __name(setInject, "setInject");
505
321
  function getInject(key) {
506
322
  return activeInstance[key];
507
323
  }
508
- __name(getInject, "getInject");
509
324
 
510
325
  // src/decorators/function.ts
511
326
  function Isolate(model) {
512
327
  set(model.prototype, "isolate", true);
513
328
  }
514
- __name(Isolate, "Isolate");
515
329
  function Tag(tag) {
516
330
  return (model) => {
517
331
  set(model.prototype, "tag", tag);
518
332
  };
519
333
  }
520
- __name(Tag, "Tag");
521
334
  function Unique(desc) {
522
335
  return (model) => {
523
336
  set(model.prototype, "tag", Symbol(desc || model.name));
524
337
  };
525
338
  }
526
- __name(Unique, "Unique");
527
339
  function Assign(cb) {
528
340
  return (model) => {
529
- setStateKey(model);
530
- setHandler(model, void 0, {
531
- init: /* @__PURE__ */ __name(async (instance) => {
341
+ setMeta(model, void 0, void 0, {
342
+ init: async (instance) => {
532
343
  const value = await cb(instance);
533
344
  if (value && typeof value === "object" && !Array.isArray(value)) {
534
- for (const i in value) instance[i] = value[i];
345
+ for (const i in value)
346
+ instance[i] = value[i];
535
347
  }
536
- }, "init")
348
+ }
537
349
  });
538
350
  };
539
351
  }
540
- __name(Assign, "Assign");
541
352
  function Global(model) {
542
- setStateKey(model);
543
- setHandler(model, void 0, {
544
- init: /* @__PURE__ */ __name(async (instance) => {
353
+ setMeta(model, void 0, void 0, {
354
+ init: async (instance) => {
545
355
  const tag = getTag(instance);
546
- if (!globalThis.__PHECDA__) globalThis.__PHECDA__ = {};
356
+ if (!globalThis.__PHECDA__)
357
+ globalThis.__PHECDA__ = {};
547
358
  globalThis.__PHECDA__[tag] = instance.constructor;
548
- }, "init")
359
+ }
549
360
  });
550
361
  }
551
- __name(Global, "Global");
552
- function To(...callbacks) {
553
- return (proto, key) => {
554
- setStateKey(proto, key);
555
- setHandler(proto, key, {
556
- async pipe(instance, addError) {
557
- for (const cb of callbacks) {
558
- try {
559
- if (isAsyncFunc(cb)) instance[key] = await cb(instance[key], instance, key);
560
- else instance[key] = cb(instance[key], instance, key);
561
- } catch (e) {
562
- addError(e.message);
563
- }
564
- }
565
- }
566
- });
567
- };
568
- }
569
- __name(To, "To");
570
- function Rule(cb, info) {
571
- return (proto, key) => {
572
- setStateKey(proto, key);
573
- setHandler(proto, key, {
574
- async pipe(instance, addError) {
575
- let ret;
576
- if (isAsyncFunc(cb)) ret = await cb(instance[key]);
577
- else ret = cb(instance[key]);
578
- if (!ret) {
579
- if (typeof info === "string") addError(info);
580
- else addError(info());
581
- }
582
- }
583
- });
584
- };
585
- }
586
- __name(Rule, "Rule");
587
362
  function Err(cb, isCatch = false) {
588
- return (proto, key) => {
589
- setStateKey(proto, key);
590
- setHandler(proto, key, {
591
- init: /* @__PURE__ */ __name((instance) => {
592
- if (typeof instance[key] === "function") {
593
- const oldFn = instance[key].bind(instance);
363
+ return (proto, property) => {
364
+ setMeta(proto, property, void 0, {
365
+ init: (instance) => {
366
+ if (typeof instance[property] === "function") {
367
+ const oldFn = instance[property].bind(instance);
594
368
  if (isAsyncFunc(oldFn)) {
595
- instance[key] = async (...args) => {
369
+ instance[property] = async (...args) => {
596
370
  try {
597
371
  await oldFn(...args);
598
372
  } catch (e) {
599
- cb(e, instance, key);
600
- if (!isCatch) throw e;
373
+ cb(e, instance, property);
374
+ if (!isCatch)
375
+ throw e;
601
376
  }
602
377
  };
603
378
  } else {
604
- instance[key] = (...args) => {
379
+ instance[property] = (...args) => {
605
380
  try {
606
381
  oldFn(...args);
607
382
  } catch (e) {
608
- cb(e, instance, key);
609
- if (!isCatch) throw e;
383
+ cb(e, instance, property);
384
+ if (!isCatch)
385
+ throw e;
610
386
  }
611
387
  };
612
388
  }
613
389
  }
614
- }, "init")
390
+ }
615
391
  });
616
392
  };
617
393
  }
618
- __name(Err, "Err");
619
394
  function Watcher(eventName, options) {
620
395
  let cb;
621
- return (proto, key) => {
622
- setStateKey(proto, key);
623
- setHandler(proto, key, {
396
+ return (proto, property) => {
397
+ setMeta(proto, property, void 0, {
624
398
  init(instance) {
625
- return cb = getInject("watcher")?.({
626
- eventName,
627
- instance,
628
- key,
629
- options
630
- });
399
+ return cb = getInject("watcher")?.({ eventName, instance, property, options });
631
400
  },
632
401
  unmount() {
633
402
  return cb?.();
@@ -635,20 +404,18 @@ function Watcher(eventName, options) {
635
404
  });
636
405
  };
637
406
  }
638
- __name(Watcher, "Watcher");
639
407
  function Effect(cb) {
640
- return (proto, key) => {
641
- setStateKey(proto, key);
642
- setHandler(proto, key, {
408
+ return (proto, property) => {
409
+ setMeta(proto, property, void 0, {
643
410
  init(instance) {
644
- instance[`$_${key}`] = instance[key];
645
- Object.defineProperty(instance, key, {
411
+ instance[`$_${property}`] = instance[property];
412
+ Object.defineProperty(instance, property, {
646
413
  get() {
647
- return instance[`$_${key}`];
414
+ return instance[`$_${property}`];
648
415
  },
649
416
  set(v) {
650
- instance[`$_${key}`] = v;
651
- cb(v, instance, key);
417
+ instance[`$_${property}`] = v;
418
+ cb(v, instance, property);
652
419
  return true;
653
420
  }
654
421
  });
@@ -656,58 +423,26 @@ function Effect(cb) {
656
423
  });
657
424
  };
658
425
  }
659
- __name(Effect, "Effect");
660
- function Storage({ key: storeKey, json, stringify } = {}) {
661
- if (!json) json = /* @__PURE__ */ __name((v) => JSON.parse(v), "json");
662
- if (!stringify) stringify = /* @__PURE__ */ __name((v) => JSON.stringify(v), "stringify");
663
- return (proto, key) => {
664
- const tag = storeKey || getTag(proto);
426
+ function Storage({ key, json, stringify } = {}) {
427
+ if (!json)
428
+ json = (v) => JSON.parse(v);
429
+ if (!stringify)
430
+ stringify = (v) => JSON.stringify(v);
431
+ return (proto, property) => {
432
+ const tag = key || getTag(proto);
665
433
  init(proto);
666
- setStateKey(proto, key);
667
- setHandler(proto, key, {
668
- init: /* @__PURE__ */ __name((instance) => {
669
- return getInject("storage")?.({
670
- instance,
671
- key,
672
- tag,
673
- toJSON: json,
674
- toString: stringify
675
- });
676
- }, "init")
434
+ setMeta(proto, property, void 0, {
435
+ init: (instance) => {
436
+ return getInject("storage")?.({ instance, property, tag, toJSON: json, toString: stringify });
437
+ }
677
438
  });
678
439
  };
679
440
  }
680
- __name(Storage, "Storage");
681
- function If(value, ...decorators) {
682
- if (value) {
683
- return (...args) => {
684
- decorators.forEach((d) => d(...args));
685
- };
686
- }
687
- return () => {
688
- };
689
- }
690
- __name(If, "If");
691
441
 
692
442
  // src/base.ts
693
- function _ts_decorate(decorators, target, key, desc) {
694
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
695
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
696
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
697
- return c > 3 && r && Object.defineProperty(target, key, r), r;
698
- }
699
- __name(_ts_decorate, "_ts_decorate");
700
- function _ts_metadata(k, v) {
701
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
702
- }
703
- __name(_ts_metadata, "_ts_metadata");
704
443
  var Base = class {
705
- static {
706
- __name(this, "Base");
707
- }
708
- __UNMOUNT_SYMBOL__ = [];
709
- __PROMISE_SYMBOL__;
710
444
  constructor() {
445
+ this.__UNMOUNT_SYMBOL__ = [];
711
446
  }
712
447
  get tag() {
713
448
  return getTag(this);
@@ -728,21 +463,22 @@ var Base = class {
728
463
  onUnmount(cb) {
729
464
  this.__UNMOUNT_SYMBOL__.push(cb);
730
465
  }
466
+ // @ts-expect-error for internal
731
467
  _unmount() {
732
468
  return Promise.all(this.__UNMOUNT_SYMBOL__.map((fn) => fn()));
733
469
  }
734
470
  };
735
- _ts_decorate([
736
- Unmount,
737
- _ts_metadata("design:type", Function),
738
- _ts_metadata("design:paramtypes", []),
739
- _ts_metadata("design:returntype", void 0)
740
- ], Base.prototype, "_unmount", null);
471
+ __decorateClass([
472
+ Unmount
473
+ ], Base.prototype, "_unmount", 1);
474
+ Base = __decorateClass([
475
+ Empty
476
+ ], Base);
741
477
  // Annotate the CommonJS export names for ESM import in node:
742
478
  0 && (module.exports = {
743
479
  Assign,
744
480
  Base,
745
- Bind,
481
+ CLEAR_KEY,
746
482
  Clear,
747
483
  DataMap,
748
484
  Effect,
@@ -751,55 +487,39 @@ _ts_decorate([
751
487
  Expose,
752
488
  Global,
753
489
  If,
754
- Ignore,
755
490
  Init,
756
491
  Inject,
492
+ Injectable,
757
493
  Isolate,
758
494
  PHECDA_KEY,
759
495
  Pipeline,
760
496
  Provide,
761
- Rule,
762
497
  SHARE_KEY,
763
498
  Storage,
764
499
  Tag,
765
- To,
766
500
  Unique,
767
501
  Unmount,
768
502
  Watcher,
769
503
  activeInstance,
770
504
  addDecoToClass,
771
- classToPlain,
772
505
  get,
773
- getBind,
774
- getExposeKey,
775
- getHandler,
776
506
  getInject,
777
- getOwnExposeKey,
778
- getOwnHandler,
779
- getOwnIgnoreKey,
780
- getOwnState,
781
- getOwnStateKey,
507
+ getMergedMeta,
508
+ getMeta,
509
+ getMetaKey,
510
+ getMetaParams,
511
+ getOwnMeta,
512
+ getOwnMetaKey,
513
+ getOwnMetaParams,
782
514
  getPhecdaFromTarget,
783
- getShareState,
784
- getState,
785
- getStateKey,
786
515
  getTag,
787
516
  init,
788
- invokeHandler,
517
+ invoke,
518
+ invokeInit,
519
+ invokeUnmount,
789
520
  isAsyncFunc,
790
521
  isPhecda,
791
- plainToClass,
792
522
  set,
793
- setExposeKey,
794
- setHandler,
795
- setIgnoreKey,
796
523
  setInject,
797
- setPropertyState,
798
- setState,
799
- setStateKey,
800
- snapShot,
801
- transformInstance,
802
- transformInstanceAsync,
803
- transformProperty,
804
- transformPropertyAsync
524
+ setMeta
805
525
  });