@zedux/core
Version:
A high-level, declarative, composable form of Redux
452 lines (439 loc) • 11.8 kB
JavaScript
const H = (t) => {
const e = Object.getPrototypeOf(t);
return e ? Object.getPrototypeOf(e) ? "complex object" : "object" : "prototype-less object";
}, ot = (t) => {
const e = typeof t;
return e !== "object" ? e : t ? Array.isArray(t) ? "array" : H(t) : "null";
}, nt = (t) => {
const e = (r) => {
const s = {
type: e.type
};
return typeof r < "u" && (s.payload = r), s;
};
return e.type = t, e;
}, N = (t, e) => typeof t == "string" ? t : t.type, P = (t, e) => t.map((r) => N(r)), it = (t) => {
const e = {}, r = (s = t, o) => {
const n = e[o.type] || [];
return M(n, s, o);
};
return r.reduce = (s, o) => {
const n = Array.isArray(s) ? P(s) : [N(s)];
return C(e, n, o), r;
}, r;
}, C = (t, e, r) => {
e.forEach((s) => {
t[s] || (t[s] = []), t[s].push(r);
});
}, M = (t, e, r) => t.reduce(
(s, o) => o(s, r.payload, r),
e
), z = (t, e) => {
var r;
return ((r = t == null ? void 0 : t.constructor) == null ? void 0 : r.$$typeof) === e.$$typeof;
}, j = (t) => {
if (typeof t != "object" || !t)
return !1;
const e = Object.getPrototypeOf(t);
return e ? Object.getPrototypeOf(e) === null : !1;
}, d = "@@zedux/", c = {
batch: `${d}batch`,
delegate: `${d}delegate`,
hydrate: `${d}hydrate`,
ignore: `${d}ignore`,
// for use with atoms in the @zedux/atoms package
inherit: `${d}inherit`,
merge: `${d}merge`,
prime: `${d}prime`
}, p = 1, l = 2, w = 3, A = 4, I = Symbol.for("zedux.store"), B = (t, e, r) => {
const s = {};
return Object.entries(t).forEach(([o, n]) => {
const i = [...r, o];
s[o] = R(
n,
e,
i
);
}), s;
}, F = (t, e, r, s) => t === l ? { type: t } : t === w ? { type: t, reducer: e } : {
type: t,
destroy: r(s, e),
reducer: v(e),
store: e
}, k = (t) => typeof t == "function" ? w : t && z(t, f) ? A : j(t) ? p : l, R = (t, e, r = []) => {
const s = k(t);
return s !== p ? F(
s,
t,
e,
r
) : {
type: s,
children: B(
t,
e,
r
)
};
}, v = (t) => (r, s) => {
(s.type === c.hydrate || s.type === c.merge) && (s = {
type: c.hydrate,
payload: r
});
const o = {
metaType: c.inherit,
payload: s
};
return t.dispatch(o);
}, K = (t, { create: e, get: r, isNode: s, set: o, size: n }) => (i = e(), u) => {
let h = e(), a = !1;
return Object.keys(t).forEach((y) => {
const { reducer: D } = t[y], m = s(i) ? r(i, y) : void 0, S = D(m, u);
h = o(h, y, S), a || (a = S !== m);
}), a || (a = !s(i) || !Object.keys(t).length && !!n(i)), a ? h : i;
}, _ = (t) => {
if (!t)
return;
const { children: e, destroy: r } = t;
r && r(), e && Object.values(e).forEach(_);
}, b = (t, e, r) => {
const s = { ...t.children };
return Object.keys(e.children).forEach((o) => {
var h;
const n = e.children[o], i = (h = t.children) == null ? void 0 : h[o], u = E(i, n, r);
if (u.type === l) {
delete s[o];
return;
}
s[o] = u;
}), {
children: s,
reducer: K(s, r),
type: p
};
}, E = (t, e, r) => e.type !== p ? (_(t), e) : !t || t.type !== p ? (_(t), b({ type: l }, e, r)) : b(t, e, r), T = (t, e, r) => {
if (!r.isNode(t) || !r.isNode(e))
return [e, e !== t];
let s = !1;
const o = r.clone(t);
return r.iterate(e, (n, i) => {
const u = r.get(o, n), [h, a] = r.isNode(i) ? (
// Recursively merge the nested nodes.
T(u, i, r)
) : (
// Not a nested node (anymore, at least)
[i, i !== u]
);
a && (s || (s = a), r.set(o, n, h));
}), [s ? o : t, s];
}, X = (t, e, r) => !e || !r ? t.payload : (e.payload = t.payload, r), q = (t, e) => {
for (; t.metaType; ) {
if (t.metaType === e)
return t.metaData;
t = t.payload;
}
}, G = (t) => {
for (; t.metaType; )
t = t.payload;
return t;
}, J = (t, e) => {
let r = t, s = null, o = null;
for (; r.metaType; ) {
if (r.metaType === e)
return X(r, s, o);
const n = { ...r };
s && (s.payload = n), s = n, r = r.payload, o || (o = s);
}
return t;
}, g = "Minified Error", L = (t, e) => {
for (const r of e) {
if (t.type !== p)
throw new ReferenceError(g);
if (t = t.children[r], !t)
throw new ReferenceError(g);
}
return t;
}, Q = (t, e) => {
const r = q(e, c.delegate);
if (!r || !t)
return !1;
const s = L(t, r);
if (s.type !== A)
throw new TypeError(g);
s.store.dispatch(J(e, c.delegate));
}, x = (t, e, r, s) => {
if (!e.length)
return r;
const o = s.clone(t), n = e[0];
return s.set(
o,
n,
x(
s.get(t, n),
e.slice(1),
r,
s
)
);
}, U = (t) => ({ ...t }), W = () => ({}), Y = (t, e) => t[e], Z = j, V = (t, e) => {
Object.entries(t).forEach(([r, s]) => e(r, s));
}, tt = (t, e, r) => (t[e] = r, t), et = (t) => Object.keys(t).length, rt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
clone: U,
create: W,
get: Y,
isNode: Z,
iterate: V,
set: tt,
size: et
}, Symbol.toStringTag, { value: "Module" })), st = {
scheduleNow: (t) => t.task()
}, O = { type: c.prime }, ct = (t, e) => new f(t, e), $ = class {
constructor(t, e) {
this._isSolo = !0, this._subscribers = [], this.dispatch = (r) => this._isSolo ? this._dispatch(r) : (this._scheduler.scheduleNow({
task: () => this._dispatch(r),
type: 0
// UpdateStore (0)
}), this._state), this.setState = (r, s) => this._isSolo ? this._setState(
r,
s
) : (this._scheduler.scheduleNow({
task: () => this._setState(
r,
s
),
type: 0
// UpdateStore (0)
}), this._state), this._state = e, this._scheduler = $._scheduler || st, t && this.use(t);
}
actionStream() {
return {
[Symbol.observable]() {
return this;
},
"@@observable"() {
return this;
},
subscribe: (t) => this.subscribe({
effects: ({ action: e, error: r }) => {
var s, o;
r && typeof t != "function" ? (s = t.error) == null || s.call(t, r) : e && (typeof t == "function" ? t(e) : (o = t.next) == null || o.call(t, e));
}
})
};
}
/**
Returns the current state of the store.
Do not mutate the returned value.
*/
getState() {
return this._state;
}
/**
Applies a partial state update to the store.
Accepts either a deep partial state object or a function that accepts the
current state and returns a deep partial state object.
This method only recursively traverses normal JS objects. If your store
deeply nests any other data structure, including arrays or maps, you'll have
to deeply merge them yourself using `store.setState()`.
Dispatches the special `merge` action to the store's reducers. This action's
`payload` property will be set to the resolved partial state update. Effects
subscribers can record this action to implement time travel.
IMPORTANT: Deep setting cannot remove properties from the state tree. Use
`store.setState()` for that.
Throws an error if called from the reducer layer.
Returns the new state.
Unlike setState, setStateDeep is not bound. You must call it with context -
e.g. by using dot-notation: `store.setStateDeep(...)`
*/
setStateDeep(t, e) {
return this._isSolo ? this._setState(
t,
e,
!0
) : (this._scheduler.scheduleNow({
task: () => this._setState(
t,
e,
!0
),
type: 0
// UpdateStore (0)
}), this._state);
}
/**
Registers a subscriber with the store.
The subscriber will be notified every time the store's state
changes.
Returns a subscription object. Calling `subscription.unsubscribe()`
unregisters the subscriber.
*/
subscribe(t) {
const e = typeof t == "function" ? { next: t } : t;
return this._subscribers.push(e), {
unsubscribe: () => {
this._subscribers = this._subscribers.filter(
(s) => s !== e
);
}
};
}
/**
Merges a hierarchy descriptor into the existing hierarchy descriptor.
Intelligently diffs the two hierarchies and only creates/recreates the
necessary reducers.
Dispatches the special `prime` action to the store.
*/
use(t) {
const e = R(
t,
(s, o) => this._registerChildStore(s, o)
), r = this._tree = E(
this._tree,
e,
this.constructor.hierarchyConfig
);
return (this._rootReducer = r.reducer) && this._dispatchAction(O, O, this._state), this;
}
/**
* Only for internal use.
*/
_register(t) {
const e = this._parents || (this._parents = []);
return e.push(t), this._isSolo = !1, () => {
const r = e.indexOf(t);
r > -1 && e.splice(r, 1);
};
}
[Symbol.observable]() {
return this;
}
"@@observable"() {
return this;
}
_dispatch(t) {
return Q(this._tree, t) !== !1 ? this._state : this._routeAction(t);
}
_dispatchAction(t, e, r) {
this._isDispatching = !0;
let s, o = r;
try {
this._rootReducer && (o = this._rootReducer(r, e));
} catch (n) {
throw s = n, n;
} finally {
this._isDispatching = !1, this._notify(o, t, s);
}
return o;
}
_dispatchHydration(t, e, r) {
const s = e === c.hydrate ? t : T(
this._state,
t,
this.constructor.hierarchyConfig
)[0];
if (s === this._state && !r)
return this._state;
const o = {
meta: r,
payload: s,
type: e
};
return this._dispatchAction(o, o, s);
}
_dispatchStateSetter(t, e, r) {
let s;
try {
s = t(this._state);
} catch (o) {
throw o;
}
return this._dispatchHydration(
s,
r ? c.merge : c.hydrate,
e
);
}
_doNotify(t) {
const { _subscribers: e } = this, r = t.newState !== t.oldState;
for (const s of e)
t.error && s.error && s.error(t.error), r && s.next && s.next(
t.newState,
t.oldState,
t.action
), s.effects && s.effects(t);
}
_notify(t, e, r) {
var o;
const s = {
action: e,
error: r,
newState: t,
oldState: this._state,
store: this
};
if (this._state = t, this._isSolo)
return this._doNotify(s);
this._scheduler.scheduleNow({
task: () => this._doNotify(s),
type: 1
// InformSubscribers (1)
}), (o = this._parents) == null || o.forEach((n) => n(s));
}
_registerChildStore(t, e) {
this._isSolo = !1;
const r = ({
action: s,
error: o,
newState: n,
oldState: i
}) => {
if (this._isDispatching)
return;
const u = n === i ? this._state : x(
this._state,
t,
n,
this.constructor.hierarchyConfig
), h = {
metaType: c.delegate,
metaData: t,
payload: s
};
this._notify(u, h, o);
};
return e._register(r);
}
_routeAction(t) {
const e = G(t);
return e.type === c.hydrate || e.type === c.merge ? this._dispatchHydration(
e.payload,
e.type,
e.meta
) : this._dispatchAction(t, e, this._state);
}
_setState(t, e, r = !1) {
return typeof t == "function" ? this._dispatchStateSetter(t, e, r) : this._dispatchHydration(
t,
r ? c.merge : c.hydrate,
e
);
}
};
let f = $;
f.hierarchyConfig = rt;
f.$$typeof = I;
export {
f as Store,
nt as actionFactory,
it as createReducer,
ct as createStore,
ot as detailedTypeof,
q as getMetaData,
z as is,
j as isPlainObject,
G as removeAllMeta,
J as removeMeta,
c as zeduxTypes
};
//# sourceMappingURL=zedux.es.min.js.map