|
1 |
| -# abortable-iterator <!-- omit in toc --> |
| 1 | +# abortable-iterator |
2 | 2 |
|
3 | 3 | [](https://codecov.io/gh/alanshaw/abortable-iterator)
|
4 | 4 | [](https://github.com/alanshaw/abortable-iterator/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
|
5 | 5 |
|
6 | 6 | > Make any iterator or iterable abortable via an AbortSignal
|
7 | 7 |
|
8 |
| -## Table of contents <!-- omit in toc --> |
9 |
| - |
10 |
| -- [Install](#install) |
11 |
| - - [Browser `<script>` tag](#browser-script-tag) |
12 |
| -- [Usage](#usage) |
13 |
| -- [API](#api) |
14 |
| - - [`abortableSource(source, signal, [options])`](#abortablesourcesource-signal-options) |
15 |
| - - [Parameters](#parameters) |
16 |
| - - [Returns](#returns) |
17 |
| - - [`abortableSink(sink, signal, [options])`](#abortablesinksink-signal-options) |
18 |
| - - [`abortableTransform(transform, signal, [options])`](#abortabletransformtransform-signal-options) |
19 |
| - - [`abortableDuplex(duplex, signal, [options])`](#abortableduplexduplex-signal-options) |
20 |
| -- [Related](#related) |
21 |
| -- [Contribute](#contribute) |
22 |
| -- [API Docs](#api-docs) |
23 |
| -- [License](#license) |
24 |
| -- [Contribution](#contribution) |
25 |
| - |
26 |
| -## Install |
| 8 | +# About |
27 | 9 |
|
28 |
| -```console |
29 |
| -$ npm i abortable-iterator |
30 |
| -``` |
| 10 | +<!-- |
31 | 11 |
|
32 |
| -### Browser `<script>` tag |
| 12 | +!IMPORTANT! |
33 | 13 |
|
34 |
| -Loading this module through a script tag will make it's exports available as `AbortableIterator` in the global namespace. |
| 14 | +Everything in this README between "# About" and "# Install" is automatically |
| 15 | +generated and will be overwritten the next time the doc generator is run. |
35 | 16 |
|
36 |
| -```html |
37 |
| -<script src="https://unpkg.com/abortable-iterator/dist/index.min.js"></script> |
38 |
| -``` |
| 17 | +To make changes to this section, please update the @packageDocumentation section |
| 18 | +of src/index.js or src/index.ts |
39 | 19 |
|
40 |
| -The [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) is used in the fetch API to abort in flight requests from, for example, a timeout or user action. The same concept is used here to halt iteration of an async iterator. |
| 20 | +To experiment with formatting, please run "npm run docs" from the root of this |
| 21 | +repo and examine the changes made. |
41 | 22 |
|
42 |
| -## Usage |
| 23 | +--> |
| 24 | + |
| 25 | +## Example |
43 | 26 |
|
44 | 27 | ```js
|
45 | 28 | import { abortableSource } from 'abortable-iterator'
|
46 | 29 |
|
47 |
| -// An example function that creates an async iterator that yields an increasing |
48 |
| -// number every x milliseconds and NEVER ENDS! |
49 |
| -const asyncCounter = async function * (start, delay) { |
50 |
| - let i = start |
51 |
| - while (true) { |
52 |
| - yield new Promise(resolve => setTimeout(() => resolve(i++), delay)) |
| 30 | +async function main () { |
| 31 | + // An example function that creates an async iterator that yields an increasing |
| 32 | + // number every x milliseconds and NEVER ENDS! |
| 33 | + const asyncCounter = async function * (start, delay) { |
| 34 | + let i = start |
| 35 | + while (true) { |
| 36 | + yield new Promise(resolve => setTimeout(() => resolve(i++), delay)) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Create a counter that'll yield numbers from 0 upwards every second |
| 41 | + const everySecond = asyncCounter(0, 1000) |
| 42 | + |
| 43 | + // Make everySecond abortable! |
| 44 | + const controller = new AbortController() |
| 45 | + const abortableEverySecond = abortableSource(everySecond, controller.signal) |
| 46 | + |
| 47 | + // Abort after 5 seconds |
| 48 | + setTimeout(() => controller.abort(), 5000) |
| 49 | + |
| 50 | + try { |
| 51 | + // Start the iteration, which will throw after 5 seconds when it is aborted |
| 52 | + for await (const n of abortableEverySecond) { |
| 53 | + console.log(n) |
| 54 | + } |
| 55 | + } catch (err) { |
| 56 | + if (err.code === 'ERR_ABORTED') { |
| 57 | + // Expected - all ok :D |
| 58 | + } else { |
| 59 | + throw err |
| 60 | + } |
53 | 61 | }
|
54 | 62 | }
|
55 | 63 |
|
56 |
| -// Create a counter that'll yield numbers from 0 upwards every second |
57 |
| -const everySecond = asyncCounter(0, 1000) |
| 64 | +main() |
| 65 | +``` |
| 66 | + |
| 67 | +# Install |
58 | 68 |
|
59 |
| -// Make everySecond abortable! |
60 |
| -const controller = new AbortController() |
61 |
| -const abortableEverySecond = abortableSource(everySecond, controller.signal) |
| 69 | +```console |
| 70 | +$ npm i abortable-iterator |
| 71 | +``` |
62 | 72 |
|
63 |
| -// Abort after 5 seconds |
64 |
| -setTimeout(() => controller.abort(), 5000) |
| 73 | +## Browser `<script>` tag |
65 | 74 |
|
66 |
| -try { |
67 |
| - // Start the iteration, which will throw after 5 seconds when it is aborted |
68 |
| - for await (const n of abortableEverySecond) { |
69 |
| - console.log(n) |
70 |
| - } |
71 |
| -} catch (err) { |
72 |
| - if (err.code === 'ERR_ABORTED') { |
73 |
| - // Expected - all ok :D |
74 |
| - } else { |
75 |
| - throw err |
76 |
| - } |
77 |
| -} |
| 75 | +Loading this module through a script tag will make its exports available as `AbortableIterator` in the global namespace. |
| 76 | + |
| 77 | +```html |
| 78 | +<script src="https://unpkg.com/abortable-iterator/dist/index.min.js"></script> |
78 | 79 | ```
|
79 | 80 |
|
80 | 81 | ## API
|
@@ -138,21 +139,17 @@ Note that this will abort *both* sides of the duplex. Use `duplex.sink = abortab
|
138 | 139 |
|
139 | 140 | - [`it-pipe`](https://www.npmjs.com/package/it-pipe) Utility to "pipe" async iterables together
|
140 | 141 |
|
141 |
| -## Contribute |
142 |
| - |
143 |
| -Feel free to dive in! [Open an issue](https://github.com/alanshaw/abortable-iterator/issues/new) or submit PRs. |
144 |
| - |
145 |
| -## API Docs |
| 142 | +# API Docs |
146 | 143 |
|
147 | 144 | - <https://alanshaw.github.io/abortable-iterator>
|
148 | 145 |
|
149 |
| -## License |
| 146 | +# License |
150 | 147 |
|
151 | 148 | Licensed under either of
|
152 | 149 |
|
153 |
| -- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>) |
154 |
| -- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>) |
| 150 | +- Apache 2.0, ([LICENSE-APACHE](https://github.com/alanshaw/abortable-iterator/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>) |
| 151 | +- MIT ([LICENSE-MIT](https://github.com/alanshaw/abortable-iterator/LICENSE-MIT) / <http://opensource.org/licenses/MIT>) |
155 | 152 |
|
156 |
| -## Contribution |
| 153 | +# Contribution |
157 | 154 |
|
158 | 155 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
0 commit comments