Skip to content

Commit b408202

Browse files
committed
add api.any
1 parent 1959e86 commit b408202

File tree

4 files changed

+99
-23
lines changed

4 files changed

+99
-23
lines changed

README.md

+75-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
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-
65
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.
76

87
## Table of Contents
@@ -16,6 +15,7 @@ This JavaScript library provides utility functions for handling observables, sig
1615
- [Checking if an Object is Observable](#checking-if-an-object-is-observable)
1716
- [Customizing for Your Reactive Library](#customizing-for-your-reactive-library)
1817
- [Examples](#examples)
18+
- [Any Source](#any-source)
1919
- [Solid.js](#solidjs)
2020
- [Preact Signals](#preact-signals)
2121
- [uSignal](#usignal)
@@ -30,14 +30,29 @@ This JavaScript library provides utility functions for handling observables, sig
3030

3131
## Installation
3232

33-
To use this utility, copy the provided JavaScript file into your project. There are no external dependencies, so no installation via npm or other package managers is required.
33+
**yarn**: `yarn add usub`
34+
35+
**npm**: `npm i usub`
36+
37+
**cdn**: https://unpkg.com/usub
38+
39+
**es**: https://unpkg.com/usub?module
40+
41+
## Installation
42+
43+
To use this utility, simply import it into your project:
44+
45+
```js
46+
import { is, api, sub, get } from "usub";
47+
```
3448

3549
## Usage
3650

3751
### Basic Setup
3852

3953
The library exports three primary functions:
4054

55+
- **`any`**: Add any type of source to react
4156
- **`is`**: Checks if a value is considered an observable or reactive signal.
4257
- **`api`**: Provides utility functions that can be customized to work with different reactive libraries.
4358
- **`sub`**: Subscribes to an observable or other async/reactive patterns.
@@ -49,35 +64,39 @@ import { is, api, sub } from "usub";
4964
```
5065

5166
### Subscribing to Observables
67+
5268
The sub function is used to subscribe to an observable or other async patterns. It handles various types of asynchronous inputs like promises, async iterables, and functions.
5369

5470
```js
5571
const observable = {
56-
subscribe: (next, error, complete) => {
57-
next(1);
58-
next(2);
59-
next(3);
60-
complete();
61-
return {
62-
unsubscribe: () => console.log('Unsubscribed')
63-
};
64-
}
72+
subscribe: (next, error, complete) => {
73+
next(1);
74+
next(2);
75+
next(3);
76+
complete();
77+
return {
78+
unsubscribe: () => console.log("Unsubscribed"),
79+
};
80+
},
6581
};
6682

67-
const unsubscribe = sub(observable)(console.log, console.error, () => console.log('Complete'));
83+
const unsubscribe = sub(observable)(console.log, console.error, () =>
84+
console.log("Complete")
85+
);
6886
```
87+
6988
### Checking if an Object is Observable
89+
7090
Use the is function to check if a value is considered an observable by the library:
7191

7292
```js
7393
const observable = {
74-
subscribe: () => {}
94+
subscribe: () => {},
7595
};
7696

77-
console.log(is(observable)); // Output: true
97+
console.log(is(observable)); // Output: true
7898
```
7999

80-
81100
### Promise
82101

83102
```js
@@ -102,20 +121,58 @@ The library is designed to be easily customized for different reactive programmi
102121

103122
### API Overview
104123

105-
- **api.effect(f)**
124+
- **api.any(target)(next, err, complete)**
125+
126+
api.any is a placeholder within the api object that can be used to represent or handle any observable-like object.
127+
128+
### Purpose
129+
130+
The primary purpose of api.any is to provide a centralized place for handling various types of observable objects, whether they are standard observables, promises, or custom implementations. This placeholder is particularly useful for abstraction and uniform handling of different observable sources in a consistent manner.
131+
132+
### Default Behavior
133+
134+
By default, api.any is set to undefined. This means that if it is not explicitly assigned a function or object, it will not provide any observable functionality. You need to assign it a specific function or observable to make it work.
135+
136+
### Usage
137+
138+
You can customize api.any to handle your specific observable implementations. For example, you might set api.any to a function that processes different observable types or provides default behavior for handling observable subscriptions and notifications.
139+
140+
- **api.effect(f)**
106141

107142
Sets up the effect execution method. This function is where you define how to apply effects in your reactive library (e.g., createEffect in Solid.js, effect in Preact Signals).
108143

109-
- **api.is(v)**
144+
- **api.is(v)**
110145

111146
Defines how to check if a value is a signal or observable. This is where you identify reactive signals from your library (e.g., checking if a value is an instance of Signal).
112147

113-
- **api.get(v)**
148+
- **api.get(v)**
114149

115150
Specifies how to retrieve the current value from a signal or observable. This function is where you define how to extract the current value from your reactive signal (e.g., v?.value or v?.()).
116151

117152
### Example API Customization
118153

154+
### Any source
155+
156+
```js
157+
export let v =
158+
(v, cb = []) =>
159+
(c) =>
160+
c === void 0
161+
? v
162+
: c.call
163+
? cb.splice.bind(cb, cb.push(c) - 1, 1, 0)
164+
: cb.map((f) => f && f((v = c)));
165+
166+
api.any = (target) => (next, error, complete) => target((v) => next(v));
167+
168+
const num = v(42);
169+
let off = sub(num)(console.log);
170+
num(20);
171+
num(3);
172+
off();
173+
num(30);
174+
```
175+
119176
### Solidjs
120177

121178
```js

package-lock.json

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
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.0.8",
4+
"version": "0.1.0",
55
"type": "module",
66
"source": "./src/index.js",
77
"main": "./dist/index.js",
@@ -55,5 +55,8 @@
5555
"bugs": {
5656
"url": "https://github.com/kethan/usub/issues"
5757
},
58-
"homepage": "https://github.com/kethan/usub#readme"
58+
"homepage": "https://github.com/kethan/usub#readme",
59+
"dependencies": {
60+
"usignal": "^0.9.0"
61+
}
5962
}

src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const
1515

1616
// API object providing basic functions for handling effects and values
1717
api = {
18+
// Placeholder for any observable value
19+
any: undefined,
1820
// Executes the provided function
1921
effect: (f) => f(),
2022
// Returns false for any value (placeholder implementation)
@@ -37,7 +39,8 @@ const
3739
),
3840

3941
sub = (target, stop, unsub) => (next, error, complete) => target && (
40-
unsub = unsubr((target[Symbol.observable]?.() || target).subscribe?.((v) => next(get(v)), error, complete), complete) ||
42+
unsub = api?.any?.(target)?.(next, error, complete) ||
43+
unsubr((target[Symbol.observable]?.() || target).subscribe?.((v) => next(get(v)), error, complete), complete) ||
4144
((target.call || api.is(target)) && api.effect(() => next(get(target)))) ||
4245
(target.then?.(v => (!stop && next(get(v)), complete?.()), error)) ||
4346
(async v => {

0 commit comments

Comments
 (0)