phecda-vue 4.0.0 → 4.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/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # phecda-vue
2
- provide store/form/table with phecda function to vue
2
+ provide state management with phecda function to vue
3
3
 
4
4
  ## store
5
5
  ```ts
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ import { App, Ref, UnwrapNestedRefs, WatchOptions } from 'vue';
4
4
 
5
5
  declare const phecdaSymbol: unique symbol;
6
6
  declare class VuePhecda extends WebPhecda {
7
+ vueApp: App;
7
8
  install(app: App): void;
8
9
  }
9
10
  declare function createPhecda(): VuePhecda;
@@ -20,18 +21,21 @@ type SchemaToObj<S> = {
20
21
  };
21
22
 
22
23
  declare function useRaw<T extends Construct>(model: T): InstanceType<T>;
23
- declare function usePhecda(phecda?: VuePhecda): VuePhecda;
24
+ declare function usePhecda(): VuePhecda;
25
+ declare function getPhecda(phecda?: VuePhecda): VuePhecda;
24
26
  declare function useEvent<Key extends keyof Events>(eventName: Key, cb: (event: Events[Key]) => void): {
25
27
  emit: (arg: Events[Key]) => void;
26
28
  cancel: () => void;
27
29
  };
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>>;
30
+ declare function useR<T extends Construct>(model: T): UnwrapNestedRefs<InstanceType<T>>;
31
+ declare function getR<T extends Construct>(model: T, phecda?: VuePhecda): UnwrapNestedRefs<InstanceType<T>>;
32
+ declare function useV<T extends Construct>(model: T): ReplaceInstanceValues<InstanceType<T>>;
33
+ declare function getV<T extends Construct>(model: T, phecda?: VuePhecda): ReplaceInstanceValues<InstanceType<T>>;
30
34
 
31
- declare function Shallow(model: any): void;
35
+ declare function Shallow(isShallow?: boolean): (model: any) => void;
32
36
  declare function WatchEffect(option?: WatchOptions): (proto: any, key: string) => void;
33
37
 
34
38
  declare function markRaw<T extends object>(value: T): Raw<T>;
35
39
  declare function createSharedReactive<F extends (...args: any) => any>(composable: F): () => ReturnType<F>;
36
40
 
37
- export { Raw, RawSymbol, ReplaceInstanceValues, SchemaToObj, Shallow, VuePhecda, WatchEffect, createPhecda, createSharedReactive, markRaw, phecdaSymbol, useEvent, usePhecda, useR, useRaw, useV };
41
+ export { Raw, RawSymbol, ReplaceInstanceValues, SchemaToObj, Shallow, VuePhecda, WatchEffect, createPhecda, createSharedReactive, getPhecda, getR, getV, markRaw, phecdaSymbol, useEvent, usePhecda, useR, useRaw, useV };
package/dist/index.js CHANGED
@@ -27,6 +27,9 @@ __export(src_exports, {
27
27
  WatchEffect: () => WatchEffect,
28
28
  createPhecda: () => createPhecda,
29
29
  createSharedReactive: () => createSharedReactive,
30
+ getPhecda: () => getPhecda,
31
+ getR: () => getR,
32
+ getV: () => getV,
30
33
  markRaw: () => markRaw,
31
34
  phecdaSymbol: () => phecdaSymbol,
32
35
  useEvent: () => useEvent,
@@ -41,16 +44,18 @@ __reExport(src_exports, require("phecda-web"), module.exports);
41
44
  // src/core.ts
42
45
  var import_vue = require("vue");
43
46
  var import_phecda_web = require("phecda-web");
44
- var phecdaSymbol = Symbol("phecda-vue");
47
+ var phecdaSymbol = Symbol(process.env.NODE_ENV === "development" ? "phecda-vue" : void 0);
45
48
  var VuePhecda = class extends import_phecda_web.WebPhecda {
49
+ vueApp;
46
50
  install(app) {
47
51
  app.provide(phecdaSymbol, this);
52
+ this.vueApp = app;
48
53
  }
49
54
  };
50
55
  __name(VuePhecda, "VuePhecda");
51
56
  function createPhecda() {
52
57
  return new VuePhecda((instance) => {
53
- return (0, import_phecda_web.get)(instance, "shallow") ? (0, import_vue.shallowReactive)(instance) : (0, import_vue.reactive)(instance);
58
+ return (0, import_phecda_web.bindMethod)((0, import_phecda_web.get)(instance, "shallow") ? (0, import_vue.shallowReactive)(instance) : (0, import_vue.reactive)(instance));
54
59
  });
55
60
  }
56
61
  __name(createPhecda, "createPhecda");
@@ -95,15 +100,26 @@ function useRaw(model) {
95
100
  return (0, import_vue3.toRaw)(useR(model));
96
101
  }
97
102
  __name(useRaw, "useRaw");
98
- function usePhecda(phecda) {
99
- const activePhecda = phecda || (0, import_vue3.hasInjectionContext)() && (0, import_vue3.inject)(phecdaSymbol);
103
+ function usePhecda() {
104
+ if (!(0, import_vue3.hasInjectionContext)())
105
+ throw new Error("[phecda-vue]: use hook inside component setup function");
106
+ const activePhecda = (0, import_vue3.inject)(phecdaSymbol);
100
107
  if (!activePhecda)
101
- throw new Error("[phecda-vue]: must install the vue plugin (if used in setup) or manually inject the phecda instance ");
108
+ throw new Error("[phecda-vue]: must install the vue plugin ");
102
109
  if (!cacheMap.has(activePhecda))
103
110
  cacheMap.set(activePhecda, (0, import_phecda_web2.bindMethod)(activePhecda));
104
111
  return cacheMap.get(activePhecda);
105
112
  }
106
113
  __name(usePhecda, "usePhecda");
114
+ function getPhecda(phecda) {
115
+ const activePhecda = phecda || (0, import_phecda_web2.getDefaultPhecda)();
116
+ if (!activePhecda)
117
+ throw new Error("[phecda-vue]: manually inject the phecda instance if there is no default phecda");
118
+ if (!cacheMap.has(activePhecda))
119
+ cacheMap.set(activePhecda, (0, import_phecda_web2.bindMethod)(activePhecda));
120
+ return cacheMap.get(activePhecda);
121
+ }
122
+ __name(getPhecda, "getPhecda");
107
123
  function useEvent(eventName, cb) {
108
124
  (0, import_vue3.onBeforeUnmount)(() => {
109
125
  import_phecda_web2.emitter.off(eventName, cb);
@@ -115,24 +131,23 @@ function useEvent(eventName, cb) {
115
131
  };
116
132
  }
117
133
  __name(useEvent, "useEvent");
118
- function useR(model, phecda) {
119
- return usePhecda(phecda).init(model);
134
+ function useR(model) {
135
+ return usePhecda().init(model);
120
136
  }
121
137
  __name(useR, "useR");
122
- function useV(model, phecda) {
123
- const instance = usePhecda(phecda).init(model);
138
+ function getR(model, phecda) {
139
+ return getPhecda(phecda).init(model);
140
+ }
141
+ __name(getR, "getR");
142
+ function useV(model) {
143
+ const instance = usePhecda().init(model);
124
144
  if (cacheMap.has(instance))
125
145
  return cacheMap.get(instance);
126
146
  const cache = {};
127
147
  const proxy = new Proxy(instance, {
128
148
  get(target, key) {
129
- if (typeof target[key] === "function") {
130
- if (target[key].toString().startsWith("("))
131
- return target[key];
132
- if (!cache[key])
133
- cache[key] = target[key].bind(target);
134
- return cache[key];
135
- }
149
+ if (typeof target[key] === "function")
150
+ return target[key];
136
151
  if (target[key]?.__v_skip)
137
152
  return target[key];
138
153
  const cacheRef = cache[key];
@@ -151,12 +166,41 @@ function useV(model, phecda) {
151
166
  return proxy;
152
167
  }
153
168
  __name(useV, "useV");
169
+ function getV(model, phecda) {
170
+ const instance = getPhecda(phecda).init(model);
171
+ if (cacheMap.has(instance))
172
+ return cacheMap.get(instance);
173
+ const cache = {};
174
+ const proxy = new Proxy(instance, {
175
+ get(target, key) {
176
+ if (typeof target[key] === "function")
177
+ return target[key];
178
+ if (target[key]?.__v_skip)
179
+ return target[key];
180
+ const cacheRef = cache[key];
181
+ if (cacheRef && cacheRef.r)
182
+ return cacheRef();
183
+ cache[key] = createSharedReactive(() => {
184
+ return (0, import_vue3.toRef)(target, key);
185
+ });
186
+ return cache[key]();
187
+ },
188
+ set() {
189
+ return false;
190
+ }
191
+ });
192
+ cacheMap.set(instance, proxy);
193
+ return proxy;
194
+ }
195
+ __name(getV, "getV");
154
196
 
155
197
  // src/decorator.ts
156
198
  var import_phecda_web3 = require("phecda-web");
157
199
  var import_vue4 = require("vue");
158
- function Shallow(model) {
159
- (0, import_phecda_web3.set)(model.prototype, "shallow", true);
200
+ function Shallow(isShallow = true) {
201
+ return (model) => {
202
+ (0, import_phecda_web3.set)(model.prototype, "shallow", isShallow);
203
+ };
160
204
  }
161
205
  __name(Shallow, "Shallow");
162
206
  function WatchEffect(option) {
@@ -183,6 +227,9 @@ __name(WatchEffect, "WatchEffect");
183
227
  WatchEffect,
184
228
  createPhecda,
185
229
  createSharedReactive,
230
+ getPhecda,
231
+ getR,
232
+ getV,
186
233
  markRaw,
187
234
  phecdaSymbol,
188
235
  useEvent,
package/dist/index.mjs CHANGED
@@ -6,23 +6,25 @@ export * from "phecda-web";
6
6
 
7
7
  // src/core.ts
8
8
  import { reactive, shallowReactive } from "vue";
9
- import { WebPhecda, get } from "phecda-web";
10
- var phecdaSymbol = Symbol("phecda-vue");
9
+ import { WebPhecda, bindMethod, get } from "phecda-web";
10
+ var phecdaSymbol = Symbol(process.env.NODE_ENV === "development" ? "phecda-vue" : void 0);
11
11
  var VuePhecda = class extends WebPhecda {
12
+ vueApp;
12
13
  install(app) {
13
14
  app.provide(phecdaSymbol, this);
15
+ this.vueApp = app;
14
16
  }
15
17
  };
16
18
  __name(VuePhecda, "VuePhecda");
17
19
  function createPhecda() {
18
20
  return new VuePhecda((instance) => {
19
- return get(instance, "shallow") ? shallowReactive(instance) : reactive(instance);
21
+ return bindMethod(get(instance, "shallow") ? shallowReactive(instance) : reactive(instance));
20
22
  });
21
23
  }
22
24
  __name(createPhecda, "createPhecda");
23
25
 
24
26
  // src/composable.ts
25
- import { bindMethod, emitter } from "phecda-web";
27
+ import { bindMethod as bindMethod2, emitter, getDefaultPhecda } from "phecda-web";
26
28
  import { hasInjectionContext, inject, onBeforeUnmount, toRaw, toRef } from "vue";
27
29
 
28
30
  // src/utils.ts
@@ -61,15 +63,26 @@ function useRaw(model) {
61
63
  return toRaw(useR(model));
62
64
  }
63
65
  __name(useRaw, "useRaw");
64
- function usePhecda(phecda) {
65
- const activePhecda = phecda || hasInjectionContext() && inject(phecdaSymbol);
66
+ function usePhecda() {
67
+ if (!hasInjectionContext())
68
+ throw new Error("[phecda-vue]: use hook inside component setup function");
69
+ const activePhecda = inject(phecdaSymbol);
66
70
  if (!activePhecda)
67
- throw new Error("[phecda-vue]: must install the vue plugin (if used in setup) or manually inject the phecda instance ");
71
+ throw new Error("[phecda-vue]: must install the vue plugin ");
68
72
  if (!cacheMap.has(activePhecda))
69
- cacheMap.set(activePhecda, bindMethod(activePhecda));
73
+ cacheMap.set(activePhecda, bindMethod2(activePhecda));
70
74
  return cacheMap.get(activePhecda);
71
75
  }
72
76
  __name(usePhecda, "usePhecda");
77
+ function getPhecda(phecda) {
78
+ const activePhecda = phecda || getDefaultPhecda();
79
+ if (!activePhecda)
80
+ throw new Error("[phecda-vue]: manually inject the phecda instance if there is no default phecda");
81
+ if (!cacheMap.has(activePhecda))
82
+ cacheMap.set(activePhecda, bindMethod2(activePhecda));
83
+ return cacheMap.get(activePhecda);
84
+ }
85
+ __name(getPhecda, "getPhecda");
73
86
  function useEvent(eventName, cb) {
74
87
  onBeforeUnmount(() => {
75
88
  emitter.off(eventName, cb);
@@ -81,24 +94,23 @@ function useEvent(eventName, cb) {
81
94
  };
82
95
  }
83
96
  __name(useEvent, "useEvent");
84
- function useR(model, phecda) {
85
- return usePhecda(phecda).init(model);
97
+ function useR(model) {
98
+ return usePhecda().init(model);
86
99
  }
87
100
  __name(useR, "useR");
88
- function useV(model, phecda) {
89
- const instance = usePhecda(phecda).init(model);
101
+ function getR(model, phecda) {
102
+ return getPhecda(phecda).init(model);
103
+ }
104
+ __name(getR, "getR");
105
+ function useV(model) {
106
+ const instance = usePhecda().init(model);
90
107
  if (cacheMap.has(instance))
91
108
  return cacheMap.get(instance);
92
109
  const cache = {};
93
110
  const proxy = new Proxy(instance, {
94
111
  get(target, key) {
95
- if (typeof target[key] === "function") {
96
- if (target[key].toString().startsWith("("))
97
- return target[key];
98
- if (!cache[key])
99
- cache[key] = target[key].bind(target);
100
- return cache[key];
101
- }
112
+ if (typeof target[key] === "function")
113
+ return target[key];
102
114
  if (target[key]?.__v_skip)
103
115
  return target[key];
104
116
  const cacheRef = cache[key];
@@ -117,12 +129,41 @@ function useV(model, phecda) {
117
129
  return proxy;
118
130
  }
119
131
  __name(useV, "useV");
132
+ function getV(model, phecda) {
133
+ const instance = getPhecda(phecda).init(model);
134
+ if (cacheMap.has(instance))
135
+ return cacheMap.get(instance);
136
+ const cache = {};
137
+ const proxy = new Proxy(instance, {
138
+ get(target, key) {
139
+ if (typeof target[key] === "function")
140
+ return target[key];
141
+ if (target[key]?.__v_skip)
142
+ return target[key];
143
+ const cacheRef = cache[key];
144
+ if (cacheRef && cacheRef.r)
145
+ return cacheRef();
146
+ cache[key] = createSharedReactive(() => {
147
+ return toRef(target, key);
148
+ });
149
+ return cache[key]();
150
+ },
151
+ set() {
152
+ return false;
153
+ }
154
+ });
155
+ cacheMap.set(instance, proxy);
156
+ return proxy;
157
+ }
158
+ __name(getV, "getV");
120
159
 
121
160
  // src/decorator.ts
122
161
  import { set, setHandler, setStateKey } from "phecda-web";
123
162
  import { watchEffect } from "vue";
124
- function Shallow(model) {
125
- set(model.prototype, "shallow", true);
163
+ function Shallow(isShallow = true) {
164
+ return (model) => {
165
+ set(model.prototype, "shallow", isShallow);
166
+ };
126
167
  }
127
168
  __name(Shallow, "Shallow");
128
169
  function WatchEffect(option) {
@@ -148,6 +189,9 @@ export {
148
189
  WatchEffect,
149
190
  createPhecda,
150
191
  createSharedReactive,
192
+ getPhecda,
193
+ getR,
194
+ getV,
151
195
  markRaw,
152
196
  phecdaSymbol,
153
197
  useEvent,
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "phecda-vue",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "description": "provide phecda function to vue",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
5
+ "author": "fgsreally",
6
+ "license": "MIT",
7
+ "repository": "https://github.com/fgsreally/phecda/tree/main/packages/vue",
8
8
  "keywords": [
9
9
  "phecda",
10
10
  "vue"
11
11
  ],
12
- "author": "fgsreally",
13
- "repository": "https://github.com/fgsreally/phecda/tree/main/packages/vue",
12
+ "main": "dist/index.js",
13
+ "module": "dist/index.mjs",
14
+ "types": "dist/index.d.ts",
14
15
  "files": [
15
16
  "dist"
16
17
  ],
17
- "license": "MIT",
18
18
  "dependencies": {
19
19
  "vue": "^3.2.45",
20
- "phecda-web": "2.0.0"
20
+ "phecda-web": "2.0.2"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@vue/test-utils": "^2.4.6",