phecda-core 3.0.0-alpha.8 → 3.0.0-beta.13

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