0.0.8 • Published 8 years ago

zeroflux v0.0.8

Weekly downloads
2
License
ISC
Repository
-
Last release
8 years ago

Zeroflux

A small, typed state management library.

Example:

'use strict';

import flux = require('zeroflux');

interface TodoAction {
  name: string;
  id: number;
}

const app = new flux.App();

const dispatcher = {
  addTodo: app.action<TodoAction>(),
  removeTodo: app.action<TodoAction>(),
};

interface State {
  todos: TodoAction[];
}

const todoStore = app.store<State>((builder) => {
  builder.reduce(dispatcher.addTodo, (state, todoAction) => {
    return {
      todos: state.todos.concat([ todoAction ])
    };
  });

  builder.reduce(dispatcher.removeTodo, (state, todoAction) => {
    return {
      todos: state.todos.filter((item) => {
        return item.id !== todoAction.id;
      })
    };
  });

  return {
    todos: []
  };
});

const julia = dispatcher.addTodo.dispatch({
  id: 0,
  name: 'julia'
});

const clone = dispatcher.addTodo.dispatch({
  id: 1,
  name: 'evil julia clone'
});

todoStore.watch((state) => {
  console.log('saw new state:', state);
});

console.log('evil clone detected:');
console.log('current state:', todoStore.state);

console.log('eliminating evil clone:');
dispatcher.removeTodo.dispatch(clone);

console.log('evil clone eliminated:');
console.log('current state:', todoStore.state);
0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.1

8 years ago