Skip to content
⚠️ This article was written in 2019. Some content may be outdated.

Node.js Stream Processing

Node.js stream processing is a topic encountered frequently in day-to-day development. This article draws from real projects to share practical implementation approaches and lessons learned.

Basic Concepts

Here is a basic usage example:

javascript
export default {
  props: ["items"],
  computed: {
    sorted() {
      return [...this.items].sort((a, b) => b.score - a.score);
    },
    count() {
      return this.items.length;
    },
  },
  filters: {
    formatDate(val) {
      return new Date(val).toLocaleDateString("en-US");
    },
  },
};

This pattern is concise and suitable for most scenarios.

Deep Dive

Here is the core code:

javascript
export default {
  directives: {
    focus: {
      inserted(el) {
        el.focus();
      },
    },
    loading: {
      bind(el, binding) {
        if (binding.value) {
          el.classList.add("loading");
        }
      },
      update(el, binding) {
        el.classList.toggle("loading", binding.value);
      },
    },
  },
};

In real projects, you also need to consider edge cases and error handling.

Project Application

Here is a real-world example:

javascript
import React, { Component } from "react";

class DataList extends Component {
  state = { items: [], loading: true };

  async componentDidMount() {
    const res = await fetch("/api/items");
    const items = await res.json();
    this.setState({ items, loading: false });
  }

  render() {
    const { items, loading } = this.state;
    if (loading) return <div>Loading...</div>;
    return (
      <ul>
        {items.map((item) => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    );
  }
}

After promoting this pattern across the team, the results were great and maintenance costs dropped noticeably.

MIT Licensed