@etherna/sdk-js
Version:
Etherna SDKs for operations on the network
469 lines (468 loc) • 12.8 kB
JavaScript
import { produce as R } from "immer";
var b = { BASE_URL: "/", MODE: "production", DEV: !1, PROD: !0, SSR: !1 };
const E = /* @__PURE__ */ new Map(), I = (c) => {
const i = E.get(c);
return i ? Object.fromEntries(
Object.entries(i.stores).map(([r, s]) => [r, s.getState()])
) : {};
}, C = (c, i, r) => {
if (c === void 0)
return {
type: "untracked",
connection: i.connect(r)
};
const s = E.get(r.name);
if (s)
return { type: "tracked", store: c, ...s };
const t = {
connection: i.connect(r),
stores: {}
};
return E.set(r.name, t), { type: "tracked", store: c, ...t };
}, D = (c, i = {}) => (r, s, t) => {
const { enabled: e, anonymousActionType: m, store: u, ...S } = i;
let f;
try {
f = (e ?? (b ? "production" : void 0) !== "production") && window.__REDUX_DEVTOOLS_EXTENSION__;
} catch {
}
if (!f)
return (b ? "production" : void 0) !== "production" && e && console.warn(
"[zustand devtools middleware] Please install/enable Redux devtools extension"
), c(r, s, t);
const { connection: l, ...y } = C(u, f, S);
let h = !0;
t.setState = (o, a, d) => {
const n = r(o, a);
if (!h)
return n;
const p = d === void 0 ? { type: m || "anonymous" } : typeof d == "string" ? { type: d } : d;
return u === void 0 ? (l?.send(p, s()), n) : (l?.send(
{
...p,
type: `${u}/${p.type}`
},
{
...I(S.name),
[u]: t.getState()
}
), n);
};
const v = (...o) => {
const a = h;
h = !1, r(...o), h = a;
}, g = c(t.setState, s, t);
if (y.type === "untracked" ? l?.init(g) : (y.stores[y.store] = t, l?.init(
Object.fromEntries(
Object.entries(y.stores).map(([o, a]) => [
o,
o === y.store ? g : a.getState()
])
)
)), t.dispatchFromDevtools && typeof t.dispatch == "function") {
let o = !1;
const a = t.dispatch;
t.dispatch = (...d) => {
(b ? "production" : void 0) !== "production" && d[0].type === "__setState" && !o && (console.warn(
'[zustand devtools middleware] "__setState" action type is reserved to set state from the devtools. Avoid using it.'
), o = !0), a(...d);
};
}
return l.subscribe((o) => {
var a;
switch (o.type) {
case "ACTION":
if (typeof o.payload != "string") {
console.error(
"[zustand devtools middleware] Unsupported action format"
);
return;
}
return w(
o.payload,
(d) => {
if (d.type === "__setState") {
if (u === void 0) {
v(d.state);
return;
}
Object.keys(d.state).length !== 1 && console.error(
`
[zustand devtools middleware] Unsupported __setState action format.
When using 'store' option in devtools(), the 'state' should have only one key, which is a value of 'store' that was passed in devtools(),
and value of this only key should be a state object. Example: { "type": "__setState", "state": { "abc123Store": { "foo": "bar" } } }
`
);
const n = d.state[u];
if (n == null)
return;
JSON.stringify(t.getState()) !== JSON.stringify(n) && v(n);
return;
}
t.dispatchFromDevtools && typeof t.dispatch == "function" && t.dispatch(d);
}
);
case "DISPATCH":
switch (o.payload.type) {
case "RESET":
return v(g), u === void 0 ? l?.init(t.getState()) : l?.init(I(S.name));
case "COMMIT":
if (u === void 0) {
l?.init(t.getState());
return;
}
return l?.init(I(S.name));
case "ROLLBACK":
return w(o.state, (d) => {
if (u === void 0) {
v(d), l?.init(t.getState());
return;
}
v(d[u]), l?.init(I(S.name));
});
case "JUMP_TO_STATE":
case "JUMP_TO_ACTION":
return w(o.state, (d) => {
if (u === void 0) {
v(d);
return;
}
JSON.stringify(t.getState()) !== JSON.stringify(d[u]) && v(d[u]);
});
case "IMPORT_STATE": {
const { nextLiftedState: d } = o.payload, n = (a = d.computedStates.slice(-1)[0]) == null ? void 0 : a.state;
if (!n)
return;
v(u === void 0 ? n : n[u]), l?.send(
null,
// FIXME no-any
d
);
return;
}
case "PAUSE_RECORDING":
return h = !h;
}
return;
}
}), g;
}, T = D, w = (c, i) => {
let r;
try {
r = JSON.parse(c);
} catch (s) {
console.error(
"[zustand devtools middleware] Could not parse the received json",
s
);
}
r !== void 0 && i(r);
};
function O(c, i) {
let r;
try {
r = c();
} catch {
return;
}
return {
getItem: (t) => {
var e;
const m = (S) => S === null ? null : JSON.parse(S, void 0), u = (e = r.getItem(t)) != null ? e : null;
return u instanceof Promise ? u.then(m) : m(u);
},
setItem: (t, e) => r.setItem(
t,
JSON.stringify(e, void 0)
),
removeItem: (t) => r.removeItem(t)
};
}
const _ = (c) => (i) => {
try {
const r = c(i);
return r instanceof Promise ? r : {
then(s) {
return _(s)(r);
},
catch(s) {
return this;
}
};
} catch (r) {
return {
then(s) {
return this;
},
catch(s) {
return _(s)(r);
}
};
}
}, N = (c, i) => (r, s, t) => {
let e = {
getStorage: () => localStorage,
serialize: JSON.stringify,
deserialize: JSON.parse,
partialize: (a) => a,
version: 0,
merge: (a, d) => ({
...d,
...a
}),
...i
}, m = !1;
const u = /* @__PURE__ */ new Set(), S = /* @__PURE__ */ new Set();
let f;
try {
f = e.getStorage();
} catch {
}
if (!f)
return c(
(...a) => {
console.warn(
`[zustand persist middleware] Unable to update item '${e.name}', the given storage is currently unavailable.`
), r(...a);
},
s,
t
);
const l = _(e.serialize), y = () => {
const a = e.partialize({ ...s() });
let d;
const n = l({ state: a, version: e.version }).then(
(p) => f.setItem(e.name, p)
).catch((p) => {
d = p;
});
if (d)
throw d;
return n;
}, h = t.setState;
t.setState = (a, d) => {
h(a, d), y();
};
const v = c(
(...a) => {
r(...a), y();
},
s,
t
);
let g;
const o = () => {
var a;
if (!f)
return;
m = !1, u.forEach((n) => n(s()));
const d = ((a = e.onRehydrateStorage) == null ? void 0 : a.call(e, s())) || void 0;
return _(f.getItem.bind(f))(e.name).then((n) => {
if (n)
return e.deserialize(n);
}).then((n) => {
if (n)
if (typeof n.version == "number" && n.version !== e.version) {
if (e.migrate)
return e.migrate(
n.state,
n.version
);
console.error(
"State loaded from storage couldn't be migrated since no migrate function was provided"
);
} else
return n.state;
}).then((n) => {
var p;
return g = e.merge(
n,
(p = s()) != null ? p : v
), r(g, !0), y();
}).then(() => {
d?.(g, void 0), m = !0, S.forEach((n) => n(g));
}).catch((n) => {
d?.(void 0, n);
});
};
return t.persist = {
setOptions: (a) => {
e = {
...e,
...a
}, a.getStorage && (f = a.getStorage());
},
clearStorage: () => {
f?.removeItem(e.name);
},
getOptions: () => e,
rehydrate: () => o(),
hasHydrated: () => m,
onHydrate: (a) => (u.add(a), () => {
u.delete(a);
}),
onFinishHydration: (a) => (S.add(a), () => {
S.delete(a);
})
}, o(), g || v;
}, x = (c, i) => (r, s, t) => {
let e = {
storage: O(() => localStorage),
partialize: (o) => o,
version: 0,
merge: (o, a) => ({
...a,
...o
}),
...i
}, m = !1;
const u = /* @__PURE__ */ new Set(), S = /* @__PURE__ */ new Set();
let f = e.storage;
if (!f)
return c(
(...o) => {
console.warn(
`[zustand persist middleware] Unable to update item '${e.name}', the given storage is currently unavailable.`
), r(...o);
},
s,
t
);
const l = () => {
const o = e.partialize({ ...s() });
return f.setItem(e.name, {
state: o,
version: e.version
});
}, y = t.setState;
t.setState = (o, a) => {
y(o, a), l();
};
const h = c(
(...o) => {
r(...o), l();
},
s,
t
);
t.getInitialState = () => h;
let v;
const g = () => {
var o, a;
if (!f)
return;
m = !1, u.forEach((n) => {
var p;
return n((p = s()) != null ? p : h);
});
const d = ((a = e.onRehydrateStorage) == null ? void 0 : a.call(e, (o = s()) != null ? o : h)) || void 0;
return _(f.getItem.bind(f))(e.name).then((n) => {
if (n)
if (typeof n.version == "number" && n.version !== e.version) {
if (e.migrate)
return e.migrate(
n.state,
n.version
);
console.error(
"State loaded from storage couldn't be migrated since no migrate function was provided"
);
} else
return n.state;
}).then((n) => {
var p;
return v = e.merge(
n,
(p = s()) != null ? p : h
), r(v, !0), l();
}).then(() => {
d?.(v, void 0), v = s(), m = !0, S.forEach((n) => n(v));
}).catch((n) => {
d?.(void 0, n);
});
};
return t.persist = {
setOptions: (o) => {
e = {
...e,
...o
}, o.storage && (f = o.storage);
},
clearStorage: () => {
f?.removeItem(e.name);
},
getOptions: () => e,
rehydrate: () => g(),
hasHydrated: () => m,
onHydrate: (o) => (u.add(o), () => {
u.delete(o);
}),
onFinishHydration: (o) => (S.add(o), () => {
S.delete(o);
})
}, e.skipHydration || g(), v || h;
}, J = (c, i) => "getStorage" in i || "serialize" in i || "deserialize" in i ? ((b ? "production" : void 0) !== "production" && console.warn(
"[DEPRECATED] `getStorage`, `serialize` and `deserialize` options are deprecated. Use `storage` option instead."
), N(c, i)) : x(c, i), H = J, j = (c) => (i, r, s) => (s.setState = (t, e, ...m) => {
const u = typeof t == "function" ? R(t) : t;
return i(u, e, ...m);
}, c(s.setState, r, s)), k = j;
var A = {};
const P = (c) => {
let i;
const r = /* @__PURE__ */ new Set(), s = (l, y) => {
const h = typeof l == "function" ? l(i) : l;
if (!Object.is(h, i)) {
const v = i;
i = y ?? (typeof h != "object" || h === null) ? h : Object.assign({}, i, h), r.forEach((g) => g(i, v));
}
}, t = () => i, S = { setState: s, getState: t, getInitialState: () => f, subscribe: (l) => (r.add(l), () => r.delete(l)), destroy: () => {
(A ? "production" : void 0) !== "production" && console.warn(
"[DEPRECATED] The `destroy` method will be unsupported in a future version. Instead use unsubscribe function returned by subscribe. Everything will be garbage-collected if store is garbage-collected."
), r.clear();
} }, f = i = c(s, t, S);
return S;
}, z = (c) => P;
var F = /* @__PURE__ */ ((c) => (c[c.Create = 1] = "Create", c[c.Topup = 2] = "Topup", c[c.Dilute = 4] = "Dilute", c))(F || {});
const M = z()(
T(
H(
k((c) => ({
updatingBatches: [],
addBatchUpdate(i, r) {
c((s) => {
const t = "id" in i ? i.id : i.batchID;
s.updatingBatches.findIndex(
(m) => m.id === t && m.depth === i.depth && m.amount === i.amount && m.flag === r
) === -1 && s.updatingBatches.push({
id: t,
depth: i.depth,
amount: i.amount,
flag: r
});
});
},
removeBatchUpdate(i) {
c((r) => {
r.updatingBatches.splice(
r.updatingBatches.findIndex((s) => s.id === i),
1
);
});
}
})),
{
name: "etherna:batches",
storage: O(() => localStorage)
}
),
{
name: "batches"
}
)
);
export {
F as BatchUpdateType,
M as batchesStore
};
//# sourceMappingURL=index.mjs.map