phecda-vue 3.0.0 → 4.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/README.md +1 -1
- package/dist/index.d.ts +16 -46
- package/dist/index.js +62 -237
- package/dist/index.mjs +55 -224
- package/package.json +9 -4
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Ref, App, UnwrapNestedRefs, WatchOptions } from 'vue';
|
|
3
|
-
import { Construct, Events } from 'phecda-web';
|
|
1
|
+
import { WebPhecda, Construct, Events } from 'phecda-web';
|
|
4
2
|
export * from 'phecda-web';
|
|
3
|
+
import { App, Ref, UnwrapNestedRefs, WatchOptions } from 'vue';
|
|
4
|
+
|
|
5
|
+
declare const phecdaSymbol: unique symbol;
|
|
6
|
+
declare class VuePhecda extends WebPhecda {
|
|
7
|
+
install(app: App): void;
|
|
8
|
+
}
|
|
9
|
+
declare function createPhecda(): VuePhecda;
|
|
5
10
|
|
|
6
11
|
declare const RawSymbol: unique symbol;
|
|
7
12
|
type Raw<T> = T & {
|
|
@@ -14,54 +19,19 @@ type SchemaToObj<S> = {
|
|
|
14
19
|
[P in keyof S]: S[P] extends object ? SchemaToObj<S[P]> : (S[P] extends string ? any : S[P]);
|
|
15
20
|
};
|
|
16
21
|
|
|
17
|
-
declare const RE: RegExp;
|
|
18
|
-
declare function createFilter<Data extends Record<string, any>>(initState?: Object, option?: {
|
|
19
|
-
RE?: RegExp;
|
|
20
|
-
needReturn?: boolean;
|
|
21
|
-
exclude?: string[];
|
|
22
|
-
errorHandler?: (error?: Error, errorPath?: string) => any;
|
|
23
|
-
}): {
|
|
24
|
-
filter: <Schema>(obj: Schema) => SchemaToObj<Schema>;
|
|
25
|
-
data: [Data] extends [vue.Ref<any>] ? Data : vue.Ref<vue.UnwrapRef<Data>>;
|
|
26
|
-
init: (params?: Data) => void;
|
|
27
|
-
setState: <Key extends string>(key: Key, value: Data[Key]) => void;
|
|
28
|
-
storeState: (key: string, params?: Data) => void;
|
|
29
|
-
store: {
|
|
30
|
-
[key: string]: Data;
|
|
31
|
-
};
|
|
32
|
-
applyStore: (key: string) => void;
|
|
33
|
-
dispose: () => void;
|
|
34
|
-
clearStore: (key: string) => void;
|
|
35
|
-
delState: (key: string) => void;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
declare const phecdaSymbol: unique symbol;
|
|
39
|
-
declare function createPhecda(): vue.Raw<{
|
|
40
|
-
install(app: App): void;
|
|
41
|
-
load(state: any): any;
|
|
42
|
-
unmount(): Promise<void>;
|
|
43
|
-
}>;
|
|
44
|
-
|
|
45
|
-
type DeepPartial<T> = {
|
|
46
|
-
[K in keyof T]?: DeepPartial<T[K]>;
|
|
47
|
-
};
|
|
48
|
-
declare function markRaw<T extends object>(value: T): Raw<T>;
|
|
49
|
-
declare function isObject(o: any): boolean;
|
|
50
|
-
declare function mergeReactiveObjects<T extends Record<any, unknown> | Map<unknown, unknown> | Set<unknown>>(target: T, patchToApply: DeepPartial<T>): T;
|
|
51
|
-
declare function createSharedReactive<F extends (...args: any) => any>(composable: F): () => ReturnType<F>;
|
|
52
|
-
|
|
53
|
-
declare function useO<T extends Construct>(model: T): UnwrapNestedRefs<InstanceType<T>>;
|
|
54
22
|
declare function useRaw<T extends Construct>(model: T): InstanceType<T>;
|
|
55
|
-
declare function
|
|
56
|
-
declare function useR<T extends Construct>(model: T): UnwrapNestedRefs<InstanceType<T>>;
|
|
57
|
-
declare function useV<T extends Construct>(model: T): ReplaceInstanceValues<InstanceType<T>>;
|
|
23
|
+
declare function usePhecda(phecda?: VuePhecda): VuePhecda;
|
|
58
24
|
declare function useEvent<Key extends keyof Events>(eventName: Key, cb: (event: Events[Key]) => void): {
|
|
59
25
|
emit: (arg: Events[Key]) => void;
|
|
60
26
|
cancel: () => void;
|
|
61
27
|
};
|
|
62
|
-
declare function
|
|
28
|
+
declare function useR<T extends Construct>(model: T, phecda?: VuePhecda): UnwrapNestedRefs<InstanceType<T>>;
|
|
29
|
+
declare function useV<T extends Construct>(model: T, phecda?: VuePhecda): ReplaceInstanceValues<InstanceType<T>>;
|
|
63
30
|
|
|
64
|
-
declare function Shallow(model: any)
|
|
31
|
+
declare function Shallow(isShallow?: boolean): (model: any) => void;
|
|
65
32
|
declare function WatchEffect(option?: WatchOptions): (proto: any, key: string) => void;
|
|
66
33
|
|
|
67
|
-
|
|
34
|
+
declare function markRaw<T extends object>(value: T): Raw<T>;
|
|
35
|
+
declare function createSharedReactive<F extends (...args: any) => any>(composable: F): () => ReturnType<F>;
|
|
36
|
+
|
|
37
|
+
export { Raw, RawSymbol, ReplaceInstanceValues, SchemaToObj, Shallow, VuePhecda, WatchEffect, createPhecda, createSharedReactive, markRaw, phecdaSymbol, useEvent, usePhecda, useR, useRaw, useV };
|
package/dist/index.js
CHANGED
|
@@ -22,181 +22,49 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
22
22
|
// src/index.ts
|
|
23
23
|
var src_exports = {};
|
|
24
24
|
__export(src_exports, {
|
|
25
|
-
RE: () => RE,
|
|
26
25
|
Shallow: () => Shallow,
|
|
26
|
+
VuePhecda: () => VuePhecda,
|
|
27
27
|
WatchEffect: () => WatchEffect,
|
|
28
|
-
createFilter: () => createFilter,
|
|
29
28
|
createPhecda: () => createPhecda,
|
|
30
29
|
createSharedReactive: () => createSharedReactive,
|
|
31
|
-
|
|
32
|
-
isObject: () => isObject,
|
|
33
|
-
markRaw: () => markRaw2,
|
|
34
|
-
mergeReactiveObjects: () => mergeReactiveObjects,
|
|
30
|
+
markRaw: () => markRaw,
|
|
35
31
|
phecdaSymbol: () => phecdaSymbol,
|
|
36
32
|
useEvent: () => useEvent,
|
|
37
|
-
|
|
38
|
-
usePatch: () => usePatch,
|
|
33
|
+
usePhecda: () => usePhecda,
|
|
39
34
|
useR: () => useR,
|
|
40
35
|
useRaw: () => useRaw,
|
|
41
36
|
useV: () => useV
|
|
42
37
|
});
|
|
43
38
|
module.exports = __toCommonJS(src_exports);
|
|
44
|
-
|
|
45
|
-
// src/filter.ts
|
|
46
|
-
var import_vue = require("vue");
|
|
47
|
-
var RE = /{{(.*)}}/;
|
|
48
|
-
function createFilter(initState = {}, option = {}) {
|
|
49
|
-
const resolveOption = Object.assign({
|
|
50
|
-
RE,
|
|
51
|
-
exclude: []
|
|
52
|
-
}, option);
|
|
53
|
-
const scope = (0, import_vue.effectScope)(true);
|
|
54
|
-
let data = scope.run(() => (0, import_vue.ref)(initState));
|
|
55
|
-
let store = {};
|
|
56
|
-
function traverse(obj, path) {
|
|
57
|
-
for (const i in obj) {
|
|
58
|
-
if (resolveOption.exclude.includes(i))
|
|
59
|
-
continue;
|
|
60
|
-
const errorPath = path ? `${path}.${i}` : i;
|
|
61
|
-
if (typeof obj[i] === "object" && obj[i])
|
|
62
|
-
traverse(obj[i], errorPath);
|
|
63
|
-
if (typeof obj[i] === "string") {
|
|
64
|
-
if (resolveOption.RE.test(obj[i])) {
|
|
65
|
-
const body = obj[i].replace(resolveOption.RE, (_, s) => {
|
|
66
|
-
return s;
|
|
67
|
-
});
|
|
68
|
-
Object.defineProperty(obj, i, {
|
|
69
|
-
get() {
|
|
70
|
-
return new Function(...Object.keys(data.value), "_eh", resolveOption.errorHandler ? `try{${resolveOption.needReturn ? "" : "return"} ${body}}catch(e){return _eh(e,"${errorPath}")}` : `${resolveOption.needReturn ? "" : "return"} ${body}`)(...Object.values(data.value), resolveOption.errorHandler);
|
|
71
|
-
},
|
|
72
|
-
set(value) {
|
|
73
|
-
try {
|
|
74
|
-
new Function("_data", "_v", `_data.${body}=_v`)(data.value, value);
|
|
75
|
-
return true;
|
|
76
|
-
} catch (e) {
|
|
77
|
-
resolveOption.errorHandler?.(e);
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
__name(traverse, "traverse");
|
|
87
|
-
function filter(obj) {
|
|
88
|
-
traverse(obj);
|
|
89
|
-
return obj;
|
|
90
|
-
}
|
|
91
|
-
__name(filter, "filter");
|
|
92
|
-
function setState(key, value) {
|
|
93
|
-
data.value[key] = value;
|
|
94
|
-
}
|
|
95
|
-
__name(setState, "setState");
|
|
96
|
-
function storeState(key, params) {
|
|
97
|
-
store[key] = data.value;
|
|
98
|
-
init(params);
|
|
99
|
-
}
|
|
100
|
-
__name(storeState, "storeState");
|
|
101
|
-
function applyStore(key) {
|
|
102
|
-
if (!store[key])
|
|
103
|
-
return;
|
|
104
|
-
data.value = store[key];
|
|
105
|
-
}
|
|
106
|
-
__name(applyStore, "applyStore");
|
|
107
|
-
function init(params) {
|
|
108
|
-
data.value = params || initState || {};
|
|
109
|
-
}
|
|
110
|
-
__name(init, "init");
|
|
111
|
-
function delState(key) {
|
|
112
|
-
delete data.value[key];
|
|
113
|
-
}
|
|
114
|
-
__name(delState, "delState");
|
|
115
|
-
function clearStore(key) {
|
|
116
|
-
delete store[key];
|
|
117
|
-
}
|
|
118
|
-
__name(clearStore, "clearStore");
|
|
119
|
-
function dispose() {
|
|
120
|
-
data = null;
|
|
121
|
-
store = null;
|
|
122
|
-
scope.stop();
|
|
123
|
-
}
|
|
124
|
-
__name(dispose, "dispose");
|
|
125
|
-
return {
|
|
126
|
-
filter,
|
|
127
|
-
data,
|
|
128
|
-
init,
|
|
129
|
-
setState,
|
|
130
|
-
storeState,
|
|
131
|
-
store,
|
|
132
|
-
applyStore,
|
|
133
|
-
dispose,
|
|
134
|
-
clearStore,
|
|
135
|
-
delState
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
__name(createFilter, "createFilter");
|
|
139
|
-
|
|
140
|
-
// src/index.ts
|
|
141
39
|
__reExport(src_exports, require("phecda-web"), module.exports);
|
|
142
40
|
|
|
143
|
-
// src/
|
|
144
|
-
var
|
|
41
|
+
// src/core.ts
|
|
42
|
+
var import_vue = require("vue");
|
|
145
43
|
var import_phecda_web = require("phecda-web");
|
|
146
|
-
var phecdaSymbol = Symbol("phecda");
|
|
44
|
+
var phecdaSymbol = Symbol(process.env.NODE_ENV === "development" ? "phecda-vue" : void 0);
|
|
45
|
+
var VuePhecda = class extends import_phecda_web.WebPhecda {
|
|
46
|
+
install(app) {
|
|
47
|
+
app.provide(phecdaSymbol, this);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
__name(VuePhecda, "VuePhecda");
|
|
147
51
|
function createPhecda() {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
install(app) {
|
|
151
|
-
const instance = (0, import_phecda_web.getActiveInstance)();
|
|
152
|
-
instance.app = app;
|
|
153
|
-
app.provide(phecdaSymbol, instance);
|
|
154
|
-
app.config.globalProperties.$phecda = instance;
|
|
155
|
-
},
|
|
156
|
-
load(state) {
|
|
157
|
-
const instance = (0, import_phecda_web.getActiveInstance)();
|
|
158
|
-
instance.state = state;
|
|
159
|
-
return this;
|
|
160
|
-
},
|
|
161
|
-
async unmount() {
|
|
162
|
-
const { state } = (0, import_phecda_web.getActiveInstance)();
|
|
163
|
-
await Object.values(state).map((ins) => (0, import_phecda_web.invokeHandler)("unmount", ins));
|
|
164
|
-
(0, import_phecda_web.resetActiveInstance)();
|
|
165
|
-
}
|
|
52
|
+
return new VuePhecda((instance) => {
|
|
53
|
+
return (0, import_phecda_web.bindMethod)((0, import_phecda_web.get)(instance, "shallow") ? (0, import_vue.shallowReactive)(instance) : (0, import_vue.reactive)(instance));
|
|
166
54
|
});
|
|
167
|
-
return phecda;
|
|
168
55
|
}
|
|
169
56
|
__name(createPhecda, "createPhecda");
|
|
170
57
|
|
|
171
58
|
// src/composable.ts
|
|
172
|
-
var import_vue4 = require("vue");
|
|
173
59
|
var import_phecda_web2 = require("phecda-web");
|
|
60
|
+
var import_vue3 = require("vue");
|
|
174
61
|
|
|
175
62
|
// src/utils.ts
|
|
176
|
-
var
|
|
177
|
-
function
|
|
178
|
-
return (0,
|
|
179
|
-
}
|
|
180
|
-
__name(markRaw2, "markRaw");
|
|
181
|
-
function isObject(o) {
|
|
182
|
-
return Object.prototype.toString.call(o) === "[object Object]";
|
|
183
|
-
}
|
|
184
|
-
__name(isObject, "isObject");
|
|
185
|
-
function mergeReactiveObjects(target, patchToApply) {
|
|
186
|
-
for (const key in patchToApply) {
|
|
187
|
-
if (!patchToApply.hasOwnProperty(key))
|
|
188
|
-
continue;
|
|
189
|
-
const subPatch = patchToApply[key];
|
|
190
|
-
const targetValue = target[key];
|
|
191
|
-
if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key) && !(0, import_vue3.isRef)(subPatch) && !(0, import_vue3.isReactive)(subPatch)) {
|
|
192
|
-
target[key] = mergeReactiveObjects(targetValue, subPatch);
|
|
193
|
-
} else {
|
|
194
|
-
target[key] = subPatch;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return target;
|
|
63
|
+
var import_vue2 = require("vue");
|
|
64
|
+
function markRaw(value) {
|
|
65
|
+
return (0, import_vue2.markRaw)(value);
|
|
198
66
|
}
|
|
199
|
-
__name(
|
|
67
|
+
__name(markRaw, "markRaw");
|
|
200
68
|
function createSharedReactive(composable) {
|
|
201
69
|
let subscribers = 0;
|
|
202
70
|
let state;
|
|
@@ -210,10 +78,10 @@ function createSharedReactive(composable) {
|
|
|
210
78
|
const cb = /* @__PURE__ */ __name(() => {
|
|
211
79
|
subscribers++;
|
|
212
80
|
if (!state) {
|
|
213
|
-
scope = (0,
|
|
81
|
+
scope = (0, import_vue2.effectScope)(true);
|
|
214
82
|
state = scope.run(() => composable());
|
|
215
83
|
}
|
|
216
|
-
(0,
|
|
84
|
+
(0, import_vue2.onScopeDispose)(dispose);
|
|
217
85
|
return state;
|
|
218
86
|
}, "cb");
|
|
219
87
|
cb.r = true;
|
|
@@ -222,60 +90,44 @@ function createSharedReactive(composable) {
|
|
|
222
90
|
__name(createSharedReactive, "createSharedReactive");
|
|
223
91
|
|
|
224
92
|
// src/composable.ts
|
|
225
|
-
var
|
|
226
|
-
function initInstance(model) {
|
|
227
|
-
const proxyFn = (0, import_phecda_web2.get)(model.prototype, "shallow") ? import_vue4.shallowReactive : import_vue4.reactive;
|
|
228
|
-
const instance = proxyFn(new model());
|
|
229
|
-
instance._promise = (0, import_phecda_web2.invokeHandler)("init", instance);
|
|
230
|
-
return instance;
|
|
231
|
-
}
|
|
232
|
-
__name(initInstance, "initInstance");
|
|
233
|
-
function useO(model) {
|
|
234
|
-
const { state, origin } = (0, import_phecda_web2.getActiveInstance)();
|
|
235
|
-
if ((0, import_phecda_web2.get)(model.prototype, "isolate"))
|
|
236
|
-
return initInstance(model);
|
|
237
|
-
const tag = (0, import_phecda_web2.getTag)(model);
|
|
238
|
-
if (tag in state) {
|
|
239
|
-
if (process.env.NODE_ENV === "development") {
|
|
240
|
-
if (origin.get(state[tag]) === model)
|
|
241
|
-
return state[tag];
|
|
242
|
-
} else {
|
|
243
|
-
if (origin.get(state[tag]) !== model)
|
|
244
|
-
console.warn(`Synonym model: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
|
|
245
|
-
return state[tag];
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
const instance = initInstance(model);
|
|
249
|
-
state[tag] = instance;
|
|
250
|
-
origin.set(instance, model);
|
|
251
|
-
return state[tag];
|
|
252
|
-
}
|
|
253
|
-
__name(useO, "useO");
|
|
93
|
+
var cacheMap = /* @__PURE__ */ new WeakMap();
|
|
254
94
|
function useRaw(model) {
|
|
255
|
-
return (0,
|
|
95
|
+
return (0, import_vue3.toRaw)(useR(model));
|
|
256
96
|
}
|
|
257
97
|
__name(useRaw, "useRaw");
|
|
258
|
-
function
|
|
259
|
-
const
|
|
260
|
-
|
|
98
|
+
function usePhecda(phecda) {
|
|
99
|
+
const activePhecda = phecda || (0, import_vue3.hasInjectionContext)() && (0, import_vue3.inject)(phecdaSymbol);
|
|
100
|
+
if (!activePhecda)
|
|
101
|
+
throw new Error("[phecda-vue]: must install the vue plugin (if used in setup) or manually inject the phecda instance ");
|
|
102
|
+
if (!cacheMap.has(activePhecda))
|
|
103
|
+
cacheMap.set(activePhecda, (0, import_phecda_web2.bindMethod)(activePhecda));
|
|
104
|
+
return cacheMap.get(activePhecda);
|
|
105
|
+
}
|
|
106
|
+
__name(usePhecda, "usePhecda");
|
|
107
|
+
function useEvent(eventName, cb) {
|
|
108
|
+
(0, import_vue3.onBeforeUnmount)(() => {
|
|
109
|
+
import_phecda_web2.emitter.off(eventName, cb);
|
|
110
|
+
});
|
|
111
|
+
import_phecda_web2.emitter.on(eventName, cb);
|
|
112
|
+
return {
|
|
113
|
+
emit: (arg) => import_phecda_web2.emitter.emit(eventName, arg),
|
|
114
|
+
cancel: () => import_phecda_web2.emitter.off(eventName, cb)
|
|
115
|
+
};
|
|
261
116
|
}
|
|
262
|
-
__name(
|
|
263
|
-
function useR(model) {
|
|
264
|
-
return
|
|
117
|
+
__name(useEvent, "useEvent");
|
|
118
|
+
function useR(model, phecda) {
|
|
119
|
+
return usePhecda(phecda).init(model);
|
|
265
120
|
}
|
|
266
121
|
__name(useR, "useR");
|
|
267
|
-
function useV(model) {
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return cache[REF_SYMBOL];
|
|
122
|
+
function useV(model, phecda) {
|
|
123
|
+
const instance = usePhecda(phecda).init(model);
|
|
124
|
+
if (cacheMap.has(instance))
|
|
125
|
+
return cacheMap.get(instance);
|
|
126
|
+
const cache = {};
|
|
273
127
|
const proxy = new Proxy(instance, {
|
|
274
128
|
get(target, key) {
|
|
275
129
|
if (typeof target[key] === "function") {
|
|
276
|
-
|
|
277
|
-
cache[key] = target[key].bind(target);
|
|
278
|
-
return cache[key];
|
|
130
|
+
return target[key];
|
|
279
131
|
}
|
|
280
132
|
if (target[key]?.__v_skip)
|
|
281
133
|
return target[key];
|
|
@@ -283,7 +135,7 @@ function useV(model) {
|
|
|
283
135
|
if (cacheRef && cacheRef.r)
|
|
284
136
|
return cacheRef();
|
|
285
137
|
cache[key] = createSharedReactive(() => {
|
|
286
|
-
return (0,
|
|
138
|
+
return (0, import_vue3.toRef)(target, key);
|
|
287
139
|
});
|
|
288
140
|
return cache[key]();
|
|
289
141
|
},
|
|
@@ -291,41 +143,18 @@ function useV(model) {
|
|
|
291
143
|
return false;
|
|
292
144
|
}
|
|
293
145
|
});
|
|
294
|
-
|
|
295
|
-
if (!cacheMap.has(instance))
|
|
296
|
-
cacheMap.set(instance, cache);
|
|
146
|
+
cacheMap.set(instance, proxy);
|
|
297
147
|
return proxy;
|
|
298
148
|
}
|
|
299
149
|
__name(useV, "useV");
|
|
300
|
-
function useEvent(eventName, cb) {
|
|
301
|
-
(0, import_vue4.onBeforeUnmount)(() => {
|
|
302
|
-
import_phecda_web2.emitter.off(eventName, cb);
|
|
303
|
-
});
|
|
304
|
-
import_phecda_web2.emitter.on(eventName, cb);
|
|
305
|
-
return {
|
|
306
|
-
emit: (arg) => import_phecda_web2.emitter.emit(eventName, arg),
|
|
307
|
-
cancel: () => import_phecda_web2.emitter.off(eventName, cb)
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
__name(useEvent, "useEvent");
|
|
311
|
-
function initialize(model, deleteOtherProperty = true) {
|
|
312
|
-
const instance = useO(model);
|
|
313
|
-
const newInstance = new model();
|
|
314
|
-
Object.assign(instance, newInstance);
|
|
315
|
-
if (deleteOtherProperty) {
|
|
316
|
-
for (const key in instance) {
|
|
317
|
-
if (!(key in newInstance))
|
|
318
|
-
delete instance[key];
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
__name(initialize, "initialize");
|
|
323
150
|
|
|
324
151
|
// src/decorator.ts
|
|
325
152
|
var import_phecda_web3 = require("phecda-web");
|
|
326
|
-
var
|
|
327
|
-
function Shallow(
|
|
328
|
-
|
|
153
|
+
var import_vue4 = require("vue");
|
|
154
|
+
function Shallow(isShallow = true) {
|
|
155
|
+
return (model) => {
|
|
156
|
+
(0, import_phecda_web3.set)(model.prototype, "shallow", isShallow);
|
|
157
|
+
};
|
|
329
158
|
}
|
|
330
159
|
__name(Shallow, "Shallow");
|
|
331
160
|
function WatchEffect(option) {
|
|
@@ -336,7 +165,7 @@ function WatchEffect(option) {
|
|
|
336
165
|
init(instance) {
|
|
337
166
|
if (typeof instance[key] !== "function")
|
|
338
167
|
throw new Error("WatchEffect must decorate function");
|
|
339
|
-
stopHandler = (0,
|
|
168
|
+
stopHandler = (0, import_vue4.watchEffect)(instance[key].bind(instance), option);
|
|
340
169
|
},
|
|
341
170
|
unmount() {
|
|
342
171
|
return stopHandler?.();
|
|
@@ -347,21 +176,17 @@ function WatchEffect(option) {
|
|
|
347
176
|
__name(WatchEffect, "WatchEffect");
|
|
348
177
|
// Annotate the CommonJS export names for ESM import in node:
|
|
349
178
|
0 && (module.exports = {
|
|
350
|
-
RE,
|
|
351
179
|
Shallow,
|
|
180
|
+
VuePhecda,
|
|
352
181
|
WatchEffect,
|
|
353
|
-
createFilter,
|
|
354
182
|
createPhecda,
|
|
355
183
|
createSharedReactive,
|
|
356
|
-
initialize,
|
|
357
|
-
isObject,
|
|
358
184
|
markRaw,
|
|
359
|
-
mergeReactiveObjects,
|
|
360
185
|
phecdaSymbol,
|
|
361
186
|
useEvent,
|
|
362
|
-
|
|
363
|
-
usePatch,
|
|
187
|
+
usePhecda,
|
|
364
188
|
useR,
|
|
365
189
|
useRaw,
|
|
366
|
-
useV
|
|
190
|
+
useV,
|
|
191
|
+
...require("phecda-web")
|
|
367
192
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,161 +1,36 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/filter.ts
|
|
5
|
-
import { effectScope, ref } from "vue";
|
|
6
|
-
var RE = /{{(.*)}}/;
|
|
7
|
-
function createFilter(initState = {}, option = {}) {
|
|
8
|
-
const resolveOption = Object.assign({
|
|
9
|
-
RE,
|
|
10
|
-
exclude: []
|
|
11
|
-
}, option);
|
|
12
|
-
const scope = effectScope(true);
|
|
13
|
-
let data = scope.run(() => ref(initState));
|
|
14
|
-
let store = {};
|
|
15
|
-
function traverse(obj, path) {
|
|
16
|
-
for (const i in obj) {
|
|
17
|
-
if (resolveOption.exclude.includes(i))
|
|
18
|
-
continue;
|
|
19
|
-
const errorPath = path ? `${path}.${i}` : i;
|
|
20
|
-
if (typeof obj[i] === "object" && obj[i])
|
|
21
|
-
traverse(obj[i], errorPath);
|
|
22
|
-
if (typeof obj[i] === "string") {
|
|
23
|
-
if (resolveOption.RE.test(obj[i])) {
|
|
24
|
-
const body = obj[i].replace(resolveOption.RE, (_, s) => {
|
|
25
|
-
return s;
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(obj, i, {
|
|
28
|
-
get() {
|
|
29
|
-
return new Function(...Object.keys(data.value), "_eh", resolveOption.errorHandler ? `try{${resolveOption.needReturn ? "" : "return"} ${body}}catch(e){return _eh(e,"${errorPath}")}` : `${resolveOption.needReturn ? "" : "return"} ${body}`)(...Object.values(data.value), resolveOption.errorHandler);
|
|
30
|
-
},
|
|
31
|
-
set(value) {
|
|
32
|
-
try {
|
|
33
|
-
new Function("_data", "_v", `_data.${body}=_v`)(data.value, value);
|
|
34
|
-
return true;
|
|
35
|
-
} catch (e) {
|
|
36
|
-
resolveOption.errorHandler?.(e);
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
__name(traverse, "traverse");
|
|
46
|
-
function filter(obj) {
|
|
47
|
-
traverse(obj);
|
|
48
|
-
return obj;
|
|
49
|
-
}
|
|
50
|
-
__name(filter, "filter");
|
|
51
|
-
function setState(key, value) {
|
|
52
|
-
data.value[key] = value;
|
|
53
|
-
}
|
|
54
|
-
__name(setState, "setState");
|
|
55
|
-
function storeState(key, params) {
|
|
56
|
-
store[key] = data.value;
|
|
57
|
-
init(params);
|
|
58
|
-
}
|
|
59
|
-
__name(storeState, "storeState");
|
|
60
|
-
function applyStore(key) {
|
|
61
|
-
if (!store[key])
|
|
62
|
-
return;
|
|
63
|
-
data.value = store[key];
|
|
64
|
-
}
|
|
65
|
-
__name(applyStore, "applyStore");
|
|
66
|
-
function init(params) {
|
|
67
|
-
data.value = params || initState || {};
|
|
68
|
-
}
|
|
69
|
-
__name(init, "init");
|
|
70
|
-
function delState(key) {
|
|
71
|
-
delete data.value[key];
|
|
72
|
-
}
|
|
73
|
-
__name(delState, "delState");
|
|
74
|
-
function clearStore(key) {
|
|
75
|
-
delete store[key];
|
|
76
|
-
}
|
|
77
|
-
__name(clearStore, "clearStore");
|
|
78
|
-
function dispose() {
|
|
79
|
-
data = null;
|
|
80
|
-
store = null;
|
|
81
|
-
scope.stop();
|
|
82
|
-
}
|
|
83
|
-
__name(dispose, "dispose");
|
|
84
|
-
return {
|
|
85
|
-
filter,
|
|
86
|
-
data,
|
|
87
|
-
init,
|
|
88
|
-
setState,
|
|
89
|
-
storeState,
|
|
90
|
-
store,
|
|
91
|
-
applyStore,
|
|
92
|
-
dispose,
|
|
93
|
-
clearStore,
|
|
94
|
-
delState
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
__name(createFilter, "createFilter");
|
|
98
|
-
|
|
99
4
|
// src/index.ts
|
|
100
5
|
export * from "phecda-web";
|
|
101
6
|
|
|
102
|
-
// src/
|
|
103
|
-
import {
|
|
104
|
-
import {
|
|
105
|
-
var phecdaSymbol = Symbol("phecda");
|
|
7
|
+
// src/core.ts
|
|
8
|
+
import { reactive, shallowReactive } from "vue";
|
|
9
|
+
import { WebPhecda, bindMethod, get } from "phecda-web";
|
|
10
|
+
var phecdaSymbol = Symbol(process.env.NODE_ENV === "development" ? "phecda-vue" : void 0);
|
|
11
|
+
var VuePhecda = class extends WebPhecda {
|
|
12
|
+
install(app) {
|
|
13
|
+
app.provide(phecdaSymbol, this);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
__name(VuePhecda, "VuePhecda");
|
|
106
17
|
function createPhecda() {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
install(app) {
|
|
110
|
-
const instance = getActiveInstance();
|
|
111
|
-
instance.app = app;
|
|
112
|
-
app.provide(phecdaSymbol, instance);
|
|
113
|
-
app.config.globalProperties.$phecda = instance;
|
|
114
|
-
},
|
|
115
|
-
load(state) {
|
|
116
|
-
const instance = getActiveInstance();
|
|
117
|
-
instance.state = state;
|
|
118
|
-
return this;
|
|
119
|
-
},
|
|
120
|
-
async unmount() {
|
|
121
|
-
const { state } = getActiveInstance();
|
|
122
|
-
await Object.values(state).map((ins) => invokeHandler("unmount", ins));
|
|
123
|
-
resetActiveInstance();
|
|
124
|
-
}
|
|
18
|
+
return new VuePhecda((instance) => {
|
|
19
|
+
return bindMethod(get(instance, "shallow") ? shallowReactive(instance) : reactive(instance));
|
|
125
20
|
});
|
|
126
|
-
return phecda;
|
|
127
21
|
}
|
|
128
22
|
__name(createPhecda, "createPhecda");
|
|
129
23
|
|
|
130
24
|
// src/composable.ts
|
|
131
|
-
import {
|
|
132
|
-
import {
|
|
25
|
+
import { bindMethod as bindMethod2, emitter } from "phecda-web";
|
|
26
|
+
import { hasInjectionContext, inject, onBeforeUnmount, toRaw, toRef } from "vue";
|
|
133
27
|
|
|
134
28
|
// src/utils.ts
|
|
135
|
-
import { effectScope
|
|
136
|
-
function
|
|
29
|
+
import { effectScope, onScopeDispose, markRaw as raw } from "vue";
|
|
30
|
+
function markRaw(value) {
|
|
137
31
|
return raw(value);
|
|
138
32
|
}
|
|
139
|
-
__name(
|
|
140
|
-
function isObject(o) {
|
|
141
|
-
return Object.prototype.toString.call(o) === "[object Object]";
|
|
142
|
-
}
|
|
143
|
-
__name(isObject, "isObject");
|
|
144
|
-
function mergeReactiveObjects(target, patchToApply) {
|
|
145
|
-
for (const key in patchToApply) {
|
|
146
|
-
if (!patchToApply.hasOwnProperty(key))
|
|
147
|
-
continue;
|
|
148
|
-
const subPatch = patchToApply[key];
|
|
149
|
-
const targetValue = target[key];
|
|
150
|
-
if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key) && !isRef(subPatch) && !isReactive(subPatch)) {
|
|
151
|
-
target[key] = mergeReactiveObjects(targetValue, subPatch);
|
|
152
|
-
} else {
|
|
153
|
-
target[key] = subPatch;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return target;
|
|
157
|
-
}
|
|
158
|
-
__name(mergeReactiveObjects, "mergeReactiveObjects");
|
|
33
|
+
__name(markRaw, "markRaw");
|
|
159
34
|
function createSharedReactive(composable) {
|
|
160
35
|
let subscribers = 0;
|
|
161
36
|
let state;
|
|
@@ -169,7 +44,7 @@ function createSharedReactive(composable) {
|
|
|
169
44
|
const cb = /* @__PURE__ */ __name(() => {
|
|
170
45
|
subscribers++;
|
|
171
46
|
if (!state) {
|
|
172
|
-
scope =
|
|
47
|
+
scope = effectScope(true);
|
|
173
48
|
state = scope.run(() => composable());
|
|
174
49
|
}
|
|
175
50
|
onScopeDispose(dispose);
|
|
@@ -181,60 +56,44 @@ function createSharedReactive(composable) {
|
|
|
181
56
|
__name(createSharedReactive, "createSharedReactive");
|
|
182
57
|
|
|
183
58
|
// src/composable.ts
|
|
184
|
-
var
|
|
185
|
-
function initInstance(model) {
|
|
186
|
-
const proxyFn = get(model.prototype, "shallow") ? shallowReactive : reactive;
|
|
187
|
-
const instance = proxyFn(new model());
|
|
188
|
-
instance._promise = invokeHandler2("init", instance);
|
|
189
|
-
return instance;
|
|
190
|
-
}
|
|
191
|
-
__name(initInstance, "initInstance");
|
|
192
|
-
function useO(model) {
|
|
193
|
-
const { state, origin } = getActiveInstance2();
|
|
194
|
-
if (get(model.prototype, "isolate"))
|
|
195
|
-
return initInstance(model);
|
|
196
|
-
const tag = getTag(model);
|
|
197
|
-
if (tag in state) {
|
|
198
|
-
if (process.env.NODE_ENV === "development") {
|
|
199
|
-
if (origin.get(state[tag]) === model)
|
|
200
|
-
return state[tag];
|
|
201
|
-
} else {
|
|
202
|
-
if (origin.get(state[tag]) !== model)
|
|
203
|
-
console.warn(`Synonym model: Module taged "${String(tag)}" has been loaded before, so won't load Module "${model.name}"`);
|
|
204
|
-
return state[tag];
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
const instance = initInstance(model);
|
|
208
|
-
state[tag] = instance;
|
|
209
|
-
origin.set(instance, model);
|
|
210
|
-
return state[tag];
|
|
211
|
-
}
|
|
212
|
-
__name(useO, "useO");
|
|
59
|
+
var cacheMap = /* @__PURE__ */ new WeakMap();
|
|
213
60
|
function useRaw(model) {
|
|
214
|
-
return toRaw(
|
|
61
|
+
return toRaw(useR(model));
|
|
215
62
|
}
|
|
216
63
|
__name(useRaw, "useRaw");
|
|
217
|
-
function
|
|
218
|
-
const
|
|
219
|
-
|
|
64
|
+
function usePhecda(phecda) {
|
|
65
|
+
const activePhecda = phecda || hasInjectionContext() && inject(phecdaSymbol);
|
|
66
|
+
if (!activePhecda)
|
|
67
|
+
throw new Error("[phecda-vue]: must install the vue plugin (if used in setup) or manually inject the phecda instance ");
|
|
68
|
+
if (!cacheMap.has(activePhecda))
|
|
69
|
+
cacheMap.set(activePhecda, bindMethod2(activePhecda));
|
|
70
|
+
return cacheMap.get(activePhecda);
|
|
71
|
+
}
|
|
72
|
+
__name(usePhecda, "usePhecda");
|
|
73
|
+
function useEvent(eventName, cb) {
|
|
74
|
+
onBeforeUnmount(() => {
|
|
75
|
+
emitter.off(eventName, cb);
|
|
76
|
+
});
|
|
77
|
+
emitter.on(eventName, cb);
|
|
78
|
+
return {
|
|
79
|
+
emit: (arg) => emitter.emit(eventName, arg),
|
|
80
|
+
cancel: () => emitter.off(eventName, cb)
|
|
81
|
+
};
|
|
220
82
|
}
|
|
221
|
-
__name(
|
|
222
|
-
function useR(model) {
|
|
223
|
-
return
|
|
83
|
+
__name(useEvent, "useEvent");
|
|
84
|
+
function useR(model, phecda) {
|
|
85
|
+
return usePhecda(phecda).init(model);
|
|
224
86
|
}
|
|
225
87
|
__name(useR, "useR");
|
|
226
|
-
function useV(model) {
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return cache[REF_SYMBOL];
|
|
88
|
+
function useV(model, phecda) {
|
|
89
|
+
const instance = usePhecda(phecda).init(model);
|
|
90
|
+
if (cacheMap.has(instance))
|
|
91
|
+
return cacheMap.get(instance);
|
|
92
|
+
const cache = {};
|
|
232
93
|
const proxy = new Proxy(instance, {
|
|
233
94
|
get(target, key) {
|
|
234
95
|
if (typeof target[key] === "function") {
|
|
235
|
-
|
|
236
|
-
cache[key] = target[key].bind(target);
|
|
237
|
-
return cache[key];
|
|
96
|
+
return target[key];
|
|
238
97
|
}
|
|
239
98
|
if (target[key]?.__v_skip)
|
|
240
99
|
return target[key];
|
|
@@ -250,41 +109,18 @@ function useV(model) {
|
|
|
250
109
|
return false;
|
|
251
110
|
}
|
|
252
111
|
});
|
|
253
|
-
|
|
254
|
-
if (!cacheMap.has(instance))
|
|
255
|
-
cacheMap.set(instance, cache);
|
|
112
|
+
cacheMap.set(instance, proxy);
|
|
256
113
|
return proxy;
|
|
257
114
|
}
|
|
258
115
|
__name(useV, "useV");
|
|
259
|
-
function useEvent(eventName, cb) {
|
|
260
|
-
onBeforeUnmount(() => {
|
|
261
|
-
emitter.off(eventName, cb);
|
|
262
|
-
});
|
|
263
|
-
emitter.on(eventName, cb);
|
|
264
|
-
return {
|
|
265
|
-
emit: (arg) => emitter.emit(eventName, arg),
|
|
266
|
-
cancel: () => emitter.off(eventName, cb)
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
__name(useEvent, "useEvent");
|
|
270
|
-
function initialize(model, deleteOtherProperty = true) {
|
|
271
|
-
const instance = useO(model);
|
|
272
|
-
const newInstance = new model();
|
|
273
|
-
Object.assign(instance, newInstance);
|
|
274
|
-
if (deleteOtherProperty) {
|
|
275
|
-
for (const key in instance) {
|
|
276
|
-
if (!(key in newInstance))
|
|
277
|
-
delete instance[key];
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
__name(initialize, "initialize");
|
|
282
116
|
|
|
283
117
|
// src/decorator.ts
|
|
284
118
|
import { set, setHandler, setStateKey } from "phecda-web";
|
|
285
119
|
import { watchEffect } from "vue";
|
|
286
|
-
function Shallow(
|
|
287
|
-
|
|
120
|
+
function Shallow(isShallow = true) {
|
|
121
|
+
return (model) => {
|
|
122
|
+
set(model.prototype, "shallow", isShallow);
|
|
123
|
+
};
|
|
288
124
|
}
|
|
289
125
|
__name(Shallow, "Shallow");
|
|
290
126
|
function WatchEffect(option) {
|
|
@@ -305,20 +141,15 @@ function WatchEffect(option) {
|
|
|
305
141
|
}
|
|
306
142
|
__name(WatchEffect, "WatchEffect");
|
|
307
143
|
export {
|
|
308
|
-
RE,
|
|
309
144
|
Shallow,
|
|
145
|
+
VuePhecda,
|
|
310
146
|
WatchEffect,
|
|
311
|
-
createFilter,
|
|
312
147
|
createPhecda,
|
|
313
148
|
createSharedReactive,
|
|
314
|
-
|
|
315
|
-
isObject,
|
|
316
|
-
markRaw2 as markRaw,
|
|
317
|
-
mergeReactiveObjects,
|
|
149
|
+
markRaw,
|
|
318
150
|
phecdaSymbol,
|
|
319
151
|
useEvent,
|
|
320
|
-
|
|
321
|
-
usePatch,
|
|
152
|
+
usePhecda,
|
|
322
153
|
useR,
|
|
323
154
|
useRaw,
|
|
324
155
|
useV
|
package/package.json
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phecda-vue",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "provide phecda function to vue",
|
|
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
|
+
"vue"
|
|
11
|
+
],
|
|
12
|
+
"author": "fgsreally",
|
|
13
|
+
"repository": "https://github.com/fgsreally/phecda/tree/main/packages/vue",
|
|
10
14
|
"files": [
|
|
11
15
|
"dist"
|
|
12
16
|
],
|
|
13
17
|
"license": "MIT",
|
|
14
18
|
"dependencies": {
|
|
15
19
|
"vue": "^3.2.45",
|
|
16
|
-
"phecda-web": "
|
|
20
|
+
"phecda-web": "2.0.1"
|
|
17
21
|
},
|
|
18
22
|
"devDependencies": {
|
|
23
|
+
"@vue/test-utils": "^2.4.6",
|
|
19
24
|
"tsup": "^6.5.0"
|
|
20
25
|
},
|
|
21
26
|
"scripts": {
|