typestar

Tiny typed store in TypeScript

A tiny observable store, typed on its state shape.

type Subscriber<T> = (state: T) => void;

class Store<T> {
  private subscribers: Subscriber<T>[] = [];

  constructor(private state: T) {}

  set(state: T): void {
    this.state = state;
    for (const fn of this.subscribers) fn(state);
  }

  subscribe(fn: Subscriber<T>): void {
    this.subscribers.push(fn);
  }
}

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
295
Tokens
95
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 52 seconds.

Type this snippet

Step 3 of 5 in Building & injecting, step 11 of 19 in Classes & patterns.

← Previous Next →