How to use the easy-peasy.listen function in easy-peasy

To help you get started, we’ve selected a few easy-peasy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ctrlplusb / easy-peasy / src / __tests__ / typescript / listen.ts View on Github external
type StoreModel = {
  user: UserModel;
  audit: AuditModel;
};

const userModel: UserModel = {
  login: thunk((dispatch, payload) => {}),
  logout: action((state, payload) => {}),
};

createStore({
  user: userModel,
  audit: {
    logs: [],
    log: action((state, payload) => {}),
    userListeners: listen(on => {
      on(
        userModel.login,
        thunk((actions, payload, helpers) => {
          const { dispatch, getState, injections, meta } = helpers;
          actions.log(`Logged in ${payload.username}`);
          getState().logs.length;
          dispatch.audit.log('Foo');
          injections.id + 7331;
          meta.parent.concat(meta.path);
        }),
      );
      on(
        userModel.logout,
        thunk((actions, payload, helpers) => {
          const {
            dispatch,
github ctrlplusb / easy-peasy / src / __tests__ / typescript / implementation.tsx View on Github external
},
    });
    const { token } = await response.json();
    actions.loggedIn(token);
  }),
};
const store = createStore({
  audit: {
    logs: [],
    set: action((state, payload) => {
      state.logs.push(payload);
    }),
    getNLog: select(state => (n: number) =>
      state.logs.length > n ? state.logs[n] : undefined,
    ),
    newUserListeners: listen(on => {
      on(
        userModel.login,
        thunk((actions, payload) => {
          actions.set(`Logging in ${payload.username}`);
        }),
      );
    }),
  },
  todos: {
    items: [],
    firstItem: select(state =>
      state.items.length > 0 ? state.items[0] : undefined,
    ),
    addTodo: action((state, payload) => {
      state.items.push(payload);
    }),
github ctrlplusb / easy-peasy-typescript / src / model / notification.ts View on Github external
import { Action, action, Listen, listen, thunk } from "easy-peasy";
import todosModel from "./todos";

export interface NotificationModel {
  msg: string;
  set: Action;
  listeners: Listen;
}

const notification: NotificationModel = {
  msg: "",
  set: action((state, payload) => {
    state.msg = payload;
  }),
  listeners: listen(on => {
    on(todosModel.add, thunk((actions, payload) => {
      actions.set(`Added "${payload}" to todos`);
    }));
  })
};

export default notification;