Wonka Stream Library

6.3.6 · active · verified Sun Apr 19

Wonka is a lightweight, capable push and pull stream library designed for TypeScript and Flow, loosely adhering to the callbag specification. Currently at stable version 6.3.6, it provides a functional approach to handling event streams and iterable data sets, offering helpers to create, transform, and consume multiple values over time. The project maintains an active release cadence, with frequent patch updates addressing minor fixes and improvements, and occasional minor versions introducing new features, such as adding the `addOne` argument to `takeWhile` in `v6.3.0`. Its key differentiators include its small bundle size, strong TypeScript integration, and a focus on composing operations through a `pipe` function, offering a pragmatic alternative to larger reactive programming libraries while maintaining clear stream semantics. Version 6 notably shifted its focus exclusively to TypeScript, Flow, and JavaScript environments, dropping support for Reason/OCaml/esy/dune that was present in v5.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a stream from an array, applying `filter` and `map` operators, and subscribing. It also shows how to create a custom observable source using `make` with teardown logic for an interval stream.

import { pipe, fromArray, map, filter, subscribe, make } from 'wonka';

// 1. Create a simple stream from an array, transform it, and log values
const numberStream = pipe(
  fromArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
  filter(n => n % 2 === 0), // Keep only even numbers
  map(n => n * 2),          // Double them
  subscribe(value => {
    console.log(`Array Stream: ${value}`);
  })
);

// For demonstration, manually unsubscribe after some time (optional for finite streams)
// setTimeout(() => numberStream.unsubscribe(), 100);

// 2. Create a custom interval stream that emits a few values
const intervalSource = (intervalMs: number) =>
  make<number>(({ next, complete }) => {
    let count = 0;
    const id = setInterval(() => {
      if (count < 3) { // Emit 3 values then complete
        next(count++);
      } else {
        complete(); // Signal completion
        clearInterval(id);
      }
    }, intervalMs);
    return () => clearInterval(id); // Teardown logic
  });

const intervalPipeline = pipe(
  intervalSource(1000), // Emit every second
  map(val => `Custom Interval Stream: ${val}`),
  subscribe(console.log)
);

// The interval stream will automatically complete and clean up after 3 emissions.

view raw JSON →