phecda-vue 4.0.1 → 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/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,13 +21,16 @@ 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
35
  declare function Shallow(isShallow?: boolean): (model: any) => void;
32
36
  declare function WatchEffect(option?: WatchOptions): (proto: any, key: string) => void;
@@ -34,4 +38,4 @@ declare function WatchEffect(option?: WatchOptions): (proto: any, key: string) =
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,
@@ -43,8 +46,10 @@ var import_vue = require("vue");
43
46
  var import_phecda_web = require("phecda-web");
44
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");
@@ -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,20 +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") {
149
+ if (typeof target[key] === "function")
130
150
  return target[key];
131
- }
132
151
  if (target[key]?.__v_skip)
133
152
  return target[key];
134
153
  const cacheRef = cache[key];
@@ -147,6 +166,33 @@ function useV(model, phecda) {
147
166
  return proxy;
148
167
  }
149
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");
150
196
 
151
197
  // src/decorator.ts
152
198
  var import_phecda_web3 = require("phecda-web");
@@ -181,6 +227,9 @@ __name(WatchEffect, "WatchEffect");
181
227
  WatchEffect,
182
228
  createPhecda,
183
229
  createSharedReactive,
230
+ getPhecda,
231
+ getR,
232
+ getV,
184
233
  markRaw,
185
234
  phecdaSymbol,
186
235
  useEvent,
package/dist/index.mjs CHANGED
@@ -9,8 +9,10 @@ import { reactive, shallowReactive } from "vue";
9
9
  import { WebPhecda, bindMethod, get } from "phecda-web";
10
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");
@@ -22,7 +24,7 @@ function createPhecda() {
22
24
  __name(createPhecda, "createPhecda");
23
25
 
24
26
  // src/composable.ts
25
- import { bindMethod as bindMethod2, 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
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,20 +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") {
112
+ if (typeof target[key] === "function")
96
113
  return target[key];
97
- }
98
114
  if (target[key]?.__v_skip)
99
115
  return target[key];
100
116
  const cacheRef = cache[key];
@@ -113,6 +129,33 @@ function useV(model, phecda) {
113
129
  return proxy;
114
130
  }
115
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");
116
159
 
117
160
  // src/decorator.ts
118
161
  import { set, setHandler, setStateKey } from "phecda-web";
@@ -146,6 +189,9 @@ export {
146
189
  WatchEffect,
147
190
  createPhecda,
148
191
  createSharedReactive,
192
+ getPhecda,
193
+ getR,
194
+ getV,
149
195
  markRaw,
150
196
  phecdaSymbol,
151
197
  useEvent,
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "phecda-vue",
3
- "version": "4.0.1",
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.1"
20
+ "phecda-web": "2.0.2"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@vue/test-utils": "^2.4.6",