@@ -5,8 +5,74 @@ import { type MaybeAsyncIterable } from '../MaybeAsyncIterable/index.js';
5
5
6
6
export { useSharedAsyncIter } ;
7
7
8
- // TODO: Enhance `useSharedAsyncIter`'s returned iter to have the `.return` method NOT optional?
9
-
8
+ /**
9
+ * Hook that takes a source async iterable and returns a version of it that will always initialize up to
10
+ * just one single instance of the source at any point in time, sharing it to any number of simultaneous consumers
11
+ * the result iterable might have (e.g multiple `<It>`s).
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const sharedIter = useSharedAsyncIter(iter);
16
+ * // ...
17
+ * ```
18
+ *
19
+ * Any number of iterators for the resulting iterable you create and consume simultaneously will only ever
20
+ * create a single iterator internally for the original source and distribute every yielded value, completion or
21
+ * possible error among each of them.
22
+ *
23
+ * In a _reference-counting_ fashion, only when the last remaining iterator is closed will the shared
24
+ * source iterator be finally closed as well, disposing of resources it held, after which instantiating a new
25
+ * iterator will restart the cycle. This way, async iterables that instantiate server connections, streams,
26
+ * etc. - can easily be consumed or rendered concurrently by multiple components without possibly opening
27
+ * duplicate resources or other undesired effects, depending on the way these source iterables were constructed.
28
+ *
29
+ * Repeated calls with the same source iterable will return the same memoized result iterable, as well as calls
30
+ * with `iterateFormatted`-returned iterables based of the same source for that matter.
31
+ *
32
+ * If given a plain non-iterable value, this hook would seamlessly return it as-is without additional effect.
33
+ *
34
+ * ---
35
+ *
36
+ * @template T The type for the source async iterable's values or in case of a plain value the source's type itself.
37
+ *
38
+ * @param value The source async iterable or plain value.
39
+ *
40
+ * @returns A shared version of the source async iterable or the source value itself in case it was a plain value.
41
+ *
42
+ * ---
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { useSharedAsyncIter, It } from 'react-async-iterators';
47
+ *
48
+ * function MyComponent(props) {
49
+ * const messagesIter = useSharedAsyncIter(props.messagesIter);
50
+ *
51
+ * return (
52
+ * <div>
53
+ * Unread messages:
54
+ * <It value={messagesIter}>
55
+ * {next => (
56
+ * next.value?.filter(msg => msg.isRead).length ?? 0
57
+ * )}
58
+ * </It>
59
+ *
60
+ * <It value={messagesIter}>
61
+ * {next => (
62
+ * next.value?.map(msg => (
63
+ * <div>
64
+ * From: {msg.from},
65
+ * Date: {msg.date},
66
+ * Was read: {msg.isRead ? 'Y' : 'N'}
67
+ * </div>
68
+ * ))
69
+ * )}
70
+ * </It>
71
+ * </div>
72
+ * );
73
+ * }
74
+ * ```
75
+ */
10
76
function useSharedAsyncIter < T > ( value : AsyncIterable < T > ) : AsyncIterable < T > ;
11
77
function useSharedAsyncIter < T > ( value : T ) : T ;
12
78
function useSharedAsyncIter ( value : unknown ) : MaybeAsyncIterable < unknown > {
@@ -15,3 +81,5 @@ function useSharedAsyncIter(value: unknown): MaybeAsyncIterable<unknown> {
15
81
[ value ]
16
82
) ;
17
83
}
84
+
85
+ // TODO: Enhance `useSharedAsyncIter`'s returned iter to have the `.return` method as NOT optional?
0 commit comments