1.0.0-alpha.118 • Published 8 months ago

@neurongsm/core v1.0.0-alpha.118

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

npm version NeuronGSM Package Package

Neuron Global State Manager (Core)

NeuronGSM Core is a small, bare bones, framework agnostic library for building framework specific global state managers. The goal is to allow devs to create small, scalable, and advanced state managers that fit their respective framework needs. See Neuron as an example.

Setup Store

Create a new Store

import NeuronGSM from '@neurongsm/core';

export const Store = NeuronGSM.Store();

Add initial state to Store

Store.add({
    key: 'name',
    state: 'Ash Ketchum'
});

Store.add({
    key: 'age',
    state: 10
});

Update state

Store.set('name', 'Gary Oak') //key, new state

Get state

Store.get('name') //key

Listen for state changes

Store.onDispatch((dispatchItem) => {
    if (dispatchItem.key === 'name') {
        console.log(dispatchItem.state)
    }
});

//initial console.log output
//name: Ash Ketchum

//new console.log output
//name: Gary Oak

Set Custom Middleware

Middleware can be applied to each individual Store Item's add method. There are three middleware types.

  • onLoad - Runs only once when the add method fires.
  • onRun - Runs every time a set is called.
  • onCallback - Runs after the store is updated. This will run even if dispatch fails.

Each of these methods have access to the payload object. You can manipulate dispatch data with this.

Payload properties and methods:

  • key - Store item key.
  • prevState - Previous state.
  • state - Writable property that holds the state that will be saved to the store.
  • data - Writable property that holds a custom object that can be passed to middleware to make advanced state calculations.
  • features - Features set for the store item. (examples: onLoad, onRun, onCallback, and module specific props passed to the features property in the add method).
  • cancelDispatch - Method that allows for dispatch to be cancelled.
  • isDispatchCancelled - Method that returns the status of the dispatch.
  • get - Method that gets state from a store item by key.
  • set - Method that sets state of a store item by key.
  • reset - Method that resets store item to it's initial state by key. Note: If no key is passed then it will reset all store items to initial. state.
const Store = NeuronGSM.Store();

Store.add({
    key: 'trainer',
    state: 'Ash Ketchum',
    features:{
        onLoad: (payload) => {
            console.log('I only run once when the add method is first set');
        },
        onRun: (payload) => {
            console.log('I run everytime a set method is fired');
        },
        onCallback: (payload) => {
            console.log('I only run after store is saved to state or the dispatch is cancelled.')
        }
    }
});

Dispatch State to store

Like the set method the dispatch method dispatches to a specific store item. The difference is, this method can dispatch a mutator function and the mutator can be ran by the onRun middleware.

Store.add({
    key: 'trainer',
    state: 'Ash Ketchum',
    features:{
        onRun: (payload) => {
            const data = payload.dispatchData;
            if(data.status === 'Beat the Elite Four'){
                payload.state = `Pokemon Master Ash Ketchum`;
            }else{
                payload.cancelDispatch();
            }
        }
    }
});

Store.dispatch('trainer', (payload) => {
   payload.data = { status: 'Lost agian...' };
});

Store.dispatch('trainer', (payload) => {
   payload.data = { status: 'Beat the Elite Four' };
});

Extend NeuronGSM with Modules

Modules like Persist allow you to extend NeuronGSM features. In this case (only works in browser) state will be persisted between page refreshes. Modules must be applied by the use method on the Store. They must be set before any add methods.

import Persist from @neurongsm/persist;

const Store = NeuronGSM.Store();

Store.use(Persist);

Store.add({
    key: 'trainer',
    state: 'Ash Ketchum',
    features:{
        persist: true
    }
});
1.0.0-alpha.118

8 months ago

1.0.0-alpha.116

8 months ago

1.0.0-alpha.115

8 months ago

1.0.0-alpha.114

8 months ago

1.0.0-alpha.113

8 months ago

1.0.0-alpha.112

8 months ago

1.0.0-alpha.111

8 months ago

1.0.0-alpha.110

8 months ago

1.0.0-alpha.109

8 months ago

1.0.0-alpha.108

8 months ago

1.0.0-alpha.107

8 months ago

1.0.0-alpha.105

8 months ago

1.0.0-alpha.104

8 months ago

1.0.0-alpha.103

8 months ago

1.0.0-alpha.102

8 months ago

1.0.0-alpha.101

8 months ago

1.0.0-alpha.100

8 months ago

1.0.0-alpha.99

8 months ago

1.0.0-alpha.98

8 months ago

1.0.0-alpha.97

8 months ago

1.0.0-alpha.96

8 months ago

1.0.0-alpha.95

8 months ago

1.0.0-alpha.94

8 months ago

1.0.0-alpha.93

8 months ago

1.0.0-alpha.92

8 months ago

1.0.0-alpha.91

8 months ago

1.0.0-alpha.89

9 months ago

1.0.0-alpha.88

9 months ago

1.0.0-alpha.87

9 months ago

1.0.0-alpha.86

9 months ago

1.0.0-alpha.85

9 months ago

1.0.0-alpha.83

9 months ago

1.0.0-alpha.82

9 months ago

1.0.0-alpha.81

9 months ago

1.0.0-alpha.80

9 months ago

1.0.0-alpha.79

9 months ago

1.0.0-alpha.78

9 months ago

1.0.0-alpha.77

9 months ago

1.0.0-alpha.76

9 months ago

1.0.0-alpha.75

9 months ago

1.0.0-alpha.74

9 months ago

1.0.0-alpha.73

9 months ago

1.0.0-alpha.72

9 months ago

1.0.0-alpha.71

9 months ago

1.0.0-alpha.70

9 months ago

1.0.0-alpha.69

9 months ago

1.0.0-alpha.68

9 months ago

1.0.0-alpha.67

9 months ago

1.0.0-alpha.66

9 months ago

1.0.0-alpha.65

9 months ago

1.0.0-alpha.64

9 months ago

1.0.0-alpha.63

9 months ago

1.0.0-alpha.62

9 months ago

1.0.0-alpha.61

9 months ago

1.0.0-alpha.60

9 months ago