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

oj-store

v1.0.8

Published

store solution with reducers (Repatch), immutability (Immer), and history undo / redo (oj-diff-patch)

Downloads

4

Readme

Store

A complete state / store solution Get the benefits of immutable data with the ease of mutable data. History (undo / redo, persistent) support with deltas (only the diffs are stored). Support for async (Promise) reducers. Subscribe to store events.

Usage

import

import { Store } from "oj-store";

Initialize

const initialState = {
  name: "Orange Juice",
  deep: { nested: [{ data: "is easy" }] }
}
const store = new Store(initialState)

Reducers

const reducers = {
  setName: (name: string) => draft => { draft.name = name },
  pushDeepNested: (data: { data: string }) => draft => { draft.deep.nested.push(data) },
  setNameFromApi: () => 
    fetch("url/user")
      .then(x => x.json())
      .then(x => draft => { draft.name = x.name})
}
const store = new Store(initialState, { reducers })

History

The history option can be true, false or an object with a getter and setter. You can use the getter and setter for persistency, for example with the localForage package.

const history = {
  set: deltas => localForage.setItem("history", deltas),
  get: () => localForage.getItem("history")
}
const store = new Store(initialState, { history, reducers })

Middleware

const middleware = [
    state => next => reducer => {
      const s = state()
      const nextState = reducer(s)
      console.log("logger", s, nextState)
      next(() => nextState)
    }
]

const store = new Store(initialState, { history, reducers, middleware })

Get state

const current = store.state()
/*
{
  name: "Orange Juice",
  deep: { nested: [ { data: "is easy" } ] }
}
*/

Dispatch reducer

store.dispatch.setName("Awesome")
store.dispatch.pushDeepNested({ data: "super easy" })
/*
{
  name: "Awesome",
  deep: { nested: [{ data: "is easy" }, { data: "super easy" }] }
}
*/

Reduce

store.reduce(d => { d.name = "Awesome" })
store.reduce(d => { d.deep.nested.push({ data: "super easy" }) })
store.reduce(d => ({ name: "Reset", deep: { nested: [] })) // set full state

console.log(store.state())
/*
{
  name: "Awesome",
  deep: { nested: [{ data: "is easy" }, { data: "super easy" }] }
}
*/

Undo / Redo

check if undo/redo is possible

  button.classList.toggle("disabled", !store.canUndo())
  button.classList.toggle("disabled", !store.canRedo())

undo / redo

  store.undo() // undo last change

  store.redo() // redo last undo

  store.undo(4) // undo last 4 changes

Events

Uses the oj-eventaggregator package for handling Events

Change

The change event is emitted after every state change caused by store.reduce(...), store.dispatchreducer, store.undo(...) or store.redo(...) actions.

store.on("change", getState => {
  console.log(getState())
})

Undo

The undo event is emitted after store.undo() changed the state.

store.on("undo", getState => {
  console.log(getState())
})

Redo

The redo event is emitted after store.redo() changed the state.

store.on("redo", getState => {
  console.log(getState())
})

Load

The load event is emitted when the store is initialized and the history is fully restored (if any).

store.on("change", getState => {
  console.log(getState())
})

Types

Store

Store<T, R>

T is the initial state object R is the store reducers object

IReducer

(...args: any[]) => (draft: Draft<T>) => void | Draft<T>

IReducerAsync

(...args: any[]) => Promise<(draft: Draft<T>) => void | Draft<T>>

IReducers<R, T>

{ [k: string]: IReducer<T> | IReducerAsync<T> }

IMiddleware

(state: () => T) => (next: (state: T) => void) => (reducer: any) => any

IStoreOptionsHistory

{
  set: (deltas: Delta[]) => any,
  get: () => Promise<Delta[]> | Delta[] | void,
  ready?: (getState: () => T) => any
}

IStoreOptions<R, T>

{
  history?: boolean | IStoreOptionsHistory<T>
  reducers?: IReducers<R, T>,
  middleware?: IMiddleware<T>[]
}