UNPKG

2.23 kBMarkdownView Raw
1# split-array-stream
2> Safely push each item of an array to a stream.
3
4```sh
5$ npm install --save split-array-stream
6```
7```js
8const split = require('split-array-stream');
9const through = require('through2');
10
11const array = [
12 { id: 1, user: 'Dave' },
13 { id: 2, user: 'Stephen' }
14];
15
16const stream = through.obj();
17
18stream.on('data', (item) => {
19 // { id: 1, user: 'Dave' }
20 // ...later...
21 // { id: 2, user: 'Stephen' }
22});
23
24split(array, stream).then((streamEnded) => {
25 if (!streamEnded) {
26 stream.push(null);
27 stream.end();
28 }
29}).catch(console.error);
30```
31
32Before pushing an item to the stream, `split-array-stream` checks that the stream hasn't been ended. This avoids those "push() after EOF" errors.
33
34### Use case
35
36Say you're getting many items from an upstream API. Multiple requests might be required to page through all of the results. You want to push the results to the stream as they come in, and only get more results if the user hasn't ended the stream.
37
38```js
39function getAllUsers() {
40 var stream = through.obj();
41
42 var requestOptions = {
43 method: 'get',
44 url: 'http://api/users',
45 };
46
47 request(requestOptions, onResponse);
48
49 function onResponse(err, response) {
50 split(response.users, stream).then((streamEnded) => {
51 if (streamEnded) {
52 return;
53 }
54
55 if (response.nextPageToken) {
56 requestOptions.pageToken = response.nextPageToken;
57 request(requestOptions, onResponse);
58 return;
59 }
60
61 stream.push(null);
62 stream.end();
63 });
64
65 });
66
67 return stream;
68}
69
70getAllUsers()
71 .on('data', function (user) {
72 // An item from the `response.users` API response
73 })
74 .on('end', function () {
75 // All users received
76 });
77```
78
79
80### split(array, stream, callback)
81
82#### array
83
84- Type: `Array`
85- Required
86
87The source array. Each item will be pushed to the provided stream.
88
89#### stream
90
91- Type: `Stream`
92- Required
93
94The destination stream to receive the items of the array.
95
96#### callback(streamEnded)
97
98- Type: `Function`
99- Required
100
101Callback function executed after all items of the array have been iterated.
102
103##### callback.streamEnded
104
105- Type: `Boolean`
106
107Lets you know if the stream has been ended while items were being pushed.