Code Monkey home page Code Monkey logo

readyx's Introduction

Readyx

INTERFACE

type State = { [keys: string]: any; };
type Action = string | number;
type Listener = (action?: Action, state?: State) => void;
type Reducer = (state: State, action: Action) => State;

abstract class ReadyxStore {
    abstract action(action: Action): ThisType<any>;
    abstract on(action: Action, listener: Listener): ThisType<any>;
}

Суть

Сначала мы создаем сам экземпляр вида:

import { Readyx } from './Readyx';
const newReadyxStorage: Readyx = new Readyx(
    // Reduces
    reducer: (state: State, action: Action) => any,
    // Initialize state
    state: State
);

После, можно назначить слушатель (то, что происходит перед изменением состояние):

newReadyxStorage.on(action: Action, listener: Listener);

И после, вызывать последующие изменения состояний вида:

newReadyxStorage.action(action: Action);

Фичи

Чейнинг

const store = new Readyx(reducer, { isVisible: true });
store
    .on('click', (state) => {
        document.body.style.overflow = state.isVisible;
    })
    .on('close', (state, action) => {
        document.body.classList.toggle(`page_action_${action}`, state.isVisible)
    })
    .action('click')
    .action('close');

Упорядоченный событийный цикл

const reducer = (state, action) => {
    console.log(state.count);
    if (action === '4') state.count -= 4;
    return state;
};

const store = new Readyx(, { count: 0 });
store
    .on('4', (state, action) => {
        console.log(state.count);
        state.count += Number(action);
    })
    .on('4', (state, action) => {
        console.log(state.count);
        state.count -= Number(action) / 2;
    })
    .action('4');
// Output:
// 0 -> 4 -> 2

readyx's People

Contributors

hyrdbyrd avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.