Skip to content

Commit a7141ec

Browse files
authored
docs: edit and add some more JSDocs (#46)
edit some JSDocs and add new ones for the `iterified` function and the `ExecutorFn` type
1 parent ec15cde commit a7141ec

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

src/iterified.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,58 @@ export {
1010
type IterifiedIterator,
1111
};
1212

13-
function iterified<TNext>(executorFn: ExecutorFn<TNext>): IterifiedIterable<TNext> {
14-
let channel = createMulticastChannel<TNext>();
13+
/**
14+
* Creates an `iterified` async iterable, yielding each value as it gets emitted from the user-provided `executorFn`.
15+
*
16+
* The given _executor function_ will be _"lazily"_ executed only upon pulling the first value from any iterator (or [`for await...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) loop) of the `iterified` iterable. Any additional iterators obtained from that point on would all feed off of the same shared execution of the _executor function_ - every value it yields will be distributed ("multicast") down to each active iterator, picking up from the time it was obtained. When the iterable is ended either by the producer (_executor function_ calls `done()` or `error(e)`) or the consumer (last active iterator is closed) - it would trigger an optionally-given teardown function before finally closing off the `iterified` iterable. This life cycle __repeats__ from the begining every time the `iterified` iterable gets reconsumed again.
17+
*
18+
* @param executorFn - a user-provided _executor function_ (see {@link ExecutorFn}) that controls the values to emit through the `iterified` iterable, closing it or erroring out, and provides logic for teardown.
19+
*
20+
* @returns an `iterified` async iterable
21+
*
22+
* @see {@link ExecutorFn}, {@link IterifiedIterable}
23+
*
24+
* @example
25+
```ts
26+
import { iterified } from 'iterified';
27+
28+
// Iterable that emits "my_value" and ends immediately:
29+
const iterable = iterified<string>((next, done, error) => {
30+
next('my_value');
31+
done();
32+
});
33+
```
34+
*
35+
* @example
36+
```ts
37+
import { iterified } from 'iterified';
38+
39+
function webSocketIterable(url: string) {
40+
return iterified<string>((next, done, error) => {
41+
const ws = new WebSocket(url);
42+
43+
ws.addEventListener('close', ev => done());
44+
ws.addEventListener('error', ev => error(ev));
45+
ws.addEventListener('message', ev => next(ev.data));
46+
47+
return () => ws.close(); // <-- Ensures the web socket will properly close on any event our iterable gets disposed
48+
});
49+
}
50+
51+
(async () => {
52+
for await (const msg of webSocketIterable('ws://localhost:8080')) {
53+
console.log(msg);
54+
}
55+
})();
56+
```
57+
*/
58+
function iterified<T>(executorFn: ExecutorFn<T>): IterifiedIterable<T> {
59+
let channel = createMulticastChannel<T>();
1560
let activeIteratorCount = 0;
16-
let executorPossiblyReturnedTeardown: ReturnType<ExecutorFn<TNext>>;
61+
let executorPossiblyReturnedTeardown: ReturnType<ExecutorFn<T>>;
1762
let teardownInProgressPromise: Promise<void> | undefined;
1863

19-
function executorPushCb(nextValue: TNext): void {
64+
function executorPushCb(nextValue: T): void {
2065
if (!teardownInProgressPromise) {
2166
channel.put(nextValue);
2267
}
@@ -129,6 +174,19 @@ type IterifiedIterator<TNextValue, TDoneValue = undefined | void> = {
129174
return(): Promise<IteratorReturnResult<TDoneValue>>;
130175
};
131176

177+
/**
178+
* A function that expresses the values to emit through an `iterified` iterable and encapsulates any logic and resource management that should be involved in generating them.
179+
*
180+
* The _executor function_ is invoked with the following arguments:
181+
*
182+
* - `next(value)` - makes the iterable yield `value` to all consuming iterators
183+
* - `done()` - makes the iterable end, closing all consuming iterators
184+
* - `error(e)` - makes the iterable error out with given `e` and end, propagating the error to every consuming iterator
185+
*
186+
* In addition, the _executor function_ may __optionally__ return a teardown function for disposing of any state and open resources that have been used during execution.
187+
*
188+
* @see {@link TeardownFn}
189+
*/
132190
type ExecutorFn<TNext> = (
133191
next: (nextValue: TNext) => void,
134192
done: () => void,

0 commit comments

Comments
 (0)