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

immer-compose

v1.1.7

Published

A utility for composing concurrent operations, yet allowing state to be merged in series.

Downloads

29

Readme

NPM Package Version GitHub Repository Changelog GitHub Workflow Status (main) Test Coverage

immer-compose

A utility for composing concurrent operations, yet allowing state to be merged in series.

Install / Prerequisites

Immer is a required peer-dependency.

npm install immer immer-compose

Motivation

I was looking for a neat way to merge async updates to an arbitrary document (JSON Schema) in a sequential order - in my use-case the updates may only be loosely related and I wanted to separate concerns by expressing them as middleware, e.g.

const pipeline = compose(middlewareOne, middlewareTwo, middleWareThree)
const newState = pipeline(initialState)

Yes it's another micro-library... but I found this idea somewhat complex to think through and felt that it warranted an isolated module - no doubt there'll be alternatives, but I couldn't find what I was looking for - with immutability in mind.

Usage

The functions compose and composeWithPatches are exposed mirroring the producer functions provided by immer. Note that to use patches, it is still required that you import enablePatches from immer - this module assumes nothing about which version you are using.

Compose arguments are higher-order functions that can execute any asynchronous task and in-turn return an immer recipe - where each recipe will consume a draft of the previous state in the chain.

const higherOrderRecipe = async (initialState) => {
    const data = await task()
    return (draftState) => {
        draftState.foo = 'bar'
    }
}

Additionally, any other input passed to the composed function will be passed through.

const higherOrderRecipe = async (initial, a, b, c) => (draft) => {}
const pipeline = compose(higherOrderRecipe /* [, 0..*] */)
const newState = pipeline(state, 1, 2, 3)

Experimental  ⚠️

const experimental = async () => async () => {}

If a recipe is returned as an async function, the compose function will prioritise the order of the synchronous recipes before processing the remaining tasks on a first-come first-serve basis. Effectively allowing middleware to self-determine whether to keep a slot in the queue or go last.

The implementation should be reliable in modern environments however, should also be considered experimental as I don't know where I want to take this idea yet and also don't know how much utility it really affords - I was just playing around.

This behaviour is reliant on native async functions and won't work with regular functions returning promises - due to this, it also will not work if you are targeting es5 through code transpilers like Babel.