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
* implmentations and tests for a new `useAsyncIterMemo` hook
* implementations and tests for a new `useAsyncIterMemo` hook
* more done
* fix test
* up
* up
* code for a new `useSharedAsyncIter` hook
* add JSDocs
* add section in readme
* make the hooks returned iter type more concrete with a non-optional `.return` method and more
Copy file name to clipboardExpand all lines: README.md
+95-3Lines changed: 95 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ Async iterables/iterators are a native language construct in JS that can be view
26
26
27
27
Somewhat obvious to say, the React ecosystem features many methods and tools that have to do with integrating promise-based data into your React components; from higher level SDK libraries, state managers - to generic async utilities, which make the different promise states available to the rendering. And just like that - `react-async-iterators` packs hooks, components and utilities written in TypeScript with the aim to make async iterables into __first-class citizens to React__ as they become gradually more prevalent across JavaScript platforms.
28
28
29
-
What can `react-async-iterators` be used for?
29
+
## What can `react-async-iterators` be used for?
30
30
31
31
- easily consuming async iterables obtained from any library, web API or composed manually - in a React-friendly declarative fashion.
32
32
<!-- ...Dynamically plug and unplug them at any place across your app's component tree with automatic teardown... -->
An array of objects with up-to-date information about each input's current value, completion status, and more - corresponding to the order by which they appear on `values` (see [Iteration state properties breakdown](#iteration-state-properties-breakdown)).
1014
1017
1018
+
</ul>
1019
+
1015
1020
### Notes
1016
1021
1017
1022
-
@@ -1166,7 +1171,11 @@ function handleValueSubmit() {
1166
1171
1167
1172
### Returns
1168
1173
1169
-
A stateful async iterable with accessible current value and a function for yielding an update. The returned async iterable is a shared iterable such that multiple simultaneous consumers (e.g multiple [`<It>`](#it)s) all pick up the same yields at the same times. The setter function, like[`React.useState`'s setter](https://react.dev/reference/react/useState#setstate), can be provided the next state directly, or a function that calculates it from the previous state.
1174
+
<ul>
1175
+
1176
+
A stateful async iterable with accessible current value and a function for yielding an update. The returned async iterable is a shared iterable such that multiple simultaneous consumers (e.g multiple [`<It>`](#it)s) all pick up the same yields at the same times. The setter function, like[`React.useState`'s setter](https://react.dev/reference/react/useState#setstate), can be provided either the next state directly, or a function that calculates it from the previous state.
1177
+
1178
+
</ul>
1170
1179
1171
1180
### Notes
1172
1181
@@ -1232,6 +1241,89 @@ function handleValueSubmit() {
1232
1241
1233
1242
1234
1243
1244
+
### useSharedAsyncIter
1245
+
1246
+
Hook that takes a source async iterable and returns a version of it that will always initialize up to
1247
+
just one single instance of the source at any point in time, sharing it to any number of simultaneous consumers
1248
+
the result iterable might have (e.g multiple [`<It>`](#it)s).
1249
+
1250
+
```tsx
1251
+
const sharedIter =useSharedAsyncIter(iter);
1252
+
// ...
1253
+
```
1254
+
1255
+
Any number of iterators for the resulting iterable you create and consume simultaneously will only ever
1256
+
create a single iterator internally for the original source and distribute every yielded value, completion or
1257
+
possible error among each of them.
1258
+
1259
+
In a _reference-counting_ fashion, only when the last remaining iterator is closed will the shared
1260
+
source iterator be finally closed as well, disposing of resources it held, after which instantiating a new iterator will restart the cycle. This way, async iterables that instantiate server connections, streams, etc. - can easily be consumed or rendered concurrently by multiple components without possibly opening duplicate resources or other undesired effects, depending on the way these source iterables were constructed.
1261
+
1262
+
If given a plain non-iterable value, this hook would seamlessly return it as-is without additional effect.
1263
+
1264
+
### Parameters
1265
+
1266
+
-`value`:
1267
+
The source async iterable or plain value.
1268
+
1269
+
### Returns
1270
+
1271
+
<ul>
1272
+
1273
+
A "_shared_" version of the source async iterable or the source value itself in case it was a plain value.
1274
+
1275
+
</ul>
1276
+
1277
+
### Notes
1278
+
1279
+
<ul>
1280
+
1281
+
> <br/>ℹ️ Repeated calls with the same source iterable will return the same memoized result iterable, as well as calls with [`iterateFormatted`](#iterateformatted)-returned iterables based of the same source for that matter.<br/><br/>
"The returned iterable's values are each shared between all its parallel consumers so that each will receives all values that will yield from the time it started consuming"
286
+
"The returned iterable's values are each shared between all its parallel consumers so that each will receive every value that will yield from the time it started consuming"
0 commit comments