phecda-core 3.0.0-alpha.1 → 3.0.0-alpha.11

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.d.ts CHANGED
@@ -1,9 +1,19 @@
1
+ declare function Init(proto: any, key: PropertyKey): void;
2
+ declare function Unmount(proto: any, key: PropertyKey): void;
3
+ declare function Bind(value: any): (proto: any, k: PropertyKey) => void;
4
+ declare function Ignore(proto: any, key: PropertyKey): void;
5
+ declare function Clear(proto: any, key: PropertyKey): void;
6
+ declare function Expose(proto: any, key: PropertyKey): void;
7
+ declare function Empty(module: any): void;
8
+
1
9
  interface NameSpace {
2
10
  [name: string]: Phecda;
3
11
  }
4
12
  interface InjectData {
5
13
  [key: string]: any;
6
14
  }
15
+ type Construct<T = any> = new (...args: any[]) => T;
16
+ type AbConstruct<T = any> = abstract new (...args: any[]) => T;
7
17
  interface Handler {
8
18
  [key: string]: any;
9
19
  }
@@ -23,37 +33,53 @@ type ClassValue<I> = {
23
33
  interface Events {
24
34
  }
25
35
 
26
- declare function Init(proto: any, key: PropertyKey): void;
27
- declare function Bind(value: any): (proto: any, k: PropertyKey) => void;
28
- declare function Ignore(proto: any, key: PropertyKey): void;
29
- declare function Clear(proto: any, key: PropertyKey): void;
30
- declare function Err<Fn extends (...args: any) => any>(cb: Fn): (proto: any, key: PropertyKey) => void;
31
- declare function Expose(proto: any, key: PropertyKey): void;
32
- declare function To(cb: (arg: any, instance: any, key: string) => any): (proto: any, key: PropertyKey) => void;
33
- declare function Tag(tag: string): (module: any) => void;
36
+ declare function Isolate(target: any): void;
37
+ declare function Tag(tag: PropertyKey): (module: any) => void;
38
+ declare function Unique(desc?: string): (module: any) => void;
34
39
  declare function Assign(cb: (instance?: any) => any): (module: any) => void;
35
40
  declare function Global(module: any): void;
36
- declare function Empty(module: any): void;
37
- declare const DataMap: InjectData;
38
- declare function Provide<K extends keyof InjectData>(key: K, value: InjectData[K]): void;
39
- declare function Inject<K extends keyof InjectData>(key: K): InjectData[K];
40
- declare function Nested<M extends new (...args: any) => any>(module: M): (proto: any, key: PropertyKey) => void;
41
+ declare function To(...callbacks: ((arg: any, instance: any, key: string) => any)[]): (proto: any, key: PropertyKey) => void;
42
+ declare function Err(cb: (e: Error | any) => void, isCatch?: boolean): (proto: any, key: PropertyKey) => void;
43
+ interface StorageParam {
44
+ key?: string;
45
+ instance: any;
46
+ tag: string;
47
+ toJSON: (str: string) => any;
48
+ toString: (arg: any) => string;
49
+ }
50
+ interface WatcherParam {
51
+ key: string;
52
+ instance: any;
53
+ eventName: string;
54
+ options?: {
55
+ once?: boolean;
56
+ };
57
+ }
58
+ declare function Watcher(eventName: keyof Events, options?: {
59
+ once?: boolean;
60
+ }): (proto: any, key: string) => void;
61
+ declare function Effect(eventName: string, options?: any): (proto: any, key: string) => void;
62
+ declare function Storage({ key: storeKey, toJSON, toString }?: {
63
+ toJSON?: (str: string) => any;
64
+ toString?: (arg: any) => string;
65
+ key?: string;
66
+ }): (proto: any, key?: PropertyKey) => void;
41
67
 
42
- declare function getTag<M extends new (...args: any) => any>(module: M): any;
43
- declare function getSymbol<M extends new (...args: any) => any>(instance: InstanceType<M>): any;
44
- declare function getBind<M extends new (...args: any) => any>(module: M): any;
45
- declare function plainToClass<M extends new (...args: any) => any, Data extends Record<PropertyKey, any>>(module: M, input: Data): InstanceType<M>;
46
- declare function transformClass<M extends new (...args: any) => any>(instance: InstanceType<M>, force?: boolean): Promise<string[]>;
47
- declare function classToValue<M>(instance: M): ClassValue<M>;
48
- declare function snapShot<T extends new (...args: any) => any>(data: InstanceType<T>): {
68
+ declare function getTag<M extends Construct | AbConstruct>(moduleOrInstance: M | InstanceType<M>): PropertyKey;
69
+ declare function getBind<M extends Construct | AbConstruct>(module: M): any;
70
+ declare function plainToClass<M extends Construct, Data extends Record<PropertyKey, any>>(module: M, input: Data): InstanceType<M>;
71
+ declare function transformClass<M extends Construct>(instance: InstanceType<M>, force?: boolean): Promise<string[]>;
72
+ declare function classToPlain<M>(instance: M): ClassValue<M>;
73
+ declare function snapShot<T extends Construct>(data: InstanceType<T>): {
49
74
  data: InstanceType<T>;
50
75
  clear(): void;
51
76
  apply(): void;
52
77
  };
53
- declare function addDecoToClass<M extends new (...args: any) => any>(c: M, key: keyof InstanceType<M> | string, handler: ((target: any, key: PropertyKey) => void), type?: 'property' | 'class'): void;
78
+ declare function addDecoToClass<M extends Construct | AbConstruct>(c: M, key: keyof InstanceType<M> | PropertyKey, handler: PropertyDecorator | ClassDecorator): void;
54
79
  declare function Pipeline(...decos: ((...args: any) => void)[]): (...args: any) => void;
80
+ declare function isAsyncFunc(fn: Function): boolean;
55
81
 
56
- declare function isPhecda(module: any): boolean;
82
+ declare function isPhecda(module: any): module is Construct;
57
83
  declare const SHARE_KEY: unique symbol;
58
84
  declare function init(proto: Phecda): void;
59
85
  declare function setVar(proto: Phecda, key: PropertyKey): void;
@@ -70,16 +96,13 @@ declare function getHandler(instance: Phecda, key: PropertyKey): any[];
70
96
  declare function setState(proto: Phecda, key: PropertyKey, state: Record<string, any>): void;
71
97
  declare function getOwnState(instance: Phecda, key: PropertyKey): Object;
72
98
  declare function getState(instance: Phecda, key: PropertyKey): any;
73
- declare function register(instance: Phecda): void;
74
- declare function registerAsync(instance: Phecda): Promise<void>;
99
+ declare function invokeHandler(event: string, instance: Phecda): Promise<any[]>;
75
100
 
101
+ declare const DataMap: InjectData;
102
+ declare function Provide<K extends keyof InjectData>(key: K, value: InjectData[K]): void;
103
+ declare function Inject<K extends keyof InjectData>(key: K): InjectData[K];
76
104
  declare const activeInstance: Record<string, any>;
77
105
  declare function injectProperty(key: string, value: any): Record<string, any>;
78
106
  declare function getProperty(key: string): any;
79
- declare function Watcher(eventName: keyof Events, options?: {
80
- once: boolean;
81
- }): (proto: any, key: string) => void;
82
- declare function Effect(eventName: string, options?: any): (proto: any, key: string) => void;
83
- declare function Storage(storeKey?: string): (proto: any, key?: PropertyKey) => void;
84
107
 
85
- export { Assign, Bind, ClassValue, Clear, DataMap, Effect, Empty, Err, Events, Expose, Global, Handler, Ignore, Init, Inject, InjectData, NameSpace, Nested, Phecda, Pipeline, Provide, SHARE_KEY, Storage, Tag, To, Watcher, activeInstance, addDecoToClass, classToValue, getBind, getExposeKey, getHandler, getModuleState, getOwnExposeKey, getOwnHandler, getOwnIgnoreKey, getOwnModuleState, getOwnState, getProperty, getState, getSymbol, getTag, init, injectProperty, isPhecda, plainToClass, regisHandler, register, registerAsync, setExposeKey, setIgnoreKey, setState, setVar, snapShot, transformClass };
108
+ export { AbConstruct, Assign, Bind, ClassValue, Clear, Construct, DataMap, Effect, Empty, Err, Events, Expose, Global, Handler, Ignore, Init, Inject, InjectData, Isolate, NameSpace, Phecda, Pipeline, Provide, SHARE_KEY, Storage, StorageParam, Tag, To, Unique, Unmount, Watcher, WatcherParam, activeInstance, addDecoToClass, classToPlain, getBind, getExposeKey, getHandler, getModuleState, getOwnExposeKey, getOwnHandler, getOwnIgnoreKey, getOwnModuleState, getOwnState, getProperty, getState, getTag, init, injectProperty, invokeHandler, isAsyncFunc, isPhecda, plainToClass, regisHandler, setExposeKey, setIgnoreKey, setState, setVar, snapShot, transformClass };
package/dist/index.js CHANGED
@@ -33,17 +33,19 @@ __export(src_exports, {
33
33
  Ignore: () => Ignore,
34
34
  Init: () => Init,
35
35
  Inject: () => Inject,
36
- Nested: () => Nested,
36
+ Isolate: () => Isolate,
37
37
  Pipeline: () => Pipeline,
38
38
  Provide: () => Provide,
39
39
  SHARE_KEY: () => SHARE_KEY,
40
40
  Storage: () => Storage,
41
41
  Tag: () => Tag,
42
42
  To: () => To,
43
+ Unique: () => Unique,
44
+ Unmount: () => Unmount,
43
45
  Watcher: () => Watcher,
44
46
  activeInstance: () => activeInstance,
45
47
  addDecoToClass: () => addDecoToClass,
46
- classToValue: () => classToValue,
48
+ classToPlain: () => classToPlain,
47
49
  getBind: () => getBind,
48
50
  getExposeKey: () => getExposeKey,
49
51
  getHandler: () => getHandler,
@@ -55,15 +57,14 @@ __export(src_exports, {
55
57
  getOwnState: () => getOwnState,
56
58
  getProperty: () => getProperty,
57
59
  getState: () => getState,
58
- getSymbol: () => getSymbol,
59
60
  getTag: () => getTag,
60
61
  init: () => init,
61
62
  injectProperty: () => injectProperty,
63
+ invokeHandler: () => invokeHandler,
64
+ isAsyncFunc: () => isAsyncFunc,
62
65
  isPhecda: () => isPhecda,
63
66
  plainToClass: () => plainToClass,
64
67
  regisHandler: () => regisHandler,
65
- register: () => register,
66
- registerAsync: () => registerAsync,
67
68
  setExposeKey: () => setExposeKey,
68
69
  setIgnoreKey: () => setIgnoreKey,
69
70
  setState: () => setState,
@@ -213,35 +214,72 @@ function getState(instance, key) {
213
214
  return ret;
214
215
  }
215
216
  __name(getState, "getState");
216
- function register(instance) {
217
+ function invokeHandler(event, instance) {
217
218
  const stateVars = getExposeKey(instance);
218
- for (const item of stateVars) {
219
- const handlers = getHandler(instance, item);
220
- for (const hanlder of handlers)
221
- hanlder.init?.(instance);
222
- }
219
+ const initHandlers = stateVars.map((item) => {
220
+ return getHandler(instance, item).filter((h) => !!h[event]).map((h) => h[event](instance));
221
+ }).flat();
222
+ return Promise.all(initHandlers);
223
223
  }
224
- __name(register, "register");
225
- async function registerAsync(instance) {
226
- const stateVars = getExposeKey(instance);
227
- for (const item of stateVars) {
228
- const handlers = getHandler(instance, item);
229
- for (const hanlder of handlers)
230
- await hanlder.init?.(instance);
231
- }
224
+ __name(invokeHandler, "invokeHandler");
225
+
226
+ // src/decorators/core.ts
227
+ function Init(proto, key) {
228
+ setVar(proto, key);
229
+ regisHandler(proto, key, {
230
+ async init(instance) {
231
+ return instance[key]();
232
+ }
233
+ });
232
234
  }
233
- __name(registerAsync, "registerAsync");
235
+ __name(Init, "Init");
236
+ function Unmount(proto, key) {
237
+ setVar(proto, key);
238
+ regisHandler(proto, key, {
239
+ async unmount(instance) {
240
+ return instance[key]();
241
+ }
242
+ });
243
+ }
244
+ __name(Unmount, "Unmount");
245
+ function Bind(value) {
246
+ return (proto, k) => {
247
+ setVar(proto, k);
248
+ setState(proto, k, {
249
+ value
250
+ });
251
+ };
252
+ }
253
+ __name(Bind, "Bind");
254
+ function Ignore(proto, key) {
255
+ setIgnoreKey(proto, key);
256
+ }
257
+ __name(Ignore, "Ignore");
258
+ function Clear(proto, key) {
259
+ init(proto);
260
+ proto._namespace.__EXPOSE_VAR__.delete(key);
261
+ proto._namespace.__IGNORE_VAR__.delete(key);
262
+ proto._namespace.__STATE_VAR__.delete(key);
263
+ proto._namespace.__STATE_HANDLER__.delete(key);
264
+ proto._namespace.__STATE_NAMESPACE__.delete(key);
265
+ }
266
+ __name(Clear, "Clear");
267
+ function Expose(proto, key) {
268
+ setExposeKey(proto, key);
269
+ }
270
+ __name(Expose, "Expose");
271
+ function Empty(module2) {
272
+ init(module2.prototype);
273
+ }
274
+ __name(Empty, "Empty");
234
275
 
235
276
  // src/helper.ts
236
- function getTag(module2) {
237
- return module2.prototype?.__TAG__;
277
+ function getTag(moduleOrInstance) {
278
+ if (typeof moduleOrInstance === "object")
279
+ moduleOrInstance = moduleOrInstance.constructor;
280
+ return moduleOrInstance.prototype?.__TAG__ || moduleOrInstance.name;
238
281
  }
239
282
  __name(getTag, "getTag");
240
- function getSymbol(instance) {
241
- const module2 = instance.constructor;
242
- return getTag(module2) || module2.name;
243
- }
244
- __name(getSymbol, "getSymbol");
245
283
  function getBind(module2) {
246
284
  const instance = new module2();
247
285
  const keys = getModuleState(instance);
@@ -283,14 +321,14 @@ async function transformClass(instance, force = false) {
283
321
  return err;
284
322
  }
285
323
  __name(transformClass, "transformClass");
286
- function classToValue(instance) {
324
+ function classToPlain(instance) {
287
325
  const data = {};
288
326
  const exposeVars = getExposeKey(instance);
289
327
  for (const item of exposeVars)
290
328
  data[item] = instance[item];
291
- return data;
329
+ return JSON.parse(JSON.stringify(data));
292
330
  }
293
- __name(classToValue, "classToValue");
331
+ __name(classToPlain, "classToPlain");
294
332
  function snapShot(data) {
295
333
  const snap = {};
296
334
  for (const i in data)
@@ -308,8 +346,8 @@ function snapShot(data) {
308
346
  };
309
347
  }
310
348
  __name(snapShot, "snapShot");
311
- function addDecoToClass(c, key, handler, type = "class") {
312
- handler(type === "class" ? c.prototype : c, key);
349
+ function addDecoToClass(c, key, handler) {
350
+ handler(key === SHARE_KEY ? c : c.prototype, key);
313
351
  }
314
352
  __name(addDecoToClass, "addDecoToClass");
315
353
  function Pipeline(...decos) {
@@ -319,63 +357,43 @@ function Pipeline(...decos) {
319
357
  };
320
358
  }
321
359
  __name(Pipeline, "Pipeline");
322
-
323
- // src/decorators.ts
324
- function Init(proto, key) {
325
- setVar(proto, key);
326
- regisHandler(proto, key, {
327
- async init(instance) {
328
- instance[key]();
329
- }
330
- });
331
- }
332
- __name(Init, "Init");
333
- function Bind(value) {
334
- return (proto, k) => {
335
- setVar(proto, k);
336
- setState(proto, k, {
337
- value
338
- });
339
- };
360
+ function isAsyncFunc(fn) {
361
+ return fn[Symbol.toStringTag] === "AsyncFunction";
340
362
  }
341
- __name(Bind, "Bind");
342
- function Ignore(proto, key) {
343
- setIgnoreKey(proto, key);
363
+ __name(isAsyncFunc, "isAsyncFunc");
364
+
365
+ // src/di.ts
366
+ var DataMap = {};
367
+ function Provide(key, value) {
368
+ DataMap[key] = value;
344
369
  }
345
- __name(Ignore, "Ignore");
346
- function Clear(proto, key) {
347
- init(proto);
348
- proto._namespace.__EXPOSE_VAR__.delete(key);
349
- proto._namespace.__IGNORE_VAR__.delete(key);
350
- proto._namespace.__STATE_VAR__.delete(key);
351
- proto._namespace.__STATE_HANDLER__.delete(key);
352
- proto._namespace.__STATE_NAMESPACE__.delete(key);
370
+ __name(Provide, "Provide");
371
+ var EmptyProxy = new Proxy(Empty, {
372
+ apply() {
373
+ return EmptyProxy;
374
+ }
375
+ });
376
+ function Inject(key) {
377
+ return DataMap[key] || EmptyProxy;
353
378
  }
354
- __name(Clear, "Clear");
355
- function Err(cb) {
356
- return (proto, key) => {
357
- setVar(proto, key);
358
- regisHandler(proto, key, {
359
- error: cb
360
- });
361
- };
379
+ __name(Inject, "Inject");
380
+ var activeInstance = {};
381
+ function injectProperty(key, value) {
382
+ activeInstance[key] = value;
383
+ return activeInstance;
362
384
  }
363
- __name(Err, "Err");
364
- function Expose(proto, key) {
365
- setExposeKey(proto, key);
385
+ __name(injectProperty, "injectProperty");
386
+ function getProperty(key) {
387
+ return activeInstance[key];
366
388
  }
367
- __name(Expose, "Expose");
368
- function To(cb) {
369
- return (proto, key) => {
370
- setVar(proto, key);
371
- regisHandler(proto, key, {
372
- async pipe(instance) {
373
- instance[key] = await cb(instance[key], instance, key);
374
- }
375
- });
376
- };
389
+ __name(getProperty, "getProperty");
390
+
391
+ // src/decorators/function.ts
392
+ function Isolate(target) {
393
+ init(target.prototype);
394
+ target.prototype.__ISOLATE__ = true;
377
395
  }
378
- __name(To, "To");
396
+ __name(Isolate, "Isolate");
379
397
  function Tag(tag) {
380
398
  return (module2) => {
381
399
  init(module2.prototype);
@@ -383,6 +401,13 @@ function Tag(tag) {
383
401
  };
384
402
  }
385
403
  __name(Tag, "Tag");
404
+ function Unique(desc) {
405
+ return (module2) => {
406
+ init(module2.prototype);
407
+ module2.prototype.__TAG__ = Symbol(desc || module2.name);
408
+ };
409
+ }
410
+ __name(Unique, "Unique");
386
411
  function Assign(cb) {
387
412
  return (module2) => {
388
413
  init(module2.prototype);
@@ -414,57 +439,67 @@ function Global(module2) {
414
439
  });
415
440
  }
416
441
  __name(Global, "Global");
417
- function Empty(module2) {
418
- init(module2.prototype);
419
- }
420
- __name(Empty, "Empty");
421
- var DataMap = {};
422
- function Provide(key, value) {
423
- DataMap[key] = value;
424
- }
425
- __name(Provide, "Provide");
426
- var EmptyProxy = new Proxy(Empty, {
427
- apply() {
428
- return EmptyProxy;
429
- }
430
- });
431
- function Inject(key) {
432
- return DataMap[key] || EmptyProxy;
433
- }
434
- __name(Inject, "Inject");
435
- function Nested(module2) {
436
- return To(async (property) => {
437
- const instance = plainToClass(module2, property);
438
- const err = await transformClass(instance);
439
- if (err.length > 0)
440
- throw new Error(err[0]);
441
- return instance;
442
- });
443
- }
444
- __name(Nested, "Nested");
445
-
446
- // src/custom/decorator.ts
447
- var activeInstance = {};
448
- function injectProperty(key, value) {
449
- activeInstance[key] = value;
450
- return activeInstance;
442
+ function To(...callbacks) {
443
+ return (proto, key) => {
444
+ setVar(proto, key);
445
+ regisHandler(proto, key, {
446
+ async pipe(instance) {
447
+ for (const cb of callbacks)
448
+ instance[key] = await cb(instance[key], instance, key);
449
+ }
450
+ });
451
+ };
451
452
  }
452
- __name(injectProperty, "injectProperty");
453
- function getProperty(key) {
454
- return activeInstance[key];
453
+ __name(To, "To");
454
+ function Err(cb, isCatch = false) {
455
+ return (proto, key) => {
456
+ setVar(proto, key);
457
+ regisHandler(proto, key, {
458
+ init: (instance) => {
459
+ if (typeof instance[key] === "function") {
460
+ const oldFn = instance[key].bind(instance);
461
+ if (isAsyncFunc(oldFn)) {
462
+ instance[key] = async (...args) => {
463
+ try {
464
+ await oldFn(...args);
465
+ } catch (e) {
466
+ cb(e);
467
+ if (!isCatch)
468
+ throw e;
469
+ }
470
+ };
471
+ } else {
472
+ instance[key] = (...args) => {
473
+ try {
474
+ oldFn(...args);
475
+ } catch (e) {
476
+ cb(e);
477
+ if (!isCatch)
478
+ throw e;
479
+ }
480
+ };
481
+ }
482
+ }
483
+ }
484
+ });
485
+ };
455
486
  }
456
- __name(getProperty, "getProperty");
487
+ __name(Err, "Err");
457
488
  function Watcher(eventName, options) {
489
+ let cb;
458
490
  return (proto, key) => {
459
491
  setVar(proto, key);
460
492
  regisHandler(proto, key, {
461
493
  init(instance) {
462
- getProperty("watcher")?.({
494
+ return cb = getProperty("watcher")?.({
463
495
  eventName,
464
496
  instance,
465
497
  key,
466
498
  options
467
499
  });
500
+ },
501
+ unmount() {
502
+ return cb?.();
468
503
  }
469
504
  });
470
505
  };
@@ -496,34 +531,40 @@ function Effect(eventName, options) {
496
531
  };
497
532
  }
498
533
  __name(Effect, "Effect");
499
- function Storage(storeKey) {
534
+ function Storage({ key: storeKey, toJSON, toString } = {}) {
535
+ if (!toJSON)
536
+ toJSON = /* @__PURE__ */ __name((v) => JSON.parse(v), "toJSON");
537
+ if (!toString)
538
+ toString = /* @__PURE__ */ __name((v) => JSON.stringify(v), "toString");
500
539
  return (proto, key) => {
501
540
  let tag;
502
541
  if (key) {
503
542
  init(proto);
504
- tag = storeKey || proto.__TAG__;
505
- const uniTag = Symbol(tag);
506
- setVar(proto, uniTag);
507
- regisHandler(proto, uniTag, {
543
+ tag = storeKey || `${getTag(proto)}_${key}`;
544
+ setVar(proto, key);
545
+ regisHandler(proto, key, {
508
546
  init: (instance) => {
509
- getProperty("storage")?.({
547
+ return getProperty("storage")?.({
510
548
  instance,
511
549
  key,
512
- tag
550
+ tag,
551
+ toJSON,
552
+ toString
513
553
  });
514
554
  }
515
555
  });
516
556
  } else {
517
557
  init(proto.prototype);
518
- tag = storeKey || `${proto.prototype.__TAG__}_${key}`;
519
- const uniTag = Symbol(tag);
520
- setVar(proto.prototype, uniTag);
521
- regisHandler(proto.prototype, uniTag, {
558
+ tag = storeKey || getTag(proto);
559
+ setVar(proto.prototype, SHARE_KEY);
560
+ regisHandler(proto.prototype, SHARE_KEY, {
522
561
  init: (instance) => {
523
- getProperty("storage")?.({
562
+ return getProperty("storage")?.({
524
563
  instance,
525
- key: "",
526
- tag
564
+ key,
565
+ tag,
566
+ toJSON,
567
+ toString
527
568
  });
528
569
  }
529
570
  });
@@ -545,17 +586,19 @@ __name(Storage, "Storage");
545
586
  Ignore,
546
587
  Init,
547
588
  Inject,
548
- Nested,
589
+ Isolate,
549
590
  Pipeline,
550
591
  Provide,
551
592
  SHARE_KEY,
552
593
  Storage,
553
594
  Tag,
554
595
  To,
596
+ Unique,
597
+ Unmount,
555
598
  Watcher,
556
599
  activeInstance,
557
600
  addDecoToClass,
558
- classToValue,
601
+ classToPlain,
559
602
  getBind,
560
603
  getExposeKey,
561
604
  getHandler,
@@ -567,15 +610,14 @@ __name(Storage, "Storage");
567
610
  getOwnState,
568
611
  getProperty,
569
612
  getState,
570
- getSymbol,
571
613
  getTag,
572
614
  init,
573
615
  injectProperty,
616
+ invokeHandler,
617
+ isAsyncFunc,
574
618
  isPhecda,
575
619
  plainToClass,
576
620
  regisHandler,
577
- register,
578
- registerAsync,
579
621
  setExposeKey,
580
622
  setIgnoreKey,
581
623
  setState,
package/dist/index.mjs CHANGED
@@ -141,35 +141,72 @@ function getState(instance, key) {
141
141
  return ret;
142
142
  }
143
143
  __name(getState, "getState");
144
- function register(instance) {
144
+ function invokeHandler(event, instance) {
145
145
  const stateVars = getExposeKey(instance);
146
- for (const item of stateVars) {
147
- const handlers = getHandler(instance, item);
148
- for (const hanlder of handlers)
149
- hanlder.init?.(instance);
150
- }
146
+ const initHandlers = stateVars.map((item) => {
147
+ return getHandler(instance, item).filter((h) => !!h[event]).map((h) => h[event](instance));
148
+ }).flat();
149
+ return Promise.all(initHandlers);
151
150
  }
152
- __name(register, "register");
153
- async function registerAsync(instance) {
154
- const stateVars = getExposeKey(instance);
155
- for (const item of stateVars) {
156
- const handlers = getHandler(instance, item);
157
- for (const hanlder of handlers)
158
- await hanlder.init?.(instance);
159
- }
151
+ __name(invokeHandler, "invokeHandler");
152
+
153
+ // src/decorators/core.ts
154
+ function Init(proto, key) {
155
+ setVar(proto, key);
156
+ regisHandler(proto, key, {
157
+ async init(instance) {
158
+ return instance[key]();
159
+ }
160
+ });
160
161
  }
161
- __name(registerAsync, "registerAsync");
162
+ __name(Init, "Init");
163
+ function Unmount(proto, key) {
164
+ setVar(proto, key);
165
+ regisHandler(proto, key, {
166
+ async unmount(instance) {
167
+ return instance[key]();
168
+ }
169
+ });
170
+ }
171
+ __name(Unmount, "Unmount");
172
+ function Bind(value) {
173
+ return (proto, k) => {
174
+ setVar(proto, k);
175
+ setState(proto, k, {
176
+ value
177
+ });
178
+ };
179
+ }
180
+ __name(Bind, "Bind");
181
+ function Ignore(proto, key) {
182
+ setIgnoreKey(proto, key);
183
+ }
184
+ __name(Ignore, "Ignore");
185
+ function Clear(proto, key) {
186
+ init(proto);
187
+ proto._namespace.__EXPOSE_VAR__.delete(key);
188
+ proto._namespace.__IGNORE_VAR__.delete(key);
189
+ proto._namespace.__STATE_VAR__.delete(key);
190
+ proto._namespace.__STATE_HANDLER__.delete(key);
191
+ proto._namespace.__STATE_NAMESPACE__.delete(key);
192
+ }
193
+ __name(Clear, "Clear");
194
+ function Expose(proto, key) {
195
+ setExposeKey(proto, key);
196
+ }
197
+ __name(Expose, "Expose");
198
+ function Empty(module) {
199
+ init(module.prototype);
200
+ }
201
+ __name(Empty, "Empty");
162
202
 
163
203
  // src/helper.ts
164
- function getTag(module) {
165
- return module.prototype?.__TAG__;
204
+ function getTag(moduleOrInstance) {
205
+ if (typeof moduleOrInstance === "object")
206
+ moduleOrInstance = moduleOrInstance.constructor;
207
+ return moduleOrInstance.prototype?.__TAG__ || moduleOrInstance.name;
166
208
  }
167
209
  __name(getTag, "getTag");
168
- function getSymbol(instance) {
169
- const module = instance.constructor;
170
- return getTag(module) || module.name;
171
- }
172
- __name(getSymbol, "getSymbol");
173
210
  function getBind(module) {
174
211
  const instance = new module();
175
212
  const keys = getModuleState(instance);
@@ -211,14 +248,14 @@ async function transformClass(instance, force = false) {
211
248
  return err;
212
249
  }
213
250
  __name(transformClass, "transformClass");
214
- function classToValue(instance) {
251
+ function classToPlain(instance) {
215
252
  const data = {};
216
253
  const exposeVars = getExposeKey(instance);
217
254
  for (const item of exposeVars)
218
255
  data[item] = instance[item];
219
- return data;
256
+ return JSON.parse(JSON.stringify(data));
220
257
  }
221
- __name(classToValue, "classToValue");
258
+ __name(classToPlain, "classToPlain");
222
259
  function snapShot(data) {
223
260
  const snap = {};
224
261
  for (const i in data)
@@ -236,8 +273,8 @@ function snapShot(data) {
236
273
  };
237
274
  }
238
275
  __name(snapShot, "snapShot");
239
- function addDecoToClass(c, key, handler, type = "class") {
240
- handler(type === "class" ? c.prototype : c, key);
276
+ function addDecoToClass(c, key, handler) {
277
+ handler(key === SHARE_KEY ? c : c.prototype, key);
241
278
  }
242
279
  __name(addDecoToClass, "addDecoToClass");
243
280
  function Pipeline(...decos) {
@@ -247,63 +284,43 @@ function Pipeline(...decos) {
247
284
  };
248
285
  }
249
286
  __name(Pipeline, "Pipeline");
250
-
251
- // src/decorators.ts
252
- function Init(proto, key) {
253
- setVar(proto, key);
254
- regisHandler(proto, key, {
255
- async init(instance) {
256
- instance[key]();
257
- }
258
- });
259
- }
260
- __name(Init, "Init");
261
- function Bind(value) {
262
- return (proto, k) => {
263
- setVar(proto, k);
264
- setState(proto, k, {
265
- value
266
- });
267
- };
287
+ function isAsyncFunc(fn) {
288
+ return fn[Symbol.toStringTag] === "AsyncFunction";
268
289
  }
269
- __name(Bind, "Bind");
270
- function Ignore(proto, key) {
271
- setIgnoreKey(proto, key);
290
+ __name(isAsyncFunc, "isAsyncFunc");
291
+
292
+ // src/di.ts
293
+ var DataMap = {};
294
+ function Provide(key, value) {
295
+ DataMap[key] = value;
272
296
  }
273
- __name(Ignore, "Ignore");
274
- function Clear(proto, key) {
275
- init(proto);
276
- proto._namespace.__EXPOSE_VAR__.delete(key);
277
- proto._namespace.__IGNORE_VAR__.delete(key);
278
- proto._namespace.__STATE_VAR__.delete(key);
279
- proto._namespace.__STATE_HANDLER__.delete(key);
280
- proto._namespace.__STATE_NAMESPACE__.delete(key);
297
+ __name(Provide, "Provide");
298
+ var EmptyProxy = new Proxy(Empty, {
299
+ apply() {
300
+ return EmptyProxy;
301
+ }
302
+ });
303
+ function Inject(key) {
304
+ return DataMap[key] || EmptyProxy;
281
305
  }
282
- __name(Clear, "Clear");
283
- function Err(cb) {
284
- return (proto, key) => {
285
- setVar(proto, key);
286
- regisHandler(proto, key, {
287
- error: cb
288
- });
289
- };
306
+ __name(Inject, "Inject");
307
+ var activeInstance = {};
308
+ function injectProperty(key, value) {
309
+ activeInstance[key] = value;
310
+ return activeInstance;
290
311
  }
291
- __name(Err, "Err");
292
- function Expose(proto, key) {
293
- setExposeKey(proto, key);
312
+ __name(injectProperty, "injectProperty");
313
+ function getProperty(key) {
314
+ return activeInstance[key];
294
315
  }
295
- __name(Expose, "Expose");
296
- function To(cb) {
297
- return (proto, key) => {
298
- setVar(proto, key);
299
- regisHandler(proto, key, {
300
- async pipe(instance) {
301
- instance[key] = await cb(instance[key], instance, key);
302
- }
303
- });
304
- };
316
+ __name(getProperty, "getProperty");
317
+
318
+ // src/decorators/function.ts
319
+ function Isolate(target) {
320
+ init(target.prototype);
321
+ target.prototype.__ISOLATE__ = true;
305
322
  }
306
- __name(To, "To");
323
+ __name(Isolate, "Isolate");
307
324
  function Tag(tag) {
308
325
  return (module) => {
309
326
  init(module.prototype);
@@ -311,6 +328,13 @@ function Tag(tag) {
311
328
  };
312
329
  }
313
330
  __name(Tag, "Tag");
331
+ function Unique(desc) {
332
+ return (module) => {
333
+ init(module.prototype);
334
+ module.prototype.__TAG__ = Symbol(desc || module.name);
335
+ };
336
+ }
337
+ __name(Unique, "Unique");
314
338
  function Assign(cb) {
315
339
  return (module) => {
316
340
  init(module.prototype);
@@ -342,57 +366,67 @@ function Global(module) {
342
366
  });
343
367
  }
344
368
  __name(Global, "Global");
345
- function Empty(module) {
346
- init(module.prototype);
347
- }
348
- __name(Empty, "Empty");
349
- var DataMap = {};
350
- function Provide(key, value) {
351
- DataMap[key] = value;
352
- }
353
- __name(Provide, "Provide");
354
- var EmptyProxy = new Proxy(Empty, {
355
- apply() {
356
- return EmptyProxy;
357
- }
358
- });
359
- function Inject(key) {
360
- return DataMap[key] || EmptyProxy;
361
- }
362
- __name(Inject, "Inject");
363
- function Nested(module) {
364
- return To(async (property) => {
365
- const instance = plainToClass(module, property);
366
- const err = await transformClass(instance);
367
- if (err.length > 0)
368
- throw new Error(err[0]);
369
- return instance;
370
- });
371
- }
372
- __name(Nested, "Nested");
373
-
374
- // src/custom/decorator.ts
375
- var activeInstance = {};
376
- function injectProperty(key, value) {
377
- activeInstance[key] = value;
378
- return activeInstance;
369
+ function To(...callbacks) {
370
+ return (proto, key) => {
371
+ setVar(proto, key);
372
+ regisHandler(proto, key, {
373
+ async pipe(instance) {
374
+ for (const cb of callbacks)
375
+ instance[key] = await cb(instance[key], instance, key);
376
+ }
377
+ });
378
+ };
379
379
  }
380
- __name(injectProperty, "injectProperty");
381
- function getProperty(key) {
382
- return activeInstance[key];
380
+ __name(To, "To");
381
+ function Err(cb, isCatch = false) {
382
+ return (proto, key) => {
383
+ setVar(proto, key);
384
+ regisHandler(proto, key, {
385
+ init: (instance) => {
386
+ if (typeof instance[key] === "function") {
387
+ const oldFn = instance[key].bind(instance);
388
+ if (isAsyncFunc(oldFn)) {
389
+ instance[key] = async (...args) => {
390
+ try {
391
+ await oldFn(...args);
392
+ } catch (e) {
393
+ cb(e);
394
+ if (!isCatch)
395
+ throw e;
396
+ }
397
+ };
398
+ } else {
399
+ instance[key] = (...args) => {
400
+ try {
401
+ oldFn(...args);
402
+ } catch (e) {
403
+ cb(e);
404
+ if (!isCatch)
405
+ throw e;
406
+ }
407
+ };
408
+ }
409
+ }
410
+ }
411
+ });
412
+ };
383
413
  }
384
- __name(getProperty, "getProperty");
414
+ __name(Err, "Err");
385
415
  function Watcher(eventName, options) {
416
+ let cb;
386
417
  return (proto, key) => {
387
418
  setVar(proto, key);
388
419
  regisHandler(proto, key, {
389
420
  init(instance) {
390
- getProperty("watcher")?.({
421
+ return cb = getProperty("watcher")?.({
391
422
  eventName,
392
423
  instance,
393
424
  key,
394
425
  options
395
426
  });
427
+ },
428
+ unmount() {
429
+ return cb?.();
396
430
  }
397
431
  });
398
432
  };
@@ -424,34 +458,40 @@ function Effect(eventName, options) {
424
458
  };
425
459
  }
426
460
  __name(Effect, "Effect");
427
- function Storage(storeKey) {
461
+ function Storage({ key: storeKey, toJSON, toString } = {}) {
462
+ if (!toJSON)
463
+ toJSON = /* @__PURE__ */ __name((v) => JSON.parse(v), "toJSON");
464
+ if (!toString)
465
+ toString = /* @__PURE__ */ __name((v) => JSON.stringify(v), "toString");
428
466
  return (proto, key) => {
429
467
  let tag;
430
468
  if (key) {
431
469
  init(proto);
432
- tag = storeKey || proto.__TAG__;
433
- const uniTag = Symbol(tag);
434
- setVar(proto, uniTag);
435
- regisHandler(proto, uniTag, {
470
+ tag = storeKey || `${getTag(proto)}_${key}`;
471
+ setVar(proto, key);
472
+ regisHandler(proto, key, {
436
473
  init: (instance) => {
437
- getProperty("storage")?.({
474
+ return getProperty("storage")?.({
438
475
  instance,
439
476
  key,
440
- tag
477
+ tag,
478
+ toJSON,
479
+ toString
441
480
  });
442
481
  }
443
482
  });
444
483
  } else {
445
484
  init(proto.prototype);
446
- tag = storeKey || `${proto.prototype.__TAG__}_${key}`;
447
- const uniTag = Symbol(tag);
448
- setVar(proto.prototype, uniTag);
449
- regisHandler(proto.prototype, uniTag, {
485
+ tag = storeKey || getTag(proto);
486
+ setVar(proto.prototype, SHARE_KEY);
487
+ regisHandler(proto.prototype, SHARE_KEY, {
450
488
  init: (instance) => {
451
- getProperty("storage")?.({
489
+ return getProperty("storage")?.({
452
490
  instance,
453
- key: "",
454
- tag
491
+ key,
492
+ tag,
493
+ toJSON,
494
+ toString
455
495
  });
456
496
  }
457
497
  });
@@ -472,17 +512,19 @@ export {
472
512
  Ignore,
473
513
  Init,
474
514
  Inject,
475
- Nested,
515
+ Isolate,
476
516
  Pipeline,
477
517
  Provide,
478
518
  SHARE_KEY,
479
519
  Storage,
480
520
  Tag,
481
521
  To,
522
+ Unique,
523
+ Unmount,
482
524
  Watcher,
483
525
  activeInstance,
484
526
  addDecoToClass,
485
- classToValue,
527
+ classToPlain,
486
528
  getBind,
487
529
  getExposeKey,
488
530
  getHandler,
@@ -494,15 +536,14 @@ export {
494
536
  getOwnState,
495
537
  getProperty,
496
538
  getState,
497
- getSymbol,
498
539
  getTag,
499
540
  init,
500
541
  injectProperty,
542
+ invokeHandler,
543
+ isAsyncFunc,
501
544
  isPhecda,
502
545
  plainToClass,
503
546
  regisHandler,
504
- register,
505
- registerAsync,
506
547
  setExposeKey,
507
548
  setIgnoreKey,
508
549
  setState,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-alpha.11",
4
4
  "description": "provide base function and abstract limit to other phecda module ",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",