Socket
Socket
Sign inDemoInstall

dumbx

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    dumbx

A very dumb way of using some Redux principles


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

dumbx-js

A very dumb way of using some Redux principles.

Usage

First of all, instantiate the store:

const Dumbx = require('./dumbx');

const store = new Dumbx({
  state: {
    isPlaying: false,
    music: ''
  },
  setters: {
    pause(state) {
      state.isPlaying = false;
    },
    play(state) {
      state.isPlaying = true;
    },
    selectMusic(state, payload) {
      state.music = payload.name;
    }
  }
});

Then, on the UI component, subscribe for store updates:

// Component render example
const render = () => {
  console.log(store.getState().isPlaying);
};

// Subscribe render returns unsubscribe function
const unsubscribe = store.subscribe(render);

Dispatch actions on user interactions (example):

// Dispatching actions
const pauseBtn = document.querySelector('#pause');
const playBtn = document.querySelector('#play');

pauseBtn.addEventListener('click', () => {
  store.dispatch('pause'); // store.getState().isPlaying => `false`
});

playBtn.addEventListener('click', () => {
  store.dispatch('play'); // store.getState().isPlaying => `true`
});

document.addEventListener('DOMContentLoaded', () => {
  store.dispatch('selectMusic', {
    name: 'Awesome!'
  }); // store.getState().music => 'Awesome!'
});

Optionally, unsubscribe whenever component is distroyed:

const destroy = () => {
  unsubscribe();
};

Testing locally

Run the example file with:

$ node example.js

FAQs

Last updated on 21 May 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc