chain-store 0.0.2 → 0.0.3
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 +2 -2
- package/dist/index.es.js +134 -170
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,9 +44,9 @@ const squareRoot = setNumber.filter((n: number) => n >= 0).map(Math.sqrt)
|
|
|
44
44
|
|
|
45
45
|
|
|
46
46
|
All actions in a chain are connected, and triggering any of them will trigger
|
|
47
|
-
the others. Each action triggers in the order
|
|
47
|
+
the others. Each action triggers in the order of chaining. Subscriptions
|
|
48
48
|
fire after all callbacks for that action have resolved, so asynchronous callbacks
|
|
49
|
-
|
|
49
|
+
will take longer to process.
|
|
50
50
|
|
|
51
51
|
```ts
|
|
52
52
|
/* Subscribe to see the output */
|
package/dist/index.es.js
CHANGED
|
@@ -1,201 +1,165 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
let
|
|
4
|
-
const
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
}),
|
|
1
|
+
const B = () => j(), C = (n) => {
|
|
2
|
+
const e = [], o = [];
|
|
3
|
+
let r = {}, u = [], t = !1, s = n;
|
|
4
|
+
const d = (c) => {
|
|
5
|
+
var a;
|
|
6
|
+
return (a = y(e, void 0, (h) => h[0] === c)) == null ? void 0 : a[1].dispose();
|
|
7
|
+
}, g = {
|
|
8
|
+
on: (c, a) => (d(c), e.push([
|
|
9
|
+
c,
|
|
10
|
+
c.watch((...h) => {
|
|
11
|
+
var f;
|
|
12
|
+
t = !0;
|
|
13
|
+
const i = o.map(([b]) => b(s)), l = a(s, ...h);
|
|
14
|
+
l !== s && (s = l, (f = r.emit) == null || f.call(r, s)), o.forEach(([b, w], x) => {
|
|
15
|
+
const A = b(s);
|
|
16
|
+
A !== (i == null ? void 0 : i[x]) && w(A);
|
|
17
|
+
}), t = !1, u.forEach((b) => b()), u = [];
|
|
18
18
|
})
|
|
19
|
-
]),
|
|
20
|
-
off: (
|
|
21
|
-
map: (
|
|
22
|
-
getState: () =>
|
|
23
|
-
watch: (
|
|
19
|
+
]), g),
|
|
20
|
+
off: (c) => (d(c), g),
|
|
21
|
+
map: (c) => ({
|
|
22
|
+
getState: () => c(s),
|
|
23
|
+
watch: (a) => (o.push([c, a]), {
|
|
24
24
|
dispose: () => {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (s) return n.push(l);
|
|
32
|
-
l();
|
|
25
|
+
const h = () => y(
|
|
26
|
+
o,
|
|
27
|
+
void 0,
|
|
28
|
+
([i, l]) => i === c && l === a
|
|
29
|
+
);
|
|
30
|
+
t ? u.push(h) : h();
|
|
33
31
|
}
|
|
34
32
|
})
|
|
35
33
|
}),
|
|
36
|
-
getState: () =>
|
|
37
|
-
watch: (
|
|
38
|
-
i.init("watch").watch(o);
|
|
39
|
-
}
|
|
34
|
+
getState: () => s,
|
|
35
|
+
watch: S(r)
|
|
40
36
|
};
|
|
41
|
-
return
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
return g;
|
|
38
|
+
}, j = (n, e, o, r) => {
|
|
39
|
+
let u = {};
|
|
40
|
+
const t = [], s = (c, a) => {
|
|
41
|
+
var h;
|
|
42
|
+
(h = u.emit) == null || h.call(u, c), t.forEach((i) => i(c, a));
|
|
43
|
+
}, d = (c, a) => {
|
|
46
44
|
try {
|
|
47
|
-
|
|
45
|
+
if (o) return q(c, o, (h) => s(h, a));
|
|
46
|
+
if (r)
|
|
47
|
+
return q(
|
|
48
|
+
c,
|
|
49
|
+
r,
|
|
50
|
+
(h) => h && s(c, a)
|
|
51
|
+
);
|
|
52
|
+
s(c, a);
|
|
48
53
|
} catch {
|
|
49
54
|
}
|
|
50
55
|
};
|
|
51
|
-
return
|
|
52
|
-
|
|
56
|
+
return e == null || e(d), Object.assign((c) => {
|
|
57
|
+
(n ?? d)(c);
|
|
53
58
|
}, {
|
|
54
|
-
|
|
55
|
-
map: (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
map: async (u) => {
|
|
64
|
-
const o = t != null && t.map ? await t.map(u) : u;
|
|
65
|
-
if (!await t.filter(
|
|
66
|
-
o
|
|
67
|
-
)) throw new Error();
|
|
68
|
-
return o;
|
|
69
|
-
},
|
|
70
|
-
target: e,
|
|
71
|
-
options: a
|
|
72
|
-
}) : E(c, {
|
|
73
|
-
map: t == null ? void 0 : t.map,
|
|
74
|
-
target: e,
|
|
75
|
-
options: a
|
|
76
|
-
})
|
|
59
|
+
watch: S(u),
|
|
60
|
+
map: (c) => j(n ?? d, E(t), c),
|
|
61
|
+
filter: (c) => j(n ?? d, E(t), void 0, c),
|
|
62
|
+
target: (c, a) => K(
|
|
63
|
+
n ?? d,
|
|
64
|
+
E(t),
|
|
65
|
+
c,
|
|
66
|
+
a
|
|
67
|
+
)
|
|
77
68
|
});
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
69
|
+
}, K = (n, e, o, r) => {
|
|
70
|
+
let u = {}, t = {}, s = {}, d = {};
|
|
71
|
+
const g = [], c = async (i, l) => {
|
|
72
|
+
var x, A, O, P, D, m;
|
|
73
|
+
const f = o(i);
|
|
74
|
+
if (!(f instanceof Promise)) {
|
|
75
|
+
(x = u.emit) == null || x.call(u, i), (A = t.emit) == null || A.call(t, f, i), l == null || l.done(f, i);
|
|
84
76
|
return;
|
|
85
77
|
}
|
|
86
|
-
const
|
|
87
|
-
!
|
|
88
|
-
try {
|
|
89
|
-
const h = await o;
|
|
90
|
-
(!d || !l) && r.emit("done")(h, a), u == null || u.done(h, a);
|
|
91
|
-
} catch (h) {
|
|
92
|
-
(!d || !l) && r.emit("fail")(h, a), u == null || u.fail(h, a);
|
|
93
|
-
} finally {
|
|
94
|
-
if (!d || !l)
|
|
95
|
-
r.emit("finally")(a);
|
|
96
|
-
else {
|
|
97
|
-
const h = i.findIndex((m) => m === o);
|
|
98
|
-
h >= 0 && i.splice(h, 1);
|
|
99
|
-
}
|
|
100
|
-
u == null || u.finish(a);
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}, s = async (a, u) => {
|
|
104
|
-
if (!t.map) return await n(a, u);
|
|
78
|
+
const b = g.includes(f), w = (r == null ? void 0 : r.skipDuplicates) !== !1;
|
|
79
|
+
!b && w && g.push(f), (!b || !w) && ((O = u.emit) == null || O.call(u, i));
|
|
105
80
|
try {
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
} catch {
|
|
81
|
+
const p = await f;
|
|
82
|
+
(!b || !w) && ((P = t.emit) == null || P.call(t, p, i)), l == null || l.done(p, i);
|
|
83
|
+
} catch (p) {
|
|
84
|
+
(!b || !w) && ((D = s.emit) == null || D.call(s, p, i)), l == null || l.fail(p, i);
|
|
85
|
+
} finally {
|
|
86
|
+
!b || !w ? (m = d.emit) == null || m.call(d, i) : window.requestAnimationFrame(() => {
|
|
87
|
+
y(g, f);
|
|
88
|
+
}), l == null || l.finish(i);
|
|
109
89
|
}
|
|
110
90
|
};
|
|
111
|
-
return
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
91
|
+
return e(async (i, l) => {
|
|
92
|
+
try {
|
|
93
|
+
await c(i, l);
|
|
94
|
+
} catch {
|
|
95
|
+
}
|
|
96
|
+
}), Object.assign((i) => {
|
|
97
|
+
const { events: l, methods: f } = I();
|
|
98
|
+
return n(i, l), f;
|
|
116
99
|
}, {
|
|
117
|
-
|
|
118
|
-
done:
|
|
119
|
-
fail:
|
|
120
|
-
finish:
|
|
100
|
+
watch: S(u),
|
|
101
|
+
done: { watch: S(t) },
|
|
102
|
+
fail: { watch: S(s) },
|
|
103
|
+
finish: { watch: S(d) }
|
|
121
104
|
});
|
|
122
|
-
},
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
},
|
|
129
|
-
|
|
130
|
-
const n = ((s = t == null ? void 0 : t.map) == null ? void 0 : s.call(t, i)) ?? i;
|
|
131
|
-
return n instanceof Promise ? n.then(x(c, t == null ? void 0 : t.filter, r)).catch(() => {
|
|
132
|
-
if (r) throw new Error();
|
|
133
|
-
return !1;
|
|
134
|
-
}) : x(c, t == null ? void 0 : t.filter, r)(n);
|
|
135
|
-
}, A = (c) => {
|
|
136
|
-
const t = {}, r = c ?? {
|
|
105
|
+
}, q = (n, e, o) => {
|
|
106
|
+
if (!e) return o(n);
|
|
107
|
+
const r = e(n);
|
|
108
|
+
if (!(r instanceof Promise)) return o(r);
|
|
109
|
+
r.then(o).catch(() => {
|
|
110
|
+
});
|
|
111
|
+
}, F = ["done", "fail", "finish"], I = (n) => {
|
|
112
|
+
const e = {}, o = n ?? {
|
|
137
113
|
state: "pending",
|
|
138
|
-
stored:
|
|
139
|
-
set: (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
r.stored.params = s, (e = t.finish) == null || e.call(t, s);
|
|
155
|
-
}
|
|
156
|
-
}, n = (s, ...e) => t[s] ? void 0 : (a) => (t[s] = a, r.state !== "pending" && a(...e.map((u) => r.stored[u])), A(r).methods);
|
|
114
|
+
stored: [],
|
|
115
|
+
set: (t) => o.state = t,
|
|
116
|
+
controller: new z(() => o.state)
|
|
117
|
+
}, r = F.reduce(
|
|
118
|
+
(t, s, d) => ({
|
|
119
|
+
...t,
|
|
120
|
+
[s]: (g, c) => {
|
|
121
|
+
var a;
|
|
122
|
+
o.stored[d] = g, d < 2 && o.set(s), (a = e[s]) == null || a.call(e, g, c);
|
|
123
|
+
}
|
|
124
|
+
}),
|
|
125
|
+
{}
|
|
126
|
+
), u = (t, s) => ({
|
|
127
|
+
[t]: e[t] ? void 0 : (d) => (e[t] = d, o.state !== "pending" && //@ts-expect-error
|
|
128
|
+
d(o.stored[s], s < 2 ? o.stored[2] : void 0), I(o).methods)
|
|
129
|
+
});
|
|
157
130
|
return {
|
|
158
|
-
events:
|
|
159
|
-
methods: Object.assign(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
131
|
+
events: r,
|
|
132
|
+
methods: Object.assign(
|
|
133
|
+
o.controller,
|
|
134
|
+
F.reduce(
|
|
135
|
+
(t, s, d) => ({
|
|
136
|
+
...t,
|
|
137
|
+
...u(s, d)
|
|
138
|
+
}),
|
|
139
|
+
{}
|
|
140
|
+
)
|
|
141
|
+
)
|
|
164
142
|
};
|
|
165
143
|
};
|
|
166
|
-
class
|
|
167
|
-
constructor(
|
|
168
|
-
this.getState =
|
|
144
|
+
class z {
|
|
145
|
+
constructor(e) {
|
|
146
|
+
this.getState = e;
|
|
169
147
|
}
|
|
170
148
|
get state() {
|
|
171
149
|
return this.getState();
|
|
172
150
|
}
|
|
173
151
|
}
|
|
174
|
-
const
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
};
|
|
184
|
-
return {
|
|
185
|
-
init: (i) => ({
|
|
186
|
-
watch: (n) => (r(i, n), {
|
|
187
|
-
dispose: () => t(i, n)
|
|
188
|
-
})
|
|
189
|
-
}),
|
|
190
|
-
emit: (i) => (...n) => {
|
|
191
|
-
var s;
|
|
192
|
-
(s = c[i]) == null || s.forEach(
|
|
193
|
-
(e) => e(...n)
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
};
|
|
152
|
+
const E = (n) => (e) => n.push(e), y = (n, e, o) => {
|
|
153
|
+
const r = n.findIndex(o ?? ((u) => e === u));
|
|
154
|
+
return r >= 0 ? n.splice(r, 1)[0] : void 0;
|
|
155
|
+
}, S = (n) => (e) => {
|
|
156
|
+
if (n != null && n.watch) return n.watch(e);
|
|
157
|
+
const o = [], r = (t) => y(o, t), u = (t) => !o.includes(t) && o.push(t);
|
|
158
|
+
return n.watch = (t) => (u(t), {
|
|
159
|
+
dispose: () => r(t)
|
|
160
|
+
}), n.emit = (...t) => o.forEach((s) => s(...t)), n.watch(e);
|
|
197
161
|
};
|
|
198
162
|
export {
|
|
199
|
-
|
|
200
|
-
|
|
163
|
+
B as createAction,
|
|
164
|
+
C as createStore
|
|
201
165
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(w,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(w=typeof globalThis<"u"?globalThis:w||self,y(w["chain-store"]={}))})(this,function(w){"use strict";const y=()=>E(),M=n=>{const s=[],e=[];let r={},u=[],t=!1,o=n;const d=c=>{var l;return(l=x(s,void 0,a=>a[0]===c))==null?void 0:l[1].dispose()},g={on:(c,l)=>(d(c),s.push([c,c.watch((...a)=>{var h;t=!0;const i=e.map(([b])=>b(o)),f=l(o,...a);f!==o&&(o=f,(h=r.emit)==null||h.call(r,o)),e.forEach(([b,p],m)=>{const A=b(o);A!==(i==null?void 0:i[m])&&p(A)}),t=!1,u.forEach(b=>b()),u=[]})]),g),off:c=>(d(c),g),map:c=>({getState:()=>c(o),watch:l=>(e.push([c,l]),{dispose:()=>{const a=()=>x(e,void 0,([i,f])=>i===c&&f===l);t?u.push(a):a()}})}),getState:()=>o,watch:S(r)};return g},E=(n,s,e,r)=>{let u={};const t=[],o=(c,l)=>{var a;(a=u.emit)==null||a.call(u,c),t.forEach(i=>i(c,l))},d=(c,l)=>{try{if(e)return P(c,e,a=>o(a,l));if(r)return P(c,r,a=>a&&o(c,l));o(c,l)}catch{}};return s==null||s(d),Object.assign(c=>{(n??d)(c)},{watch:S(u),map:c=>E(n??d,O(t),c),filter:c=>E(n??d,O(t),void 0,c),target:(c,l)=>z(n??d,O(t),c,l)})},z=(n,s,e,r)=>{let u={},t={},o={},d={};const g=[],c=async(i,f)=>{var m,A,q,F,I,K;const h=e(i);if(!(h instanceof Promise)){(m=u.emit)==null||m.call(u,i),(A=t.emit)==null||A.call(t,h,i),f==null||f.done(h,i);return}const b=g.includes(h),p=(r==null?void 0:r.skipDuplicates)!==!1;!b&&p&&g.push(h),(!b||!p)&&((q=u.emit)==null||q.call(u,i));try{const j=await h;(!b||!p)&&((F=t.emit)==null||F.call(t,j,i)),f==null||f.done(j,i)}catch(j){(!b||!p)&&((I=o.emit)==null||I.call(o,j,i)),f==null||f.fail(j,i)}finally{!b||!p?(K=d.emit)==null||K.call(d,i):window.requestAnimationFrame(()=>{x(g,h)}),f==null||f.finish(i)}};return s(async(i,f)=>{try{await c(i,f)}catch{}}),Object.assign(i=>{const{events:f,methods:h}=D();return n(i,f),h},{watch:S(u),done:{watch:S(t)},fail:{watch:S(o)},finish:{watch:S(d)}})},P=(n,s,e)=>{if(!s)return e(n);const r=s(n);if(!(r instanceof Promise))return e(r);r.then(e).catch(()=>{})},T=["done","fail","finish"],D=n=>{const s={},e=n??{state:"pending",stored:[],set:t=>e.state=t,controller:new B(()=>e.state)},r=T.reduce((t,o,d)=>({...t,[o]:(g,c)=>{var l;e.stored[d]=g,d<2&&e.set(o),(l=s[o])==null||l.call(s,g,c)}}),{}),u=(t,o)=>({[t]:s[t]?void 0:d=>(s[t]=d,e.state!=="pending"&&d(e.stored[o],o<2?e.stored[2]:void 0),D(e).methods)});return{events:r,methods:Object.assign(e.controller,T.reduce((t,o,d)=>({...t,...u(o,d)}),{}))}};class B{constructor(s){this.getState=s}get state(){return this.getState()}}const O=n=>s=>n.push(s),x=(n,s,e)=>{const r=n.findIndex(e??(u=>s===u));return r>=0?n.splice(r,1)[0]:void 0},S=n=>s=>{if(n!=null&&n.watch)return n.watch(s);const e=[],r=t=>x(e,t),u=t=>!e.includes(t)&&e.push(t);return n.watch=t=>(u(t),{dispose:()=>r(t)}),n.emit=(...t)=>e.forEach(o=>o(...t)),n.watch(s)};w.createAction=y,w.createStore=M,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
|
package/dist/types.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
type ActionCall<Params, Result extends unknown = void> = (params: Params) => Result;
|
|
2
|
-
export type ActionPromise<Params, Result, Error, Methods =
|
|
2
|
+
export type ActionPromise<Params, Result, Error, Methods = ActionPromiseEvents<Params, Result, Error>, Remaining extends keyof Methods = keyof Methods> = {
|
|
3
3
|
state: "pending" | "done" | "fail";
|
|
4
4
|
} & {
|
|
5
5
|
[K in Remaining]: (fn: Methods[K]) => ActionPromise<Params, Result, Error, Methods, Exclude<Remaining, K>>;
|
|
6
6
|
};
|
|
7
|
-
export type
|
|
7
|
+
export type ActionPromiseEvents<Params = any, Result = any, Error = any> = {
|
|
8
8
|
done: (result: Result, params: Params) => void;
|
|
9
9
|
fail: (error: Error, params: Params) => void;
|
|
10
10
|
finish: (params: Params) => void;
|
|
@@ -20,7 +20,7 @@ export interface Subscription {
|
|
|
20
20
|
export interface Unit<Params> {
|
|
21
21
|
watch: (watcher: (...args: Args<Params>) => void) => Subscription;
|
|
22
22
|
}
|
|
23
|
-
export interface Action<Input, Output = Input> extends ActionCall<Input>, Unit<
|
|
23
|
+
export interface Action<Input, Output = Input> extends ActionCall<Input>, Unit<Output> {
|
|
24
24
|
filter: (fn: ChainFn<Output, boolean>) => Action<Input, Output>;
|
|
25
25
|
map: <Mapped>(fn: ChainFn<Output, Mapped>) => Action<Input, Mapped>;
|
|
26
26
|
target: <Result, Err = Error>(fn: ChainFn<Output, Result>) => AsyncAction<Input, Result, Output, Err>;
|