GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Stream this.push and spread operator does not work · Issue #26582 · nodejs/node

Stream this.push and spread operator does not work #26582

New issue
Copy link
New issue
Copy link
Closed
Closed
Stream this.push and spread operator does not work#26582
Copy link
Labels
invalidIssues and PRs that are invalid.streamIssues and PRs related to Node.js streams.

Description

@akaNightmare
akaNightmare
opened on Mar 11, 2019
Issue body actions
  • Version: v11.10.0
  • Platform: Darwin Ivans-MacBook-Pro.local 18.2.0 Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64 x86_64

Hello, I have written a simple Transform stream to getting some extended user info:

import { Transform } from 'stream';
import { getUsersPublicInfo } from '../../user/services';

const USERS_BATCH_SIZE = 100;

/**
 * Class (extended from Transform stream)
 * for getting minimum user info for diff
 * types of reports. Returns `id`, `email`
 * and `name` in a stream as an object.
 */
export class FillUserTransform extends Transform {
    constructor(options) {
        super({
            readableObjectMode: true,
            writableObjectMode: true,
            ...(options || {}),
        });
        this._userIds = [];
    }

    /**
     * @param {{user_id: string}} chunk
     * @param {string} encoding
     * @param {Function} cb
     * @return {*}
     * @private
     */
    _transform(chunk, encoding, cb) {
        this._userIds.push(chunk.user_id);
        if (this._userIds.length >= USERS_BATCH_SIZE) {
            this._writeUserInfo(cb);
        } else {
            return void cb();
        }
    }

    _flush(cb) {
        if (this._userIds.length > 0) {
            this._writeUserInfo(cb);
        } else {
            return void cb();
        }
    }

    /**
     * @param {Function} cb
     * @return {*}
     * @private
     */
    _writeUserInfo(cb) {
        this.pause();
        getUsersPublicInfo(this._userIds)
            .then(users => {
                if (users.length > 0) {
                    this.push(...users);
                }
                this._userIds.length = 0;
                this.resume();
                cb();
            })
            .catch(err => {
                this._userIds.length = 0;
                this.resume();
                cb(err);
            });
    }
}

but this.push(...users); does not work as expected, replaced spread operator with forEach - works

users.forEach(user => {
    this.push(user);
});

Activity

  1. lpinca commented on Mar 11, 2019

    @lpinca
    lpinca
    on Mar 11, 2019
    Member
    More actions

    I think it works as expected. readable.push() takes one chunk at max.

    Reacted by thales-gaddini
  2. lpinca
    added
    streamIssues and PRs related to Node.js streams.
    on Mar 11, 2019
  3. bnoordhuis commented on Mar 12, 2019

    @bnoordhuis
    bnoordhuis
    on Mar 12, 2019
    Member
    More actions

    Closing as answered.

  4. bnoordhuis
    closed this as completedon Mar 12, 2019
  5. bnoordhuis
    added
    invalidIssues and PRs that are invalid.
    on Mar 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidIssues and PRs that are invalid.streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.