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

@riot-tools/meiosis

v4.0.0

Published

Dependency free state manager for Riot inspired by Meiosis pattern

Downloads

82

Readme

Riot Meiosis

Full documentation can be found here

npm i --save @riot-tools/meiosis

./appState.js

import { RiotMeiosis } from '@riot-tools/meiosis';

// can be an object
const state = {
    initial: true,
    isNew: true,
    mutable: false,
    nested: {
        hasPasta: true
    }
};

// can be a map
const state = new Map([

    ['init', true],
    ['mutable', true],
    ['nested', {
        hasPasta: true
    }]
])

// merge the states when object
const reducer = (newState: object, oldState: object) => ({
    ...oldState,
    ...newState
});

// merge the states when map
const reducer = (newState: object, oldState: Map) => {

    for (const [key, value] of Object.entries(newState)) {

        oldState.set(key, value);
    }

    return oldState;
}

const stateManager = new RiotMeiosis(state);

// Extract the state stream
const { stream } = stateManager;

// Add your state reducer
stream.addReducer(reducer);



// send updated
stream.dispatch({
    initial: false,
    isNew: false
});

// Gets an immutable clone of the current state
console.log(stream.state());
// > {
//     initial: false,
//     isNew: false,
//     mutable: false,
//     nested: {
//         hasPasta: true
//     }
// }

export default stateManager;

mycomponent.riot


<myComponent>

    <p if={ hasPasta }>I have pasta!</p>

    <script>

        import { connect } from './appState';
        import myActions from './actions';

        const mapToState = (appState, ownState, ownProps) => ({
            ...ownState,
            ...appState.nested
        });

        // Optional mapping of functions or objects to component
        const mapToComponent = myActions;
        // OR
        const mapToComponent = (ownProps, ownState) => myActions;

        const component = {

            onBeforeMount() {

                // connect will respect original onBeforeMount
                this.state = {
                    lala: true
                }
            },

            onMounted() {

                // Component will have access to dispatch from lexical this
                this.dispatch({ myComponentMounted: true });
            }
        }

        export default connect(mapToState)(component);
        // OR
        export default connect(mapToState, mapToComponent)(component);
    </script>
</myComponent>

Have many state managers


const stateManager1 = new RiotMeiosis(state1);
const stateManager2 = new RiotMeiosis(state2);

stateManager1.dispatch({ weee: true });
stateManager2.dispatch({ wooo: true });

You do wierd things

const state = {
    // ...
};


const genericReducer = (newState: object, oldState: object, ignore: Symbol) => {

    if (newState.$set) {
        return ignore;
    }

    return {
        ...oldState,
        ...newState
    }
};

const setterReducer = (newState: object, oldState: object, ignore: Symbol) => {

    if (!newState.$set) {
        return ignore;
    }

    lodash.set(oldState, newState.$set, newState.$update);

    return oldState;
};

const stateManager = new RiotMeiosis(state);

stateManager.addReducer(genericReducer, setterReducer);

stateManager.dispatch({

    $set: 'some.nested.value',
    $update: {
        name: 'pepe',
        email: '[email protected]'
    }
});

Go back in time


const stateManager = new RiotMeiosis(state, { statesToKeep: 20 });

stateManager.dispatch({ ... });
stateManager.dispatch({ ... });
stateManager.dispatch({ ... });
stateManager.dispatch({ ... });
stateManager.dispatch({ ... });
stateManager.dispatch({ ... });

// Sends update to all listeners
stateManager.prevState();
stateManager.nextState();
stateManager.goToState(2);
stateManager.resetState();

Listen for incoming changes


const stateSpy = (newState, oldState) => {

    console.log(newState);
};

stateManager.addListener(stateSpy);

Full documentation can be found here