Skip to content

Commit 61f3e1c

Browse files
committed
reduce the code little bit
1 parent 3531467 commit 61f3e1c

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

lite/index.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
const
22
// FinalizationRegistry to clean up subscriptions when objects are garbage collected
3-
registry = new FinalizationRegistry(unsub => (unsub && unsub?.call?.())),
3+
registry = new FinalizationRegistry(unsub => unsub?.call()),
44

55
// API object providing basic functions for handling effects and values
66
api = {
77
// Handle any reactive subscription
8-
any: undefined,
9-
// If any cleanup is requested
10-
cleanup: undefined,
8+
// any: undefined,
9+
// // If any cleanup is requested
10+
// cleanup: undefined,
1111
// Executes the provided function
12-
effect: (f) => f(),
12+
effect: f => f(),
1313
// Returns false for any value (placeholder implementation)
14-
is: (v) => v?.call,
14+
is: v => v?.call,
1515
// Retrieves the value (returns it as is)
16-
get: (v) => v?.call(),
16+
get: v => v?.call(),
1717
},
1818

1919
// Utility function to handle and unwrap values of signals, observable, etc especially functions
@@ -26,10 +26,10 @@ const
2626
api.is(arg) || // Custom observable check
2727
arg.call // Function
2828
),
29-
29+
3030
// Subscribe to an observable or value, and provide a callback for each value
3131
sub = (target, stop, unsub) => (next, error, cleanup) => target && (
32-
unsub = ((!api?.any && (api.is(target) || target?.call)) && api.effect(() => (next(get(target)), api?.cleanup?.(cleanup), cleanup))) ||
32+
unsub = ((!api.any && (api.is(target) || target.call)) && api.effect(() => (next(get(target)), api.cleanup?.(cleanup), cleanup))) ||
3333
(
3434
target.then?.(v => (!stop && next(get(v)), cleanup?.()), error) ||
3535
target[Symbol.asyncIterator] && (async v => {
@@ -40,7 +40,7 @@ const
4040
} catch (err) { error?.(err) }
4141
})()
4242
) && (_ => stop = 1) ||
43-
(api?.any?.(target)?.(next, error, cleanup)),
43+
(api.any?.(target)?.(next, error, cleanup)),
4444
// register autocleanup
4545
registry.register(target, unsub),
4646
unsub

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.5.0",
4+
"version": "0.5.1",
55
"type": "module",
66
"source": "./src/index.js",
77
"main": "./dist/index.js",

src/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const
1313
// API object providing basic functions for handling effects and values
1414
api = {
1515
// Handle any reactive subscription
16-
any: undefined,
17-
// If any cleanup is requested
18-
cleanup: undefined,
16+
// any: undefined,
17+
// // If any cleanup is requested
18+
// cleanup: undefined,
1919
// Executes the provided function
2020
effect: (f) => f(),
2121
// Returns false for any value (placeholder implementation)
@@ -41,7 +41,7 @@ const
4141
sub = (target, stop, unsub) => (next, error, cleanup) => target && (
4242
unsub = unsubr((target[Symbol.observable]?.() || target).subscribe?.((v) => next(get(v)), error, cleanup), cleanup) ||
4343
target.set && target.call?.(stop, next) ||
44-
((!api?.any && (api.is(target) || target?.call)) && api.effect(() => (next(get(target)), api?.cleanup?.(cleanup), cleanup))) ||
44+
((!api.any && (api.is(target) || target.call)) && api.effect(() => (next(get(target)), api.cleanup?.(cleanup), cleanup))) ||
4545
(
4646
target.then?.(v => (!stop && next(get(v)), cleanup?.()), error) ||
4747
target[Symbol.asyncIterator] && (async v => {
@@ -52,7 +52,7 @@ const
5252
} catch (err) { error?.(err) }
5353
})()
5454
) && (_ => stop = 1) ||
55-
(api?.any?.(target)?.(next, error, cleanup)),
55+
(api.any?.(target)?.(next, error, cleanup)),
5656
// register autocleanup
5757
registry.register(target, unsub),
5858
unsub

0 commit comments

Comments
 (0)