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

state-actuator

v0.9.6

Published

A state management library that models state change as an iterable

Downloads

34

Readme

state-actuator

state-actuator is a state management library that expands on the common "reducer" style of state management. It differs from vanilla React useReducer or Redux in the following ways:

  • All state changes are driven by the submission of a message to the state actuator.
  • Asynchronous state changes are built-in allowing the reducer to return Promises of messages.
  • Messages sent to one actuator can be handled by another. When using state-actuator with React components, it allows a child component to send messages to a parent or ancestor.
  • The implementation uses ES2018 AsyncIterator, which allows for customizing the processing of messages and models.

Why state-actuator

There are a lot of choices for state management, both in the React ecosystem and beyond. I wanted to explore a state system that imagines application or component state as an (asynchronous) iteration over instances of models. What drives the changes to the model? Messages. Messages have been used in GUI application development for decades, and messages provide a structured but flexible mechanism to implement UI changes.

ES2018 introduced asynchronous iteration to the JavaScript language. This means we can now model application state using basic language features such as Promises and Iterators.

Getting Started

Install

$ npm install state-actuator

or

$ yarn add state-actuator

Install with React

If you're planning on using state-actuator to manage state for React components, you also need to make sure react and hoist-non-react-statics is installed. (Those packages are not specified as peer dependencies since they're necessary only when importing the React-specific HOC.)

$ npm install react hoist-non-react-statics

or

$ yarn add react hoist-non-react-statics

Usage Example

The state actuator needs two functions to manage your state. The first is an init() function which creates an initial value for the state. The second is an update() function to process messages into new instances of the model.

function init() {
  return { value: "" }
}

function update(model, msg) {
  switch (msg.type) {
    case "AddDigit":
      return { value: model.value + msg.digit }
    case "DeleteDigit:
      return { value: model.value.slice(0, -1) }
  }
}

const actuator = StateActuator({ init, update })

Concepts

Models

The Model is the interface that defines the structure of the state. A model should contain all the properties that change over time.

Messages

Messages are operations that change the state of the component. They typically represent user actions, but they might also represent changes from the outside world. For example, a message ChangeLocation might be sent every time the OS notifies the app that the location has changed. (See "Subscriptions" below.)

Views

Views are responsible for two crucial parts of an application: (1) rendering the UI based on current state and (2) sending messages based on user actions from the UI.

Subscriptions

A subscription is an optional feature in state actuators. It allows messages to be sent from sources other than the UI view.

Context

Context provides access to state that is managed outside state-actuator. For example, if you're populating your model from a database connection, the Context could include a database session object.

Integrations

See how to use state-actuator with React components: react-integration.md