Socket
Socket
Sign inDemoInstall

flux-common-store

Package Overview
Dependencies
3
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    flux-common-store

Common Flux Store


Version published
Weekly downloads
11
increased by266.67%
Maintainers
1
Install size
68.7 kB
Created
Weekly downloads
 

Readme

Source

flux-common-store

Dependency Status devDependency Status

Flux Common Store

Installation

npm install --save-dev flux-common-store

API

CHANGE_EVENT (change)

Constant event name, equals change

emitChange()

Emits CHANGE_EVENT

addChangeListener(callback): unsubscribeFunction

Adds callback listener for CHANGE_EVENT and returns unsubscribe function

removeChangeListener(callback)

Alternative way to unsubscribe from CHANGE_EVENT.

WARN For safety reasons, use unsubscribe function returned by addChangeListener instead

Usage

./stores/TodoStore.js
import FluxCommonStore from 'flux-common-store';
import Dispatcher from '../Dispatcher';
import Constants from '../Constants';

let todos = {};

const create = text => {
  const id = Date.now();
  todos[id] = {id: id, complete: false, text: text};
};

const destroy = id => delete todos[id];

const TodoStore = {
  ...FluxCommonStore,

  getAll() {
    return todos;
  }
};

TodoStore.dispatcherIndex = Dispatcher.register(({actionType, payload}) => {
  switch (actionType) {
    case Constants.TODO_CREATE:
      const text = payload.text.trim();
      if (text !== '') {
        create(text);
        TodoStore.emitChange();
      }
      break;

    case Constants.TODO_DESTROY:
      destroy(payload.id);
      TodoStore.emitChange();
      break;

    default:
    // Empty
  }

  return true;
});

export default TodoStore;
./components/TodoApp.js
import TodoStore from '../stores/TodoStore';

const TodoApp = React.createClass({
  getInitialState() {
    return {todos: TodoStore.getAll()};
  },

  componentDidMount() {
    // Subscribe to TodoStore changes which returns `unsubscribe` function
    this.unsubscribe = TodoStore.addChangeListener(this.onChange);
  },

  componentWillUnmount() {
    this.unsubscribe();
  },

  onChange() {
    this.setState({todos: TodoStore.getAll()});
  },

  render() {
    return (
      <ul>
        {this.state.todos.map(todo => <li key={todo.id}>{todo.text}</li>)}
      </ul>
    );
  }
});

export default TodoApp;

License

MIT

Keywords

FAQs

Last updated on 12 Aug 2015

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