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