Skip to content

Commit 02a7ef9

Browse files
committed
Add lite version
1 parent c567f35 commit 02a7ef9

File tree

5 files changed

+95
-8
lines changed

5 files changed

+95
-8
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
[![tests](https://github.com/kethan/usub/actions/workflows/node.js.yml/badge.svg)](https://github.com/kethan/usub/actions/workflows/node.js.yml) [![Version](https://img.shields.io/npm/v/usub.svg?color=success&style=flat-square)](https://www.npmjs.com/package/usub) [![Badge size](https://deno.bundlejs.com/badge?q=usub&treeshake=[*]&config={"compression":"brotli"})](https://unpkg.com/usub) [![Badge size](https://deno.bundlejs.com/badge?q=usub&treeshake=[*]&config={"compression":"gzip"})](https://unpkg.com/usub)
44

5+
#### lite
6+
7+
[![Badge size](https://deno.bundlejs.com/badge?q=usub/lite&treeshake=[*]&config={"compression":"brotli"})](https://unpkg.com/usub/lite) [![Badge size](https://deno.bundlejs.com/badge?q=usub/lite&treeshake=[*]&config={"compression":"gzip"})](https://unpkg.com/usub/lite)
8+
59
This javascript library provides utility functions for handling observables, signals, and asynchronous data streams across various reactive programming libraries. It supports flexible customization to integrate with different libraries, ensuring seamless subscription management and automatic cleanup.
610

711
## Table of Contents
812

913
- [Installation](#installation)
14+
- [Lite](#lite)
1015
- [Usage](#usage)
1116
- [Basic Setup](#basic-setup)
1217
- [Promise](#promise)
@@ -46,6 +51,22 @@ To use this utility, simply import it into your project:
4651
import { is, api, sub, get } from "usub";
4752
```
4853

54+
### lite
55+
56+
The lite utility does not contain subscribe, and set method for observ-* patterns.
57+
58+
**cdn**: https://unpkg.com/usub/lite
59+
60+
**es**: https://unpkg.com/usub/lite?module
61+
62+
## Installation
63+
64+
To use lite version, simply import it into your project:
65+
66+
```js
67+
import { is, api, sub, get } from "usub/lite";
68+
```
69+
4970
## Usage
5071

5172
### Basic Setup

lite/index.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const
2+
// FinalizationRegistry to clean up subscriptions when objects are garbage collected
3+
registry = new FinalizationRegistry(unsub => (unsub && unsub?.call?.())),
4+
5+
// API object providing basic functions for handling effects and values
6+
api = {
7+
// Handle any reactive subscription
8+
any: undefined,
9+
// If any cleanup is requested
10+
cleanup: undefined,
11+
// Executes the provided function
12+
effect: (f) => f(),
13+
// Returns false for any value (placeholder implementation)
14+
is: (v) => v?.call,
15+
// Retrieves the value (returns it as is)
16+
get: (v) => v?.call(),
17+
},
18+
19+
// Utility function to handle and unwrap values of signals, observable, etc especially functions
20+
get = (v) => api.is(v) ? get(api.get(v)) : v?.call ? get(v()) : v,
21+
22+
// Checks if the argument is considered an observable
23+
is = (arg) => arg && !!(
24+
arg[Symbol.asyncIterator] || // Async iterator
25+
arg[Symbol.iterator] || // Sync iterator
26+
arg.then || // Promise
27+
api.is(arg) || // Custom observable check
28+
arg.call // Function
29+
),
30+
// Subscribe to an observable or value, and provide a callback for each value
31+
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))) ||
33+
(
34+
target.then?.(v => (!stop && next(get(v)), cleanup?.()), error) ||
35+
(target[Symbol.asyncIterator] || target[Symbol.iterator]) && (async v => {
36+
try {
37+
// FIXME: possible drawback: it will catch error happened in next, not only in iterator
38+
for await (v of target) { if (stop) return; next(get(v)) }
39+
cleanup?.()
40+
} catch (err) { error?.(err) }
41+
})()
42+
) && (_ => stop = 1) ||
43+
(api?.any?.(target)?.(next, error, cleanup)),
44+
// register autocleanup
45+
registry.register(target, unsub),
46+
unsub
47+
);
48+
49+
export {
50+
api,
51+
is,
52+
sub,
53+
get
54+
}

package.json

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
{
22
"name": "usub",
33
"description": "Subscribe to any reactive sources",
4-
"version": "0.3.2",
4+
"version": "0.4.0",
55
"type": "module",
66
"source": "./src/index.js",
77
"main": "./dist/index.js",
88
"module": "./dist/index.es.js",
99
"unpkg": "./dist/index.min.js",
1010
"umd:main": "./dist/index.umd.js",
1111
"exports": {
12-
"browser": "./dist/index.es.js",
13-
"umd": "./dist/index.umd.js",
14-
"require": "./dist/index.js",
15-
"import": "./dist/index.es.js",
16-
"default": "./dist/index.es.js"
12+
".": {
13+
"browser": "./dist/index.es.js",
14+
"umd": "./dist/index.umd.js",
15+
"require": "./dist/index.js",
16+
"import": "./dist/index.es.js",
17+
"default": "./dist/index.es.js"
18+
},
19+
"./lite": {
20+
"browser": "./lite/dist/index.es.js",
21+
"umd": "./lite/dist/index.umd.js",
22+
"require": "./lite/dist/index.js",
23+
"import": "./lite/dist/index.es.js",
24+
"default": "./lite/dist/index.es.js"
25+
},
26+
"./package.json": "./package.json"
1727
},
1828
"repository": {
1929
"type": "git",
2030
"url": "git+https://github.com/kethan/usub.git"
2131
},
2232
"files": [
23-
"dist"
33+
"dist",
34+
"lite"
2435
],
2536
"scripts": {
2637
"build": "rollup --config rollup.config.js",

rollup.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ const resolve = (pkg, input = "src/index", output = "dist/index") => ({
3636

3737
export default [
3838
resolve("usub"),
39+
resolve("usub", "lite/index", "lite/dist/index")
3940
]

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const
5353
} catch (err) { error?.(err) }
5454
})()
5555
) && (_ => stop = 1) ||
56-
(api?.any?.(target)?.(next, cleanup, error)),
56+
(api?.any?.(target)?.(next, error, cleanup)),
5757
// register autocleanup
5858
registry.register(target, unsub),
5959
unsub

0 commit comments

Comments
 (0)