@kaokei/use-vue-service 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  灵感来自 angular 中的服务的概念。在 angular 中不需要全局唯一的数据源 store。而是通过声明服务,以及向组件中注入服务来达到数据管理以及数据共享的。
15
15
 
16
- 本库也是实现了类似的效果,可以通过依赖注入实现面向服务编程、实现领域驱动开发。从而可以代替 vuex。
16
+ 本库也是实现了类似的效果,可以通过依赖注入实现面向服务编程、实现领域驱动开发。从而可以代替 vuex/pinia
17
17
 
18
18
  本库通过类来声明服务,对 typescript 支持非常棒。
19
19
 
@@ -1,7 +1,5 @@
1
1
  import { Token } from '@kaokei/di';
2
- import { ComponentInternalInstance } from 'vue';
3
2
  import { FindChildService, FindChildrenServices } from './interface.ts';
4
- export declare const CURRENT_COMPONENT: Token<ComponentInternalInstance>;
5
3
  export declare const FIND_CHILD_SERVICE: Token<FindChildService>;
6
4
  export declare const FIND_CHILDREN_SERVICES: Token<FindChildrenServices>;
7
5
  export declare const SCOPE_KEY: unique symbol;
@@ -1,7 +1,5 @@
1
1
  import { Token } from '@kaokei/di';
2
- import { ComponentInternalInstance } from 'vue';
3
2
  import { FindChildService, FindChildrenServices } from './interface.ts';
4
- export declare const CURRENT_COMPONENT: Token<ComponentInternalInstance>;
5
3
  export declare const FIND_CHILD_SERVICE: Token<FindChildService>;
6
4
  export declare const FIND_CHILDREN_SERVICES: Token<FindChildrenServices>;
7
5
  export declare const SCOPE_KEY: unique symbol;
package/dist/core.d.cts CHANGED
@@ -2,7 +2,7 @@ import { App } from 'vue';
2
2
  import { CommonToken } from '@kaokei/di';
3
3
  import { NewableProvider, FunctionProvider, Provider } from './interface.ts';
4
4
  export declare function useService<T>(token: CommonToken<T>): T;
5
- export declare function useRootService<T>(token: CommonToken<T>): T;
5
+ export declare function getRootService<T>(token: CommonToken<T>): T;
6
6
  export declare function useAppService<T>(token: CommonToken<T>, app: App): T;
7
7
  export declare function declareProviders(providers: FunctionProvider): void;
8
8
  export declare function declareProviders(providers: NewableProvider): void;
package/dist/core.d.ts CHANGED
@@ -2,7 +2,7 @@ import { App } from 'vue';
2
2
  import { CommonToken } from '@kaokei/di';
3
3
  import { NewableProvider, FunctionProvider, Provider } from './interface.ts';
4
4
  export declare function useService<T>(token: CommonToken<T>): T;
5
- export declare function useRootService<T>(token: CommonToken<T>): T;
5
+ export declare function getRootService<T>(token: CommonToken<T>): T;
6
6
  export declare function useAppService<T>(token: CommonToken<T>, app: App): T;
7
7
  export declare function declareProviders(providers: FunctionProvider): void;
8
8
  export declare function declareProviders(providers: NewableProvider): void;
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("@kaokei/di"),r=require("vue");function E(e,t,n){return e&&e.forEach(o=>{o&&o.isCurrentBound(t)&&n.push(o.get(t)),o.children&&E(o.children,t,n)}),n}function y(e,t){const n=[];return E(t.children,e,n),n[0]}function b(e,t){const n=[];return E(t.children,e,n),n}function T(e){const t=e,n=r.effectScope(!0);return t[S]=n,n}function R(e){return e[S]}function _(e){const t=R(e);return t||T(e)}function D(e){const t=e;if(t){const n=R(t);n&&(n.stop(),delete t[S])}}function V(e){return e!==null&&typeof e=="object"}function A(e,t){return V(t)?r.reactive(t):t}function U(e){return D(e)}function w({container:e}){return t=>y(t,e)}function j({container:e}){return t=>b(t,e)}function p(e,t){let n;return e?n=e.createChild():n=new s.Container,t&&n.bind(I).toConstantValue(r.markRaw(t)),n.bind(h).toDynamicValue(w),n.bind(N).toDynamicValue(j),n.onActivation(A),n.onDeactivation(U),n}const I=new s.Token("USE_VUE_SERVICE_CURRENT_COMPONENT"),h=new s.Token("USE_REACT_SERVICE_FIND_CHILD_SERVICE"),N=new s.Token("USE_REACT_SERVICE_FIND_CHILDREN_SERVICES"),S=Symbol(),a="USE_VUE_SERVICE_CONTAINER_TOKEN",d=p();function l(e,t){if(typeof t=="function")t(e);else for(let n=0;n<t.length;n++)e.bind(t[n]).toSelf()}function g(){const e=r.getCurrentInstance();if(e){const t=a,n=e.provides,o=e.parent&&e.parent.provides;if(n&&n!==o&&Object.prototype.hasOwnProperty.call(n,t))return n[t]}}function v(){if(r.hasInjectionContext())return r.inject(a,d);throw new Error("getProvideContainer 只能在 setup 中使用")}function F(e){return(g()||v()).get(e)}function H(e){return d.get(e)}function L(e,t){return t.runWithContext(()=>v().get(e))}function M(e){const t=g();if(t)l(t,e);else{const n=v(),o=r.getCurrentInstance();let c=p(n,o);l(c,e),r.provide(a,c),r.onUnmounted(()=>{c.destroy(),c=null})}}function x(e){l(d,e)}function m(e,t){t.runWithContext(()=>{const n=r.inject(a,null);if(n)l(n,e);else{let o=p(d);l(o,e),t.provide(a,o),t.onUnmount(()=>{o.destroy(),o=null})}})}function K(e){return t=>m(e,t)}function k(e,t,n){const o=n.get,c=n.set;return{configurable:!0,enumerable:!0,get(){const i=r.reactive(this),f=Symbol.for(t);let u=i[f];if(!u){const O=_(i).run(()=>c?r.computed({get:()=>o.call(i),set:P=>c.call(i,P)}):r.computed(()=>o.call(i)));u=r.markRaw({value:O}),i[f]=u}return u.value.value},set(i){const f=r.reactive(this),u=Symbol.for(t),C=f[u];C&&c&&(C.value.value=i)}}}exports.CURRENT_COMPONENT=I;exports.Computed=k;exports.FIND_CHILDREN_SERVICES=N;exports.FIND_CHILD_SERVICE=h;exports.declareAppProviders=m;exports.declareAppProvidersPlugin=K;exports.declareProviders=M;exports.declareRootProviders=x;exports.getEffectScope=_;exports.useAppService=L;exports.useRootService=H;exports.useService=F;Object.keys(s).forEach(e=>{e!=="default"&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:()=>s[e]})});
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@kaokei/di"),o=require("vue");function b(e){const t=e,n=o.effectScope(!0);return t[S]=n,n}function _(e){return e[S]}function N(e){const t=e;if(t){const n=_(t);n&&(n.stop(),delete t[S])}}function h(e){const t=_(e);return t||b(e)}function C(e,t,n){return e&&e.forEach(r=>{r&&r.isCurrentBound(t)&&n.push(r.get(t)),r.children&&C(r.children,t,n)}),n}function D(e,t){const n=[];return C(t.children,e,n),n[0]}function O(e,t){const n=[];return C(t.children,e,n),n}function A(e){return e!==null&&typeof e=="object"}function V(e,t){return A(t)?o.reactive(t):t}function T(e){return N(e)}function j({container:e}){return t=>D(t,e)}function w({container:e}){return t=>O(t,e)}function E(e){let t;return e?t=e.createChild():t=new u.Container,t.bind(I).toDynamicValue(j),t.bind(g).toDynamicValue(w),t.onActivation(V),t.onDeactivation(T),t}const I=new u.Token("USE_REACT_SERVICE_FIND_CHILD_SERVICE"),g=new u.Token("USE_REACT_SERVICE_FIND_CHILDREN_SERVICES"),S=Symbol(),s="USE_VUE_SERVICE_CONTAINER_TOKEN",f=E();function a(e,t){if(typeof t=="function")t(e);else for(let n=0;n<t.length;n++)e.bind(t[n]).toSelf()}function R(){const e=o.getCurrentInstance();if(e){const t=s,n=e.provides,r=e.parent&&e.parent.provides;if(n&&n!==r&&Object.prototype.hasOwnProperty.call(n,t))return n[t]}}function v(){if(o.hasInjectionContext())return o.inject(s,f);throw new Error("getProvideContainer 只能在 setup 中使用")}function F(e){return(R()||v()).get(e)}function H(e){return f.get(e)}function L(e,t){return t.runWithContext(()=>v().get(e))}function U(e){const t=R();if(t)a(t,e);else{const n=v();let r=E(n);a(r,e),o.provide(s,r),o.onUnmounted(()=>{r.destroy(),r=null})}}function x(e){a(f,e)}function m(e,t){t.runWithContext(()=>{const n=o.inject(s,null);if(n)a(n,e);else{let r=E(f);a(r,e),t.provide(s,r),t.onUnmount(()=>{r.destroy(),r=null})}})}function K(e){return t=>m(e,t)}function q(e,t,n){const r=n.get,d=n.set;return{configurable:!0,enumerable:!0,get(){const c=o.reactive(this),l=Symbol.for(t);let i=c[l];if(!i){const y=h(c).run(()=>d?o.computed({get:()=>r.call(c),set:P=>d.call(c,P)}):o.computed(()=>r.call(c)));i=o.markRaw({value:y}),c[l]=i}return i.value.value},set(c){const l=o.reactive(this),i=Symbol.for(t),p=l[i];p&&d&&(p.value.value=c)}}}exports.Computed=q;exports.FIND_CHILDREN_SERVICES=g;exports.FIND_CHILD_SERVICE=I;exports.declareAppProviders=m;exports.declareAppProvidersPlugin=K;exports.declareProviders=U;exports.declareRootProviders=x;exports.getEffectScope=h;exports.getRootService=H;exports.useAppService=L;exports.useService=F;Object.keys(u).forEach(e=>{e!=="default"&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:()=>u[e]})});
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from '@kaokei/di';
2
- export { CURRENT_COMPONENT, FIND_CHILD_SERVICE, FIND_CHILDREN_SERVICES, } from './constants.ts';
3
- export { useService, declareProviders, useRootService, declareRootProviders, useAppService, declareAppProviders, declareAppProvidersPlugin, } from './core.ts';
2
+ export { FIND_CHILD_SERVICE, FIND_CHILDREN_SERVICES } from './constants.ts';
3
+ export { useService, declareProviders, getRootService, declareRootProviders, useAppService, declareAppProviders, declareAppProvidersPlugin, } from './core.ts';
4
4
  export { Computed } from './computed.ts';
5
5
  export { getEffectScope } from './scope.ts';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from '@kaokei/di';
2
- export { CURRENT_COMPONENT, FIND_CHILD_SERVICE, FIND_CHILDREN_SERVICES, } from './constants.ts';
3
- export { useService, declareProviders, useRootService, declareRootProviders, useAppService, declareAppProviders, declareAppProvidersPlugin, } from './core.ts';
2
+ export { FIND_CHILD_SERVICE, FIND_CHILDREN_SERVICES } from './constants.ts';
3
+ export { useService, declareProviders, getRootService, declareRootProviders, useAppService, declareAppProviders, declareAppProvidersPlugin, } from './core.ts';
4
4
  export { Computed } from './computed.ts';
5
5
  export { getEffectScope } from './scope.ts';
package/dist/index.js CHANGED
@@ -1,167 +1,164 @@
1
- import { Container as y, Token as d } from "@kaokei/di";
1
+ import { Container as y, Token as h } from "@kaokei/di";
2
2
  export * from "@kaokei/di";
3
- import { effectScope as P, markRaw as R, reactive as C, getCurrentInstance as h, provide as T, onUnmounted as V, inject as m, hasInjectionContext as b, computed as v } from "vue";
4
- function E(t, e, n) {
5
- return t && t.forEach((o) => {
6
- o && o.isCurrentBound(e) && n.push(o.get(e)), o.children && E(o.children, e, n);
7
- }), n;
3
+ import { effectScope as N, reactive as d, provide as D, onUnmounted as b, inject as _, getCurrentInstance as O, hasInjectionContext as P, computed as v, markRaw as A } from "vue";
4
+ function V(e) {
5
+ const t = e, n = N(!0);
6
+ return t[E] = n, n;
8
7
  }
9
- function D(t, e) {
10
- const n = [];
11
- return E(e.children, t, n), n[0];
8
+ function m(e) {
9
+ return e[E];
12
10
  }
13
- function U(t, e) {
14
- const n = [];
15
- return E(e.children, t, n), n;
11
+ function T(e) {
12
+ const t = e;
13
+ if (t) {
14
+ const n = m(t);
15
+ n && (n.stop(), delete t[E]);
16
+ }
16
17
  }
17
- function A(t) {
18
- const e = t, n = P(!0);
19
- return e[S] = n, n;
18
+ function w(e) {
19
+ const t = m(e);
20
+ return t || V(e);
20
21
  }
21
- function I(t) {
22
- return t[S];
22
+ function C(e, t, n) {
23
+ return e && e.forEach((o) => {
24
+ o && o.isCurrentBound(t) && n.push(o.get(t)), o.children && C(o.children, t, n);
25
+ }), n;
23
26
  }
24
- function w(t) {
25
- const e = I(t);
26
- return e || A(t);
27
+ function F(e, t) {
28
+ const n = [];
29
+ return C(t.children, e, n), n[0];
27
30
  }
28
- function F(t) {
29
- const e = t;
30
- if (e) {
31
- const n = I(e);
32
- n && (n.stop(), delete e[S]);
33
- }
31
+ function U(e, t) {
32
+ const n = [];
33
+ return C(t.children, e, n), n;
34
34
  }
35
- function H(t) {
36
- return t !== null && typeof t == "object";
35
+ function H(e) {
36
+ return e !== null && typeof e == "object";
37
37
  }
38
- function x(t, e) {
39
- return H(e) ? C(e) : e;
38
+ function x(e, t) {
39
+ return H(t) ? d(t) : t;
40
40
  }
41
- function L(t) {
42
- return F(t);
41
+ function L(e) {
42
+ return T(e);
43
43
  }
44
- function j({ container: t }) {
45
- return (e) => D(e, t);
44
+ function j({ container: e }) {
45
+ return (t) => F(t, e);
46
46
  }
47
47
  function K({
48
- container: t
48
+ container: e
49
49
  }) {
50
- return (e) => U(e, t);
50
+ return (t) => U(t, e);
51
51
  }
52
- function p(t, e) {
53
- let n;
54
- return t ? n = t.createChild() : n = new y(), e && n.bind(M).toConstantValue(R(e)), n.bind(W).toDynamicValue(j), n.bind(k).toDynamicValue(K), n.onActivation(x), n.onDeactivation(L), n;
52
+ function p(e) {
53
+ let t;
54
+ return e ? t = e.createChild() : t = new y(), t.bind(W).toDynamicValue(j), t.bind(k).toDynamicValue(K), t.onActivation(x), t.onDeactivation(L), t;
55
55
  }
56
- const M = new d(
57
- "USE_VUE_SERVICE_CURRENT_COMPONENT"
58
- ), W = new d(
56
+ const W = new h(
59
57
  "USE_REACT_SERVICE_FIND_CHILD_SERVICE"
60
- ), k = new d(
58
+ ), k = new h(
61
59
  "USE_REACT_SERVICE_FIND_CHILDREN_SERVICES"
62
- ), S = Symbol(), u = "USE_VUE_SERVICE_CONTAINER_TOKEN", a = p();
63
- function s(t, e) {
64
- if (typeof e == "function")
65
- e(t);
60
+ ), E = Symbol(), i = "USE_VUE_SERVICE_CONTAINER_TOKEN", f = p();
61
+ function u(e, t) {
62
+ if (typeof t == "function")
63
+ t(e);
66
64
  else
67
- for (let n = 0; n < e.length; n++)
68
- t.bind(e[n]).toSelf();
65
+ for (let n = 0; n < t.length; n++)
66
+ e.bind(t[n]).toSelf();
69
67
  }
70
- function N() {
71
- const t = h();
72
- if (t) {
73
- const e = u, n = t.provides, o = t.parent && t.parent.provides;
68
+ function I() {
69
+ const e = O();
70
+ if (e) {
71
+ const t = i, n = e.provides, o = e.parent && e.parent.provides;
74
72
  if (
75
73
  // 当前实例的provides存在
76
74
  n && // 当前实例的provides已经和parentProvides不是一个对象,说明已经Object.create(parentProvides)
77
75
  n !== o && // provides自身已经具有CONTAINER_TOKEN属性
78
- Object.prototype.hasOwnProperty.call(n, e)
76
+ Object.prototype.hasOwnProperty.call(n, t)
79
77
  )
80
- return n[e];
78
+ return n[t];
81
79
  }
82
80
  }
83
- function _() {
84
- if (b())
85
- return m(u, a);
81
+ function S() {
82
+ if (P())
83
+ return _(i, f);
86
84
  throw new Error("getProvideContainer 只能在 setup 中使用");
87
85
  }
88
- function q(t) {
89
- return (N() || _()).get(t);
86
+ function q(e) {
87
+ return (I() || S()).get(e);
90
88
  }
91
- function z(t) {
92
- return a.get(t);
89
+ function z(e) {
90
+ return f.get(e);
93
91
  }
94
- function J(t, e) {
95
- return e.runWithContext(() => _().get(t));
92
+ function J(e, t) {
93
+ return t.runWithContext(() => S().get(e));
96
94
  }
97
- function Q(t) {
98
- const e = N();
99
- if (e)
100
- s(e, t);
95
+ function M(e) {
96
+ const t = I();
97
+ if (t)
98
+ u(t, e);
101
99
  else {
102
- const n = _(), o = h();
103
- let r = p(n, o);
104
- s(r, t), T(u, r), V(() => {
105
- r.destroy(), r = null;
100
+ const n = S();
101
+ let o = p(n);
102
+ u(o, e), D(i, o), b(() => {
103
+ o.destroy(), o = null;
106
104
  });
107
105
  }
108
106
  }
109
- function X(t) {
110
- s(a, t);
107
+ function Q(e) {
108
+ u(f, e);
111
109
  }
112
- function B(t, e) {
113
- e.runWithContext(() => {
114
- const n = m(
115
- u,
110
+ function B(e, t) {
111
+ t.runWithContext(() => {
112
+ const n = _(
113
+ i,
116
114
  null
117
115
  );
118
116
  if (n)
119
- s(n, t);
117
+ u(n, e);
120
118
  else {
121
- let o = p(a);
122
- s(o, t), e.provide(u, o), e.onUnmount(() => {
119
+ let o = p(f);
120
+ u(o, e), t.provide(i, o), t.onUnmount(() => {
123
121
  o.destroy(), o = null;
124
122
  });
125
123
  }
126
124
  });
127
125
  }
128
- function Z(t) {
129
- return (e) => B(t, e);
126
+ function X(e) {
127
+ return (t) => B(e, t);
130
128
  }
131
- function $(t, e, n) {
132
- const o = n.get, r = n.set;
129
+ function Z(e, t, n) {
130
+ const o = n.get, l = n.set;
133
131
  return {
134
132
  configurable: !0,
135
133
  enumerable: !0,
136
134
  get() {
137
- const c = C(this), f = Symbol.for(e);
138
- let i = c[f];
139
- if (!i) {
140
- const g = w(c).run(() => r ? v({
141
- get: () => o.call(c),
142
- set: (O) => r.call(c, O)
143
- }) : v(() => o.call(c)));
144
- i = R({ value: g }), c[f] = i;
135
+ const r = d(this), s = Symbol.for(t);
136
+ let c = r[s];
137
+ if (!c) {
138
+ const R = w(r).run(() => l ? v({
139
+ get: () => o.call(r),
140
+ set: (g) => l.call(r, g)
141
+ }) : v(() => o.call(r)));
142
+ c = A({ value: R }), r[s] = c;
145
143
  }
146
- return i.value.value;
144
+ return c.value.value;
147
145
  },
148
- set(c) {
149
- const f = C(this), i = Symbol.for(e), l = f[i];
150
- l && r && (l.value.value = c);
146
+ set(r) {
147
+ const s = d(this), c = Symbol.for(t), a = s[c];
148
+ a && l && (a.value.value = r);
151
149
  }
152
150
  };
153
151
  }
154
152
  export {
155
- M as CURRENT_COMPONENT,
156
- $ as Computed,
153
+ Z as Computed,
157
154
  k as FIND_CHILDREN_SERVICES,
158
155
  W as FIND_CHILD_SERVICE,
159
156
  B as declareAppProviders,
160
- Z as declareAppProvidersPlugin,
161
- Q as declareProviders,
162
- X as declareRootProviders,
157
+ X as declareAppProvidersPlugin,
158
+ M as declareProviders,
159
+ Q as declareRootProviders,
163
160
  w as getEffectScope,
161
+ z as getRootService,
164
162
  J as useAppService,
165
- z as useRootService,
166
163
  q as useService
167
164
  };
package/dist/scope.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { EffectScope } from 'vue';
2
2
  export declare function createScope(obj: object): EffectScope;
3
3
  export declare function getScope(obj: object): EffectScope | undefined;
4
- export declare function getEffectScope(obj: object): EffectScope;
5
4
  export declare function removeScope(obj: object): void;
5
+ export declare function getEffectScope(obj: object): EffectScope;
package/dist/scope.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { EffectScope } from 'vue';
2
2
  export declare function createScope(obj: object): EffectScope;
3
3
  export declare function getScope(obj: object): EffectScope | undefined;
4
- export declare function getEffectScope(obj: object): EffectScope;
5
4
  export declare function removeScope(obj: object): void;
5
+ export declare function getEffectScope(obj: object): EffectScope;
package/dist/utils.d.cts CHANGED
@@ -1,3 +1,2 @@
1
1
  import { Container } from '@kaokei/di';
2
- import { ComponentInternalInstance } from 'vue';
3
- export declare function createContainer(parent?: Container, instance?: ComponentInternalInstance | null): Container;
2
+ export declare function createContainer(parent?: Container): Container;
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  import { Container } from '@kaokei/di';
2
- import { ComponentInternalInstance } from 'vue';
3
- export declare function createContainer(parent?: Container, instance?: ComponentInternalInstance | null): Container;
2
+ export declare function createContainer(parent?: Container): Container;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaokei/use-vue-service",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
4
4
  "type": "module",
5
5
  "description": "Lightweight Vue 3 state management with dependency injection, inspired by Angular services.",
6
6
  "main": "./dist/index.cjs",