phecda-core 1.0.7 → 1.1.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.
package/dist/index.d.ts CHANGED
@@ -22,6 +22,8 @@ interface Phecda {
22
22
  type ClassValue<I> = {
23
23
  [P in keyof I]: I[P] extends Function ? undefined : I[P];
24
24
  };
25
+ interface PhecdaEvents {
26
+ }
25
27
 
26
28
  declare function plainToClass<M extends new (...args: any) => any, Data extends Record<PropertyKey, any>>(Model: M, input: Data, options?: UsePipeOptions): Promise<{
27
29
  err: string[];
@@ -74,11 +76,11 @@ declare function Rule(rule: RegExp | string | Function | number, info: string, m
74
76
  declare function Ignore(target: any, key: PropertyKey): void;
75
77
  declare function Clear(target: any, key: PropertyKey): void;
76
78
  declare function Err<Fn extends (...args: any) => any>(cb: Fn): (target: any, key: PropertyKey) => void;
77
- declare function Get(target: any, key: PropertyKey): void;
79
+ declare function P(target: any, key: PropertyKey): void;
78
80
  declare function Pipe(v: ReturnType<typeof to>): (obj: any, key: PropertyKey) => void;
79
81
  declare function Tag(tag: string): (target: any) => void;
80
82
  declare function Storage(target: any): void;
81
- declare function Window(target: any): void;
83
+ declare function Global(target: any): void;
82
84
 
83
85
  declare function validate(p: RegExp | string | Function | Object | Number, v: any): Promise<any>;
84
86
  declare function getTag<M extends new (...args: any) => any>(Model: M): any;
@@ -99,6 +101,7 @@ declare function getHandler(target: Phecda, key: PropertyKey): PhecdaHandler[];
99
101
  declare function mergeState(target: Phecda, key: PropertyKey, state: any): void;
100
102
  declare function getState(target: Phecda, key: PropertyKey): Object | undefined;
101
103
  declare function register(instance: Phecda): void;
104
+ declare function registerAsync(instance: Phecda): Promise<void>;
102
105
 
103
106
  declare function isArray(info?: string): (obj: any, key: PropertyKey) => void;
104
107
  declare function isBoolean(info?: string): (obj: any, key: PropertyKey) => void;
@@ -123,6 +126,8 @@ declare const activeInstance: Record<string, any>;
123
126
  declare function injectProperty(key: string, value: any): Record<string, any>;
124
127
  declare function getProperty(key: string): any;
125
128
 
126
- declare function Watcher(eventName: string): (obj: any, key: string) => void;
129
+ declare function Watcher(eventName: keyof PhecdaEvents, options?: {
130
+ once: boolean;
131
+ }): (obj: any, key: string) => void;
127
132
 
128
- export { ClassValue, Clear, Err, Get, Ignore, Init, Phecda, PhecdaHandler, PhecdaNameSpace, Pipe, Rule, Storage, Tag, UsePipeOptions, Watcher, Window, activeInstance, addDecoToClass, classToValue, getExposeKey, getHandler, getIgnoreKey, getInitEvent, getModelState, getProperty, getState, getTag, init, injectProperty, isArray, isBoolean, isCnName, isDate, isEnName, isHexColor, isIdCard, isLandline, isMailBox, isMobile, isNumber, isObject, isPhecda, isPostalCode, isString, isWechat, mergeOptions, mergeState, plainToClass, regisHandler, regisInitEvent, register, setExposeKey, setIgnoreKey, setModalVar, snapShot, to, toNumber, toString, validate };
133
+ export { ClassValue, Clear, Err, Global, Ignore, Init, P, Phecda, PhecdaEvents, PhecdaHandler, PhecdaNameSpace, Pipe, Rule, Storage, Tag, UsePipeOptions, Watcher, activeInstance, addDecoToClass, classToValue, getExposeKey, getHandler, getIgnoreKey, getInitEvent, getModelState, getProperty, getState, getTag, init, injectProperty, isArray, isBoolean, isCnName, isDate, isEnName, isHexColor, isIdCard, isLandline, isMailBox, isMobile, isNumber, isObject, isPhecda, isPostalCode, isString, isWechat, mergeOptions, mergeState, plainToClass, regisHandler, regisInitEvent, register, registerAsync, setExposeKey, setIgnoreKey, setModalVar, snapShot, to, toNumber, toString, validate };
package/dist/index.js CHANGED
@@ -23,15 +23,15 @@ var src_exports = {};
23
23
  __export(src_exports, {
24
24
  Clear: () => Clear,
25
25
  Err: () => Err,
26
- Get: () => Get,
26
+ Global: () => Global,
27
27
  Ignore: () => Ignore,
28
28
  Init: () => Init,
29
+ P: () => P,
29
30
  Pipe: () => Pipe,
30
31
  Rule: () => Rule,
31
32
  Storage: () => Storage,
32
33
  Tag: () => Tag,
33
34
  Watcher: () => Watcher,
34
- Window: () => Window,
35
35
  activeInstance: () => activeInstance,
36
36
  addDecoToClass: () => addDecoToClass,
37
37
  classToValue: () => classToValue,
@@ -67,6 +67,7 @@ __export(src_exports, {
67
67
  regisHandler: () => regisHandler,
68
68
  regisInitEvent: () => regisInitEvent,
69
69
  register: () => register,
70
+ registerAsync: () => registerAsync,
70
71
  setExposeKey: () => setExposeKey,
71
72
  setIgnoreKey: () => setIgnoreKey,
72
73
  setModalVar: () => setModalVar,
@@ -175,7 +176,7 @@ function getExposeKey(target) {
175
176
  init(target);
176
177
  return [
177
178
  ...target._namespace.__EXPOSE_VAR__
178
- ];
179
+ ].filter((item) => !target._namespace.__IGNORE_VAR__.has(item));
179
180
  }
180
181
  __name(getExposeKey, "getExposeKey");
181
182
  function getIgnoreKey(target) {
@@ -214,7 +215,7 @@ function getState(target, key) {
214
215
  }
215
216
  __name(getState, "getState");
216
217
  function register(instance) {
217
- const stateVars = getModelState(instance);
218
+ const stateVars = getExposeKey(instance);
218
219
  for (const item of stateVars) {
219
220
  const handlers = getHandler(instance, item);
220
221
  for (const hanlder of handlers)
@@ -222,6 +223,15 @@ function register(instance) {
222
223
  }
223
224
  }
224
225
  __name(register, "register");
226
+ async function registerAsync(instance) {
227
+ const stateVars = getExposeKey(instance);
228
+ for (const item of stateVars) {
229
+ const handlers = getHandler(instance, item);
230
+ for (const hanlder of handlers)
231
+ await hanlder.init?.(instance);
232
+ }
233
+ }
234
+ __name(registerAsync, "registerAsync");
225
235
 
226
236
  // src/decorators.ts
227
237
  function Init(target, key) {
@@ -255,6 +265,7 @@ function Clear(target, key) {
255
265
  target._namespace.__IGNORE_VAR__.delete(key);
256
266
  target._namespace.__STATE_VAR__.delete(key);
257
267
  target._namespace.__STATE_HANDLER__.delete(key);
268
+ target._namespace.__STATE_NAMESPACE__.delete(key);
258
269
  }
259
270
  __name(Clear, "Clear");
260
271
  function Err(cb) {
@@ -266,10 +277,10 @@ function Err(cb) {
266
277
  };
267
278
  }
268
279
  __name(Err, "Err");
269
- function Get(target, key) {
280
+ function P(target, key) {
270
281
  setExposeKey(target, key);
271
282
  }
272
- __name(Get, "Get");
283
+ __name(P, "P");
273
284
  function Pipe(v) {
274
285
  return (obj, key) => {
275
286
  setModalVar(obj, key);
@@ -300,7 +311,7 @@ function Storage(target) {
300
311
  regisHandler(target.prototype, uniTag, {
301
312
  init: (instance) => {
302
313
  const { state } = instance;
303
- window.addEventListener("beforeunload", () => {
314
+ globalThis.addEventListener("beforeunload", () => {
304
315
  localStorage.setItem(`_phecda_${tag}`, JSON.stringify(state));
305
316
  });
306
317
  const lastObjStr = localStorage.getItem(`_phecda_${tag}`);
@@ -313,14 +324,14 @@ function Storage(target) {
313
324
  });
314
325
  }
315
326
  __name(Storage, "Storage");
316
- function Window(target) {
317
- if (!window.__PHECDA__)
318
- window.__PHECDA__ = {};
327
+ function Global(target) {
328
+ if (!globalThis.__PHECDA__)
329
+ globalThis.__PHECDA__ = {};
319
330
  const tag = target.prototype._namespace.__TAG__;
320
331
  if (tag)
321
- window.__PHECDA__[tag] = target;
332
+ globalThis.__PHECDA__[tag] = target;
322
333
  }
323
- __name(Window, "Window");
334
+ __name(Global, "Global");
324
335
 
325
336
  // src/helper.ts
326
337
  async function plainToClass(Model, input, options = {}) {
@@ -361,12 +372,8 @@ __name(plainToClass, "plainToClass");
361
372
  function classToValue(instance) {
362
373
  const data = {};
363
374
  const exposeVar = getExposeKey(instance);
364
- const ignoreVars = getIgnoreKey(instance);
365
- for (const item of exposeVar) {
366
- if (ignoreVars.includes(item))
367
- continue;
375
+ for (const item of exposeVar)
368
376
  data[item] = instance[item];
369
- }
370
377
  return data;
371
378
  }
372
379
  __name(classToValue, "classToValue");
@@ -491,7 +498,7 @@ function getProperty(key) {
491
498
  __name(getProperty, "getProperty");
492
499
 
493
500
  // src/custom/decorator.ts
494
- function Watcher(eventName) {
501
+ function Watcher(eventName, options) {
495
502
  return (obj, key) => {
496
503
  setModalVar(obj, key);
497
504
  regisHandler(obj, key, {
@@ -499,7 +506,8 @@ function Watcher(eventName) {
499
506
  getProperty("watcher")?.({
500
507
  eventName,
501
508
  instance,
502
- key
509
+ key,
510
+ options
503
511
  });
504
512
  }
505
513
  });
@@ -510,15 +518,15 @@ __name(Watcher, "Watcher");
510
518
  0 && (module.exports = {
511
519
  Clear,
512
520
  Err,
513
- Get,
521
+ Global,
514
522
  Ignore,
515
523
  Init,
524
+ P,
516
525
  Pipe,
517
526
  Rule,
518
527
  Storage,
519
528
  Tag,
520
529
  Watcher,
521
- Window,
522
530
  activeInstance,
523
531
  addDecoToClass,
524
532
  classToValue,
@@ -554,6 +562,7 @@ __name(Watcher, "Watcher");
554
562
  regisHandler,
555
563
  regisInitEvent,
556
564
  register,
565
+ registerAsync,
557
566
  setExposeKey,
558
567
  setIgnoreKey,
559
568
  setModalVar,
package/dist/index.mjs CHANGED
@@ -98,7 +98,7 @@ function getExposeKey(target) {
98
98
  init(target);
99
99
  return [
100
100
  ...target._namespace.__EXPOSE_VAR__
101
- ];
101
+ ].filter((item) => !target._namespace.__IGNORE_VAR__.has(item));
102
102
  }
103
103
  __name(getExposeKey, "getExposeKey");
104
104
  function getIgnoreKey(target) {
@@ -137,7 +137,7 @@ function getState(target, key) {
137
137
  }
138
138
  __name(getState, "getState");
139
139
  function register(instance) {
140
- const stateVars = getModelState(instance);
140
+ const stateVars = getExposeKey(instance);
141
141
  for (const item of stateVars) {
142
142
  const handlers = getHandler(instance, item);
143
143
  for (const hanlder of handlers)
@@ -145,6 +145,15 @@ function register(instance) {
145
145
  }
146
146
  }
147
147
  __name(register, "register");
148
+ async function registerAsync(instance) {
149
+ const stateVars = getExposeKey(instance);
150
+ for (const item of stateVars) {
151
+ const handlers = getHandler(instance, item);
152
+ for (const hanlder of handlers)
153
+ await hanlder.init?.(instance);
154
+ }
155
+ }
156
+ __name(registerAsync, "registerAsync");
148
157
 
149
158
  // src/decorators.ts
150
159
  function Init(target, key) {
@@ -178,6 +187,7 @@ function Clear(target, key) {
178
187
  target._namespace.__IGNORE_VAR__.delete(key);
179
188
  target._namespace.__STATE_VAR__.delete(key);
180
189
  target._namespace.__STATE_HANDLER__.delete(key);
190
+ target._namespace.__STATE_NAMESPACE__.delete(key);
181
191
  }
182
192
  __name(Clear, "Clear");
183
193
  function Err(cb) {
@@ -189,10 +199,10 @@ function Err(cb) {
189
199
  };
190
200
  }
191
201
  __name(Err, "Err");
192
- function Get(target, key) {
202
+ function P(target, key) {
193
203
  setExposeKey(target, key);
194
204
  }
195
- __name(Get, "Get");
205
+ __name(P, "P");
196
206
  function Pipe(v) {
197
207
  return (obj, key) => {
198
208
  setModalVar(obj, key);
@@ -223,7 +233,7 @@ function Storage(target) {
223
233
  regisHandler(target.prototype, uniTag, {
224
234
  init: (instance) => {
225
235
  const { state } = instance;
226
- window.addEventListener("beforeunload", () => {
236
+ globalThis.addEventListener("beforeunload", () => {
227
237
  localStorage.setItem(`_phecda_${tag}`, JSON.stringify(state));
228
238
  });
229
239
  const lastObjStr = localStorage.getItem(`_phecda_${tag}`);
@@ -236,14 +246,14 @@ function Storage(target) {
236
246
  });
237
247
  }
238
248
  __name(Storage, "Storage");
239
- function Window(target) {
240
- if (!window.__PHECDA__)
241
- window.__PHECDA__ = {};
249
+ function Global(target) {
250
+ if (!globalThis.__PHECDA__)
251
+ globalThis.__PHECDA__ = {};
242
252
  const tag = target.prototype._namespace.__TAG__;
243
253
  if (tag)
244
- window.__PHECDA__[tag] = target;
254
+ globalThis.__PHECDA__[tag] = target;
245
255
  }
246
- __name(Window, "Window");
256
+ __name(Global, "Global");
247
257
 
248
258
  // src/helper.ts
249
259
  async function plainToClass(Model, input, options = {}) {
@@ -284,12 +294,8 @@ __name(plainToClass, "plainToClass");
284
294
  function classToValue(instance) {
285
295
  const data = {};
286
296
  const exposeVar = getExposeKey(instance);
287
- const ignoreVars = getIgnoreKey(instance);
288
- for (const item of exposeVar) {
289
- if (ignoreVars.includes(item))
290
- continue;
297
+ for (const item of exposeVar)
291
298
  data[item] = instance[item];
292
- }
293
299
  return data;
294
300
  }
295
301
  __name(classToValue, "classToValue");
@@ -414,7 +420,7 @@ function getProperty(key) {
414
420
  __name(getProperty, "getProperty");
415
421
 
416
422
  // src/custom/decorator.ts
417
- function Watcher(eventName) {
423
+ function Watcher(eventName, options) {
418
424
  return (obj, key) => {
419
425
  setModalVar(obj, key);
420
426
  regisHandler(obj, key, {
@@ -422,7 +428,8 @@ function Watcher(eventName) {
422
428
  getProperty("watcher")?.({
423
429
  eventName,
424
430
  instance,
425
- key
431
+ key,
432
+ options
426
433
  });
427
434
  }
428
435
  });
@@ -432,15 +439,15 @@ __name(Watcher, "Watcher");
432
439
  export {
433
440
  Clear,
434
441
  Err,
435
- Get,
442
+ Global,
436
443
  Ignore,
437
444
  Init,
445
+ P,
438
446
  Pipe,
439
447
  Rule,
440
448
  Storage,
441
449
  Tag,
442
450
  Watcher,
443
- Window,
444
451
  activeInstance,
445
452
  addDecoToClass,
446
453
  classToValue,
@@ -476,6 +483,7 @@ export {
476
483
  regisHandler,
477
484
  regisInitEvent,
478
485
  register,
486
+ registerAsync,
479
487
  setExposeKey,
480
488
  setIgnoreKey,
481
489
  setModalVar,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "1.0.7",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",