phecda-core 3.0.0 → 3.0.2
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.mts +137 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.js +141 -97
- package/dist/index.mjs +139 -97
- package/package.json +8 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
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
|
+
declare abstract class Base {
|
|
124
|
+
private readonly __UNMOUNT_SYMBOL__;
|
|
125
|
+
private readonly __PROMISE_SYMBOL__;
|
|
126
|
+
abstract emitter: any;
|
|
127
|
+
constructor();
|
|
128
|
+
get tag(): PropertyKey;
|
|
129
|
+
then(cb: () => void, reject?: (e: any) => void): Promise<void>;
|
|
130
|
+
on<Key extends keyof Events>(type: Key, handler: (arg: Events[Key]) => void): void;
|
|
131
|
+
emit<Key extends keyof Events>(type: Key, param: Events[Key]): void;
|
|
132
|
+
off<Key extends keyof Events>(type: Key, handler?: (arg: Events[Key]) => void): void;
|
|
133
|
+
protected onUnmount(cb: () => void): void;
|
|
134
|
+
private _unmount;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export { type AbConstruct, Assign, Base, 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,18 @@ 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
|
-
|
|
123
|
+
declare abstract class Base {
|
|
124
|
+
private readonly __UNMOUNT_SYMBOL__;
|
|
125
|
+
private readonly __PROMISE_SYMBOL__;
|
|
126
|
+
abstract emitter: any;
|
|
127
|
+
constructor();
|
|
128
|
+
get tag(): PropertyKey;
|
|
129
|
+
then(cb: () => void, reject?: (e: any) => void): Promise<void>;
|
|
130
|
+
on<Key extends keyof Events>(type: Key, handler: (arg: Events[Key]) => void): void;
|
|
131
|
+
emit<Key extends keyof Events>(type: Key, param: Events[Key]): void;
|
|
132
|
+
off<Key extends keyof Events>(type: Key, handler?: (arg: Events[Key]) => void): void;
|
|
133
|
+
protected onUnmount(cb: () => void): void;
|
|
134
|
+
private _unmount;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export { type AbConstruct, Assign, Base, 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
|
@@ -22,6 +22,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
22
22
|
var src_exports = {};
|
|
23
23
|
__export(src_exports, {
|
|
24
24
|
Assign: () => Assign,
|
|
25
|
+
Base: () => Base,
|
|
25
26
|
Bind: () => Bind,
|
|
26
27
|
Clear: () => Clear,
|
|
27
28
|
DataMap: () => DataMap,
|
|
@@ -30,6 +31,7 @@ __export(src_exports, {
|
|
|
30
31
|
Err: () => Err,
|
|
31
32
|
Expose: () => Expose,
|
|
32
33
|
Global: () => Global,
|
|
34
|
+
If: () => If,
|
|
33
35
|
Ignore: () => Ignore,
|
|
34
36
|
Init: () => Init,
|
|
35
37
|
Inject: () => Inject,
|
|
@@ -88,31 +90,49 @@ module.exports = __toCommonJS(src_exports);
|
|
|
88
90
|
var SHARE_KEY = Symbol("phecda");
|
|
89
91
|
var PHECDA_KEY = Symbol("phecda");
|
|
90
92
|
function isPhecda(model) {
|
|
91
|
-
if (typeof model === "function")
|
|
92
|
-
return !!model.prototype[PHECDA_KEY];
|
|
93
|
+
if (typeof model === "function") return !!model.prototype[PHECDA_KEY];
|
|
93
94
|
return false;
|
|
94
95
|
}
|
|
95
96
|
__name(isPhecda, "isPhecda");
|
|
96
97
|
function init(proto) {
|
|
97
|
-
if (!proto)
|
|
98
|
-
return;
|
|
98
|
+
if (!proto) return;
|
|
99
99
|
if (!proto.hasOwnProperty(PHECDA_KEY)) {
|
|
100
100
|
proto[PHECDA_KEY] = {
|
|
101
|
+
/**
|
|
102
|
+
* 暴露的变量,
|
|
103
|
+
* 只要属性上存在至少一个装饰器,该属性就会被捕捉到
|
|
104
|
+
*/
|
|
101
105
|
__EXPOSE_KEY: /* @__PURE__ */ new Set(),
|
|
106
|
+
/**
|
|
107
|
+
* @Ignore 绑定的属性,
|
|
108
|
+
* 某属性即使被捕捉,可被强行忽略,优先级最高
|
|
109
|
+
*/
|
|
102
110
|
__IGNORE_KEY: /* @__PURE__ */ new Set(),
|
|
111
|
+
/**
|
|
112
|
+
* @Clear 绑定的属性,
|
|
113
|
+
* 消除父类在该key上的state/handler, 但export key 和 state
|
|
114
|
+
*/
|
|
103
115
|
__CLEAR_KEY: /* @__PURE__ */ new Set(),
|
|
116
|
+
/**
|
|
117
|
+
* 存在状态的变量
|
|
118
|
+
* @deprecated
|
|
119
|
+
*/
|
|
104
120
|
__STATE_KEY: /* @__PURE__ */ new Set(),
|
|
121
|
+
/**
|
|
122
|
+
* 状态变量的处理器
|
|
123
|
+
*/
|
|
105
124
|
__STATE_HANDLER__: /* @__PURE__ */ new Map(),
|
|
125
|
+
/**
|
|
126
|
+
* 状态变量的共有状态
|
|
127
|
+
*/
|
|
106
128
|
__STATE_NAMESPACE__: /* @__PURE__ */ new Map()
|
|
107
129
|
};
|
|
108
130
|
}
|
|
109
131
|
}
|
|
110
132
|
__name(init, "init");
|
|
111
133
|
function getPhecdaFromTarget(target) {
|
|
112
|
-
if (typeof target === "function")
|
|
113
|
-
|
|
114
|
-
if (target.hasOwnProperty(PHECDA_KEY))
|
|
115
|
-
return target;
|
|
134
|
+
if (typeof target === "function") return target.prototype;
|
|
135
|
+
if (target.hasOwnProperty(PHECDA_KEY)) return target;
|
|
116
136
|
return Object.getPrototypeOf(target);
|
|
117
137
|
}
|
|
118
138
|
__name(getPhecdaFromTarget, "getPhecdaFromTarget");
|
|
@@ -150,12 +170,10 @@ function setHandler(proto, key, handler) {
|
|
|
150
170
|
proto = proto.prototype;
|
|
151
171
|
}
|
|
152
172
|
init(proto);
|
|
153
|
-
if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key))
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
else
|
|
158
|
-
proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
|
|
173
|
+
if (!proto[PHECDA_KEY].__STATE_HANDLER__.has(key)) proto[PHECDA_KEY].__STATE_HANDLER__.set(key, [
|
|
174
|
+
handler
|
|
175
|
+
]);
|
|
176
|
+
else proto[PHECDA_KEY].__STATE_HANDLER__.get(key).push(handler);
|
|
159
177
|
}
|
|
160
178
|
__name(setHandler, "setHandler");
|
|
161
179
|
function setState(proto, key, state) {
|
|
@@ -179,8 +197,7 @@ function getStateKey(target) {
|
|
|
179
197
|
let proto = getPhecdaFromTarget(target);
|
|
180
198
|
const set2 = /* @__PURE__ */ new Set();
|
|
181
199
|
while (proto?.[PHECDA_KEY]) {
|
|
182
|
-
if (proto.hasOwnProperty(PHECDA_KEY))
|
|
183
|
-
proto[PHECDA_KEY].__STATE_KEY.forEach((item) => set2.add(item));
|
|
200
|
+
if (proto.hasOwnProperty(PHECDA_KEY)) proto[PHECDA_KEY].__STATE_KEY.forEach((item) => set2.add(item));
|
|
184
201
|
proto = Object.getPrototypeOf(proto);
|
|
185
202
|
}
|
|
186
203
|
return [
|
|
@@ -200,10 +217,9 @@ function getExposeKey(target) {
|
|
|
200
217
|
const set2 = /* @__PURE__ */ new Set();
|
|
201
218
|
const origin = proto;
|
|
202
219
|
while (proto?.[PHECDA_KEY]) {
|
|
203
|
-
if (proto.hasOwnProperty(PHECDA_KEY))
|
|
204
|
-
[
|
|
205
|
-
|
|
206
|
-
].forEach((item) => !origin[PHECDA_KEY].__IGNORE_KEY.has(item) && set2.add(item));
|
|
220
|
+
if (proto.hasOwnProperty(PHECDA_KEY)) [
|
|
221
|
+
...proto[PHECDA_KEY].__EXPOSE_KEY
|
|
222
|
+
].forEach((item) => !origin[PHECDA_KEY].__IGNORE_KEY.has(item) && set2.add(item));
|
|
207
223
|
proto = Object.getPrototypeOf(proto);
|
|
208
224
|
}
|
|
209
225
|
return [
|
|
@@ -229,8 +245,7 @@ function getHandler(target, key) {
|
|
|
229
245
|
while (proto?.[PHECDA_KEY]) {
|
|
230
246
|
if (proto.hasOwnProperty(PHECDA_KEY)) {
|
|
231
247
|
proto[PHECDA_KEY].__STATE_HANDLER__.get(key)?.forEach((item) => set2.add(item));
|
|
232
|
-
if (proto[PHECDA_KEY].__CLEAR_KEY.has(key))
|
|
233
|
-
break;
|
|
248
|
+
if (proto[PHECDA_KEY].__CLEAR_KEY.has(key)) break;
|
|
234
249
|
}
|
|
235
250
|
proto = Object.getPrototypeOf(proto);
|
|
236
251
|
}
|
|
@@ -245,13 +260,11 @@ function getState(target, key = SHARE_KEY) {
|
|
|
245
260
|
while (proto?.[PHECDA_KEY]) {
|
|
246
261
|
if (proto.hasOwnProperty(PHECDA_KEY)) {
|
|
247
262
|
const state = proto[PHECDA_KEY].__STATE_NAMESPACE__.get(key);
|
|
248
|
-
if (state)
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (proto[PHECDA_KEY].__CLEAR_KEY.has(key))
|
|
254
|
-
break;
|
|
263
|
+
if (state) ret = {
|
|
264
|
+
...state,
|
|
265
|
+
...ret
|
|
266
|
+
};
|
|
267
|
+
if (proto[PHECDA_KEY].__CLEAR_KEY.has(key)) break;
|
|
255
268
|
}
|
|
256
269
|
proto = Object.getPrototypeOf(proto);
|
|
257
270
|
}
|
|
@@ -337,8 +350,7 @@ __name(Empty, "Empty");
|
|
|
337
350
|
|
|
338
351
|
// src/helper.ts
|
|
339
352
|
function getTag(moduleOrInstance) {
|
|
340
|
-
if (typeof moduleOrInstance === "object")
|
|
341
|
-
moduleOrInstance = moduleOrInstance.constructor;
|
|
353
|
+
if (typeof moduleOrInstance === "object") moduleOrInstance = moduleOrInstance.constructor;
|
|
342
354
|
return get(moduleOrInstance.prototype, "tag") || moduleOrInstance.name;
|
|
343
355
|
}
|
|
344
356
|
__name(getTag, "getTag");
|
|
@@ -348,8 +360,7 @@ function getBind(model) {
|
|
|
348
360
|
const ret = {};
|
|
349
361
|
for (const item of keys) {
|
|
350
362
|
const state = getState(instance, item);
|
|
351
|
-
if (state.value)
|
|
352
|
-
ret[item] = state.value;
|
|
363
|
+
if (state.value) ret[item] = state.value;
|
|
353
364
|
}
|
|
354
365
|
return ret;
|
|
355
366
|
}
|
|
@@ -357,8 +368,7 @@ __name(getBind, "getBind");
|
|
|
357
368
|
function plainToClass(model, input) {
|
|
358
369
|
const instance = new model();
|
|
359
370
|
const keys = getExposeKey(instance);
|
|
360
|
-
for (const item of keys)
|
|
361
|
-
instance[item] = input[item];
|
|
371
|
+
for (const item of keys) instance[item] = input[item];
|
|
362
372
|
return instance;
|
|
363
373
|
}
|
|
364
374
|
__name(plainToClass, "plainToClass");
|
|
@@ -371,11 +381,9 @@ function transformInstance(instance, force = false) {
|
|
|
371
381
|
if (handlers) {
|
|
372
382
|
for (const handler of handlers) {
|
|
373
383
|
const pipe = handler.pipe;
|
|
374
|
-
if (!pipe)
|
|
375
|
-
continue;
|
|
384
|
+
if (!pipe) continue;
|
|
376
385
|
pipe(instance, addError);
|
|
377
|
-
if (err.length && !force)
|
|
378
|
-
return err;
|
|
386
|
+
if (err.length && !force) return err;
|
|
379
387
|
}
|
|
380
388
|
}
|
|
381
389
|
}
|
|
@@ -391,11 +399,9 @@ async function transformInstanceAsync(instance, force = false) {
|
|
|
391
399
|
if (handlers) {
|
|
392
400
|
for (const handler of handlers) {
|
|
393
401
|
const pipe = handler.pipe;
|
|
394
|
-
if (!pipe)
|
|
395
|
-
continue;
|
|
402
|
+
if (!pipe) continue;
|
|
396
403
|
await pipe(instance, addError);
|
|
397
|
-
if (err.length && !force)
|
|
398
|
-
return err;
|
|
404
|
+
if (err.length && !force) return err;
|
|
399
405
|
}
|
|
400
406
|
}
|
|
401
407
|
}
|
|
@@ -409,11 +415,9 @@ function transformProperty(instance, property, force = false) {
|
|
|
409
415
|
if (handlers) {
|
|
410
416
|
for (const handler of handlers) {
|
|
411
417
|
const pipe = handler.pipe;
|
|
412
|
-
if (!pipe)
|
|
413
|
-
continue;
|
|
418
|
+
if (!pipe) continue;
|
|
414
419
|
pipe(instance, addError);
|
|
415
|
-
if (err.length && !force)
|
|
416
|
-
return err;
|
|
420
|
+
if (err.length && !force) return err;
|
|
417
421
|
}
|
|
418
422
|
}
|
|
419
423
|
return err;
|
|
@@ -426,11 +430,9 @@ async function transformPropertyAsync(instance, property, force = false) {
|
|
|
426
430
|
if (handlers) {
|
|
427
431
|
for (const handler of handlers) {
|
|
428
432
|
const pipe = handler.pipe;
|
|
429
|
-
if (!pipe)
|
|
430
|
-
continue;
|
|
433
|
+
if (!pipe) continue;
|
|
431
434
|
await pipe(instance, addError);
|
|
432
|
-
if (err.length && !force)
|
|
433
|
-
return err;
|
|
435
|
+
if (err.length && !force) return err;
|
|
434
436
|
}
|
|
435
437
|
}
|
|
436
438
|
return err;
|
|
@@ -439,24 +441,20 @@ __name(transformPropertyAsync, "transformPropertyAsync");
|
|
|
439
441
|
function classToPlain(instance) {
|
|
440
442
|
const data = {};
|
|
441
443
|
const exposeVars = getExposeKey(instance);
|
|
442
|
-
for (const item of exposeVars)
|
|
443
|
-
data[item] = instance[item];
|
|
444
|
+
for (const item of exposeVars) data[item] = instance[item];
|
|
444
445
|
return JSON.parse(JSON.stringify(data));
|
|
445
446
|
}
|
|
446
447
|
__name(classToPlain, "classToPlain");
|
|
447
448
|
function snapShot(data) {
|
|
448
449
|
const snap = {};
|
|
449
|
-
for (const i in data)
|
|
450
|
-
snap[i] = data[i];
|
|
450
|
+
for (const i in data) snap[i] = data[i];
|
|
451
451
|
return {
|
|
452
452
|
data,
|
|
453
453
|
clear() {
|
|
454
|
-
for (const i in snap)
|
|
455
|
-
delete data[i];
|
|
454
|
+
for (const i in snap) delete data[i];
|
|
456
455
|
},
|
|
457
456
|
apply() {
|
|
458
|
-
for (const i in snap)
|
|
459
|
-
data[i] = snap[i];
|
|
457
|
+
for (const i in snap) data[i] = snap[i];
|
|
460
458
|
}
|
|
461
459
|
};
|
|
462
460
|
}
|
|
@@ -467,8 +465,7 @@ function addDecoToClass(c, key, handler) {
|
|
|
467
465
|
__name(addDecoToClass, "addDecoToClass");
|
|
468
466
|
function Pipeline(...decos) {
|
|
469
467
|
return (...args) => {
|
|
470
|
-
for (const d of decos)
|
|
471
|
-
d(...args);
|
|
468
|
+
for (const d of decos) d(...args);
|
|
472
469
|
};
|
|
473
470
|
}
|
|
474
471
|
__name(Pipeline, "Pipeline");
|
|
@@ -513,7 +510,6 @@ __name(getInject, "getInject");
|
|
|
513
510
|
// src/decorators/function.ts
|
|
514
511
|
function Isolate(model) {
|
|
515
512
|
set(model.prototype, "isolate", true);
|
|
516
|
-
model.prototype[PHECDA_KEY].__ISOLATE__ = true;
|
|
517
513
|
}
|
|
518
514
|
__name(Isolate, "Isolate");
|
|
519
515
|
function Tag(tag) {
|
|
@@ -532,13 +528,12 @@ function Assign(cb) {
|
|
|
532
528
|
return (model) => {
|
|
533
529
|
setStateKey(model);
|
|
534
530
|
setHandler(model, void 0, {
|
|
535
|
-
init: async (instance) => {
|
|
531
|
+
init: /* @__PURE__ */ __name(async (instance) => {
|
|
536
532
|
const value = await cb(instance);
|
|
537
533
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
538
|
-
for (const i in value)
|
|
539
|
-
instance[i] = value[i];
|
|
534
|
+
for (const i in value) instance[i] = value[i];
|
|
540
535
|
}
|
|
541
|
-
}
|
|
536
|
+
}, "init")
|
|
542
537
|
});
|
|
543
538
|
};
|
|
544
539
|
}
|
|
@@ -546,14 +541,11 @@ __name(Assign, "Assign");
|
|
|
546
541
|
function Global(model) {
|
|
547
542
|
setStateKey(model);
|
|
548
543
|
setHandler(model, void 0, {
|
|
549
|
-
init: async (instance) => {
|
|
550
|
-
const tag = instance
|
|
551
|
-
if (!
|
|
552
|
-
return;
|
|
553
|
-
if (!globalThis.__PHECDA__)
|
|
554
|
-
globalThis.__PHECDA__ = {};
|
|
544
|
+
init: /* @__PURE__ */ __name(async (instance) => {
|
|
545
|
+
const tag = getTag(instance);
|
|
546
|
+
if (!globalThis.__PHECDA__) globalThis.__PHECDA__ = {};
|
|
555
547
|
globalThis.__PHECDA__[tag] = instance.constructor;
|
|
556
|
-
}
|
|
548
|
+
}, "init")
|
|
557
549
|
});
|
|
558
550
|
}
|
|
559
551
|
__name(Global, "Global");
|
|
@@ -564,10 +556,8 @@ function To(...callbacks) {
|
|
|
564
556
|
async pipe(instance, addError) {
|
|
565
557
|
for (const cb of callbacks) {
|
|
566
558
|
try {
|
|
567
|
-
if (isAsyncFunc(cb))
|
|
568
|
-
|
|
569
|
-
else
|
|
570
|
-
instance[key] = cb(instance[key], instance, key);
|
|
559
|
+
if (isAsyncFunc(cb)) instance[key] = await cb(instance[key], instance, key);
|
|
560
|
+
else instance[key] = cb(instance[key], instance, key);
|
|
571
561
|
} catch (e) {
|
|
572
562
|
addError(e.message);
|
|
573
563
|
}
|
|
@@ -583,15 +573,11 @@ function Rule(cb, info) {
|
|
|
583
573
|
setHandler(proto, key, {
|
|
584
574
|
async pipe(instance, addError) {
|
|
585
575
|
let ret;
|
|
586
|
-
if (isAsyncFunc(cb))
|
|
587
|
-
|
|
588
|
-
else
|
|
589
|
-
ret = cb(instance[key]);
|
|
576
|
+
if (isAsyncFunc(cb)) ret = await cb(instance[key]);
|
|
577
|
+
else ret = cb(instance[key]);
|
|
590
578
|
if (!ret) {
|
|
591
|
-
if (typeof info === "string")
|
|
592
|
-
|
|
593
|
-
else
|
|
594
|
-
addError(info());
|
|
579
|
+
if (typeof info === "string") addError(info);
|
|
580
|
+
else addError(info());
|
|
595
581
|
}
|
|
596
582
|
}
|
|
597
583
|
});
|
|
@@ -602,7 +588,7 @@ function Err(cb, isCatch = false) {
|
|
|
602
588
|
return (proto, key) => {
|
|
603
589
|
setStateKey(proto, key);
|
|
604
590
|
setHandler(proto, key, {
|
|
605
|
-
init: (instance) => {
|
|
591
|
+
init: /* @__PURE__ */ __name((instance) => {
|
|
606
592
|
if (typeof instance[key] === "function") {
|
|
607
593
|
const oldFn = instance[key].bind(instance);
|
|
608
594
|
if (isAsyncFunc(oldFn)) {
|
|
@@ -611,8 +597,7 @@ function Err(cb, isCatch = false) {
|
|
|
611
597
|
await oldFn(...args);
|
|
612
598
|
} catch (e) {
|
|
613
599
|
cb(e, instance, key);
|
|
614
|
-
if (!isCatch)
|
|
615
|
-
throw e;
|
|
600
|
+
if (!isCatch) throw e;
|
|
616
601
|
}
|
|
617
602
|
};
|
|
618
603
|
} else {
|
|
@@ -621,13 +606,12 @@ function Err(cb, isCatch = false) {
|
|
|
621
606
|
oldFn(...args);
|
|
622
607
|
} catch (e) {
|
|
623
608
|
cb(e, instance, key);
|
|
624
|
-
if (!isCatch)
|
|
625
|
-
throw e;
|
|
609
|
+
if (!isCatch) throw e;
|
|
626
610
|
}
|
|
627
611
|
};
|
|
628
612
|
}
|
|
629
613
|
}
|
|
630
|
-
}
|
|
614
|
+
}, "init")
|
|
631
615
|
});
|
|
632
616
|
};
|
|
633
617
|
}
|
|
@@ -674,16 +658,14 @@ function Effect(cb) {
|
|
|
674
658
|
}
|
|
675
659
|
__name(Effect, "Effect");
|
|
676
660
|
function Storage({ key: storeKey, json, stringify } = {}) {
|
|
677
|
-
if (!json)
|
|
678
|
-
|
|
679
|
-
if (!stringify)
|
|
680
|
-
stringify = /* @__PURE__ */ __name((v) => JSON.stringify(v), "stringify");
|
|
661
|
+
if (!json) json = /* @__PURE__ */ __name((v) => JSON.parse(v), "json");
|
|
662
|
+
if (!stringify) stringify = /* @__PURE__ */ __name((v) => JSON.stringify(v), "stringify");
|
|
681
663
|
return (proto, key) => {
|
|
682
664
|
const tag = storeKey || getTag(proto);
|
|
683
665
|
init(proto);
|
|
684
666
|
setStateKey(proto, key);
|
|
685
667
|
setHandler(proto, key, {
|
|
686
|
-
init: (instance) => {
|
|
668
|
+
init: /* @__PURE__ */ __name((instance) => {
|
|
687
669
|
return getInject("storage")?.({
|
|
688
670
|
instance,
|
|
689
671
|
key,
|
|
@@ -691,14 +673,75 @@ function Storage({ key: storeKey, json, stringify } = {}) {
|
|
|
691
673
|
toJSON: json,
|
|
692
674
|
toString: stringify
|
|
693
675
|
});
|
|
694
|
-
}
|
|
676
|
+
}, "init")
|
|
695
677
|
});
|
|
696
678
|
};
|
|
697
679
|
}
|
|
698
680
|
__name(Storage, "Storage");
|
|
681
|
+
function If(value, ...decorators) {
|
|
682
|
+
if (value) {
|
|
683
|
+
return (...args) => {
|
|
684
|
+
decorators.forEach((d) => d(...args));
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
return () => {
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
__name(If, "If");
|
|
691
|
+
|
|
692
|
+
// src/base.ts
|
|
693
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
694
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
695
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
696
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
697
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
698
|
+
}
|
|
699
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
700
|
+
function _ts_metadata(k, v) {
|
|
701
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
702
|
+
}
|
|
703
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
704
|
+
var Base = class {
|
|
705
|
+
static {
|
|
706
|
+
__name(this, "Base");
|
|
707
|
+
}
|
|
708
|
+
__UNMOUNT_SYMBOL__ = [];
|
|
709
|
+
__PROMISE_SYMBOL__;
|
|
710
|
+
constructor() {
|
|
711
|
+
}
|
|
712
|
+
get tag() {
|
|
713
|
+
return getTag(this);
|
|
714
|
+
}
|
|
715
|
+
then(cb, reject) {
|
|
716
|
+
return this.__PROMISE_SYMBOL__.then(cb, reject);
|
|
717
|
+
}
|
|
718
|
+
on(type, handler) {
|
|
719
|
+
this.emitter.on(type, handler);
|
|
720
|
+
this.onUnmount(() => this.emitter.off(type, handler));
|
|
721
|
+
}
|
|
722
|
+
emit(type, param) {
|
|
723
|
+
this.emitter.emit(type, param);
|
|
724
|
+
}
|
|
725
|
+
off(type, handler) {
|
|
726
|
+
this.emitter.off(type, handler);
|
|
727
|
+
}
|
|
728
|
+
onUnmount(cb) {
|
|
729
|
+
this.__UNMOUNT_SYMBOL__.push(cb);
|
|
730
|
+
}
|
|
731
|
+
_unmount() {
|
|
732
|
+
return Promise.all(this.__UNMOUNT_SYMBOL__.map((fn) => fn()));
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
_ts_decorate([
|
|
736
|
+
Unmount,
|
|
737
|
+
_ts_metadata("design:type", Function),
|
|
738
|
+
_ts_metadata("design:paramtypes", []),
|
|
739
|
+
_ts_metadata("design:returntype", void 0)
|
|
740
|
+
], Base.prototype, "_unmount", null);
|
|
699
741
|
// Annotate the CommonJS export names for ESM import in node:
|
|
700
742
|
0 && (module.exports = {
|
|
701
743
|
Assign,
|
|
744
|
+
Base,
|
|
702
745
|
Bind,
|
|
703
746
|
Clear,
|
|
704
747
|
DataMap,
|
|
@@ -707,6 +750,7 @@ __name(Storage, "Storage");
|
|
|
707
750
|
Err,
|
|
708
751
|
Expose,
|
|
709
752
|
Global,
|
|
753
|
+
If,
|
|
710
754
|
Ignore,
|
|
711
755
|
Init,
|
|
712
756
|
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
|
-
|
|
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
|
-
|
|
72
|
-
|
|
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
|
-
|
|
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
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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
|
|
468
|
-
if (!
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,13 +588,74 @@ 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");
|
|
606
|
+
|
|
607
|
+
// src/base.ts
|
|
608
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
609
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
610
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
611
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
612
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
613
|
+
}
|
|
614
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
615
|
+
function _ts_metadata(k, v) {
|
|
616
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
617
|
+
}
|
|
618
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
619
|
+
var Base = class {
|
|
620
|
+
static {
|
|
621
|
+
__name(this, "Base");
|
|
622
|
+
}
|
|
623
|
+
__UNMOUNT_SYMBOL__ = [];
|
|
624
|
+
__PROMISE_SYMBOL__;
|
|
625
|
+
constructor() {
|
|
626
|
+
}
|
|
627
|
+
get tag() {
|
|
628
|
+
return getTag(this);
|
|
629
|
+
}
|
|
630
|
+
then(cb, reject) {
|
|
631
|
+
return this.__PROMISE_SYMBOL__.then(cb, reject);
|
|
632
|
+
}
|
|
633
|
+
on(type, handler) {
|
|
634
|
+
this.emitter.on(type, handler);
|
|
635
|
+
this.onUnmount(() => this.emitter.off(type, handler));
|
|
636
|
+
}
|
|
637
|
+
emit(type, param) {
|
|
638
|
+
this.emitter.emit(type, param);
|
|
639
|
+
}
|
|
640
|
+
off(type, handler) {
|
|
641
|
+
this.emitter.off(type, handler);
|
|
642
|
+
}
|
|
643
|
+
onUnmount(cb) {
|
|
644
|
+
this.__UNMOUNT_SYMBOL__.push(cb);
|
|
645
|
+
}
|
|
646
|
+
_unmount() {
|
|
647
|
+
return Promise.all(this.__UNMOUNT_SYMBOL__.map((fn) => fn()));
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
_ts_decorate([
|
|
651
|
+
Unmount,
|
|
652
|
+
_ts_metadata("design:type", Function),
|
|
653
|
+
_ts_metadata("design:paramtypes", []),
|
|
654
|
+
_ts_metadata("design:returntype", void 0)
|
|
655
|
+
], Base.prototype, "_unmount", null);
|
|
616
656
|
export {
|
|
617
657
|
Assign,
|
|
658
|
+
Base,
|
|
618
659
|
Bind,
|
|
619
660
|
Clear,
|
|
620
661
|
DataMap,
|
|
@@ -623,6 +664,7 @@ export {
|
|
|
623
664
|
Err,
|
|
624
665
|
Expose,
|
|
625
666
|
Global,
|
|
667
|
+
If,
|
|
626
668
|
Ignore,
|
|
627
669
|
Init,
|
|
628
670
|
Inject,
|
package/package.json
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phecda-core",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
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
|
-
|
|
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": "^
|
|
19
|
+
"tsup": "^8.1.0"
|
|
16
20
|
},
|
|
17
21
|
"scripts": {
|
|
18
22
|
"dev": "tsup --watch",
|