phecda-vue 3.0.0-beta.22 → 4.0.0
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 +15 -45
- package/dist/index.js +59 -232
- package/dist/index.mjs +52 -219
- package/package.json +9 -4
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
31
|
declare function Shallow(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("phecda-vue");
|
|
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.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,57 +90,45 @@ 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") {
|
|
130
|
+
if (target[key].toString().startsWith("("))
|
|
131
|
+
return target[key];
|
|
276
132
|
if (!cache[key])
|
|
277
133
|
cache[key] = target[key].bind(target);
|
|
278
134
|
return cache[key];
|
|
@@ -283,7 +139,7 @@ function useV(model) {
|
|
|
283
139
|
if (cacheRef && cacheRef.r)
|
|
284
140
|
return cacheRef();
|
|
285
141
|
cache[key] = createSharedReactive(() => {
|
|
286
|
-
return (0,
|
|
142
|
+
return (0, import_vue3.toRef)(target, key);
|
|
287
143
|
});
|
|
288
144
|
return cache[key]();
|
|
289
145
|
},
|
|
@@ -291,39 +147,14 @@ function useV(model) {
|
|
|
291
147
|
return false;
|
|
292
148
|
}
|
|
293
149
|
});
|
|
294
|
-
|
|
295
|
-
if (!cacheMap.has(instance))
|
|
296
|
-
cacheMap.set(instance, cache);
|
|
150
|
+
cacheMap.set(instance, proxy);
|
|
297
151
|
return proxy;
|
|
298
152
|
}
|
|
299
153
|
__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
154
|
|
|
324
155
|
// src/decorator.ts
|
|
325
156
|
var import_phecda_web3 = require("phecda-web");
|
|
326
|
-
var
|
|
157
|
+
var import_vue4 = require("vue");
|
|
327
158
|
function Shallow(model) {
|
|
328
159
|
(0, import_phecda_web3.set)(model.prototype, "shallow", true);
|
|
329
160
|
}
|
|
@@ -336,7 +167,7 @@ function WatchEffect(option) {
|
|
|
336
167
|
init(instance) {
|
|
337
168
|
if (typeof instance[key] !== "function")
|
|
338
169
|
throw new Error("WatchEffect must decorate function");
|
|
339
|
-
stopHandler = (0,
|
|
170
|
+
stopHandler = (0, import_vue4.watchEffect)(instance[key].bind(instance), option);
|
|
340
171
|
},
|
|
341
172
|
unmount() {
|
|
342
173
|
return stopHandler?.();
|
|
@@ -347,21 +178,17 @@ function WatchEffect(option) {
|
|
|
347
178
|
__name(WatchEffect, "WatchEffect");
|
|
348
179
|
// Annotate the CommonJS export names for ESM import in node:
|
|
349
180
|
0 && (module.exports = {
|
|
350
|
-
RE,
|
|
351
181
|
Shallow,
|
|
182
|
+
VuePhecda,
|
|
352
183
|
WatchEffect,
|
|
353
|
-
createFilter,
|
|
354
184
|
createPhecda,
|
|
355
185
|
createSharedReactive,
|
|
356
|
-
initialize,
|
|
357
|
-
isObject,
|
|
358
186
|
markRaw,
|
|
359
|
-
mergeReactiveObjects,
|
|
360
187
|
phecdaSymbol,
|
|
361
188
|
useEvent,
|
|
362
|
-
|
|
363
|
-
usePatch,
|
|
189
|
+
usePhecda,
|
|
364
190
|
useR,
|
|
365
191
|
useRaw,
|
|
366
|
-
useV
|
|
192
|
+
useV,
|
|
193
|
+
...require("phecda-web")
|
|
367
194
|
});
|
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, get } from "phecda-web";
|
|
10
|
+
var phecdaSymbol = Symbol("phecda-vue");
|
|
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 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, 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,57 +56,45 @@ 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, bindMethod(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") {
|
|
96
|
+
if (target[key].toString().startsWith("("))
|
|
97
|
+
return target[key];
|
|
235
98
|
if (!cache[key])
|
|
236
99
|
cache[key] = target[key].bind(target);
|
|
237
100
|
return cache[key];
|
|
@@ -250,35 +113,10 @@ function useV(model) {
|
|
|
250
113
|
return false;
|
|
251
114
|
}
|
|
252
115
|
});
|
|
253
|
-
|
|
254
|
-
if (!cacheMap.has(instance))
|
|
255
|
-
cacheMap.set(instance, cache);
|
|
116
|
+
cacheMap.set(instance, proxy);
|
|
256
117
|
return proxy;
|
|
257
118
|
}
|
|
258
119
|
__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
120
|
|
|
283
121
|
// src/decorator.ts
|
|
284
122
|
import { set, setHandler, setStateKey } from "phecda-web";
|
|
@@ -305,20 +143,15 @@ function WatchEffect(option) {
|
|
|
305
143
|
}
|
|
306
144
|
__name(WatchEffect, "WatchEffect");
|
|
307
145
|
export {
|
|
308
|
-
RE,
|
|
309
146
|
Shallow,
|
|
147
|
+
VuePhecda,
|
|
310
148
|
WatchEffect,
|
|
311
|
-
createFilter,
|
|
312
149
|
createPhecda,
|
|
313
150
|
createSharedReactive,
|
|
314
|
-
|
|
315
|
-
isObject,
|
|
316
|
-
markRaw2 as markRaw,
|
|
317
|
-
mergeReactiveObjects,
|
|
151
|
+
markRaw,
|
|
318
152
|
phecdaSymbol,
|
|
319
153
|
useEvent,
|
|
320
|
-
|
|
321
|
-
usePatch,
|
|
154
|
+
usePhecda,
|
|
322
155
|
useR,
|
|
323
156
|
useRaw,
|
|
324
157
|
useV
|
package/package.json
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phecda-vue",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
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.0"
|
|
17
21
|
},
|
|
18
22
|
"devDependencies": {
|
|
23
|
+
"@vue/test-utils": "^2.4.6",
|
|
19
24
|
"tsup": "^6.5.0"
|
|
20
25
|
},
|
|
21
26
|
"scripts": {
|