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

yocto-flux

v1.1.5

Published

A yocto-scale lightweight substitute for Flux/Redux

Downloads

4

Readme

yocto-flux

npm version Build Status Coverage Status

A yocto-scale lightweight substitute for Flux/Redux.

yocto-flux is a teeny-tiny event handler (emitter/receiver) that can be used as a very simple store for smaller applications.

If you just want to bootstrap a page and don't want to add all the complexities and abstractions of Flux/Redux/Mobx, use yocto-flux!

Install

Just drop it to your packages.json:

    npm i --save yocto-flux

Simple Example

Just import the store anywhere you desire and either send a message with emit or register a callback with on:

    const store = require('yocto-flux');

    store.on('overlay', v => $('#overlay').toggle(v));

    // elsewhere
    store.emit('overlay', true);

This is just a silly example, anything can be emitted and will be received by every handler.

The full API is quite simple, actually:

  • evt(event_name): get the array of registered callbacks for this event (this is used internally)
  • next(event_name, callback): like on, but doesn't call for the last event emitted. It returns the id of the callback as well.
  • on(event_name, callback): register the callback to be called every time event_name is emitted, it will fire for the last sent event if any. It returns an assigned id for the registered callback to be cleared later with clear.
  • once(event_name, callback): register the callback to run once in the future (like next, but unregister after first called). It also returns a Promise that resolves when the next value is emitted. The callback parameter is optional.
  • emit(event_name, value): emits the value provided to every listener of the given event_name.
  • get(event_name): gets the last emitted value for this event, or undefined if the event was never emitted.
  • clear(...ids): clear the given ids, that is, unregister their callbacks.
  • reset(): completely clears everything (useful for testing).

The common parameters are:

  • event_name: a name for the event, in order to identify it. Technically it is compared with the equality operator, so any reference would work, but the intended use is for Strings. You can define your own patterns for namespacing and conventions.
  • callback: the callback function to be called when an event is emitted; it takes as first parameter the value emitted.
  • id: the id of the registered callback. Any calls to next or on will return an id to the registered callback, that is basically a new unique Symbol. It can be used to later clear that callback.
  • value: the value that was emitted; it can be anything.

React Example

On React, it's very easy to use this to message stuff between your components.

Again, be warned that this is a very simplistic approach and will rot quickly in larger projects; consider using a more robust solution, like nanoflux for that.

But you could use it like so:

    class ListPeople extends Component {
        componentWillMount() {
          this.ids = [];
          this.ids.push(store.on('login', login_data => this.setState({ login_data })));
        }

        componentWillUnmount() {
          store.clear(this.ids);
        }
    }

Don't forget to clear any registered callbacks on componentWillUnmount method!

Want more?

This is very simple. It's yocto in size. So, 10^12 times smaller than nano (maybe not literally).

If you have something bigger, this is hard to keep organized. I would suggest using a more complex and feature-full implementation of Flux/Redux/anything, starting with nanoflux or the standard libs (from Facebook or others).

Contribute!

Please, do so!

  • Fork it!
  • Create a feature branch.
  • Make your modifications (remember to KISS)
  • Submit your PR

Any help is appreciated! Thanks!