Skip to content

Commit 3531467

Browse files
committed
remove Symbol.iterator
1 parent 02a7ef9 commit 3531467

File tree

4 files changed

+39
-71
lines changed

4 files changed

+39
-71
lines changed

README.md

+35-66
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,35 @@
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
5+
#### lite version
66

77
[![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)
88

99
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.
1010

1111
## Table of Contents
1212

13-
- [Installation](#installation)
14-
- [Lite](#lite)
15-
- [Usage](#usage)
16-
- [Basic Setup](#basic-setup)
17-
- [Promise](#promise)
18-
- [Subscribing to Observables](#subscribing-to-observables)
19-
- [API Overview](#api-overview)
20-
- [Checking if an Object is Observable](#checking-if-an-object-is-observable)
21-
- [Customizing for Your Reactive Library](#customizing-for-your-reactive-library)
22-
- [Examples](#examples)
23-
- [Any Source](#any-source)
24-
- [Solid.js](#solidjs)
25-
- [Preact Signals](#preact-signals)
26-
- [usignal](#usignal)
27-
- [@webreflection/signal](#webreflectionsignal)
28-
- [ulive](#ulive)
29-
- [RxJS Subject](#rxjs-subject)
30-
- [Async Iterable](#async-iterable)
31-
- [Synchronous Iterable](#synchronous-iterable)
32-
- [Finalization and Cleanup](#finalization-and-cleanup)
33-
- [License](#license)
34-
- [Thanks](#thanks)
13+
- [Installation](#installation)
14+
- [Lite](#lite)
15+
- [Usage](#usage)
16+
- [Basic Setup](#basic-setup)
17+
- [Promise](#promise)
18+
- [Subscribing to Observables](#subscribing-to-observables)
19+
- [API Overview](#api-overview)
20+
- [Checking if an Object is Observable](#checking-if-an-object-is-observable)
21+
- [Customizing for Your Reactive Library](#customizing-for-your-reactive-library)
22+
- [Examples](#examples)
23+
- [Any Source](#any-source)
24+
- [Solid.js](#solidjs)
25+
- [Preact Signals](#preact-signals)
26+
- [usignal](#usignal)
27+
- [@webreflection/signal](#webreflectionsignal)
28+
- [ulive](#ulive)
29+
- [RxJS Subject](#rxjs-subject)
30+
- [Async Iterable](#async-iterable)
31+
- [Finalization and Cleanup](#finalization-and-cleanup)
32+
- [License](#license)
33+
- [Thanks](#thanks)
3534

3635
## Installation
3736

@@ -43,8 +42,6 @@ This javascript library provides utility functions for handling observables, sig
4342

4443
**es**: https://unpkg.com/usub?module
4544

46-
## Installation
47-
4845
To use this utility, simply import it into your project:
4946

5047
```js
@@ -53,13 +50,13 @@ import { is, api, sub, get } from "usub";
5350

5451
### lite
5552

56-
The lite utility does not contain subscribe, and set method for observ-* patterns.
53+
The lite utility does not contain subscribe, and set method for observ-\* patterns.
5754

5855
**cdn**: https://unpkg.com/usub/lite
5956

6057
**es**: https://unpkg.com/usub/lite?module
6158

62-
## Installation
59+
## Lite Installation
6360

6461
To use lite version, simply import it into your project:
6562

@@ -73,10 +70,10 @@ import { is, api, sub, get } from "usub/lite";
7370

7471
The library exports four primary functions:
7572

76-
- **`is`**: Checks if a value is considered an observable or reactive signal.
77-
- **`api`**: Provides utility functions that can be customized to work with different reactive libraries.
78-
- **`sub`**: Subscribes to an observable or other async/reactive patterns.
79-
- **`get`**: get function to retrieve reactive data
73+
- **`is`**: Checks if a value is considered an observable or reactive signal.
74+
- **`api`**: Provides utility functions that can be customized to work with different reactive libraries.
75+
- **`sub`**: Subscribes to an observable or other async/reactive patterns.
76+
- **`get`**: get function to retrieve reactive data
8077

8178
### Subscribing to Observables
8279

@@ -136,7 +133,7 @@ The library is designed to be easily customized for different reactive programmi
136133

137134
### API Overview
138135

139-
- **api.any(target)(next, err, complete)**
136+
- **api.any(target)(next, err, complete)**
140137

141138
api.any is a placeholder within the api object that can be used to represent or handle any observable-like object.
142139

@@ -152,19 +149,19 @@ By default, api.any is set to undefined. This means that if it is not explicitly
152149

153150
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.
154151

155-
- **api.effect(f)**
152+
- **api.effect(f)**
156153

157154
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).
158155

159-
- **api.is(v)**
156+
- **api.is(v)**
160157

161158
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).
162159

163-
- **api.get(v)**
160+
- **api.get(v)**
164161

165162
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?.()).
166163

167-
- **api.cleanup()**
164+
- **api.cleanup()**
168165

169166
Provides a function to handle cleanup logic. This can be used to define any custom cleanup behavior required when a subscription is no longer needed.
170167

@@ -324,14 +321,7 @@ const asyncIterable = {
324321
next() {
325322
if (this.i < 5)
326323
return new Promise((ok) =>
327-
setTimeout(
328-
() =>
329-
ok({
330-
value: this.i++,
331-
done: false,
332-
}),
333-
10
334-
)
324+
setTimeout(() => ok({ value: this.i++, done: false }), 10)
335325
);
336326
return new Promise((ok) => ok({ done: true }));
337327
},
@@ -342,27 +332,6 @@ const asyncIterable = {
342332
sub(asyncIterable)(console.log, console.error, () => console.log("end"));
343333
```
344334
345-
### Synchronous Iterable
346-
347-
```js
348-
const myIterable = {
349-
[Symbol.iterator]() {
350-
let step = 10;
351-
return {
352-
next() {
353-
if (step++ <= 13) {
354-
return { value: step, done: false };
355-
} else {
356-
return { value: undefined, done: true };
357-
}
358-
},
359-
};
360-
},
361-
};
362-
363-
sub(myIterable)(console.log);
364-
```
365-
366335
### Finalization and Cleanup
367336
368337
The library uses FinalizationRegistry to automatically clean up subscriptions when objects are garbage collected. This helps prevent memory leaks by ensuring that subscriptions are properly terminated when no longer needed.
@@ -373,4 +342,4 @@ This library is provided "as-is" under the MIT license. Feel free to use, modify
373342
374343
### Thanks and Inspiration
375344
376-
- **[dy](https://github.com/dy)**
345+
- **[dy](https://github.com/dy)**

lite/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ const
2222
// Checks if the argument is considered an observable
2323
is = (arg) => arg && !!(
2424
arg[Symbol.asyncIterator] || // Async iterator
25-
arg[Symbol.iterator] || // Sync iterator
2625
arg.then || // Promise
2726
api.is(arg) || // Custom observable check
2827
arg.call // Function
2928
),
29+
3030
// Subscribe to an observable or value, and provide a callback for each value
3131
sub = (target, stop, unsub) => (next, error, cleanup) => target && (
3232
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) ||
35-
(target[Symbol.asyncIterator] || target[Symbol.iterator]) && (async v => {
35+
target[Symbol.asyncIterator] && (async v => {
3636
try {
3737
// FIXME: possible drawback: it will catch error happened in next, not only in iterator
3838
for await (v of target) { if (stop) return; next(get(v)) }

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

src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const
3131
is = (arg) => arg && !!(
3232
arg[Symbol.observable] || // Observable symbol
3333
arg[Symbol.asyncIterator] || // Async iterator
34-
arg[Symbol.iterator] || // Sync iterator
3534
arg.call && arg.set || // Observ-*
3635
arg.then || // Promise
3736
arg.subscribe || // Observable with subscribe method
@@ -45,7 +44,7 @@ const
4544
((!api?.any && (api.is(target) || target?.call)) && api.effect(() => (next(get(target)), api?.cleanup?.(cleanup), cleanup))) ||
4645
(
4746
target.then?.(v => (!stop && next(get(v)), cleanup?.()), error) ||
48-
(target[Symbol.asyncIterator] || target[Symbol.iterator]) && (async v => {
47+
target[Symbol.asyncIterator] && (async v => {
4948
try {
5049
// FIXME: possible drawback: it will catch error happened in next, not only in iterator
5150
for await (v of target) { if (stop) return; next(get(v)) }

0 commit comments

Comments
 (0)