@kaokei/use-vue-service
Version:
Lightweight Vue 3 state management with dependency injection, inspired by Angular services.
165 lines (164 loc) • 3.6 kB
JavaScript
import { Container as y, Token as h } from "@kaokei/di";
export * from "@kaokei/di";
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";
function V(e) {
const t = e, n = N(!0);
return t[E] = n, n;
}
function m(e) {
return e[E];
}
function T(e) {
const t = e;
if (t) {
const n = m(t);
n && (n.stop(), delete t[E]);
}
}
function w(e) {
const t = m(e);
return t || V(e);
}
function C(e, t, n) {
return e && e.forEach((o) => {
o && o.isCurrentBound(t) && n.push(o.get(t)), o.children && C(o.children, t, n);
}), n;
}
function F(e, t) {
const n = [];
return C(t.children, e, n), n[0];
}
function U(e, t) {
const n = [];
return C(t.children, e, n), n;
}
function H(e) {
return e !== null && typeof e == "object";
}
function x(e, t) {
return H(t) ? d(t) : t;
}
function L(e) {
return T(e);
}
function j({ container: e }) {
return (t) => F(t, e);
}
function K({
container: e
}) {
return (t) => U(t, e);
}
function p(e) {
let t;
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;
}
const W = new h(
"USE_REACT_SERVICE_FIND_CHILD_SERVICE"
), k = new h(
"USE_REACT_SERVICE_FIND_CHILDREN_SERVICES"
), E = Symbol(), i = "USE_VUE_SERVICE_CONTAINER_TOKEN", f = p();
function u(e, t) {
if (typeof t == "function")
t(e);
else
for (let n = 0; n < t.length; n++)
e.bind(t[n]).toSelf();
}
function I() {
const e = O();
if (e) {
const t = i, n = e.provides, o = e.parent && e.parent.provides;
if (
// 当前实例的provides存在
n && // 当前实例的provides已经和parentProvides不是一个对象,说明已经Object.create(parentProvides)
n !== o && // provides自身已经具有CONTAINER_TOKEN属性
Object.prototype.hasOwnProperty.call(n, t)
)
return n[t];
}
}
function S() {
if (P())
return _(i, f);
throw new Error("getProvideContainer 只能在 setup 中使用");
}
function q(e) {
return (I() || S()).get(e);
}
function z(e) {
return f.get(e);
}
function J(e, t) {
return t.runWithContext(() => S().get(e));
}
function M(e) {
const t = I();
if (t)
u(t, e);
else {
const n = S();
let o = p(n);
u(o, e), D(i, o), b(() => {
o.destroy(), o = null;
});
}
}
function Q(e) {
u(f, e);
}
function B(e, t) {
t.runWithContext(() => {
const n = _(
i,
null
);
if (n)
u(n, e);
else {
let o = p(f);
u(o, e), t.provide(i, o), t.onUnmount(() => {
o.destroy(), o = null;
});
}
});
}
function X(e) {
return (t) => B(e, t);
}
function Z(e, t, n) {
const o = n.get, l = n.set;
return {
configurable: !0,
enumerable: !0,
get() {
const r = d(this), s = Symbol.for(t);
let c = r[s];
if (!c) {
const R = w(r).run(() => l ? v({
get: () => o.call(r),
set: (g) => l.call(r, g)
}) : v(() => o.call(r)));
c = A({ value: R }), r[s] = c;
}
return c.value.value;
},
set(r) {
const s = d(this), c = Symbol.for(t), a = s[c];
a && l && (a.value.value = r);
}
};
}
export {
Z as Computed,
k as FIND_CHILDREN_SERVICES,
W as FIND_CHILD_SERVICE,
B as declareAppProviders,
X as declareAppProvidersPlugin,
M as declareProviders,
Q as declareRootProviders,
w as getEffectScope,
z as getRootService,
J as useAppService,
q as useService
};