phecda-core 1.0.0 → 1.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.
package/dist/index.d.ts CHANGED
@@ -1,20 +1,12 @@
1
- import * as mitt from 'mitt';
2
- import { EventType, Emitter, Handler } from 'mitt';
3
- import * as vue from 'vue';
4
- import { Ref, UnwrapNestedRefs, App } from 'vue';
5
-
6
1
  interface PhecdaNameSpace {
7
2
  [name: string]: Phecda;
8
3
  }
9
- interface PhecdaEvents extends Record<EventType, unknown> {
10
- [key: EventType]: any;
11
- }
12
4
  interface UsePipeOptions {
13
5
  transform: boolean;
14
6
  collectError: boolean;
15
7
  }
16
8
  interface PhecdaHandler {
17
- init?: (instance: Phecda, isFirstTime: boolean) => any;
9
+ init?: (instance: Phecda) => any;
18
10
  validate?: (instance: Phecda) => string | void;
19
11
  pipe?: (instance: Phecda) => void;
20
12
  error?: any;
@@ -26,35 +18,19 @@ interface PhecdaHandler {
26
18
  beforeUnload?: (instance: Phecda) => any;
27
19
  beforeLoad?: (instance: Phecda, v: any) => any;
28
20
  }
29
- type Vret<I> = {
30
- [P in keyof I]: I[P] extends Function ? I[P] : Ref<I[P]>;
31
- };
32
- type ClassValue<I> = {
33
- [P in keyof I]: I[P] extends Function ? undefined : I[P];
34
- };
35
- type SchemaToObj<S> = {
36
- [P in keyof S]: S[P] extends object ? SchemaToObj<S[P]> : (S[P] extends string ? any : S[P]);
37
- };
38
-
39
- declare class Phecda {
40
- protected static __PHECDA_EMIT__: Emitter<PhecdaEvents>;
41
- protected static __PHECDA_NAMESPACE__: PhecdaNameSpace;
42
- protected static __PHECDA_MODEL__: string[];
43
- protected _symbol: string;
44
- protected _namespace: {
21
+ interface Phecda {
22
+ _namespace: {
23
+ __TAG__: string;
45
24
  __INIT_EVENT__: Set<PropertyKey>;
25
+ __EXPOSE_VAR__: Set<PropertyKey>;
26
+ __IGNORE_VAR__: Set<PropertyKey>;
46
27
  __STATE_VAR__: Set<PropertyKey>;
47
- __STATE_HANDLER__: Map<PropertyKey, PhecdaHandler>;
28
+ __STATE_HANDLER__: Map<PropertyKey, PhecdaHandler[]>;
48
29
  };
49
- constructor();
50
- on<Key extends keyof PhecdaEvents>(type: Key, handler: Handler<PhecdaEvents[Key]>): void;
51
- emit(type: keyof PhecdaEvents, event: PhecdaEvents[keyof PhecdaEvents]): void;
52
- off<Key extends keyof PhecdaEvents>(type: Key, handler?: Handler<PhecdaEvents[Key]>): void;
53
- }
54
- declare class PhecdaWeb extends Phecda {
55
- constructor();
56
30
  }
57
- declare function register(instance: any): void;
31
+ type ClassValue<I> = {
32
+ [P in keyof I]: I[P] extends Function ? undefined : I[P];
33
+ };
58
34
 
59
35
  declare function plainToClass<M extends new (...args: any) => any, Data extends Record<PropertyKey, any>>(Model: M, input: Data, options?: Partial<UsePipeOptions>): Promise<{
60
36
  err: string[];
@@ -95,86 +71,36 @@ declare function to<T extends (...args: any) => any>(task: T, oldTasks?: Functio
95
71
  };
96
72
  value: Function[];
97
73
  };
98
- declare function snapShot<T extends new (...args: any) => any>(data: UnwrapNestedRefs<InstanceType<T>>): {
99
- data: UnwrapNestedRefs<InstanceType<T>>;
74
+ declare function snapShot<T extends new (...args: any) => any>(data: InstanceType<T>): {
75
+ data: InstanceType<T>;
100
76
  clear(): void;
101
77
  apply(): void;
102
78
  };
79
+ declare function addDecoToClass<M extends new (...args: any) => any>(c: M, key: keyof InstanceType<M> | string, handler: ((target: any, key: PropertyKey) => void), type?: 'static' | 'class' | 'normal'): void;
103
80
 
104
- declare function regisInitEvent(target: any, key: string): void;
105
- declare function getInitEvent(target: any): string[];
106
- declare function setModalState(target: any, key: PropertyKey): void;
107
- declare function setExposeKey(target: any, key: PropertyKey): void;
108
- declare function setIgnoreKey(target: any, key: PropertyKey): void;
109
- declare function getModelState(target: any): string[];
110
- declare function getExposeKey(target: any): string[];
111
- declare function getIgnoreKey(target: any): string[];
112
- declare function regisHandler(target: any, key: PropertyKey, handler: PhecdaHandler): void;
113
- declare function getHandler(target: any, key: PropertyKey): any;
114
- declare function Init(target: any, key: string): void;
115
- declare function Rule(rule: RegExp | string | Function | number, info: string, meta?: any): (obj: any, key: string) => void;
116
- declare function Ignore(target: any, key: string): void;
117
- declare function Clear(target: any, key: string): void;
81
+ declare function Init(target: any, key: PropertyKey): void;
82
+ declare function Rule(rule: RegExp | string | Function | number, info: string, meta?: any): (obj: any, key: PropertyKey) => void;
83
+ declare function Ignore(target: any, key: PropertyKey): void;
84
+ declare function Clear(target: any, key: PropertyKey): void;
118
85
  declare function Err<Fn extends (...args: any) => any>(cb: Fn): (target: any, key: PropertyKey) => void;
119
- declare function Get(target: any, key: string): void;
120
- declare function Pipe(v: ReturnType<typeof to>): (obj: any, key: string) => void;
121
- declare function Watcher(eventName: string): (obj: any, key: string) => void;
86
+ declare function Get(target: any, key: PropertyKey): void;
87
+ declare function Pipe(v: ReturnType<typeof to>): (obj: any, key: PropertyKey) => void;
122
88
  declare function Tag(tag: string): (target: any) => void;
123
89
  declare function Storage(target: any): void;
124
90
 
125
- declare const EXPRESS_RE: RegExp;
126
- declare const FN_RE: RegExp;
127
- declare function createFilter<Data extends Record<string, any>>(initState?: Object, option?: {
128
- expressionRE?: RegExp;
129
- fnRE?: RegExp;
130
- exclude?: string[];
131
- }): {
132
- filter: <Schema>(obj: Schema) => SchemaToObj<Schema>;
133
- data: [Data] extends [vue.Ref<any>] ? Data : vue.Ref<vue.UnwrapRef<Data>>;
134
- init: (params?: Data) => void;
135
- setState: <Key extends string>(key: Key, value: Data[Key]) => void;
136
- storeState: (key: string, params?: Data) => void;
137
- store: {
138
- [key: string]: Data;
139
- };
140
- applyStore: (key: string) => void;
141
- dispose: () => void;
142
- clearStore: (key: string) => void;
143
- };
144
-
145
91
  declare function validate(p: RegExp | string | Function | Object | Number, v: any): Promise<any>;
146
- declare function isAsyncFunc(fn: Function): boolean;
147
-
148
- declare const emitter: mitt.Emitter<PhecdaEvents>;
149
- declare const emit: {
150
- <Key extends keyof PhecdaEvents>(type: Key, event: PhecdaEvents[Key]): void;
151
- <Key_1 extends keyof PhecdaEvents>(type: undefined extends PhecdaEvents[Key_1] ? Key_1 : never): void;
152
- };
153
-
154
- declare const phecdaSymbol: unique symbol;
155
- declare function createPhecda(): vue.Raw<{
156
- install(app: App): void;
157
- vProxyMap: WeakMap<object, any>;
158
- vMap: WeakMap<object, any>;
159
- }>;
160
- interface PhecdaInstance {
161
- vProxyMap: WeakMap<any, any>;
162
- vMap: WeakMap<any, any>;
163
- }
164
- declare function setActivePhecda(phecda: PhecdaInstance): void;
165
- declare function getActivePhecda(): PhecdaInstance;
166
-
167
- type _DeepPartial<T> = {
168
- [K in keyof T]?: _DeepPartial<T[K]>;
169
- };
170
92
 
171
- declare function useR<T extends new (...args: any) => any>(Model: T): UnwrapNestedRefs<InstanceType<T>>;
172
- declare function usePatch<T extends new (...args: any) => any>(Model: T, Data: _DeepPartial<InstanceType<T>>): void;
173
- declare function useListen(eventName: Parameters<typeof emitter['on']>[0], cb: Parameters<typeof emitter['on']>[1]): () => void;
174
- declare function useV<T extends new (...args: any) => any>(Model: T): Vret<InstanceType<T>>;
175
- declare function cloneV<Instance extends Object>(instance: Instance): Instance;
176
- declare function initalize<M extends new (...args: any) => any>(Model: M): InstanceType<M> | void;
177
- declare function clearStorage<M extends new (...args: any) => any>(Model: M, isForceUpdate?: boolean): void;
178
- declare function deleteStorage(tag: string): void;
93
+ declare function init(target: Phecda): void;
94
+ declare function regisInitEvent(target: Phecda, key: string): void;
95
+ declare function getInitEvent(target: Phecda): string[];
96
+ declare function setModalState(target: Phecda, key: PropertyKey): void;
97
+ declare function setExposeKey(target: Phecda, key: PropertyKey): void;
98
+ declare function setIgnoreKey(target: Phecda, key: PropertyKey): void;
99
+ declare function getModelState(target: Phecda): string[];
100
+ declare function getExposeKey(target: Phecda): string[];
101
+ declare function getIgnoreKey(target: Phecda): string[];
102
+ declare function regisHandler(target: Phecda, key: PropertyKey, handler: PhecdaHandler): void;
103
+ declare function getHandler(target: Phecda, key: PropertyKey): PhecdaHandler[];
104
+ declare function register(instance: Phecda): void;
179
105
 
180
- export { ClassValue, Clear, EXPRESS_RE, Err, FN_RE, Get, Ignore, Init, Phecda, PhecdaEvents, PhecdaHandler, PhecdaNameSpace, PhecdaWeb, Pipe, Rule, SchemaToObj, Storage, Tag, UsePipeOptions, Vret, Watcher, classToValue, clearStorage, cloneV, createFilter, createPhecda, deleteStorage, emit, emitter, getActivePhecda, getExposeKey, getHandler, getIgnoreKey, getInitEvent, getModelState, initalize, isAsyncFunc, phecdaSymbol, plainToClass, regisHandler, regisInitEvent, register, setActivePhecda, setExposeKey, setIgnoreKey, setModalState, snapShot, to, useListen, usePatch, useR, useV, validate };
106
+ export { ClassValue, Clear, Err, Get, Ignore, Init, Phecda, PhecdaHandler, PhecdaNameSpace, Pipe, Rule, Storage, Tag, UsePipeOptions, addDecoToClass, classToValue, getExposeKey, getHandler, getIgnoreKey, getInitEvent, getModelState, init, plainToClass, regisHandler, regisInitEvent, register, setExposeKey, setIgnoreKey, setModalState, snapShot, to, validate };
package/dist/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,73 +15,46 @@ var __copyProps = (to2, from, except, desc) => {
17
15
  }
18
16
  return to2;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
19
 
26
20
  // src/index.ts
27
21
  var src_exports = {};
28
22
  __export(src_exports, {
29
23
  Clear: () => Clear,
30
- EXPRESS_RE: () => EXPRESS_RE,
31
24
  Err: () => Err,
32
- FN_RE: () => FN_RE,
33
25
  Get: () => Get,
34
26
  Ignore: () => Ignore,
35
27
  Init: () => Init,
36
- Phecda: () => Phecda,
37
- PhecdaWeb: () => PhecdaWeb,
38
28
  Pipe: () => Pipe,
39
29
  Rule: () => Rule,
40
30
  Storage: () => Storage,
41
31
  Tag: () => Tag,
42
- Watcher: () => Watcher,
32
+ addDecoToClass: () => addDecoToClass,
43
33
  classToValue: () => classToValue,
44
- clearStorage: () => clearStorage,
45
- cloneV: () => cloneV,
46
- createFilter: () => createFilter,
47
- createPhecda: () => createPhecda,
48
- deleteStorage: () => deleteStorage,
49
- emit: () => emit,
50
- emitter: () => emitter,
51
- getActivePhecda: () => getActivePhecda,
52
34
  getExposeKey: () => getExposeKey,
53
35
  getHandler: () => getHandler,
54
36
  getIgnoreKey: () => getIgnoreKey,
55
37
  getInitEvent: () => getInitEvent,
56
38
  getModelState: () => getModelState,
57
- initalize: () => initalize,
58
- isAsyncFunc: () => isAsyncFunc,
59
- phecdaSymbol: () => phecdaSymbol,
39
+ init: () => init,
60
40
  plainToClass: () => plainToClass,
61
41
  regisHandler: () => regisHandler,
62
42
  regisInitEvent: () => regisInitEvent,
63
43
  register: () => register,
64
- setActivePhecda: () => setActivePhecda,
65
44
  setExposeKey: () => setExposeKey,
66
45
  setIgnoreKey: () => setIgnoreKey,
67
46
  setModalState: () => setModalState,
68
47
  snapShot: () => snapShot,
69
48
  to: () => to,
70
- useListen: () => useListen,
71
- usePatch: () => usePatch,
72
- useR: () => useR,
73
- useV: () => useV,
74
49
  validate: () => validate
75
50
  });
76
51
  module.exports = __toCommonJS(src_exports);
77
52
 
78
- // src/emitter.ts
79
- var import_mitt = __toESM(require("mitt"));
80
- var emitter = (0, import_mitt.default)();
81
- var emit = emitter.emit.bind(emitter);
82
-
83
- // src/decorators.ts
53
+ // src/core.ts
84
54
  function init(target) {
85
55
  if (!target._namespace) {
86
56
  target._namespace = {
57
+ __TAG__: "",
87
58
  __INIT_EVENT__: /* @__PURE__ */ new Set(),
88
59
  __EXPOSE_VAR__: /* @__PURE__ */ new Set(),
89
60
  __IGNORE_VAR__: /* @__PURE__ */ new Set(),
@@ -115,7 +86,7 @@ function setIgnoreKey(target, key) {
115
86
  }
116
87
  function getModelState(target) {
117
88
  init(target);
118
- return [...target._namespace.__STATE_VAR__].reverse();
89
+ return [...target._namespace.__STATE_VAR__];
119
90
  }
120
91
  function getExposeKey(target) {
121
92
  init(target);
@@ -135,6 +106,16 @@ function regisHandler(target, key, handler) {
135
106
  function getHandler(target, key) {
136
107
  return target._namespace.__STATE_HANDLER__.get(key) || [];
137
108
  }
109
+ function register(instance) {
110
+ const stateVars = getModelState(instance);
111
+ for (const item of stateVars) {
112
+ const handlers = getHandler(instance, item);
113
+ for (const hanlder of handlers)
114
+ hanlder.init?.(instance);
115
+ }
116
+ }
117
+
118
+ // src/decorators.ts
138
119
  function Init(target, key) {
139
120
  setModalState(target, key);
140
121
  regisHandler(target, key, {
@@ -187,24 +168,17 @@ function Pipe(v) {
187
168
  });
188
169
  };
189
170
  }
190
- function Watcher(eventName) {
191
- return (obj, key) => {
192
- setModalState(obj, key);
193
- regisHandler(obj, key, {
194
- init(instance) {
195
- emitter.on(eventName, instance[key].bind(instance));
196
- }
197
- });
198
- };
199
- }
200
171
  function Tag(tag) {
201
172
  return (target) => {
202
- target.prototype._symbol = tag;
173
+ init(target.prototype);
174
+ target.prototype._namespace.__TAG__ = tag;
203
175
  };
204
176
  }
205
177
  function Storage(target) {
206
178
  init(target.prototype);
207
- const tag = target.prototype._symbol;
179
+ const tag = target.prototype._namespace.__TAG__;
180
+ if (tag === "")
181
+ throw new Error("miss tag");
208
182
  const uniTag = Symbol(tag);
209
183
  setModalState(target.prototype, uniTag);
210
184
  regisHandler(target.prototype, uniTag, {
@@ -223,46 +197,6 @@ function Storage(target) {
223
197
  });
224
198
  }
225
199
 
226
- // src/model.ts
227
- var Phecda = class {
228
- constructor() {
229
- if (!this._symbol)
230
- throw new Error("phecda miss tag");
231
- if (!Phecda.__PHECDA_MODEL__) {
232
- Phecda.__PHECDA_MODEL__ = [];
233
- Phecda.__PHECDA_NAMESPACE__ = {};
234
- Phecda.__PHECDA_EMIT__ = emitter;
235
- }
236
- }
237
- on(type, handler) {
238
- Phecda.__PHECDA_EMIT__.on(type, handler);
239
- }
240
- emit(type, event) {
241
- Phecda.__PHECDA_EMIT__.emit(type, event);
242
- }
243
- off(type, handler) {
244
- Phecda.__PHECDA_EMIT__.off(type, handler);
245
- }
246
- };
247
- var PhecdaWeb = class extends Phecda {
248
- constructor() {
249
- super();
250
- if (!window.__PHECDA_EMIT__) {
251
- window.__PHECDA_EMIT__ = Phecda.__PHECDA_EMIT__;
252
- window.__PHECDA_MODEL__ = Phecda.__PHECDA_MODEL__;
253
- window.__PHECDA_NAMESPACE__ = Phecda.__PHECDA_NAMESPACE__;
254
- }
255
- }
256
- };
257
- function register(instance) {
258
- const stateVars = getModelState(instance);
259
- for (const item of stateVars) {
260
- const handlers = getHandler(instance, item);
261
- for (const hanlder of handlers)
262
- hanlder.init?.(instance);
263
- }
264
- }
265
-
266
200
  // src/utils.ts
267
201
  async function validate(p, v) {
268
202
  if (typeof p === "string" || typeof p === "number") {
@@ -275,11 +209,8 @@ async function validate(p, v) {
275
209
  return p.test(v);
276
210
  return false;
277
211
  }
278
- function isAsyncFunc(fn) {
279
- return fn[Symbol.toStringTag] === "AsyncFunction";
280
- }
281
212
 
282
- // src/composable.ts
213
+ // src/helper.ts
283
214
  async function plainToClass(Model, input, options = {}) {
284
215
  const data = new Model();
285
216
  const resolvedOptions = {
@@ -341,269 +272,36 @@ function snapShot(data) {
341
272
  }
342
273
  };
343
274
  }
344
-
345
- // src/filter.ts
346
- var import_vue = require("vue");
347
- var EXPRESS_RE = /^{{(.*)}}$/;
348
- var FN_RE = /^\[\[(.*)\]\]$/;
349
- function createFilter(initState = {}, option = {}) {
350
- const resolveOption = Object.assign(
351
- {
352
- expressionRE: EXPRESS_RE,
353
- fnRE: FN_RE,
354
- exclude: []
355
- },
356
- option
357
- );
358
- let data = (0, import_vue.ref)(initState);
359
- let store = {};
360
- function traverse(obj) {
361
- for (const i in obj) {
362
- if (Array.isArray(obj[i]) || resolveOption.exclude.includes(i))
363
- continue;
364
- if (typeof obj[i] === "object" && obj[i])
365
- traverse(obj[i]);
366
- if (typeof obj[i] === "string") {
367
- if (resolveOption.expressionRE.test(obj[i])) {
368
- const body = obj[i].match(resolveOption.expressionRE)[1];
369
- Object.defineProperty(obj, i, {
370
- get() {
371
- return new Function(...Object.keys(data.value), `return ${body}`)(
372
- ...Object.values(data.value)
373
- );
374
- }
375
- });
376
- }
377
- if (resolveOption.fnRE.test(obj[i])) {
378
- const body = obj[i].match(resolveOption.fnRE)[1];
379
- Object.defineProperty(obj, i, {
380
- get() {
381
- return new Function(...Object.keys(data.value), `${body}`)(
382
- ...Object.values(data.value)
383
- );
384
- }
385
- });
386
- }
387
- }
388
- }
389
- }
390
- function filter(obj) {
391
- obj = (0, import_vue.reactive)(obj);
392
- traverse(obj);
393
- return obj;
394
- }
395
- function setState(key, value) {
396
- data.value[key] = value;
397
- }
398
- function storeState(key, params) {
399
- store[key] = data.value;
400
- init2(params);
401
- }
402
- function applyStore(key) {
403
- if (!store[key])
404
- return;
405
- data.value = store[key];
406
- }
407
- function init2(params) {
408
- data.value = params || initState || {};
409
- }
410
- function clearStore(key) {
411
- delete store[key];
412
- }
413
- function dispose() {
414
- data = null;
415
- store = null;
416
- }
417
- return { filter, data, init: init2, setState, storeState, store, applyStore, dispose, clearStore };
418
- }
419
-
420
- // src/vue/phecda.ts
421
- var import_vue2 = require("vue");
422
- var phecdaSymbol = Symbol("phecda");
423
- function createPhecda() {
424
- const phecda = (0, import_vue2.markRaw)({
425
- install(app) {
426
- app.provide(phecdaSymbol, phecda);
427
- app.config.globalProperties.$phecda = phecda;
428
- },
429
- vProxyMap: /* @__PURE__ */ new WeakMap(),
430
- vMap: /* @__PURE__ */ new WeakMap()
431
- });
432
- return phecda;
433
- }
434
- var activePhecda = {
435
- vProxyMap: /* @__PURE__ */ new WeakMap(),
436
- vMap: /* @__PURE__ */ new WeakMap()
437
- };
438
- function setActivePhecda(phecda) {
439
- activePhecda = phecda;
440
- }
441
- function getActivePhecda() {
442
- return activePhecda;
443
- }
444
-
445
- // src/vue/composable.ts
446
- var import_vue4 = require("vue");
447
-
448
- // src/vue/utils.ts
449
- var import_vue3 = require("vue");
450
- function isObject(o) {
451
- return Object.prototype.toString.call(o) === "[object Object]";
452
- }
453
- function mergeReactiveObjects(target, patchToApply) {
454
- for (const key in patchToApply) {
455
- if (!patchToApply.hasOwnProperty(key))
456
- continue;
457
- const subPatch = patchToApply[key];
458
- const targetValue = target[key];
459
- if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key) && !(0, import_vue3.isRef)(subPatch) && !(0, import_vue3.isReactive)(subPatch)) {
460
- target[key] = mergeReactiveObjects(targetValue, subPatch);
461
- } else {
462
- target[key] = subPatch;
463
- }
464
- }
465
- return target;
466
- }
467
-
468
- // src/vue/composable.ts
469
- function useR(Model) {
470
- if ((0, import_vue4.getCurrentInstance)()) {
471
- const cur = (0, import_vue4.inject)(phecdaSymbol, null);
472
- if (cur)
473
- setActivePhecda(cur);
474
- }
475
- const { vMap } = getActivePhecda();
476
- if (!vMap.has(Model)) {
477
- const instance = (0, import_vue4.reactive)(new Model());
478
- vMap.set(Model, instance);
479
- register(instance);
480
- }
481
- return vMap.get(Model);
482
- }
483
- function usePatch(Model, Data) {
484
- useR(Model);
485
- const { vMap } = getActivePhecda();
486
- const target = vMap.get(Model);
487
- mergeReactiveObjects(target, Data);
488
- }
489
- function useListen(eventName, cb) {
490
- (0, import_vue4.onUnmounted)(() => {
491
- emitter.off(eventName, cb);
492
- });
493
- emitter.on(eventName, cb);
494
- return () => emitter.off(eventName, cb);
495
- }
496
- function useV(Model) {
497
- useR(Model);
498
- const { vProxyMap, vMap } = getActivePhecda();
499
- if (vProxyMap.has(Model))
500
- return vProxyMap.get(Model);
501
- const instance = vMap.get(Model);
502
- const proxy = new Proxy(instance, {
503
- get(target, key) {
504
- if (typeof target[key] === "function") {
505
- const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
506
- if (!errorHandler)
507
- return target[key].bind(target);
508
- if (isAsyncFunc(target[key])) {
509
- return (...args) => {
510
- return target[key].apply(target, args).catch(errorHandler);
511
- };
512
- } else {
513
- return (...args) => {
514
- try {
515
- return target[key].apply(target, args);
516
- } catch (e) {
517
- return errorHandler(e);
518
- }
519
- };
520
- }
521
- }
522
- return (0, import_vue4.computed)({
523
- get() {
524
- return target[key];
525
- },
526
- set(v) {
527
- return target[key] = v;
528
- }
529
- });
530
- },
531
- set(target, key, v) {
532
- target[key] = v;
533
- return true;
534
- }
535
- });
536
- vProxyMap.set(Model, proxy);
537
- return proxy;
538
- }
539
- function cloneV(instance) {
540
- const newInstance = {};
541
- for (const key in instance) {
542
- if (instance.hasOwnProperty(key))
543
- newInstance[key] = instance[key];
544
- }
545
- return newInstance;
546
- }
547
- function initalize(Model) {
548
- const instance = useR(Model);
549
- if (instance) {
550
- Object.assign(instance, new Model());
551
- return instance;
552
- }
553
- }
554
- function clearStorage(Model, isForceUpdate = true) {
555
- localStorage.removeItem(`_phecda_${useR(Model)._symbol}`);
556
- isForceUpdate && initalize(Model);
557
- }
558
- function deleteStorage(tag) {
559
- localStorage.removeItem(`_phecda_${tag}`);
275
+ function addDecoToClass(c, key, handler, type = "normal") {
276
+ handler(type === "normal" ? c.prototype : c, key);
560
277
  }
561
278
  // Annotate the CommonJS export names for ESM import in node:
562
279
  0 && (module.exports = {
563
280
  Clear,
564
- EXPRESS_RE,
565
281
  Err,
566
- FN_RE,
567
282
  Get,
568
283
  Ignore,
569
284
  Init,
570
- Phecda,
571
- PhecdaWeb,
572
285
  Pipe,
573
286
  Rule,
574
287
  Storage,
575
288
  Tag,
576
- Watcher,
289
+ addDecoToClass,
577
290
  classToValue,
578
- clearStorage,
579
- cloneV,
580
- createFilter,
581
- createPhecda,
582
- deleteStorage,
583
- emit,
584
- emitter,
585
- getActivePhecda,
586
291
  getExposeKey,
587
292
  getHandler,
588
293
  getIgnoreKey,
589
294
  getInitEvent,
590
295
  getModelState,
591
- initalize,
592
- isAsyncFunc,
593
- phecdaSymbol,
296
+ init,
594
297
  plainToClass,
595
298
  regisHandler,
596
299
  regisInitEvent,
597
300
  register,
598
- setActivePhecda,
599
301
  setExposeKey,
600
302
  setIgnoreKey,
601
303
  setModalState,
602
304
  snapShot,
603
305
  to,
604
- useListen,
605
- usePatch,
606
- useR,
607
- useV,
608
306
  validate
609
307
  });
package/dist/index.mjs CHANGED
@@ -1,12 +1,8 @@
1
- // src/emitter.ts
2
- import mitt from "mitt";
3
- var emitter = mitt();
4
- var emit = emitter.emit.bind(emitter);
5
-
6
- // src/decorators.ts
1
+ // src/core.ts
7
2
  function init(target) {
8
3
  if (!target._namespace) {
9
4
  target._namespace = {
5
+ __TAG__: "",
10
6
  __INIT_EVENT__: /* @__PURE__ */ new Set(),
11
7
  __EXPOSE_VAR__: /* @__PURE__ */ new Set(),
12
8
  __IGNORE_VAR__: /* @__PURE__ */ new Set(),
@@ -38,7 +34,7 @@ function setIgnoreKey(target, key) {
38
34
  }
39
35
  function getModelState(target) {
40
36
  init(target);
41
- return [...target._namespace.__STATE_VAR__].reverse();
37
+ return [...target._namespace.__STATE_VAR__];
42
38
  }
43
39
  function getExposeKey(target) {
44
40
  init(target);
@@ -58,6 +54,16 @@ function regisHandler(target, key, handler) {
58
54
  function getHandler(target, key) {
59
55
  return target._namespace.__STATE_HANDLER__.get(key) || [];
60
56
  }
57
+ function register(instance) {
58
+ const stateVars = getModelState(instance);
59
+ for (const item of stateVars) {
60
+ const handlers = getHandler(instance, item);
61
+ for (const hanlder of handlers)
62
+ hanlder.init?.(instance);
63
+ }
64
+ }
65
+
66
+ // src/decorators.ts
61
67
  function Init(target, key) {
62
68
  setModalState(target, key);
63
69
  regisHandler(target, key, {
@@ -110,24 +116,17 @@ function Pipe(v) {
110
116
  });
111
117
  };
112
118
  }
113
- function Watcher(eventName) {
114
- return (obj, key) => {
115
- setModalState(obj, key);
116
- regisHandler(obj, key, {
117
- init(instance) {
118
- emitter.on(eventName, instance[key].bind(instance));
119
- }
120
- });
121
- };
122
- }
123
119
  function Tag(tag) {
124
120
  return (target) => {
125
- target.prototype._symbol = tag;
121
+ init(target.prototype);
122
+ target.prototype._namespace.__TAG__ = tag;
126
123
  };
127
124
  }
128
125
  function Storage(target) {
129
126
  init(target.prototype);
130
- const tag = target.prototype._symbol;
127
+ const tag = target.prototype._namespace.__TAG__;
128
+ if (tag === "")
129
+ throw new Error("miss tag");
131
130
  const uniTag = Symbol(tag);
132
131
  setModalState(target.prototype, uniTag);
133
132
  regisHandler(target.prototype, uniTag, {
@@ -146,46 +145,6 @@ function Storage(target) {
146
145
  });
147
146
  }
148
147
 
149
- // src/model.ts
150
- var Phecda = class {
151
- constructor() {
152
- if (!this._symbol)
153
- throw new Error("phecda miss tag");
154
- if (!Phecda.__PHECDA_MODEL__) {
155
- Phecda.__PHECDA_MODEL__ = [];
156
- Phecda.__PHECDA_NAMESPACE__ = {};
157
- Phecda.__PHECDA_EMIT__ = emitter;
158
- }
159
- }
160
- on(type, handler) {
161
- Phecda.__PHECDA_EMIT__.on(type, handler);
162
- }
163
- emit(type, event) {
164
- Phecda.__PHECDA_EMIT__.emit(type, event);
165
- }
166
- off(type, handler) {
167
- Phecda.__PHECDA_EMIT__.off(type, handler);
168
- }
169
- };
170
- var PhecdaWeb = class extends Phecda {
171
- constructor() {
172
- super();
173
- if (!window.__PHECDA_EMIT__) {
174
- window.__PHECDA_EMIT__ = Phecda.__PHECDA_EMIT__;
175
- window.__PHECDA_MODEL__ = Phecda.__PHECDA_MODEL__;
176
- window.__PHECDA_NAMESPACE__ = Phecda.__PHECDA_NAMESPACE__;
177
- }
178
- }
179
- };
180
- function register(instance) {
181
- const stateVars = getModelState(instance);
182
- for (const item of stateVars) {
183
- const handlers = getHandler(instance, item);
184
- for (const hanlder of handlers)
185
- hanlder.init?.(instance);
186
- }
187
- }
188
-
189
148
  // src/utils.ts
190
149
  async function validate(p, v) {
191
150
  if (typeof p === "string" || typeof p === "number") {
@@ -198,11 +157,8 @@ async function validate(p, v) {
198
157
  return p.test(v);
199
158
  return false;
200
159
  }
201
- function isAsyncFunc(fn) {
202
- return fn[Symbol.toStringTag] === "AsyncFunction";
203
- }
204
160
 
205
- // src/composable.ts
161
+ // src/helper.ts
206
162
  async function plainToClass(Model, input, options = {}) {
207
163
  const data = new Model();
208
164
  const resolvedOptions = {
@@ -264,268 +220,35 @@ function snapShot(data) {
264
220
  }
265
221
  };
266
222
  }
267
-
268
- // src/filter.ts
269
- import { reactive, ref } from "vue";
270
- var EXPRESS_RE = /^{{(.*)}}$/;
271
- var FN_RE = /^\[\[(.*)\]\]$/;
272
- function createFilter(initState = {}, option = {}) {
273
- const resolveOption = Object.assign(
274
- {
275
- expressionRE: EXPRESS_RE,
276
- fnRE: FN_RE,
277
- exclude: []
278
- },
279
- option
280
- );
281
- let data = ref(initState);
282
- let store = {};
283
- function traverse(obj) {
284
- for (const i in obj) {
285
- if (Array.isArray(obj[i]) || resolveOption.exclude.includes(i))
286
- continue;
287
- if (typeof obj[i] === "object" && obj[i])
288
- traverse(obj[i]);
289
- if (typeof obj[i] === "string") {
290
- if (resolveOption.expressionRE.test(obj[i])) {
291
- const body = obj[i].match(resolveOption.expressionRE)[1];
292
- Object.defineProperty(obj, i, {
293
- get() {
294
- return new Function(...Object.keys(data.value), `return ${body}`)(
295
- ...Object.values(data.value)
296
- );
297
- }
298
- });
299
- }
300
- if (resolveOption.fnRE.test(obj[i])) {
301
- const body = obj[i].match(resolveOption.fnRE)[1];
302
- Object.defineProperty(obj, i, {
303
- get() {
304
- return new Function(...Object.keys(data.value), `${body}`)(
305
- ...Object.values(data.value)
306
- );
307
- }
308
- });
309
- }
310
- }
311
- }
312
- }
313
- function filter(obj) {
314
- obj = reactive(obj);
315
- traverse(obj);
316
- return obj;
317
- }
318
- function setState(key, value) {
319
- data.value[key] = value;
320
- }
321
- function storeState(key, params) {
322
- store[key] = data.value;
323
- init2(params);
324
- }
325
- function applyStore(key) {
326
- if (!store[key])
327
- return;
328
- data.value = store[key];
329
- }
330
- function init2(params) {
331
- data.value = params || initState || {};
332
- }
333
- function clearStore(key) {
334
- delete store[key];
335
- }
336
- function dispose() {
337
- data = null;
338
- store = null;
339
- }
340
- return { filter, data, init: init2, setState, storeState, store, applyStore, dispose, clearStore };
341
- }
342
-
343
- // src/vue/phecda.ts
344
- import { markRaw } from "vue";
345
- var phecdaSymbol = Symbol("phecda");
346
- function createPhecda() {
347
- const phecda = markRaw({
348
- install(app) {
349
- app.provide(phecdaSymbol, phecda);
350
- app.config.globalProperties.$phecda = phecda;
351
- },
352
- vProxyMap: /* @__PURE__ */ new WeakMap(),
353
- vMap: /* @__PURE__ */ new WeakMap()
354
- });
355
- return phecda;
356
- }
357
- var activePhecda = {
358
- vProxyMap: /* @__PURE__ */ new WeakMap(),
359
- vMap: /* @__PURE__ */ new WeakMap()
360
- };
361
- function setActivePhecda(phecda) {
362
- activePhecda = phecda;
363
- }
364
- function getActivePhecda() {
365
- return activePhecda;
366
- }
367
-
368
- // src/vue/composable.ts
369
- import { computed, getCurrentInstance, inject, onUnmounted, reactive as reactive2 } from "vue";
370
-
371
- // src/vue/utils.ts
372
- import { isReactive, isRef } from "vue";
373
- function isObject(o) {
374
- return Object.prototype.toString.call(o) === "[object Object]";
375
- }
376
- function mergeReactiveObjects(target, patchToApply) {
377
- for (const key in patchToApply) {
378
- if (!patchToApply.hasOwnProperty(key))
379
- continue;
380
- const subPatch = patchToApply[key];
381
- const targetValue = target[key];
382
- if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key) && !isRef(subPatch) && !isReactive(subPatch)) {
383
- target[key] = mergeReactiveObjects(targetValue, subPatch);
384
- } else {
385
- target[key] = subPatch;
386
- }
387
- }
388
- return target;
389
- }
390
-
391
- // src/vue/composable.ts
392
- function useR(Model) {
393
- if (getCurrentInstance()) {
394
- const cur = inject(phecdaSymbol, null);
395
- if (cur)
396
- setActivePhecda(cur);
397
- }
398
- const { vMap } = getActivePhecda();
399
- if (!vMap.has(Model)) {
400
- const instance = reactive2(new Model());
401
- vMap.set(Model, instance);
402
- register(instance);
403
- }
404
- return vMap.get(Model);
405
- }
406
- function usePatch(Model, Data) {
407
- useR(Model);
408
- const { vMap } = getActivePhecda();
409
- const target = vMap.get(Model);
410
- mergeReactiveObjects(target, Data);
411
- }
412
- function useListen(eventName, cb) {
413
- onUnmounted(() => {
414
- emitter.off(eventName, cb);
415
- });
416
- emitter.on(eventName, cb);
417
- return () => emitter.off(eventName, cb);
418
- }
419
- function useV(Model) {
420
- useR(Model);
421
- const { vProxyMap, vMap } = getActivePhecda();
422
- if (vProxyMap.has(Model))
423
- return vProxyMap.get(Model);
424
- const instance = vMap.get(Model);
425
- const proxy = new Proxy(instance, {
426
- get(target, key) {
427
- if (typeof target[key] === "function") {
428
- const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
429
- if (!errorHandler)
430
- return target[key].bind(target);
431
- if (isAsyncFunc(target[key])) {
432
- return (...args) => {
433
- return target[key].apply(target, args).catch(errorHandler);
434
- };
435
- } else {
436
- return (...args) => {
437
- try {
438
- return target[key].apply(target, args);
439
- } catch (e) {
440
- return errorHandler(e);
441
- }
442
- };
443
- }
444
- }
445
- return computed({
446
- get() {
447
- return target[key];
448
- },
449
- set(v) {
450
- return target[key] = v;
451
- }
452
- });
453
- },
454
- set(target, key, v) {
455
- target[key] = v;
456
- return true;
457
- }
458
- });
459
- vProxyMap.set(Model, proxy);
460
- return proxy;
461
- }
462
- function cloneV(instance) {
463
- const newInstance = {};
464
- for (const key in instance) {
465
- if (instance.hasOwnProperty(key))
466
- newInstance[key] = instance[key];
467
- }
468
- return newInstance;
469
- }
470
- function initalize(Model) {
471
- const instance = useR(Model);
472
- if (instance) {
473
- Object.assign(instance, new Model());
474
- return instance;
475
- }
476
- }
477
- function clearStorage(Model, isForceUpdate = true) {
478
- localStorage.removeItem(`_phecda_${useR(Model)._symbol}`);
479
- isForceUpdate && initalize(Model);
480
- }
481
- function deleteStorage(tag) {
482
- localStorage.removeItem(`_phecda_${tag}`);
223
+ function addDecoToClass(c, key, handler, type = "normal") {
224
+ handler(type === "normal" ? c.prototype : c, key);
483
225
  }
484
226
  export {
485
227
  Clear,
486
- EXPRESS_RE,
487
228
  Err,
488
- FN_RE,
489
229
  Get,
490
230
  Ignore,
491
231
  Init,
492
- Phecda,
493
- PhecdaWeb,
494
232
  Pipe,
495
233
  Rule,
496
234
  Storage,
497
235
  Tag,
498
- Watcher,
236
+ addDecoToClass,
499
237
  classToValue,
500
- clearStorage,
501
- cloneV,
502
- createFilter,
503
- createPhecda,
504
- deleteStorage,
505
- emit,
506
- emitter,
507
- getActivePhecda,
508
238
  getExposeKey,
509
239
  getHandler,
510
240
  getIgnoreKey,
511
241
  getInitEvent,
512
242
  getModelState,
513
- initalize,
514
- isAsyncFunc,
515
- phecdaSymbol,
243
+ init,
516
244
  plainToClass,
517
245
  regisHandler,
518
246
  regisInitEvent,
519
247
  register,
520
- setActivePhecda,
521
248
  setExposeKey,
522
249
  setIgnoreKey,
523
250
  setModalState,
524
251
  snapShot,
525
252
  to,
526
- useListen,
527
- usePatch,
528
- useR,
529
- useV,
530
253
  validate
531
254
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -11,10 +11,6 @@
11
11
  "dist"
12
12
  ],
13
13
  "license": "MIT",
14
- "dependencies": {
15
- "mitt": "^3.0.0",
16
- "vue": "^3.2.45"
17
- },
18
14
  "devDependencies": {
19
15
  "tsup": "^6.5.0"
20
16
  },