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

riotjs-simple-state

v0.4.2

Published

Simple centralized state management for RiotJS

Downloads

7

Readme

riotjs-simple-state

A straight forward centralized state management system for RiotJS in ~150 lines of TypeScript.

There are two different types of shared objects:

  1. Serializable shared objects used with publish(), load(), watch(), delete(). These objects can traverse window/CPU boundaries if you make them and they follow a publish/subscribe pattern. This is the common case of working with shared objects.

  2. Any non necessarily serializable object set on the .ref object. These objects are just as they are and can be used for singleton type of uses cases such as shared database connection instances, etc.

Installation

npm i riotjs-simple-state

Setup

There are two ways of instantiating the StateController. Either use the global instance or instantiate it yourself and share that objects as you see fit.

1. Use the global instance

import {
    stateController,  // note: lower-case "s", this is an instance.
} from "riotjs-simple-state";

This is a ready to use StateController instance shared across your app.

Note that this option does not rely on riotjs and can be used in plain JS/TS.

2. Instantiate yourself

In main.js:

import {StateController} from "riotjs-simple-state";

const stateController = new StateController();

riot.install(function(component) {
    component.stateController = stateController;
});

Now, every riotjs component will have the stateController object accessible as this.stateController.

Usage

One of your components must create the state, then any component can retrieve the state and update the state.

Note that all objects must be serializable and cannot contain class instances of any kind.

// It is generally advised to deal with state creation and loading/watching using onMounted,
// and not onBeforeMount.
// This is to avoid tricky situations where components who have not yet mounted are updated by riot
// which leads to unexpected behaviour.
//
onMounted(props, state) {
    // Create state on before mount so it is existing prior to initing child components.
    //
    this.stateController.create("myState", {a: 1});

    // watch can be done in same component creating the state and/or in any other component using the state.
    //
    this.stateController.watch("myState", myState => {
        this.myState = myState;
        this.update();
    );

    // or to only load the state once, do:
    //
    this.stateController.load("myState").then( myState => {
        this.myState = myState;

        this.update();
    }
}

onUnmounted(props, state) {
    // delete state on unmount so that child components are already unmounted.
    //
    this.stateController.delete("myState");
}

// At some point you want to share the updated state with other components interested, do:
//
this.stateController.publish("myState", this.myState);

// the `.ref` can be be used to share data without publishing it.
// An object set on `.ref` can be any object or instance, it will never be
// serialized like other shared objects will.
// Features such as watch() cannot be used with `.ref`.
//
this.stateController.ref.databaseConnection.fetch(...);

A good practice is to only allow one writer for each state. But it is OK to have multiple writers, however the programmer must make sure there are no concurrent updates ofr the state since that is a race condition which can give unexpected results. Also, make sure that changes to state is made to latest state object retrieved by load() or watch().