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

fysics

v1.1.56

Published

State Management Module

Downloads

19

Readme

fysics

fysics — Future of State Management

An Open Source Universe Project


Contents

Features ✨

  • Define a State Store
  • Add Reducers & Actions to your State Store
  • Define Async Side Effects via Saga Functions
  • Create Bulk Actions by calling other Actions
  • Subscribe to the State Object
  • Subscribe to Deltas of the State Object
  • Typescript Support

Install 🛠

npm install fysics

Usage 🔭

Read more about the Design behind fysics here.

import { createStore } from 'fysics';
// Create the store
const STORE = createStore({
 initialState: { count: 0 },
 actions: {
  INCREMENT: {
    reducer(state, { amount }) {
      state.count += amount ?? 1;
    }
  },
  DECREMENT: {
    reducer(state, { amount }) {
      state.count -= amount ?? 1;
    }
  },
  INCREMENT_SAGA: {
    async saga(state, { url }) {
      const response = await fetch(url);
      const { amount } = await response.data.json()
      return amount;
    },
    reducer(state, payload, { amount }) {
      state.count += amount ?? 1;
    }
  },
  DECREMENT_BULK: {
    reducer(state, { listOfAmounts }) {
      for (let amount of listOfAmounts) {
        this.DECREMENT.reducer(state, amount);
      }
    }
  }
 }
});
// And Use It :)
STORE.dispatch({ name: 'INCREMENT', payload: { amount: 5 } });
// { count: 5 }
STORE.dispatch({ name: 'DECREMENT_BULK', payload: { listOfAmounts: [2, 3] } });
// { count: 0 }
STORE.undo();
// { count: 5 }
const unsubscribe = STORE.subscribe((stateObject) => {
  console.log(stateObject);
});

Documentation 🛰

The Store returned by the createStore function is contains methods to interface with two internal components: the store's present state (of type State), and the store's full state, which is defined as

FullState<State, Payload>;

where

type FullState<State, Payload> = {
  past: Array<TimelineEvent<PayloadType>>;
  present: State;
  future: Array<TimelineEvent<PayloadType>>;
};

type TimelineEvent<Payload> = {
  action: DispatchAction<Payload>;
  patches: Array<Patch>;
  inversePatches: Array<Patch>;
};

type DispatchAction<Payload> = {
  name: string;
  payload?: Payload;
};

are the relevant type definitions. See here to understand the Patch type imported from immer.

subject

subject: () => Subject<State>;

Gets the subject for the store's present state.

subjectAll

subject: () => Subject<FullState<State, Payload>>;

Gets the subject for the store's full state.

subscribe

subscribe: (observer?: Partial<Observer<State>> | ((value: State) => void)) =>
  Subscription;

Creates a subscription to the store's present state with the specified observer or function to run when the store's present state subject pushes a new value (i.e. when the store's present state changes).

subscribeAll

subscribe: (observer?: Partial<Observer<State>> | ((value: State) => void)) =>
  Subscription;

Creates a subscription to the store's full state with the specified observer or function to run when the store's full state subject pushes a new value (i.e. when the store's full state changes).

get

get: () => State;

Returns a deep copy of the store's present state.

getAll

getAll: () => FullState<State, Payload>;

Returns a deep copy of the store's full state.

dispatch

dispatch: (action: DispatchAction<Payload>) => void

Dispatches an Action (see the Usage section) by name, with the provided payload.

undo

undo: () => void

Undoes actions in the store's past until an action with skipUndo: false is undone, or does nothing if all the actions in the store's past have skipUndo: true.

redo

redo: () => void

Redoes actions in the store's future until the second most recent action with skipUndo: false is on top of the stack, or redoes everything if there is no such action.

rebase

rebase: () => void

Clears the past and future of the store's full state.

Contributing 🌎

We would love for you to contribute your ideas, code, & fixes to fysics.

We encourage everyone to read our Design Document to learn more about the thought process behind fysics.

Also check out the rewards offered for contributing to the Open Source Universe.

License ⚖️

MIT