npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mount-ts

v1.1.0

Published

Just-enough glue for writing stateful modules.

Downloads

13

Readme

mount.ts

npm version

"Just-enough" opinionated glue for wiring stateful modules in TypeScript. Heavily inspired by tolitus/mount.

Usage

npm install mount-ts

Why

Real world applications need to manage lifecycle of stateful objects, such as connection pools, http servers, background workers all these sorts of things.

mount.ts provides enough glue for declaring these stateful objects by tapping into the same mechanism for module resolution.

How

Creating state is easy. You provide a name, and a function of 0-arguments that constructors a connection.

import { defstate, enableDebugLogs, start } from 'mount-ts'

export const conn = defstate(
  "conn",
  () => createConnection())
  
enableDebugLogs(); // enable logs
start(); // This executes the start hook of all defstate

In the case where the defstate needs clean up, add an additional argument that takes the state as a parameter.

export const conn = defstate(
  "conn",
  () => createConnection(),
  (conn) => connection.close())

Using State

Any app that relies on the state can import and dereference it for use.

import { conn } from "./conn"

conn() // Invoke as function to dereference

Listening to State Changes

To listen to lifecycle events, you can register event listeners onto the emitter.

import { start, emitter } from 'mount-ts';

emitter.on('start', (s) => console.debug(`<< [mount] ${s.order} - Starting ${s.name}`));
emitter.on('stop', (s) => console.debug(`>> [mount] ${s.order} - Stopping ${s.name}`));

start();
// -- example output
<< [mount] 1 - Starting config
<< [mount] 2 - Starting store
<< [mount] 3 - Starting store.reaper
<< [mount] 4 - Starting http
<< [mount] 5 - Starting app

Dependency Management

mount.ts is a sweet spot where the stateful parts of the application compromise of mainly low-level components of the application, i.e. (I/O, queues, connections). Your system tends to have simple dependency graphs. Application logic is built on top of the stateful layer.

Real world applications can specify dependencies by de-referencing defstate from dependent modules. Example:

// in config.ts
import { defstate } from "mount-ts"

export const config = defstate("config", () => loadConfig());

// in database.ts
import { defstate } from "mount-ts";
import { config } from "./config"
export const pool = defstate("db.pool", () => db.createPool(config().db))

Start and Stop Order

Dependencies are injected by require-ing or import-ing modules. mount.ts relies on module load order to determine which order of sequence is appropriate to load in the correct order.

start() executes the start function of each state in order. stop() executes the stop function (if present) of each state in reverse order.

Examples

See the examples/ folder for how a system is wired together.