phecda-core 3.0.0-alpha.9 → 3.0.0-beta.14

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
- function isPhecda(module) {
6
- if (typeof module === "function")
7
- return !!module.prototype?._namespace;
5
+ var SHARE_KEY = Symbol("phecda");
6
+ var PHECDA_KEY = Symbol("phecda");
7
+ function isPhecda(model) {
8
+ if (typeof model === "function")
9
+ return !!model.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,25 +143,89 @@ function getState(instance, key) {
141
143
  return ret;
142
144
  }
143
145
  __name(getState, "getState");
144
- function invokeHandler(event, instance) {
145
- const stateVars = getExposeKey(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, module) {
152
+ const stateVars = getExposeKey(module);
146
153
  const initHandlers = stateVars.map((item) => {
147
- return getHandler(instance, item).filter((h) => h[event]).map((h) => h[event](instance));
154
+ return getHandler(module, item).filter((h) => !!h[event]).map((h) => h[event](module));
148
155
  }).flat();
149
156
  return Promise.all(initHandlers);
150
157
  }
151
158
  __name(invokeHandler, "invokeHandler");
159
+ function set(proto, key, value) {
160
+ init(proto);
161
+ proto[PHECDA_KEY][`__${key.toUpperCase()}__`] = value;
162
+ }
163
+ __name(set, "set");
164
+ function get(proto, key) {
165
+ return proto[PHECDA_KEY]?.[`__${key.toUpperCase()}__`];
166
+ }
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(model) {
215
+ init(model.prototype);
216
+ }
217
+ __name(Empty, "Empty");
152
218
 
153
219
  // src/helper.ts
154
220
  function getTag(moduleOrInstance) {
155
221
  if (typeof moduleOrInstance === "object")
156
222
  moduleOrInstance = moduleOrInstance.constructor;
157
- return moduleOrInstance.prototype?.__TAG__ || moduleOrInstance.name;
223
+ return moduleOrInstance.prototype[PHECDA_KEY]?.__TAG__ || moduleOrInstance.name;
158
224
  }
159
225
  __name(getTag, "getTag");
160
- function getBind(module) {
161
- const instance = new module();
162
- const keys = getModuleState(instance);
226
+ function getBind(model) {
227
+ const instance = new model();
228
+ const keys = getStateVars(instance);
163
229
  const ret = {};
164
230
  for (const item of keys) {
165
231
  const state = getState(instance, item);
@@ -169,43 +235,96 @@ function getBind(module) {
169
235
  return ret;
170
236
  }
171
237
  __name(getBind, "getBind");
172
- function plainToClass(module, input) {
173
- const instance = new module();
238
+ function plainToClass(model, input) {
239
+ const instance = new model();
174
240
  const keys = getExposeKey(instance);
175
241
  for (const item of keys)
176
242
  instance[item] = input[item];
177
243
  return instance;
178
244
  }
179
245
  __name(plainToClass, "plainToClass");
180
- async function transformClass(instance, force = false) {
246
+ function transformInstance(instance, force = false) {
181
247
  const err = [];
182
- const stateVars = getModuleState(instance);
183
- for (const item of stateVars) {
248
+ const keys = getExposeKey(instance);
249
+ const addError = err.push.bind(err);
250
+ for (const item of keys) {
184
251
  const handlers = getHandler(instance, item);
185
252
  if (handlers) {
186
253
  for (const handler of handlers) {
187
254
  const pipe = handler.pipe;
188
- try {
189
- await pipe(instance);
190
- } catch (e) {
191
- err.push(e.message);
192
- if (!force)
193
- return err;
194
- }
255
+ if (!pipe)
256
+ continue;
257
+ pipe(instance, addError);
258
+ if (err.length && !force)
259
+ return err;
195
260
  }
196
261
  }
197
262
  }
198
263
  return err;
199
264
  }
200
- __name(transformClass, "transformClass");
201
- function classToValue(instance) {
265
+ __name(transformInstance, "transformInstance");
266
+ async function transformInstanceAsync(instance, force = false) {
267
+ const err = [];
268
+ const keys = getExposeKey(instance);
269
+ const addError = err.push.bind(err);
270
+ for (const item of keys) {
271
+ const handlers = getHandler(instance, item);
272
+ if (handlers) {
273
+ for (const handler of handlers) {
274
+ const pipe = handler.pipe;
275
+ if (!pipe)
276
+ continue;
277
+ await pipe(instance, addError);
278
+ if (err.length && !force)
279
+ return err;
280
+ }
281
+ }
282
+ }
283
+ return err;
284
+ }
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) {
202
321
  const data = {};
203
322
  const exposeVars = getExposeKey(instance);
204
323
  for (const item of exposeVars)
205
324
  data[item] = instance[item];
206
- return data;
325
+ return JSON.parse(JSON.stringify(data));
207
326
  }
208
- __name(classToValue, "classToValue");
327
+ __name(classToPlain, "classToPlain");
209
328
  function snapShot(data) {
210
329
  const snap = {};
211
330
  for (const i in data)
@@ -223,8 +342,8 @@ function snapShot(data) {
223
342
  };
224
343
  }
225
344
  __name(snapShot, "snapShot");
226
- function addDecoToClass(c, key, handler, type = "class") {
227
- handler(type === "class" ? c.prototype : c, key);
345
+ function addDecoToClass(c, key, handler) {
346
+ handler(key === SHARE_KEY ? c : c.prototype, key);
228
347
  }
229
348
  __name(addDecoToClass, "addDecoToClass");
230
349
  function Pipeline(...decos) {
@@ -234,92 +353,55 @@ function Pipeline(...decos) {
234
353
  };
235
354
  }
236
355
  __name(Pipeline, "Pipeline");
237
-
238
- // src/decorators.ts
239
- function Init(proto, key) {
240
- setVar(proto, key);
241
- regisHandler(proto, key, {
242
- async init(instance) {
243
- return instance[key]();
244
- }
245
- });
246
- }
247
- __name(Init, "Init");
248
- function Unmount(proto, key) {
249
- setVar(proto, key);
250
- regisHandler(proto, key, {
251
- async unmount(instance) {
252
- return instance[key]();
253
- }
254
- });
356
+ function isAsyncFunc(fn) {
357
+ return fn[Symbol.toStringTag] === "AsyncFunction";
255
358
  }
256
- __name(Unmount, "Unmount");
257
- function Bind(value) {
258
- return (proto, k) => {
259
- setVar(proto, k);
260
- setState(proto, k, {
261
- value
262
- });
263
- };
264
- }
265
- __name(Bind, "Bind");
266
- function Ignore(proto, key) {
267
- setIgnoreKey(proto, key);
359
+ __name(isAsyncFunc, "isAsyncFunc");
360
+
361
+ // src/di.ts
362
+ var DataMap = {};
363
+ function Provide(key, value) {
364
+ DataMap[key] = value;
268
365
  }
269
- __name(Ignore, "Ignore");
270
- function Clear(proto, key) {
271
- init(proto);
272
- proto._namespace.__EXPOSE_VAR__.delete(key);
273
- proto._namespace.__IGNORE_VAR__.delete(key);
274
- proto._namespace.__STATE_VAR__.delete(key);
275
- proto._namespace.__STATE_HANDLER__.delete(key);
276
- proto._namespace.__STATE_NAMESPACE__.delete(key);
366
+ __name(Provide, "Provide");
367
+ function Inject(key) {
368
+ return DataMap[key];
277
369
  }
278
- __name(Clear, "Clear");
279
- function Err(cb) {
280
- return (proto, key) => {
281
- setVar(proto, key);
282
- regisHandler(proto, key, {
283
- error: cb
284
- });
285
- };
370
+ __name(Inject, "Inject");
371
+ var activeInstance = {};
372
+ function injectKey(key, value) {
373
+ activeInstance[key] = value;
374
+ return activeInstance;
286
375
  }
287
- __name(Err, "Err");
288
- function Expose(proto, key) {
289
- setExposeKey(proto, key);
376
+ __name(injectKey, "injectKey");
377
+ function getKey(key) {
378
+ return activeInstance[key];
290
379
  }
291
- __name(Expose, "Expose");
292
- function To(...callbacks) {
293
- return (proto, key) => {
294
- setVar(proto, key);
295
- regisHandler(proto, key, {
296
- async pipe(instance) {
297
- for (const cb of callbacks)
298
- instance[key] = await cb(instance[key], instance, key);
299
- }
300
- });
301
- };
380
+ __name(getKey, "getKey");
381
+
382
+ // src/decorators/function.ts
383
+ function Isolate(model) {
384
+ set(model.prototype, "isolate", true);
385
+ model.prototype[PHECDA_KEY].__ISOLATE__ = true;
302
386
  }
303
- __name(To, "To");
387
+ __name(Isolate, "Isolate");
304
388
  function Tag(tag) {
305
- return (module) => {
306
- init(module.prototype);
307
- module.prototype.__TAG__ = tag;
389
+ return (model) => {
390
+ set(model.prototype, "tag", tag);
308
391
  };
309
392
  }
310
393
  __name(Tag, "Tag");
311
394
  function Unique(desc) {
312
- return (module) => {
313
- init(module.prototype);
314
- module.prototype.__TAG__ = Symbol(desc || module.name);
395
+ return (model) => {
396
+ set(model.prototype, "tag", Symbol(desc || model.name));
315
397
  };
316
398
  }
317
399
  __name(Unique, "Unique");
318
400
  function Assign(cb) {
319
- return (module) => {
320
- init(module.prototype);
321
- setVar(module.prototype, SHARE_KEY);
322
- regisHandler(module.prototype, SHARE_KEY, {
401
+ return (model) => {
402
+ init(model.prototype);
403
+ setStateVar(model.prototype, SHARE_KEY);
404
+ setHandler(model.prototype, SHARE_KEY, {
323
405
  init: async (instance) => {
324
406
  const value = await cb(instance);
325
407
  if (value && typeof value === "object" && !Array.isArray(value)) {
@@ -331,12 +413,12 @@ function Assign(cb) {
331
413
  };
332
414
  }
333
415
  __name(Assign, "Assign");
334
- function Global(module) {
335
- init(module.prototype);
336
- setVar(module.prototype, SHARE_KEY);
337
- regisHandler(module.prototype, SHARE_KEY, {
416
+ function Global(model) {
417
+ init(model.prototype);
418
+ setStateVar(model.prototype, SHARE_KEY);
419
+ setHandler(model.prototype, SHARE_KEY, {
338
420
  init: async (instance) => {
339
- const tag = instance.__TAG__;
421
+ const tag = instance[PHECDA_KEY].__TAG__;
340
422
  if (!tag)
341
423
  return;
342
424
  if (!globalThis.__PHECDA__)
@@ -346,59 +428,88 @@ function Global(module) {
346
428
  });
347
429
  }
348
430
  __name(Global, "Global");
349
- function Empty(module) {
350
- init(module.prototype);
351
- }
352
- __name(Empty, "Empty");
353
- var DataMap = {};
354
- function Provide(key, value) {
355
- DataMap[key] = value;
356
- }
357
- __name(Provide, "Provide");
358
- var EmptyProxy = new Proxy(Empty, {
359
- apply() {
360
- return EmptyProxy;
361
- }
362
- });
363
- function Inject(key) {
364
- return DataMap[key] || EmptyProxy;
365
- }
366
- __name(Inject, "Inject");
367
- function Nested(module) {
368
- return To(async (property) => {
369
- const instance = plainToClass(module, property);
370
- const err = await transformClass(instance);
371
- if (err.length > 0)
372
- throw new Error(err[0]);
373
- return instance;
374
- });
375
- }
376
- __name(Nested, "Nested");
377
- function Isolate() {
378
- return (target) => {
379
- 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
+ });
380
448
  };
381
449
  }
382
- __name(Isolate, "Isolate");
383
-
384
- // src/custom/decorator.ts
385
- var activeInstance = {};
386
- function injectProperty(key, value) {
387
- activeInstance[key] = value;
388
- 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
+ };
389
470
  }
390
- __name(injectProperty, "injectProperty");
391
- function getProperty(key) {
392
- 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
+ };
393
504
  }
394
- __name(getProperty, "getProperty");
505
+ __name(Err, "Err");
395
506
  function Watcher(eventName, options) {
396
507
  let cb;
397
508
  return (proto, key) => {
398
- setVar(proto, key);
399
- regisHandler(proto, key, {
509
+ setStateVar(proto, key);
510
+ setHandler(proto, key, {
400
511
  init(instance) {
401
- return cb = getProperty("watcher")?.({
512
+ return cb = getKey("watcher")?.({
402
513
  eventName,
403
514
  instance,
404
515
  key,
@@ -412,10 +523,10 @@ function Watcher(eventName, options) {
412
523
  };
413
524
  }
414
525
  __name(Watcher, "Watcher");
415
- function Effect(eventName, options) {
526
+ function Effect(cb) {
416
527
  return (proto, key) => {
417
- setVar(proto, key);
418
- regisHandler(proto, key, {
528
+ setStateVar(proto, key);
529
+ setHandler(proto, key, {
419
530
  init(instance) {
420
531
  instance[`$_${key}`] = instance[key];
421
532
  Object.defineProperty(instance, key, {
@@ -424,12 +535,7 @@ function Effect(eventName, options) {
424
535
  },
425
536
  set(v) {
426
537
  instance[`$_${key}`] = v;
427
- getProperty(`effect-${eventName}`)?.({
428
- instance,
429
- key,
430
- value: v,
431
- options
432
- });
538
+ cb(v, instance, key);
433
539
  return true;
434
540
  }
435
541
  });
@@ -438,34 +544,40 @@ function Effect(eventName, options) {
438
544
  };
439
545
  }
440
546
  __name(Effect, "Effect");
441
- 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");
442
552
  return (proto, key) => {
443
553
  let tag;
444
554
  if (key) {
445
555
  init(proto);
446
- tag = storeKey || proto.__TAG__;
447
- const uniTag = Symbol(tag);
448
- setVar(proto, uniTag);
449
- regisHandler(proto, uniTag, {
556
+ tag = storeKey || `${getTag(proto)}_${key}`;
557
+ setStateVar(proto, key);
558
+ setHandler(proto, key, {
450
559
  init: (instance) => {
451
- return getProperty("storage")?.({
560
+ return getKey("storage")?.({
452
561
  instance,
453
562
  key,
454
- tag
563
+ tag,
564
+ toJSON,
565
+ toString
455
566
  });
456
567
  }
457
568
  });
458
569
  } else {
459
570
  init(proto.prototype);
460
- tag = storeKey || `${proto.prototype.__TAG__}_${key}`;
461
- const uniTag = Symbol(tag);
462
- setVar(proto.prototype, uniTag);
463
- regisHandler(proto.prototype, uniTag, {
571
+ tag = storeKey || getTag(proto);
572
+ setStateVar(proto.prototype, SHARE_KEY);
573
+ setHandler(proto.prototype, SHARE_KEY, {
464
574
  init: (instance) => {
465
- return getProperty("storage")?.({
575
+ return getKey("storage")?.({
466
576
  instance,
467
- key: "",
468
- tag
577
+ key,
578
+ tag,
579
+ toJSON,
580
+ toString
469
581
  });
470
582
  }
471
583
  });
@@ -487,9 +599,10 @@ export {
487
599
  Init,
488
600
  Inject,
489
601
  Isolate,
490
- Nested,
602
+ PHECDA_KEY,
491
603
  Pipeline,
492
604
  Provide,
605
+ Rule,
493
606
  SHARE_KEY,
494
607
  Storage,
495
608
  Tag,
@@ -499,29 +612,35 @@ export {
499
612
  Watcher,
500
613
  activeInstance,
501
614
  addDecoToClass,
502
- classToValue,
615
+ classToPlain,
616
+ get,
503
617
  getBind,
504
618
  getExposeKey,
505
619
  getHandler,
506
- getModuleState,
620
+ getKey,
507
621
  getOwnExposeKey,
508
622
  getOwnHandler,
509
623
  getOwnIgnoreKey,
510
- getOwnModuleState,
511
624
  getOwnState,
512
- getProperty,
625
+ getOwnStateVars,
513
626
  getState,
627
+ getStateVars,
514
628
  getTag,
515
629
  init,
516
- injectProperty,
630
+ injectKey,
517
631
  invokeHandler,
632
+ isAsyncFunc,
518
633
  isPhecda,
519
634
  plainToClass,
520
- regisHandler,
635
+ set,
521
636
  setExposeKey,
637
+ setHandler,
522
638
  setIgnoreKey,
523
639
  setState,
524
- setVar,
640
+ setStateVar,
525
641
  snapShot,
526
- transformClass
642
+ transformInstance,
643
+ transformInstanceAsync,
644
+ transformProperty,
645
+ transformPropertyAsync
527
646
  };