How to use the easy-peasy.thunkOn 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 / listener-thunk.ts View on Github external
() => 'ADD_TODO',
  (actions, target) => {
    actions.log(target.payload);
  },
);

const invalid3: ThunkOn = thunkOn(
  // typings:expect-error
  () => 1,
  // typings:expect-error
  (actions, target) => {
    actions.log(target.payload);
  },
);

const invalid4: ThunkOn = thunkOn(
  // typings:expect-error
  () => undefined,
  // typings:expect-error
  (actions, target) => {
    actions.log(target.payload);
  },
);

const valid4: ThunkOn = thunkOn(
  actions => [
    actions.doActionString.type,
    actions.doThunkString.type,
    actions.doThunkString.startType,
    actions.doThunkString.successType,
    actions.doThunkString.failType,
  ],
github ctrlplusb / easy-peasy / src / __tests__ / typescript / listener-thunk.ts View on Github external
actions.doThunkString.failType,
  ],
  (actions, target) => {
    actions.log(target.payload);
  },
);

const invalid5: ThunkOn = thunkOn(
  actions => [actions.doActionString, actions.doThunkNumber],
  (actions, target) => {
    // typings:expect-error
    actions.log(target.payload);
  },
);

const valid5: ThunkOn = thunkOn(
  actions => [actions.doActionString, actions.doThunkNumber],
  (actions, target) => {
    if (typeof target.payload === 'number') {
    } else {
      actions.log(target.payload);
    }
  },
);

const valid6: ThunkOn = thunkOn(
  () => ['foo', 'bar'],
  (actions, target) => {
    actions.log(target.payload);
  },
);
github ctrlplusb / easy-peasy / src / __tests__ / typescript / listener-thunk.ts View on Github external
if (target.error != null) {
      target.error.stack;
    }
    actions.log(target.payload);
  },
);

const invalid1: ThunkOn = thunkOn(
  actions => actions.doActionNumber,
  (actions, target) => {
    // typings:expect-error
    actions.log(target.payload);
  },
);

const valid2: ThunkOn = thunkOn(
  actions => actions.doThunkString,
  (actions, target) => {
    actions.log(target.payload);
  },
);

const invalid2: ThunkOn = thunkOn(
  actions => actions.doThunkNumber,
  (actions, target) => {
    // typings:expect-error
    actions.log(target.payload);
  },
);

const valid3: ThunkOn = thunkOn(
  () => 'ADD_TODO',
github ctrlplusb / easy-peasy / src / __tests__ / typescript / listener-thunk.ts View on Github external
(actions, target) => {
    if (typeof target.payload === 'number') {
    } else {
      actions.log(target.payload);
    }
  },
);

const valid6: ThunkOn = thunkOn(
  () => ['foo', 'bar'],
  (actions, target) => {
    actions.log(target.payload);
  },
);

const valid7: ThunkOn = thunkOn(
  () => 'foo',
  (actions, target) => {
    actions.log(target.payload);
  },
);

// #region multiple listeners with different payload types

interface User {
  firstName: string;
  lastName: string;
}

interface SessionModel {
  user?: User;
  register: Action;
github ctrlplusb / easy-peasy / src / __tests__ / typescript / listener-thunk.ts View on Github external
import { action, Action, Thunk, ThunkOn, thunkOn } from 'easy-peasy';

interface Model {
  log: Action;
  doActionString: Action;
  doThunkString: Thunk;
  doActionNumber: Action;
  doThunkNumber: Thunk;
}

const valid1: ThunkOn = thunkOn(
  actions => actions.doActionString,
  (actions, target) => {
    const [foo] = target.resolvedTargets;
    foo + 'bar';
    target.type + 'foo';
    if (target.error != null) {
      target.error.stack;
    }
    actions.log(target.payload);
  },
);

const invalid1: ThunkOn = thunkOn(
  actions => actions.doActionNumber,
  (actions, target) => {
    // typings:expect-error
github ctrlplusb / easy-peasy / src / __tests__ / typescript / listener-thunk.ts View on Github external
const valid2: ThunkOn = thunkOn(
  actions => actions.doThunkString,
  (actions, target) => {
    actions.log(target.payload);
  },
);

const invalid2: ThunkOn = thunkOn(
  actions => actions.doThunkNumber,
  (actions, target) => {
    // typings:expect-error
    actions.log(target.payload);
  },
);

const valid3: ThunkOn = thunkOn(
  () => 'ADD_TODO',
  (actions, target) => {
    actions.log(target.payload);
  },
);

const invalid3: ThunkOn = thunkOn(
  // typings:expect-error
  () => 1,
  // typings:expect-error
  (actions, target) => {
    actions.log(target.payload);
  },
);

const invalid4: ThunkOn = thunkOn(
github ctrlplusb / easy-peasy / src / __tests__ / typescript / listener-thunk.ts View on Github external
// typings:expect-error
    actions.log(target.payload);
  },
);

const valid5: ThunkOn = thunkOn(
  actions => [actions.doActionString, actions.doThunkNumber],
  (actions, target) => {
    if (typeof target.payload === 'number') {
    } else {
      actions.log(target.payload);
    }
  },
);

const valid6: ThunkOn = thunkOn(
  () => ['foo', 'bar'],
  (actions, target) => {
    actions.log(target.payload);
  },
);

const valid7: ThunkOn = thunkOn(
  () => 'foo',
  (actions, target) => {
    actions.log(target.payload);
  },
);

// #region multiple listeners with different payload types

interface User {
github jamaljsr / polar / src / store / models / designer.ts View on Github external
} else if (fromNode.type === 'bitcoin' && toNode.type === 'bitcoin') {
        // connecting bitcoin to bitcoin isn't supported
        return showError(l('linkErrBitcoin'));
      } else {
        // connecting an LN node to a bitcoin node
        if (fromPortId !== 'backend' || toPortId !== 'backend') {
          return showError(l('linkErrPorts'));
        }

        const lnName = fromNode.type === 'lightning' ? fromNodeId : toNodeId;
        const backendName = fromNode.type === 'lightning' ? toNodeId : fromNodeId;
        getStoreActions().modals.showChangeBackend({ lnName, backendName, linkId });
      }
    },
  ),
  onCanvasDropListener: thunkOn(
    actions => actions.onCanvasDrop,
    async (actions, { payload }, { getStoreState, getStoreActions }) => {
      const { data, position } = payload;
      const { activeId } = getStoreState().designer;
      const network = getStoreState().network.networkById(activeId);
      if (![Status.Started, Status.Stopped].includes(network.status)) {
        getStoreActions().app.notify({
          message: l('dropErrTitle'),
          error: new Error(l('dropErrMsg')),
        });
        // remove the loading node added in onCanvasDrop
        actions.removeNode(LOADING_NODE_ID);
      } else if (['lnd', 'c-lightning', 'bitcoind'].includes(data.type)) {
        const { addNode, toggleNode } = getStoreActions().network;
        try {
          const newNode = await addNode({
github jamaljsr / polar / src / store / models / lightning.ts View on Github external
// TODO: move this check into the delay() func
    if (process.env.NODE_ENV === 'test') return;
    // mapping of the number of seconds to wait for each implementation
    const nodeDelays: Record = {
      LND: 1,
      'c-lightning': 2,
      eclair: 1,
    };
    // determine the highest delay of all implementations
    const longestDelay = nodes.reduce(
      (d, node) => Math.max(d, nodeDelays[node.implementation]),
      0,
    );
    await delay(longestDelay * 1000);
  }),
  mineListener: thunkOn(
    (actions, storeActions) => storeActions.bitcoind.mine,
    async (actions, { payload }, { getStoreState, getStoreActions }) => {
      const { notify } = getStoreActions().app;
      // update all lightning nodes info when a block in mined
      const network = getStoreState().network.networkById(payload.node.networkId);
      await actions.waitForNodes(network.nodes.lightning);
      await Promise.all(
        network.nodes.lightning
          .filter(n => n.status === Status.Started)
          .map(async n => {
            try {
              await actions.getAllInfo(n);
            } catch (error) {
              notify({ message: `Unable to retrieve node info from ${n.name}`, error });
            }
          }),
github jamaljsr / polar / src / store / models / designer.ts View on Github external
if ([link.to.nodeId, link.from.nodeId].includes(nodeId)) {
        delete chart.links[link.id];
      }
    });
  }),
  addNode: action((state, { newNode, position }) => {
    const chart = state.allCharts[state.activeId];
    const { node, link } =
      newNode.type === 'lightning'
        ? createLightningChartNode(newNode)
        : createBitcoinChartNode(newNode);
    node.position = position;
    chart.nodes[node.id] = node;
    if (link) chart.links[link.id] = link;
  }),
  onLinkCompleteListener: thunkOn(
    actions => actions.onLinkComplete,
    (actions, { payload }, { getState, getStoreState, getStoreActions }) => {
      const { activeId, activeChart } = getState();
      const { linkId, fromNodeId, toNodeId, fromPortId, toPortId } = payload;
      if (!activeChart.links[linkId]) return;
      const fromNode = activeChart.nodes[fromNodeId];
      const toNode = activeChart.nodes[toNodeId];

      const showError = (errorMsg: string) => {
        actions.removeLink(linkId);
        getStoreActions().app.notify({
          message: l('linkErrTitle'),
          error: new Error(errorMsg),
        });
      };
      let network: Network;