split-array-stream

WebJar for split-array-stream

Лицензия

Лицензия

MIT
Группа

Группа

org.webjars.npm
Идентификатор

Идентификатор

split-array-stream
Последняя версия

Последняя версия

2.0.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

split-array-stream
WebJar for split-array-stream
Ссылка на сайт

Ссылка на сайт

http://webjars.org
Система контроля версий

Система контроля версий

https://github.com/stephenplusplus/split-array-stream

Скачать split-array-stream

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/org.webjars.npm/split-array-stream/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>split-array-stream</artifactId>
    <version>2.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/split-array-stream/
implementation 'org.webjars.npm:split-array-stream:2.0.0'
// https://jarcasting.com/artifacts/org.webjars.npm/split-array-stream/
implementation ("org.webjars.npm:split-array-stream:2.0.0")
'org.webjars.npm:split-array-stream:jar:2.0.0'
<dependency org="org.webjars.npm" name="split-array-stream" rev="2.0.0">
  <artifact name="split-array-stream" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='split-array-stream', version='2.0.0')
)
libraryDependencies += "org.webjars.npm" % "split-array-stream" % "2.0.0"
[org.webjars.npm/split-array-stream "2.0.0"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.webjars.npm : is-stream-ended jar [0.1.4,0.2)

Модули Проекта

Данный проект не имеет модулей.

split-array-stream

Safely push each item of an array to a stream.

$ npm install --save split-array-stream
const split = require('split-array-stream');
const through = require('through2');

const array = [
  { id: 1, user: 'Dave' },
  { id: 2, user: 'Stephen' }
];

const stream = through.obj();

stream.on('data', (item) => {
  // { id: 1, user: 'Dave' }
  // ...later...
  // { id: 2, user: 'Stephen' }
});

split(array, stream).then((streamEnded) => {
  if (!streamEnded) {
    stream.push(null);
    stream.end();
  }
}).catch(console.error);

Before pushing an item to the stream, split-array-stream checks that the stream hasn't been ended. This avoids those "push() after EOF" errors.

Use case

Say 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.

function getAllUsers() {
  var stream = through.obj();

  var requestOptions = {
    method: 'get',
    url: 'http://api/users',
  };

  request(requestOptions, onResponse);

  function onResponse(err, response) {
    split(response.users, stream).then((streamEnded) => {
      if (streamEnded) {
        return;
      }

      if (response.nextPageToken) {
        requestOptions.pageToken = response.nextPageToken;
        request(requestOptions, onResponse);
        return;
      }

      stream.push(null);
      stream.end();
    });

  });

  return stream;
}

getAllUsers()
  .on('data', function (user) {
    // An item from the `response.users` API response
  })
  .on('end', function () {
    // All users received
  });

split(array, stream, callback)

array

  • Type: Array
  • Required

The source array. Each item will be pushed to the provided stream.

stream

  • Type: Stream
  • Required

The destination stream to receive the items of the array.

callback(streamEnded)

  • Type: Function
  • Required

Callback function executed after all items of the array have been iterated.

callback.streamEnded
  • Type: Boolean

Lets you know if the stream has been ended while items were being pushed.

Версии библиотеки

Версия
2.0.0
1.0.0