Skip to content

Commit 9b4d87a

Browse files
committed
fix sub()
1 parent b408202 commit 9b4d87a

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export let v =
163163
? cb.splice.bind(cb, cb.push(c) - 1, 1, 0)
164164
: cb.map((f) => f && f((v = c)));
165165

166-
api.any = (target) => (next, error, complete) => target((v) => next(v));
166+
api.any = (target) => (next, error, complete) => target?.(v => next(v));
167167

168168
const num = v(42);
169169
let off = sub(num)(console.log);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "usub",
33
"description": "Subscribe to any reactive sources",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"type": "module",
66
"source": "./src/index.js",
77
"main": "./dist/index.js",

src/index.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const
1515

1616
// API object providing basic functions for handling effects and values
1717
api = {
18-
// Placeholder for any observable value
1918
any: undefined,
2019
// Executes the provided function
2120
effect: (f) => f(),
@@ -39,17 +38,19 @@ const
3938
),
4039

4140
sub = (target, stop, unsub) => (next, error, complete) => target && (
42-
unsub = api?.any?.(target)?.(next, error, complete) ||
43-
unsubr((target[Symbol.observable]?.() || target).subscribe?.((v) => next(get(v)), error, complete), complete) ||
41+
unsub = unsubr((target[Symbol.observable]?.() || target).subscribe?.((v) => next(get(v)), error, complete), complete) ||
4442
((target.call || api.is(target)) && api.effect(() => next(get(target)))) ||
45-
(target.then?.(v => (!stop && next(get(v)), complete?.()), error)) ||
46-
(async v => {
47-
try {
48-
for await (v of target) { if (stop) return; next(get(v)) }
49-
complete?.()
50-
} catch (err) { error?.(err) }
51-
})()
52-
&& (_ => stop = 1),
43+
(
44+
target.then?.(v => (!stop && next(get(v)), complete?.()), error) ||
45+
target[Symbol.asyncIterator] && (async v => {
46+
try {
47+
// FIXME: possible drawback: it will catch error happened in next, not only in iterator
48+
for await (v of target) { if (stop) return; next(get(v)) }
49+
complete?.()
50+
} catch (err) { error?.(err) }
51+
})()
52+
) && (_ => stop = 1) ||
53+
(api?.any?.(target)?.(next, error, complete)),
5354
// register autocleanup
5455
registry.register(target, unsub),
5556
unsub

0 commit comments

Comments
 (0)