项目作者: alexhoma

项目描述 :
:notes: Best javascript latino event dispatcher.
高级语言: JavaScript
项目地址: git://github.com/alexhoma/dispachito.git
创建时间: 2018-03-31T23:39:11Z
项目社区:https://github.com/alexhoma/dispachito

开源协议:

下载


:notes: Dispachito

A javascript event dispatcher with flow, a lot of flow.

Usage

  1. import dispachito from 'dispachito';
  2. // side effect function
  3. const log = (e) => console.log(e);
  4. // side cause function
  5. const now = () => Date.now();
  6. // instance dispachito
  7. const d = dispachito();
  8. // register your side effects and side causes
  9. d.effect(log);
  10. d.cause(now);
  11. // register your pure events
  12. d.event(function whatTimeIsIt({ now }, payload) {
  13. return {
  14. log: payload + now,
  15. };
  16. });
  17. d.dispatch('whatTimeIsIt', 'Current time is: ');
  18. // result: Current time is: 1588975886908

Usage with store

Setup your dispachito instance:

  1. import dispachito, { withStore } from 'dispachito';
  2. const d = withStore(dispachito(), { counter: 0 });
  3. d.event(function increment({ state, now }, payload) {
  4. return {
  5. state: {
  6. counter: state.counter + 1,
  7. },
  8. // another side effect
  9. log: payload + now,
  10. };
  11. });
  12. export default d;

And then in the view side:

  1. import React from 'react';
  2. import { render } from 'react-dom';
  3. import { Subscribe, connect } from 'dispachito/react';
  4. import d from './setup';
  5. const StateAwareCounter = connect(
  6. function mapToProps(state) {
  7. return {
  8. count: state.counter,
  9. };
  10. },
  11. function dispatchToProps(dispatch) {
  12. return {
  13. increment: () => dispatch('increment'),
  14. };
  15. }
  16. )(function Counter({ count, increment }) {
  17. return (
  18. <>
  19. <button onCLick={increment}>Increase</button>
  20. <div>Current count: {count}</div>
  21. </>
  22. );
  23. });
  24. ReactDOM.render(
  25. <Subscribe store={d}>
  26. <StateAwareCounter ></StateAwareCounter>
  27. </Subscribe>,
  28. document.querySelector('#root')
  29. );