phecda-core 3.0.0-beta.17 → 3.0.1

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.
@@ -0,0 +1,123 @@
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(model: any): void;
8
+
9
+ declare const SHARE_KEY: unique symbol;
10
+ declare const PHECDA_KEY: unique symbol;
11
+ declare function isPhecda(model: any): model is Construct;
12
+ declare function init(proto: Phecda): void;
13
+ declare function getPhecdaFromTarget(target: any): any;
14
+ declare function setStateKey(proto: Phecda, key?: PropertyKey): void;
15
+ declare function setExposeKey(proto: Phecda, key?: PropertyKey): void;
16
+ declare function setIgnoreKey(proto: Phecda, key?: PropertyKey): void;
17
+ declare function setHandler(proto: Phecda, key: PropertyKey | undefined, handler: Handler): void;
18
+ declare function setState(proto: Phecda, key: PropertyKey | undefined, state: Record<string, any>): void;
19
+ declare function getOwnStateKey(target: any): string[];
20
+ declare function getStateKey(target: any): PropertyKey[];
21
+ declare function getOwnExposeKey(target: any): string[];
22
+ declare function getExposeKey(target: any): PropertyKey[];
23
+ declare function getOwnIgnoreKey(target: any): string[];
24
+ declare function getOwnHandler(target: any, key: PropertyKey): Handler[];
25
+ declare function getHandler(target: any, key: PropertyKey): any[];
26
+ declare function getState(target: any, key?: PropertyKey): any;
27
+ declare function getOwnState(target: any, key?: PropertyKey): Record<string, any>;
28
+ declare function invokeHandler(event: string, module: Phecda): Promise<any[]>;
29
+ declare function set(proto: any, key: string, value: any): void;
30
+ declare function get(proto: any, key: string): any;
31
+
32
+ interface NameSpace {
33
+ [name: string]: Phecda;
34
+ }
35
+ interface InjectData {
36
+ [key: string]: any;
37
+ }
38
+ type Construct<T = any> = new (...args: any[]) => T;
39
+ type AbConstruct<T = any> = abstract new (...args: any[]) => T;
40
+ interface Handler {
41
+ [key: string]: any;
42
+ }
43
+ interface Phecda {
44
+ prototype: any;
45
+ [PHECDA_KEY]: {
46
+ __EXPOSE_KEY: Set<PropertyKey>;
47
+ __IGNORE_KEY: Set<PropertyKey>;
48
+ __CLEAR_KEY: Set<PropertyKey>;
49
+ __STATE_KEY: Set<PropertyKey>;
50
+ __STATE_HANDLER__: Map<PropertyKey, Handler[]>;
51
+ __STATE_NAMESPACE__: Map<PropertyKey, Object>;
52
+ };
53
+ }
54
+ type ClassValue<I> = {
55
+ [P in keyof I]: I[P] extends Function ? undefined : I[P];
56
+ };
57
+ interface Events {
58
+ }
59
+
60
+ declare function Isolate(model: any): void;
61
+ declare function Tag(tag: PropertyKey): (model: any) => void;
62
+ declare function Unique(desc?: string): (model: any) => void;
63
+ declare function Assign(cb: (instance?: any) => any): (model: any) => void;
64
+ declare function Global(model: any): void;
65
+ declare function To(...callbacks: ((arg: any, instance: any, key: string) => any)[]): (proto: any, key: PropertyKey) => void;
66
+ declare function Rule(cb: ((arg: any) => boolean | Promise<boolean>), info: string | (() => string)): (proto: any, key: PropertyKey) => void;
67
+ declare function Err(cb: (e: Error | any, instance: any, key: string) => void, isCatch?: boolean): (proto: any, key: PropertyKey) => void;
68
+ interface StorageParam {
69
+ key: PropertyKey;
70
+ instance: any;
71
+ tag: string;
72
+ toJSON: (str: string) => any;
73
+ toString: (arg: any) => string;
74
+ }
75
+ interface WatcherParam {
76
+ key: string;
77
+ instance: any;
78
+ eventName: string;
79
+ options?: {
80
+ once?: boolean;
81
+ };
82
+ }
83
+ declare function Watcher(eventName: keyof Events, options?: {
84
+ once?: boolean;
85
+ }): (proto: any, key: string) => void;
86
+ declare function Effect(cb: (value: any, instance: any, key: string) => void): (proto: any, key: string) => void;
87
+ declare function Storage({ key: storeKey, json, stringify }?: {
88
+ json?: (str: string) => any;
89
+ stringify?: (arg: any) => string;
90
+ key?: string;
91
+ }): (proto: any, key?: PropertyKey) => void;
92
+ declare function If(value: Boolean, ...decorators: (ClassDecorator[]) | (PropertyDecorator[]) | (ParameterDecorator[])): (...args: any[]) => void;
93
+
94
+ declare function getTag<M extends Construct | AbConstruct>(moduleOrInstance: M | InstanceType<M>): PropertyKey;
95
+ declare function getBind<M extends Construct | AbConstruct>(model: M): any;
96
+ declare function plainToClass<M extends Construct, Data extends Record<PropertyKey, any>>(model: M, input: Data): InstanceType<M>;
97
+ declare function transformInstance<M extends Construct>(instance: InstanceType<M>, force?: boolean): string[];
98
+ declare function transformInstanceAsync<M extends Construct>(instance: InstanceType<M>, force?: boolean): Promise<string[]>;
99
+ declare function transformProperty<M extends Construct>(instance: InstanceType<M>, property: keyof InstanceType<M>, force?: boolean): string[];
100
+ declare function transformPropertyAsync<M extends Construct>(instance: InstanceType<M>, property: keyof InstanceType<M>, force?: boolean): Promise<string[]>;
101
+ declare function classToPlain<M>(instance: M): ClassValue<M>;
102
+ declare function snapShot<T extends Construct>(data: InstanceType<T>): {
103
+ data: InstanceType<T>;
104
+ clear(): void;
105
+ apply(): void;
106
+ };
107
+ /**
108
+ * add decorator to a class by function
109
+ */
110
+ declare function addDecoToClass<M extends Construct | AbConstruct>(c: M, key: keyof InstanceType<M> | PropertyKey | undefined, handler: PropertyDecorator | ClassDecorator): void;
111
+ declare function Pipeline(...decos: ((...args: any) => void)[]): (...args: any) => void;
112
+ declare function isAsyncFunc(fn: Function): boolean;
113
+ declare function setPropertyState(target: any, k: undefined | PropertyKey, setter: (state: Record<string, any>) => void): void;
114
+ declare function getShareState<State>(target: any, getter: (state: Record<string, any>) => State): State;
115
+
116
+ declare const DataMap: InjectData;
117
+ declare function Provide<K extends keyof InjectData>(key: K, value: InjectData[K]): void;
118
+ declare function Inject<K extends keyof InjectData>(key: K): InjectData[K];
119
+ declare const activeInstance: Record<string, any>;
120
+ declare function setInject(key: string, value: any): Record<string, any>;
121
+ declare function getInject(key: string): any;
122
+
123
+ export { type AbConstruct, Assign, Bind, type ClassValue, Clear, type Construct, DataMap, Effect, Empty, Err, type Events, Expose, Global, type Handler, If, Ignore, Init, Inject, type InjectData, Isolate, type NameSpace, PHECDA_KEY, type Phecda, Pipeline, Provide, Rule, SHARE_KEY, Storage, type StorageParam, Tag, To, Unique, Unmount, Watcher, type WatcherParam, activeInstance, addDecoToClass, classToPlain, get, getBind, getExposeKey, getHandler, getInject, getOwnExposeKey, getOwnHandler, getOwnIgnoreKey, getOwnState, getOwnStateKey, getPhecdaFromTarget, getShareState, getState, getStateKey, getTag, init, invokeHandler, isAsyncFunc, isPhecda, plainToClass, set, setExposeKey, setHandler, setIgnoreKey, setInject, setPropertyState, setState, setStateKey, snapShot, transformInstance, transformInstanceAsync, transformProperty, transformPropertyAsync };
package/dist/index.d.ts CHANGED
@@ -89,6 +89,7 @@ declare function Storage({ key: storeKey, json, stringify }?: {
89
89
  stringify?: (arg: any) => string;
90
90
  key?: string;
91
91
  }): (proto: any, key?: PropertyKey) => void;
92
+ declare function If(value: Boolean, ...decorators: (ClassDecorator[]) | (PropertyDecorator[]) | (ParameterDecorator[])): (...args: any[]) => void;
92
93
 
93
94
  declare function getTag<M extends Construct | AbConstruct>(moduleOrInstance: M | InstanceType<M>): PropertyKey;
94
95
  declare function getBind<M extends Construct | AbConstruct>(model: M): any;
@@ -103,6 +104,9 @@ declare function snapShot<T extends Construct>(data: InstanceType<T>): {
103
104
  clear(): void;
104
105
  apply(): void;
105
106
  };
107
+ /**
108
+ * add decorator to a class by function
109
+ */
106
110
  declare function addDecoToClass<M extends Construct | AbConstruct>(c: M, key: keyof InstanceType<M> | PropertyKey | undefined, handler: PropertyDecorator | ClassDecorator): void;
107
111
  declare function Pipeline(...decos: ((...args: any) => void)[]): (...args: any) => void;
108
112
  declare function isAsyncFunc(fn: Function): boolean;
@@ -116,4 +120,4 @@ declare const activeInstance: Record<string, any>;
116
120
  declare function setInject(key: string, value: any): Record<string, any>;
117
121
  declare function getInject(key: string): any;
118
122
 
119
- export { AbConstruct, Assign, Bind, ClassValue, Clear, Construct, DataMap, Effect, Empty, Err, Events, Expose, Global, Handler, Ignore, Init, Inject, InjectData, Isolate, NameSpace, PHECDA_KEY, Phecda, Pipeline, Provide, Rule, SHARE_KEY, Storage, StorageParam, Tag, To, Unique, Unmount, Watcher, WatcherParam, activeInstance, addDecoToClass, classToPlain, get, getBind, getExposeKey, getHandler, getInject, getOwnExposeKey, getOwnHandler, getOwnIgnoreKey, getOwnState, getOwnStateKey, getPhecdaFromTarget, getShareState, getState, getStateKey, getTag, init, invokeHandler, isAsyncFunc, isPhecda, plainToClass, set, setExposeKey, setHandler, setIgnoreKey, setInject, setPropertyState, setState, setStateKey, snapShot, transformInstance, transformInstanceAsync, transformProperty, transformPropertyAsync };
123
+ export { type AbConstruct, Assign, Bind, type ClassValue, Clear, type Construct, DataMap, Effect, Empty, Err, type Events, Expose, Global, type Handler, If, Ignore, Init, Inject, type InjectData, Isolate, type NameSpace, PHECDA_KEY, type Phecda, Pipeline, Provide, Rule, SHARE_KEY, Storage, type StorageParam, Tag, To, Unique, Unmount, Watcher, type WatcherParam, activeInstance, addDecoToClass, classToPlain, get, getBind, getExposeKey, getHandler, getInject, getOwnExposeKey, getOwnHandler, getOwnIgnoreKey, getOwnState, getOwnStateKey, getPhecdaFromTarget, getShareState, getState, getStateKey, getTag, init, invokeHandler, isAsyncFunc, isPhecda, plainToClass, set, setExposeKey, setHandler, setIgnoreKey, setInject, setPropertyState, setState, setStateKey, snapShot, transformInstance, transformInstanceAsync, transformProperty, transformPropertyAsync };
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ __export(src_exports, {
30
30
  Err: () => Err,
31
31
  Expose: () => Expose,
32
32
  Global: () => Global,
33
+ If: () => If,
33
34
  Ignore: () => Ignore,
34
35
  Init: () => Init,
35
36
  Inject: () => Inject,
@@ -88,31 +89,49 @@ module.exports = __toCommonJS(src_exports);
88
89
  var SHARE_KEY = Symbol("phecda");
89
90
  var PHECDA_KEY = Symbol("phecda");
90
91
  function isPhecda(model) {
91
- if (typeof model === "function")
92
- return !!model.prototype[PHECDA_KEY];
92
+ if (typeof model === "function") return !!model.prototype[PHECDA_KEY];
93
93
  return false;
94
94
  }
95
95
  __name(isPhecda, "isPhecda");
96
96
  function init(proto) {
97
- if (!proto)
98
- return;
97
+ if (!proto) return;
99
98
  if (!proto.hasOwnProperty(PHECDA_KEY)) {
100
99
  proto[PHECDA_KEY] = {
100
+ /**
101
+ * 暴露的变量,
102
+ * 只要属性上存在至少一个装饰器,该属性就会被捕捉到
103
+ */
101
104
  __EXPOSE_KEY: /* @__PURE__ */ new Set(),
105
+ /**
106
+ * @Ignore 绑定的属性,
107
+ * 某属性即使被捕捉,可被强行忽略,优先级最高
108
+ */
102
109
  __IGNORE_KEY: /* @__PURE__ */ new Set(),
110
+ /**
111
+ * @Clear 绑定的属性,
112
+ * 消除父类在该key上的state/handler, 但export key 和 state
113
+ */
103
114
  __CLEAR_KEY: /* @__PURE__ */ new Set(),
115
+ /**
116
+ * 存在状态的变量
117
+ * @deprecated
118
+ */
104
119
  __STATE_KEY: /* @__PURE__ */ new Set(),
120
+ /**
121
+ * 状态变量的处理器
122
+ */
105
123
  __STATE_HANDLER__: /* @__PURE__ */ new Map(),
124
+ /**
125
+ * 状态变量的共有状态
126
+ */
106
127
  __STATE_NAMESPACE__: /* @__PURE__ */ new Map()
107
128
  };
108
129
  }
109
130
  }
110
131
  __name(init, "init");
111
132
  function getPhecdaFromTarget(target) {
112
- if (typeof target === "function")
113
- return target.prototype;
114
- if (target.hasOwnProperty(PHECDA_KEY))
115
- return target;
133
+ if (typeof target === "function") return target.prototype;
134
+ if (target.hasOwnProperty(PHECDA_KEY)) return target;
116
135
  return Object.getPrototypeOf(target);
117
136
  }
118
137
  __name(getPhecdaFromTarget, "getPhecdaFromTarget");
@@ -150,12 +169,10 @@ function setHandler(proto, key, handler) {
150
169
  proto = proto.prototype;
151
170
  }
152
171
  init(proto);
153
- if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key))
154
- proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [
155
- handler
156
- ]);
157
- else
158
- proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
172
+ if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key)) proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [
173
+ handler
174
+ ]);
175
+ else proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
159
176
  }
160
177
  __name(setHandler, "setHandler");
161
178
  function setState(proto, key, state) {
@@ -179,8 +196,7 @@ function getStateKey(target) {
179
196
  let proto = getPhecdaFromTarget(target);
180
197
  const set2 = /* @__PURE__ */ new Set();
181
198
  while (proto?.[PHECDA_KEY]) {
182
- if (proto.hasOwnProperty(PHECDA_KEY))
183
- proto[PHECDA_KEY].__STATE_KEY.forEach((item) => set2.add(item));
199
+ if (proto.hasOwnProperty(PHECDA_KEY)) proto[PHECDA_KEY].__STATE_KEY.forEach((item) => set2.add(item));
184
200
  proto = Object.getPrototypeOf(proto);
185
201
  }
186
202
  return [
@@ -200,10 +216,9 @@ function getExposeKey(target) {
200
216
  const set2 = /* @__PURE__ */ new Set();
201
217
  const origin = proto;
202
218
  while (proto?.[PHECDA_KEY]) {
203
- if (proto.hasOwnProperty(PHECDA_KEY))
204
- [
205
- ...proto[PHECDA_KEY].__EXPOSE_KEY
206
- ].forEach((item) => !origin[PHECDA_KEY].__IGNORE_KEY.has(item) && set2.add(item));
219
+ if (proto.hasOwnProperty(PHECDA_KEY)) [
220
+ ...proto[PHECDA_KEY].__EXPOSE_KEY
221
+ ].forEach((item) => !origin[PHECDA_KEY].__IGNORE_KEY.has(item) && set2.add(item));
207
222
  proto = Object.getPrototypeOf(proto);
208
223
  }
209
224
  return [
@@ -229,8 +244,7 @@ function getHandler(target, key) {
229
244
  while (proto?.[PHECDA_KEY]) {
230
245
  if (proto.hasOwnProperty(PHECDA_KEY)) {
231
246
  proto[PHECDA_KEY].__STATE_HANDLER__.get(key)?.forEach((item) => set2.add(item));
232
- if (proto[PHECDA_KEY].__CLEAR_KEY.has(key))
233
- break;
247
+ if (proto[PHECDA_KEY].__CLEAR_KEY.has(key)) break;
234
248
  }
235
249
  proto = Object.getPrototypeOf(proto);
236
250
  }
@@ -245,13 +259,11 @@ function getState(target, key = SHARE_KEY) {
245
259
  while (proto?.[PHECDA_KEY]) {
246
260
  if (proto.hasOwnProperty(PHECDA_KEY)) {
247
261
  const state = proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key);
248
- if (state)
249
- ret = {
250
- ...state,
251
- ...ret
252
- };
253
- if (proto[PHECDA_KEY].__CLEAR_KEY.has(key))
254
- break;
262
+ if (state) ret = {
263
+ ...state,
264
+ ...ret
265
+ };
266
+ if (proto[PHECDA_KEY].__CLEAR_KEY.has(key)) break;
255
267
  }
256
268
  proto = Object.getPrototypeOf(proto);
257
269
  }
@@ -337,8 +349,7 @@ __name(Empty, "Empty");
337
349
 
338
350
  // src/helper.ts
339
351
  function getTag(moduleOrInstance) {
340
- if (typeof moduleOrInstance === "object")
341
- moduleOrInstance = moduleOrInstance.constructor;
352
+ if (typeof moduleOrInstance === "object") moduleOrInstance = moduleOrInstance.constructor;
342
353
  return get(moduleOrInstance.prototype, "tag") || moduleOrInstance.name;
343
354
  }
344
355
  __name(getTag, "getTag");
@@ -348,8 +359,7 @@ function getBind(model) {
348
359
  const ret = {};
349
360
  for (const item of keys) {
350
361
  const state = getState(instance, item);
351
- if (state.value)
352
- ret[item] = state.value;
362
+ if (state.value) ret[item] = state.value;
353
363
  }
354
364
  return ret;
355
365
  }
@@ -357,8 +367,7 @@ __name(getBind, "getBind");
357
367
  function plainToClass(model, input) {
358
368
  const instance = new model();
359
369
  const keys = getExposeKey(instance);
360
- for (const item of keys)
361
- instance[item] = input[item];
370
+ for (const item of keys) instance[item] = input[item];
362
371
  return instance;
363
372
  }
364
373
  __name(plainToClass, "plainToClass");
@@ -371,11 +380,9 @@ function transformInstance(instance, force = false) {
371
380
  if (handlers) {
372
381
  for (const handler of handlers) {
373
382
  const pipe = handler.pipe;
374
- if (!pipe)
375
- continue;
383
+ if (!pipe) continue;
376
384
  pipe(instance, addError);
377
- if (err.length && !force)
378
- return err;
385
+ if (err.length && !force) return err;
379
386
  }
380
387
  }
381
388
  }
@@ -391,11 +398,9 @@ async function transformInstanceAsync(instance, force = false) {
391
398
  if (handlers) {
392
399
  for (const handler of handlers) {
393
400
  const pipe = handler.pipe;
394
- if (!pipe)
395
- continue;
401
+ if (!pipe) continue;
396
402
  await pipe(instance, addError);
397
- if (err.length && !force)
398
- return err;
403
+ if (err.length && !force) return err;
399
404
  }
400
405
  }
401
406
  }
@@ -409,11 +414,9 @@ function transformProperty(instance, property, force = false) {
409
414
  if (handlers) {
410
415
  for (const handler of handlers) {
411
416
  const pipe = handler.pipe;
412
- if (!pipe)
413
- continue;
417
+ if (!pipe) continue;
414
418
  pipe(instance, addError);
415
- if (err.length && !force)
416
- return err;
419
+ if (err.length && !force) return err;
417
420
  }
418
421
  }
419
422
  return err;
@@ -426,11 +429,9 @@ async function transformPropertyAsync(instance, property, force = false) {
426
429
  if (handlers) {
427
430
  for (const handler of handlers) {
428
431
  const pipe = handler.pipe;
429
- if (!pipe)
430
- continue;
432
+ if (!pipe) continue;
431
433
  await pipe(instance, addError);
432
- if (err.length && !force)
433
- return err;
434
+ if (err.length && !force) return err;
434
435
  }
435
436
  }
436
437
  return err;
@@ -439,24 +440,20 @@ __name(transformPropertyAsync, "transformPropertyAsync");
439
440
  function classToPlain(instance) {
440
441
  const data = {};
441
442
  const exposeVars = getExposeKey(instance);
442
- for (const item of exposeVars)
443
- data[item] = instance[item];
443
+ for (const item of exposeVars) data[item] = instance[item];
444
444
  return JSON.parse(JSON.stringify(data));
445
445
  }
446
446
  __name(classToPlain, "classToPlain");
447
447
  function snapShot(data) {
448
448
  const snap = {};
449
- for (const i in data)
450
- snap[i] = data[i];
449
+ for (const i in data) snap[i] = data[i];
451
450
  return {
452
451
  data,
453
452
  clear() {
454
- for (const i in snap)
455
- delete data[i];
453
+ for (const i in snap) delete data[i];
456
454
  },
457
455
  apply() {
458
- for (const i in snap)
459
- data[i] = snap[i];
456
+ for (const i in snap) data[i] = snap[i];
460
457
  }
461
458
  };
462
459
  }
@@ -467,8 +464,7 @@ function addDecoToClass(c, key, handler) {
467
464
  __name(addDecoToClass, "addDecoToClass");
468
465
  function Pipeline(...decos) {
469
466
  return (...args) => {
470
- for (const d of decos)
471
- d(...args);
467
+ for (const d of decos) d(...args);
472
468
  };
473
469
  }
474
470
  __name(Pipeline, "Pipeline");
@@ -513,7 +509,6 @@ __name(getInject, "getInject");
513
509
  // src/decorators/function.ts
514
510
  function Isolate(model) {
515
511
  set(model.prototype, "isolate", true);
516
- model.prototype[PHECDA_KEY].__ISOLATE__ = true;
517
512
  }
518
513
  __name(Isolate, "Isolate");
519
514
  function Tag(tag) {
@@ -532,13 +527,12 @@ function Assign(cb) {
532
527
  return (model) => {
533
528
  setStateKey(model);
534
529
  setHandler(model, void 0, {
535
- init: async (instance) => {
530
+ init: /* @__PURE__ */ __name(async (instance) => {
536
531
  const value = await cb(instance);
537
532
  if (value && typeof value === "object" && !Array.isArray(value)) {
538
- for (const i in value)
539
- instance[i] = value[i];
533
+ for (const i in value) instance[i] = value[i];
540
534
  }
541
- }
535
+ }, "init")
542
536
  });
543
537
  };
544
538
  }
@@ -546,14 +540,11 @@ __name(Assign, "Assign");
546
540
  function Global(model) {
547
541
  setStateKey(model);
548
542
  setHandler(model, void 0, {
549
- init: async (instance) => {
550
- const tag = instance[PHECDA_KEY].__TAG__;
551
- if (!tag)
552
- return;
553
- if (!globalThis.__PHECDA__)
554
- globalThis.__PHECDA__ = {};
543
+ init: /* @__PURE__ */ __name(async (instance) => {
544
+ const tag = getTag(instance);
545
+ if (!globalThis.__PHECDA__) globalThis.__PHECDA__ = {};
555
546
  globalThis.__PHECDA__[tag] = instance.constructor;
556
- }
547
+ }, "init")
557
548
  });
558
549
  }
559
550
  __name(Global, "Global");
@@ -564,10 +555,8 @@ function To(...callbacks) {
564
555
  async pipe(instance, addError) {
565
556
  for (const cb of callbacks) {
566
557
  try {
567
- if (isAsyncFunc(cb))
568
- instance[key] = await cb(instance[key], instance, key);
569
- else
570
- instance[key] = cb(instance[key], instance, key);
558
+ if (isAsyncFunc(cb)) instance[key] = await cb(instance[key], instance, key);
559
+ else instance[key] = cb(instance[key], instance, key);
571
560
  } catch (e) {
572
561
  addError(e.message);
573
562
  }
@@ -583,15 +572,11 @@ function Rule(cb, info) {
583
572
  setHandler(proto, key, {
584
573
  async pipe(instance, addError) {
585
574
  let ret;
586
- if (isAsyncFunc(cb))
587
- ret = await cb(instance[key]);
588
- else
589
- ret = cb(instance[key]);
575
+ if (isAsyncFunc(cb)) ret = await cb(instance[key]);
576
+ else ret = cb(instance[key]);
590
577
  if (!ret) {
591
- if (typeof info === "string")
592
- addError(info);
593
- else
594
- addError(info());
578
+ if (typeof info === "string") addError(info);
579
+ else addError(info());
595
580
  }
596
581
  }
597
582
  });
@@ -602,7 +587,7 @@ function Err(cb, isCatch = false) {
602
587
  return (proto, key) => {
603
588
  setStateKey(proto, key);
604
589
  setHandler(proto, key, {
605
- init: (instance) => {
590
+ init: /* @__PURE__ */ __name((instance) => {
606
591
  if (typeof instance[key] === "function") {
607
592
  const oldFn = instance[key].bind(instance);
608
593
  if (isAsyncFunc(oldFn)) {
@@ -611,8 +596,7 @@ function Err(cb, isCatch = false) {
611
596
  await oldFn(...args);
612
597
  } catch (e) {
613
598
  cb(e, instance, key);
614
- if (!isCatch)
615
- throw e;
599
+ if (!isCatch) throw e;
616
600
  }
617
601
  };
618
602
  } else {
@@ -621,13 +605,12 @@ function Err(cb, isCatch = false) {
621
605
  oldFn(...args);
622
606
  } catch (e) {
623
607
  cb(e, instance, key);
624
- if (!isCatch)
625
- throw e;
608
+ if (!isCatch) throw e;
626
609
  }
627
610
  };
628
611
  }
629
612
  }
630
- }
613
+ }, "init")
631
614
  });
632
615
  };
633
616
  }
@@ -674,16 +657,14 @@ function Effect(cb) {
674
657
  }
675
658
  __name(Effect, "Effect");
676
659
  function Storage({ key: storeKey, json, stringify } = {}) {
677
- if (!json)
678
- json = /* @__PURE__ */ __name((v) => JSON.parse(v), "json");
679
- if (!stringify)
680
- stringify = /* @__PURE__ */ __name((v) => JSON.stringify(v), "stringify");
660
+ if (!json) json = /* @__PURE__ */ __name((v) => JSON.parse(v), "json");
661
+ if (!stringify) stringify = /* @__PURE__ */ __name((v) => JSON.stringify(v), "stringify");
681
662
  return (proto, key) => {
682
663
  const tag = storeKey || getTag(proto);
683
664
  init(proto);
684
665
  setStateKey(proto, key);
685
666
  setHandler(proto, key, {
686
- init: (instance) => {
667
+ init: /* @__PURE__ */ __name((instance) => {
687
668
  return getInject("storage")?.({
688
669
  instance,
689
670
  key,
@@ -691,11 +672,21 @@ function Storage({ key: storeKey, json, stringify } = {}) {
691
672
  toJSON: json,
692
673
  toString: stringify
693
674
  });
694
- }
675
+ }, "init")
695
676
  });
696
677
  };
697
678
  }
698
679
  __name(Storage, "Storage");
680
+ function If(value, ...decorators) {
681
+ if (value) {
682
+ return (...args) => {
683
+ decorators.forEach((d) => d(...args));
684
+ };
685
+ }
686
+ return () => {
687
+ };
688
+ }
689
+ __name(If, "If");
699
690
  // Annotate the CommonJS export names for ESM import in node:
700
691
  0 && (module.exports = {
701
692
  Assign,
@@ -707,6 +698,7 @@ __name(Storage, "Storage");
707
698
  Err,
708
699
  Expose,
709
700
  Global,
701
+ If,
710
702
  Ignore,
711
703
  Init,
712
704
  Inject,
package/dist/index.mjs CHANGED
@@ -5,31 +5,49 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
5
5
  var SHARE_KEY = Symbol("phecda");
6
6
  var PHECDA_KEY = Symbol("phecda");
7
7
  function isPhecda(model) {
8
- if (typeof model === "function")
9
- return !!model.prototype[PHECDA_KEY];
8
+ if (typeof model === "function") return !!model.prototype[PHECDA_KEY];
10
9
  return false;
11
10
  }
12
11
  __name(isPhecda, "isPhecda");
13
12
  function init(proto) {
14
- if (!proto)
15
- return;
13
+ if (!proto) return;
16
14
  if (!proto.hasOwnProperty(PHECDA_KEY)) {
17
15
  proto[PHECDA_KEY] = {
16
+ /**
17
+ * 暴露的变量,
18
+ * 只要属性上存在至少一个装饰器,该属性就会被捕捉到
19
+ */
18
20
  __EXPOSE_KEY: /* @__PURE__ */ new Set(),
21
+ /**
22
+ * @Ignore 绑定的属性,
23
+ * 某属性即使被捕捉,可被强行忽略,优先级最高
24
+ */
19
25
  __IGNORE_KEY: /* @__PURE__ */ new Set(),
26
+ /**
27
+ * @Clear 绑定的属性,
28
+ * 消除父类在该key上的state/handler, 但export key 和 state
29
+ */
20
30
  __CLEAR_KEY: /* @__PURE__ */ new Set(),
31
+ /**
32
+ * 存在状态的变量
33
+ * @deprecated
34
+ */
21
35
  __STATE_KEY: /* @__PURE__ */ new Set(),
36
+ /**
37
+ * 状态变量的处理器
38
+ */
22
39
  __STATE_HANDLER__: /* @__PURE__ */ new Map(),
40
+ /**
41
+ * 状态变量的共有状态
42
+ */
23
43
  __STATE_NAMESPACE__: /* @__PURE__ */ new Map()
24
44
  };
25
45
  }
26
46
  }
27
47
  __name(init, "init");
28
48
  function getPhecdaFromTarget(target) {
29
- if (typeof target === "function")
30
- return target.prototype;
31
- if (target.hasOwnProperty(PHECDA_KEY))
32
- return target;
49
+ if (typeof target === "function") return target.prototype;
50
+ if (target.hasOwnProperty(PHECDA_KEY)) return target;
33
51
  return Object.getPrototypeOf(target);
34
52
  }
35
53
  __name(getPhecdaFromTarget, "getPhecdaFromTarget");
@@ -67,12 +85,10 @@ function setHandler(proto, key, handler) {
67
85
  proto = proto.prototype;
68
86
  }
69
87
  init(proto);
70
- if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key))
71
- proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [
72
- handler
73
- ]);
74
- else
75
- proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
88
+ if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key)) proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [
89
+ handler
90
+ ]);
91
+ else proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
76
92
  }
77
93
  __name(setHandler, "setHandler");
78
94
  function setState(proto, key, state) {
@@ -96,8 +112,7 @@ function getStateKey(target) {
96
112
  let proto = getPhecdaFromTarget(target);
97
113
  const set2 = /* @__PURE__ */ new Set();
98
114
  while (proto?.[PHECDA_KEY]) {
99
- if (proto.hasOwnProperty(PHECDA_KEY))
100
- proto[PHECDA_KEY].__STATE_KEY.forEach((item) => set2.add(item));
115
+ if (proto.hasOwnProperty(PHECDA_KEY)) proto[PHECDA_KEY].__STATE_KEY.forEach((item) => set2.add(item));
101
116
  proto = Object.getPrototypeOf(proto);
102
117
  }
103
118
  return [
@@ -117,10 +132,9 @@ function getExposeKey(target) {
117
132
  const set2 = /* @__PURE__ */ new Set();
118
133
  const origin = proto;
119
134
  while (proto?.[PHECDA_KEY]) {
120
- if (proto.hasOwnProperty(PHECDA_KEY))
121
- [
122
- ...proto[PHECDA_KEY].__EXPOSE_KEY
123
- ].forEach((item) => !origin[PHECDA_KEY].__IGNORE_KEY.has(item) && set2.add(item));
135
+ if (proto.hasOwnProperty(PHECDA_KEY)) [
136
+ ...proto[PHECDA_KEY].__EXPOSE_KEY
137
+ ].forEach((item) => !origin[PHECDA_KEY].__IGNORE_KEY.has(item) && set2.add(item));
124
138
  proto = Object.getPrototypeOf(proto);
125
139
  }
126
140
  return [
@@ -146,8 +160,7 @@ function getHandler(target, key) {
146
160
  while (proto?.[PHECDA_KEY]) {
147
161
  if (proto.hasOwnProperty(PHECDA_KEY)) {
148
162
  proto[PHECDA_KEY].__STATE_HANDLER__.get(key)?.forEach((item) => set2.add(item));
149
- if (proto[PHECDA_KEY].__CLEAR_KEY.has(key))
150
- break;
163
+ if (proto[PHECDA_KEY].__CLEAR_KEY.has(key)) break;
151
164
  }
152
165
  proto = Object.getPrototypeOf(proto);
153
166
  }
@@ -162,13 +175,11 @@ function getState(target, key = SHARE_KEY) {
162
175
  while (proto?.[PHECDA_KEY]) {
163
176
  if (proto.hasOwnProperty(PHECDA_KEY)) {
164
177
  const state = proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key);
165
- if (state)
166
- ret = {
167
- ...state,
168
- ...ret
169
- };
170
- if (proto[PHECDA_KEY].__CLEAR_KEY.has(key))
171
- break;
178
+ if (state) ret = {
179
+ ...state,
180
+ ...ret
181
+ };
182
+ if (proto[PHECDA_KEY].__CLEAR_KEY.has(key)) break;
172
183
  }
173
184
  proto = Object.getPrototypeOf(proto);
174
185
  }
@@ -254,8 +265,7 @@ __name(Empty, "Empty");
254
265
 
255
266
  // src/helper.ts
256
267
  function getTag(moduleOrInstance) {
257
- if (typeof moduleOrInstance === "object")
258
- moduleOrInstance = moduleOrInstance.constructor;
268
+ if (typeof moduleOrInstance === "object") moduleOrInstance = moduleOrInstance.constructor;
259
269
  return get(moduleOrInstance.prototype, "tag") || moduleOrInstance.name;
260
270
  }
261
271
  __name(getTag, "getTag");
@@ -265,8 +275,7 @@ function getBind(model) {
265
275
  const ret = {};
266
276
  for (const item of keys) {
267
277
  const state = getState(instance, item);
268
- if (state.value)
269
- ret[item] = state.value;
278
+ if (state.value) ret[item] = state.value;
270
279
  }
271
280
  return ret;
272
281
  }
@@ -274,8 +283,7 @@ __name(getBind, "getBind");
274
283
  function plainToClass(model, input) {
275
284
  const instance = new model();
276
285
  const keys = getExposeKey(instance);
277
- for (const item of keys)
278
- instance[item] = input[item];
286
+ for (const item of keys) instance[item] = input[item];
279
287
  return instance;
280
288
  }
281
289
  __name(plainToClass, "plainToClass");
@@ -288,11 +296,9 @@ function transformInstance(instance, force = false) {
288
296
  if (handlers) {
289
297
  for (const handler of handlers) {
290
298
  const pipe = handler.pipe;
291
- if (!pipe)
292
- continue;
299
+ if (!pipe) continue;
293
300
  pipe(instance, addError);
294
- if (err.length && !force)
295
- return err;
301
+ if (err.length && !force) return err;
296
302
  }
297
303
  }
298
304
  }
@@ -308,11 +314,9 @@ async function transformInstanceAsync(instance, force = false) {
308
314
  if (handlers) {
309
315
  for (const handler of handlers) {
310
316
  const pipe = handler.pipe;
311
- if (!pipe)
312
- continue;
317
+ if (!pipe) continue;
313
318
  await pipe(instance, addError);
314
- if (err.length && !force)
315
- return err;
319
+ if (err.length && !force) return err;
316
320
  }
317
321
  }
318
322
  }
@@ -326,11 +330,9 @@ function transformProperty(instance, property, force = false) {
326
330
  if (handlers) {
327
331
  for (const handler of handlers) {
328
332
  const pipe = handler.pipe;
329
- if (!pipe)
330
- continue;
333
+ if (!pipe) continue;
331
334
  pipe(instance, addError);
332
- if (err.length && !force)
333
- return err;
335
+ if (err.length && !force) return err;
334
336
  }
335
337
  }
336
338
  return err;
@@ -343,11 +345,9 @@ async function transformPropertyAsync(instance, property, force = false) {
343
345
  if (handlers) {
344
346
  for (const handler of handlers) {
345
347
  const pipe = handler.pipe;
346
- if (!pipe)
347
- continue;
348
+ if (!pipe) continue;
348
349
  await pipe(instance, addError);
349
- if (err.length && !force)
350
- return err;
350
+ if (err.length && !force) return err;
351
351
  }
352
352
  }
353
353
  return err;
@@ -356,24 +356,20 @@ __name(transformPropertyAsync, "transformPropertyAsync");
356
356
  function classToPlain(instance) {
357
357
  const data = {};
358
358
  const exposeVars = getExposeKey(instance);
359
- for (const item of exposeVars)
360
- data[item] = instance[item];
359
+ for (const item of exposeVars) data[item] = instance[item];
361
360
  return JSON.parse(JSON.stringify(data));
362
361
  }
363
362
  __name(classToPlain, "classToPlain");
364
363
  function snapShot(data) {
365
364
  const snap = {};
366
- for (const i in data)
367
- snap[i] = data[i];
365
+ for (const i in data) snap[i] = data[i];
368
366
  return {
369
367
  data,
370
368
  clear() {
371
- for (const i in snap)
372
- delete data[i];
369
+ for (const i in snap) delete data[i];
373
370
  },
374
371
  apply() {
375
- for (const i in snap)
376
- data[i] = snap[i];
372
+ for (const i in snap) data[i] = snap[i];
377
373
  }
378
374
  };
379
375
  }
@@ -384,8 +380,7 @@ function addDecoToClass(c, key, handler) {
384
380
  __name(addDecoToClass, "addDecoToClass");
385
381
  function Pipeline(...decos) {
386
382
  return (...args) => {
387
- for (const d of decos)
388
- d(...args);
383
+ for (const d of decos) d(...args);
389
384
  };
390
385
  }
391
386
  __name(Pipeline, "Pipeline");
@@ -430,7 +425,6 @@ __name(getInject, "getInject");
430
425
  // src/decorators/function.ts
431
426
  function Isolate(model) {
432
427
  set(model.prototype, "isolate", true);
433
- model.prototype[PHECDA_KEY].__ISOLATE__ = true;
434
428
  }
435
429
  __name(Isolate, "Isolate");
436
430
  function Tag(tag) {
@@ -449,13 +443,12 @@ function Assign(cb) {
449
443
  return (model) => {
450
444
  setStateKey(model);
451
445
  setHandler(model, void 0, {
452
- init: async (instance) => {
446
+ init: /* @__PURE__ */ __name(async (instance) => {
453
447
  const value = await cb(instance);
454
448
  if (value && typeof value === "object" && !Array.isArray(value)) {
455
- for (const i in value)
456
- instance[i] = value[i];
449
+ for (const i in value) instance[i] = value[i];
457
450
  }
458
- }
451
+ }, "init")
459
452
  });
460
453
  };
461
454
  }
@@ -463,14 +456,11 @@ __name(Assign, "Assign");
463
456
  function Global(model) {
464
457
  setStateKey(model);
465
458
  setHandler(model, void 0, {
466
- init: async (instance) => {
467
- const tag = instance[PHECDA_KEY].__TAG__;
468
- if (!tag)
469
- return;
470
- if (!globalThis.__PHECDA__)
471
- globalThis.__PHECDA__ = {};
459
+ init: /* @__PURE__ */ __name(async (instance) => {
460
+ const tag = getTag(instance);
461
+ if (!globalThis.__PHECDA__) globalThis.__PHECDA__ = {};
472
462
  globalThis.__PHECDA__[tag] = instance.constructor;
473
- }
463
+ }, "init")
474
464
  });
475
465
  }
476
466
  __name(Global, "Global");
@@ -481,10 +471,8 @@ function To(...callbacks) {
481
471
  async pipe(instance, addError) {
482
472
  for (const cb of callbacks) {
483
473
  try {
484
- if (isAsyncFunc(cb))
485
- instance[key] = await cb(instance[key], instance, key);
486
- else
487
- instance[key] = cb(instance[key], instance, key);
474
+ if (isAsyncFunc(cb)) instance[key] = await cb(instance[key], instance, key);
475
+ else instance[key] = cb(instance[key], instance, key);
488
476
  } catch (e) {
489
477
  addError(e.message);
490
478
  }
@@ -500,15 +488,11 @@ function Rule(cb, info) {
500
488
  setHandler(proto, key, {
501
489
  async pipe(instance, addError) {
502
490
  let ret;
503
- if (isAsyncFunc(cb))
504
- ret = await cb(instance[key]);
505
- else
506
- ret = cb(instance[key]);
491
+ if (isAsyncFunc(cb)) ret = await cb(instance[key]);
492
+ else ret = cb(instance[key]);
507
493
  if (!ret) {
508
- if (typeof info === "string")
509
- addError(info);
510
- else
511
- addError(info());
494
+ if (typeof info === "string") addError(info);
495
+ else addError(info());
512
496
  }
513
497
  }
514
498
  });
@@ -519,7 +503,7 @@ function Err(cb, isCatch = false) {
519
503
  return (proto, key) => {
520
504
  setStateKey(proto, key);
521
505
  setHandler(proto, key, {
522
- init: (instance) => {
506
+ init: /* @__PURE__ */ __name((instance) => {
523
507
  if (typeof instance[key] === "function") {
524
508
  const oldFn = instance[key].bind(instance);
525
509
  if (isAsyncFunc(oldFn)) {
@@ -528,8 +512,7 @@ function Err(cb, isCatch = false) {
528
512
  await oldFn(...args);
529
513
  } catch (e) {
530
514
  cb(e, instance, key);
531
- if (!isCatch)
532
- throw e;
515
+ if (!isCatch) throw e;
533
516
  }
534
517
  };
535
518
  } else {
@@ -538,13 +521,12 @@ function Err(cb, isCatch = false) {
538
521
  oldFn(...args);
539
522
  } catch (e) {
540
523
  cb(e, instance, key);
541
- if (!isCatch)
542
- throw e;
524
+ if (!isCatch) throw e;
543
525
  }
544
526
  };
545
527
  }
546
528
  }
547
- }
529
+ }, "init")
548
530
  });
549
531
  };
550
532
  }
@@ -591,16 +573,14 @@ function Effect(cb) {
591
573
  }
592
574
  __name(Effect, "Effect");
593
575
  function Storage({ key: storeKey, json, stringify } = {}) {
594
- if (!json)
595
- json = /* @__PURE__ */ __name((v) => JSON.parse(v), "json");
596
- if (!stringify)
597
- stringify = /* @__PURE__ */ __name((v) => JSON.stringify(v), "stringify");
576
+ if (!json) json = /* @__PURE__ */ __name((v) => JSON.parse(v), "json");
577
+ if (!stringify) stringify = /* @__PURE__ */ __name((v) => JSON.stringify(v), "stringify");
598
578
  return (proto, key) => {
599
579
  const tag = storeKey || getTag(proto);
600
580
  init(proto);
601
581
  setStateKey(proto, key);
602
582
  setHandler(proto, key, {
603
- init: (instance) => {
583
+ init: /* @__PURE__ */ __name((instance) => {
604
584
  return getInject("storage")?.({
605
585
  instance,
606
586
  key,
@@ -608,11 +588,21 @@ function Storage({ key: storeKey, json, stringify } = {}) {
608
588
  toJSON: json,
609
589
  toString: stringify
610
590
  });
611
- }
591
+ }, "init")
612
592
  });
613
593
  };
614
594
  }
615
595
  __name(Storage, "Storage");
596
+ function If(value, ...decorators) {
597
+ if (value) {
598
+ return (...args) => {
599
+ decorators.forEach((d) => d(...args));
600
+ };
601
+ }
602
+ return () => {
603
+ };
604
+ }
605
+ __name(If, "If");
616
606
  export {
617
607
  Assign,
618
608
  Bind,
@@ -623,6 +613,7 @@ export {
623
613
  Err,
624
614
  Expose,
625
615
  Global,
616
+ If,
626
617
  Ignore,
627
618
  Init,
628
619
  Inject,
package/package.json CHANGED
@@ -1,18 +1,22 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "3.0.0-beta.17",
3
+ "version": "3.0.1",
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",
7
7
  "types": "dist/index.d.ts",
8
- "keywords": [],
9
- "author": "",
8
+ "keywords": [
9
+ "phecda",
10
+ "core"
11
+ ],
12
+ "author": "fgsreally",
13
+ "repository": "https://github.com/fgsreally/phecda/tree/main/packages/core",
10
14
  "files": [
11
15
  "dist"
12
16
  ],
13
17
  "license": "MIT",
14
18
  "devDependencies": {
15
- "tsup": "^6.5.0"
19
+ "tsup": "^8.1.0"
16
20
  },
17
21
  "scripts": {
18
22
  "dev": "tsup --watch",