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

@zuze/modifiable

v6.2.2

Published

Declarative state manipulation for handling transient/reversible modifications

Downloads

9

Readme

modifiable

npm version Coverage Status Build Status Bundle Phobia

What?

State Management - a lot like redux except completely different.

No, really, what?

@zuze/modifiable originally came about when figuring out an efficient way to deal with conditional things happening in forms - when user inputs X into field Y, then field Z should behave a certain way.

In the above scenario @zuze/modifiable refers to the user input as context and state as anything that may be modified in response to a change in context.

The concept of context and state interacting this way led to the creation of this super tiny (but tremendously powerful) package.

Why?

Think of it as a tool for performing transient/reversible state modifications.

A little like the super cool immer, but much more narrowly focused.

How?

  • Create a modifiable with a state (any value) and optionally modifiers and initial context.

  • State is changed by setting context - a plain JS object - on the modifiable, however it can also be changed by adding/removing modifiers (using modify)

  • A modifier is a higher order function that accepts a single argument - context - and returns a function (modification function) that accepts the last modified state and returns the next modified state.

  • A modifier SHOULD specify it's "dependencies" - keys of the context, or functions that accept context and return a value. Specifying dependencies serve to memoize the modifier so it's only run when necessary.

  • Whenever context changes, a modifiable will run all modifiers that have

    • no dependencies or

    • dependencies that have changed since the last run

  • All modification functions returned from modifiers will then be applied sequentially to the state

  • Just like redux, modifier and modification functions, must be pure

API

Create a new modifiable by

modifiable<T,R>(state: T, options?: { modifiers: Array<Modifier<T,R>>, context: R} )

- subscribe(Subscriber: (state: T) => void): () => void

Subscribes to changes in the state and returns an unsubscribe function. (identical to function of the same name on a redux store).

For better control, this function also accepts a signature idential to reselect:

- subscribe(...selectors: Array<(state: T) => any>, resultFunc(...selectorResults: any[], state: T) => void): () => void

- getState(): T

Returns the modified state (identical to function of same name on a redux store)

- setContext(Partial<R> | (existing: R) => R)

Just like React's setState. Causes necessary ModifiedFunctions to run. Setting context is the primary way to change a modifiable's state. (Think of it as redux dispatch).

Note: When using a patch function, if you return a new reference (regardless of shallow equality), the applicable modifiers will be run again and subscribers will be notified if anything has changed. If you return the same reference, nothing will happen.

- modify(ModifierFunction, ...deps?: Array<string | (context: R) => any)>: () => void

Adds a modifier at run time. Modifiers added a runtime will be run immediately. Returns a function to remove the ModifierFunction. Adding/removing modifiers are the second way to change a modifiable's state.

- clear(context?: R)

Removes all modifiers and (optionally) sets the context. If no context is supplied, context won't be changed.

Additional

- identity<T>(arg: T) => T

- patch<T>(next: Partial<T> | ((current: T) => T, current: T) => T

Small

Core implementation of modifiable, minified and gzipped, is about 700 bytes.