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 🙏

© 2026 – Pkg Stats / Ryan Hefner

sam-typescript

v1.1.7

Published

Scalable State Management

Readme

SAM Typescript

State Management made Scalable. Save yourself from state management hell.

Based on Jean-Jacques Dubray's SAM programming model (http://sam.js.org/). All credits go to him for this brilliant model. For a Javascript implementation please see: https://www.npmjs.com/package/sam-pattern.

In SAM (State Action Model architecture), the following is the process.

Action -> Proposals -> Model Mutation -> State -> (Optionally) Next Action

So let's create a simple counter state machine. A counter state machine can be in 2 conceptual states: when the machine is counting and when it is done. This can be expressed as:

State = ShowCountState | MaxCountState
  1. Define your states: Define your states as classes one property 'id' and as readonly. This is required.
import { SAMStateDefinition } from 'sam-typescript';

class ShowCountState implements SAMStateDefinition {
  readonly id = 'show-count';
}

class MaxCountState implements SAMStateDefinition {
  readonly id = 'max-count';
}

type AllStates = ShowCountState | MaxCountState;
  1. Define your model: A model is the global object you will need to mutate. Define this in your typescript file using an interface.
interface SimpleCounterModel {
  count: number;
}
  1. Define your actions that you will call to create a proposal:
import { SAMActionRequestDefinition } from 'sam-typescript';

class ChangeCountAction implements SAMActionRequestDefinition {
  readonly id = 'change-count-action';
  constructor(readonly count: number) {}
}

type AllActions = ChangeCountAction; // for this example, one is all of them.
  1. Define any proposal that an action could create:
import { SAMProposalDefinition } from 'sam-typescript';

class ChangeCountProposal implements SAMProposalDefinition {
  readonly id = 'change-count-proposal';
  constructor(readonly count: number) {}
}

type AllProposals = ChangeCountProposal;
  1. Define a proposal creator
const createProposal = async ({action}) => {
    switch (action.id) {
      case 'change-count-action':
        return new ChangeCountProposal(action.count);
    }
}
  1. Create a model mutator This will accept a previous model and proposal and mutate the model as necessary.
const presenter = ({ model, proposal }) => {
  if (proposal.id === 'change-count-proposal' && proposal.count >= 0 && proposal.count <= COUNT_MAX) {
    model.count = proposal.count;
  }

  return model;
}
  1. Create dynamic state evaluation based on the current model:
const stateDefinitions = () => [
    {
      state: { id: 'show-count' },
      isState: ({ model }) => {
        return model.count < COUNT_MAX;
      },
    },
    {
      state: { id: 'max-count' },
      isState: ({ model }) => {
        return model.count === COUNT_MAX;
      },
    },
  ];
  1. Finally instantiate the the SAM instance.
const COUNT_MAX = 10;

const sam = new SAM<SimpleCounterModel, AllActions, AllProposals, AllStates>({
  model: { 
    count: 0,
  },
  actions: {
    createProposal
  },
  presenter,
  stateDefinitions,
  subscriptions: [{ afterNewState: ({model,state}) => console.log(model,state)}],
});
  1. Call 'execute' to call any action: This will start the loop and end up with state.
sam.execute({ action: new ChangeCountAction(model.count - 1) })

Here is an example with React:

import React from 'react';
import ReactDOM from 'react-dom';

// after setting up the above
const sam = new SAM<SimpleCounterModel, AllActions, AllProposals, AllStates>({
  model: { 
    count: 0,
  },
  actions: {
    createProposal
  },
  presenter,
  stateDefinitions,
  subscriptions: [{ afterNewState: represent }], // change subscription to call represent instead of 'console.log'. 
});

function represent({ model, state }: { model: SimpleCounterModel; state: AllStates }) {
  let representation;
  switch (state.id) {
    case 'show-count':
      representation = (
        <div>
          <div>count is {model.count}</div>
          <button onClick={() => sam.execute({ action: new ChangeCountAction(model.count + 1) })}>increase</button>
          <button onClick={() => sam.execute({ action: new ChangeCountAction(model.count - 1) })}>decrease</button>
        </div>
      );
      break;
    case 'max-count':
      representation = (
        <div>
          <div>count has reached its max at: {model.count}</div>
          <button onClick={() => sam.execute({ action: new ChangeCountAction(model.count - 1) })}>decrease</button>
        </div>
      );
      break;
    default:
      const _exhaustiveCheck: never = state;
  }

  ReactDOM.render(representation, document.getElementById('root'));
}

For a slightly more complicated example, please see 'rocket-launcher.tsx' under the 'example' folder.