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

flux-lite

v3.0.0

Published

A simplistic implementation of the flux pattern

Downloads

9

Readme

Build Status npm version

flux-lite

This is a simplistic implementation of the flux pattern based off of Facebook's own implementation. This project aims to provide a simple, lightweight, and unopinionated interface that works well with asynchronous tasks.

This project was written in TypeScript, so no additional definitions are necessary if that's what you're using too. Of course, everything still works great with vanilla JavaScript.

API

Dispatcher

The dispatcher handles routing of actions to stores. Constructs that create actions send the actions via the dispatch method, while stores subscribe to actions using the register and unregister methods.

dispatch(payload)

Dispatch a new action to all stores that have registered. Every store will be invoked with the action. It's up to you to decide how actions are structured, but usually each action has a type property to identify what's going on.

dispatcher.dispatch({
  type: 'cartItemAdded',
  itemName: 'boglin'
});

register(callback)

Register a new callback with the dispatcher. The callback will be invoked every time that an action is dispatched. Returns a dispatch token which can be used to unregister the callback later.

unregister(token)

Unregister a callback from the dispatcher, unsubscribing from all actions that are dispatched. This function takes as input the token that was returned from register.

waitFor([ tokens, ... ], action)

This function is invoked by stores during the dispatch process. waitFor waits for other stores to update before proceeding with the current one. Pass the tokens for each store that should update before this one in addition to the action that is currently being processed.

FluxStore

This is an abstract class that you should extend to create stores of your own. Typical implementations will need to override areEqual, getInitialState, and reduce.

getInitialState() [REQUIRED]

Override this function to return the initial state of the store. This is typically an empty array, object, etc.

class ShoppingCartStore extends FluxReduceStore {
  // ...
  
  getInitialState() {
    return [ ];
  }
  
  // ...
}

reduce(state, action) [REQUIRED]

Override this function to tell flux-lite how to update your stores. Reduce takes the current state of the store as well as an action and returns a promise resolving to what the new state of the store should be. Simply resolve to state to ignore the action, or perform some calculations and return a new state object.

class ShoppingCartStore extends FluxReduceStore {
  // ...
  
  reduce(state, action) {
    switch (action.type) {
      case 'cartItemAdded':
        return Promise.resolve(state.slice().push({ name: action.itemName }));
      case 'cartItemRemoved':
        let itemIndex = state.indexOf(action.item);
        let stateCopy = state.slice().splice(itemIndex, 1);
        return Promise.resolve(stateCopy);
      default:
        return Promise.resolve(state);
    }
  }
  
  // ...
}

areEqual(x, y)

This is how the store determines if two states are equivilent. By default areEqual does a reference compare (===), but it can be overridden for custom equality checking logic.

class ShoppingCartStore extends FluxReduceStore {
  // ...
  
  areEqual(x, y) {
    return x.name === y.name;
  }
  
  // ...
}

state

Get the current state of the store. Consumers of the store can access the full state of the store through this method, although you may want to provide more specialized methods that expose only needed data as well.

dispatcher

Get a reference to the dispatcher that this store was registered with

dispatchToken

Get the dispatch token given to this store when it registered with the dispatcher

addListener(callback)

Registers a callback with the store that will be invoked every time the store changes. No parameters are passed to the callback. If you need to know about the state of the store, query it directly from the callback.