liveBook · Manning

Micro-library

As explained in Vanilla Web

A small software library providing narrowly scoped functionality.

When browser APIs leave the same awkward glue in several places, a micro-library can remove it without taking over the application. It is a focused tool with a narrow contract: perhaps a Promise wrapper for IndexedDB, a tiny shared store, or a DOM helper. Its value is not its size alone, but whether the application remains recognizable as standard HTML, CSS, and JavaScript if the helper is removed. section 14.1 section 14.1.1

Extract a helper after a pattern repeats in real code, then keep its boundaries explicit:

  • Give it one clear job, explicit inputs and outputs, and predictable cleanup. Do not let a store quietly acquire routing, forms, and a component model. section 14.1.5 section 14.1.1
  • Preserve platform objects and integration points: URLs and browser history for routing, Nodes for DOM work, and transactions beneath an IndexedDB wrapper. Compose through modules, functions, promises, events, and AbortSignal rather than a hidden runtime. section 14.1.1 section 14.1.7
  • Account for unavailable APIs with feature detection and progressive enhancement. Avoid patching native prototypes, which can create collisions and make debugging harder. section 14.1.1 section 14.1.2

Extract only the store-and-render glue

A small store can expose get, set, and subscribe; subscriptions receive the current state and return an unsubscribe function. State notifications can be batched with queueMicrotask. A binding helper may connect selected state to an effect without imposing a template syntax, component base class, or build pipeline. The contract should state when notifications run and how callers clean up. section 14.1.6 section 14.10

export function on(target, type, listener, options = {}) {
  target.addEventListener(type, listener, options);
  if (options.signal) {
    options.signal.addEventListener("abort", () => {
      target.removeEventListener(type, listener, options);
    }, { once: true });
  }
  return () => target.removeEventListener(type, listener, options);
}

An event-listener helper shows the scale of a useful micro-library: it accepts ordinary EventTarget inputs, supports AbortSignal cancellation, and returns an explicit cleanup function. section 14.1.7

Package the basic API as an ES module with named exports, usable without a mandatory bundler. Choose a dependency by the friction it removes: idb and idb-keyval reduce IndexedDB ceremony; Lit and uhtml offer rendering helpers, with Lit nearer the category’s upper edge. A small download is not enough to justify hidden assumptions, maintenance risk, or weak documentation. section 14.1.10 section 14.2 section 14.10

How Vanilla Web covers it

How deep it goes
Works through extraction rules and a tiny store implementation, stopping short of a framework or custom build pipeline.
Examples
Uses a batched shared store, an event-listener cleanup helper, and a local Promise wrapper for IndexedDB.
What it assumes
Assumes familiarity with browser primitives such as components, routing, data loading, and local persistence before evaluating abstractions over them.
What you can do after
Build it from scratch by extracting repeated glue into a narrow module with explicit behavior and cleanup.
How it connects
Connects ES modules, IndexedDB, DOM events, AbortSignal, custom elements, and progressive enhancement through standard browser contracts.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.