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