You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
7
6
8
7
## Table of Contents
@@ -16,6 +15,7 @@ This JavaScript library provides utility functions for handling observables, sig
16
15
-[Checking if an Object is Observable](#checking-if-an-object-is-observable)
17
16
-[Customizing for Your Reactive Library](#customizing-for-your-reactive-library)
18
17
-[Examples](#examples)
18
+
-[Any Source](#any-source)
19
19
-[Solid.js](#solidjs)
20
20
-[Preact Signals](#preact-signals)
21
21
-[uSignal](#usignal)
@@ -30,14 +30,29 @@ This JavaScript library provides utility functions for handling observables, sig
30
30
31
31
## Installation
32
32
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
+
```
34
48
35
49
## Usage
36
50
37
51
### Basic Setup
38
52
39
53
The library exports three primary functions:
40
54
55
+
-**`any`**: Add any type of source to react
41
56
-**`is`**: Checks if a value is considered an observable or reactive signal.
42
57
-**`api`**: Provides utility functions that can be customized to work with different reactive libraries.
43
58
-**`sub`**: Subscribes to an observable or other async/reactive patterns.
@@ -49,35 +64,39 @@ import { is, api, sub } from "usub";
49
64
```
50
65
51
66
### Subscribing to Observables
67
+
52
68
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.
Use the is function to check if a value is considered an observable by the library:
71
91
72
92
```js
73
93
constobservable= {
74
-
subscribe: () => {}
94
+
subscribe: () => {},
75
95
};
76
96
77
-
console.log(is(observable)); // Output: true
97
+
console.log(is(observable)); // Output: true
78
98
```
79
99
80
-
81
100
### Promise
82
101
83
102
```js
@@ -102,20 +121,58 @@ The library is designed to be easily customized for different reactive programmi
102
121
103
122
### API Overview
104
123
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)**
106
141
107
142
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).
108
143
109
-
-**api.is(v)**
144
+
-**api.is(v)**
110
145
111
146
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).
112
147
113
-
-**api.get(v)**
148
+
-**api.get(v)**
114
149
115
150
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?.()).
0 commit comments