How to use the easy-peasy.thunk 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 jamaljsr / polar / src / store / models / network.ts View on Github external
})
            .catch(error =>
              actions.setStatus({ id, status: Status.Error, only: btc.name, error }),
            );
        }
      }
      // after all LN nodes are online, connect each of them to eachother. This helps
      // ensure that each node is aware of the entire graph and can route payments properly
      if (allNodesOnline.length) {
        Promise.all(allNodesOnline).then(async () => {
          await getStoreActions().lightning.connectAllPeers(network);
        });
      }
    },
  ),
  rename: thunk(async (actions, { id, name }, { getState }) => {
    if (!name) throw new Error(l('renameErr', { name }));
    const { networks } = getState();
    const network = networks.find(n => n.id === id);
    if (!network) throw new Error(l('networkByIdErr', { networkId: id }));
    network.name = name;
    actions.setNetworks(networks);
    await actions.save();
  }),
  remove: thunk(async (actions, networkId, { getState, getStoreActions }) => {
    const { networks } = getState();
    const network = networks.find(n => n.id === networkId);
    if (!network) throw new Error(l('networkByIdErr', { networkId }));
    const statuses = [
      network.status,
      ...network.nodes.lightning.map(n => n.status),
      ...network.nodes.bitcoin.map(n => n.status),
github jamaljsr / polar / src / store / models / network.ts View on Github external
await actions.save();
      // delete the docker volume data from disk
      const volumeDir = node.implementation.toLocaleLowerCase().replace('-', '');
      rm(join(network.path, 'volumes', volumeDir, node.name));
      if (network.status === Status.Started) {
        // wait for the LN nodes to come back online then update the chart
        await Promise.all(
          lightning.map(n => lightningFactory.getService(n).waitUntilOnline(n)),
        );
      }
      // remove the node from the chart's redux state
      designer.removeNode(node.name);
      await designer.syncChart(network);
    },
  ),
  updateBackendNode: thunk(
    async (
      actions,
      { id, lnName, backendName },
      { injections, getState, getStoreActions },
    ) => {
      const networks = getState().networks;
      const network = networks.find(n => n.id === id);
      if (!network) throw new Error(l('networkByIdErr', { networkId: id }));
      const lnNode = network.nodes.lightning.find(n => n.name === lnName);
      if (!lnNode) throw new Error(l('nodeByNameErr', { name: lnName }));
      const btcNode = network.nodes.bitcoin.find(n => n.name === backendName);
      if (!btcNode) throw new Error(l('nodeByNameErr', { name: backendName }));
      if (lnNode.backendName === backendName)
        throw new Error(l('connectedErr', { lnName, backendName }));

      if (network.status === Status.Started) {
github jamaljsr / polar / src / store / models / lightning.ts View on Github external
// add a small delay to allow nodes to create the closing txn and broadcast it
      await actions.waitForNodes([node]);
      // mine some blocks to confirm the txn
      const network = getStoreState().network.networkById(node.networkId);
      const btcNode = network.nodes.bitcoin[0];
      await getStoreActions().bitcoind.mine({
        blocks: BLOCKS_TIL_CONFIRMED,
        node: btcNode,
      });
      // add a small delay to allow nodes to process the mined blocks
      await actions.waitForNodes([node]);
      // synchronize the chart with the new channel
      await getStoreActions().designer.syncChart(network);
    },
  ),
  createInvoice: thunk(async (actions, { node, amount, memo }, { injections }) => {
    const api = injections.lightningFactory.getService(node);
    return await api.createInvoice(node, amount, memo);
  }),
  payInvoice: thunk(
    async (
      actions,
      { node, invoice, amount },
      { injections, getStoreState, getStoreActions },
    ) => {
      const api = injections.lightningFactory.getService(node);
      const receipt = await api.payInvoice(node, invoice, amount);

      const network = getStoreState().network.networkById(node.networkId);
      // synchronize the chart with the new channel
      await getStoreActions().designer.syncChart(network);
github moltin / shopkit / src / model / index.js View on Github external
import { thunk } from 'easy-peasy'

import modal from './modal'
import user from './user'
import cart from './cart'
import checkout from './checkout'

export default {
  initialize: thunk(
    async (_, { cartId, customerId, customerToken }, { dispatch }) => {
      await dispatch.cart.getCart(cartId)
      await dispatch.cart.setCartId(cartId)
      // await dispatch.user.setCustomerId(customerId)
      // await dispatch.user.setCustomerToken(customerToken)
      // await dispatch.user.getCustomer({ customerId, customerToken })
    }
  ),
  modal,
  user,
  cart,
  checkout
}
github jamaljsr / polar / src / store / models / network.ts View on Github external
}
  }),
  stop: thunk(async (actions, networkId, { getState, injections }) => {
    const network = getState().networks.find(n => n.id === networkId);
    if (!network) throw new Error(l('networkByIdErr', { networkId }));
    actions.setStatus({ id: network.id, status: Status.Stopping });
    try {
      await injections.dockerService.stop(network);
      actions.setStatus({ id: network.id, status: Status.Stopped });
    } catch (e) {
      actions.setStatus({ id: network.id, status: Status.Error });
      info(`unable to stop network '${network.name}'`, e.message);
      throw e;
    }
  }),
  toggle: thunk(async (actions, networkId, { getState }) => {
    const network = getState().networks.find(n => n.id === networkId);
    if (!network) throw new Error(l('networkByIdErr', { networkId }));
    if (network.status === Status.Stopped || network.status === Status.Error) {
      await actions.start(network.id);
    } else if (network.status === Status.Started) {
      await actions.stop(network.id);
    }
    await actions.save();
  }),
  toggleNode: thunk(async (actions, node, { getState, injections }) => {
    const { networkId } = node;
    const network = getState().networks.find(n => n.id === networkId);
    if (!network) throw new Error(l('networkByIdErr', { networkId }));
    const only = node.name;
    if (node.status === Status.Stopped || node.status === Status.Error) {
      // start the node container
github ctrlplusb / easy-peasy / src / __tests__ / typescript / thunk.ts View on Github external
interface AuditModel {
  logs: string[];
  log: Thunk>;
  empty: Thunk;
  syncThunk: Thunk;
}

interface StoreModel {
  audit: AuditModel;
}

const model: StoreModel = {
  audit: {
    logs: [],
    log: thunk(
      async (
        actions,
        payload,
        {
          injections,
          getState,
          getStoreActions,
          getStoreState,
          dispatch,
          meta,
        },
      ) => {
        actions.log(payload);
        getState().logs.length;
        getStoreState().audit.logs.length;
        injections.fetch().then(() => 'done');
github moltin / shopkit / src / model / cart.js View on Github external
updateItem: thunk(
    async (actions, { id, quantity }, { getState, injections: { api } }) => {
      const { id: cartId } = getState()

      const payload = await api.put(`carts/${cartId}/items/${id}`, {
        type: 'cart_item',
        id,
        quantity
      })

      await actions.setCart(payload)
    }
  ),

  removeItem: thunk(async (actions, id, { getState, injections: { api } }) => {
    const { id: cartId } = getState()

    const payload = await api.delete(`carts/${cartId}/items/${id}`)

    actions.setCart(payload)
  }),

  addPromotion: thunk(
    async (actions, code, { getState, injections: { api } }) => {
      const { id: cartId } = getState()

      try {
        const payload = await api.post(`carts/${cartId}/items`, {
          type: 'promotion_item',
          code
        })
github jamaljsr / polar / src / store / models / lightning.ts View on Github external
}
  }),
  setInfo: action((state, { node, info }) => {
    if (!state.nodes[node.name]) state.nodes[node.name] = {};
    state.nodes[node.name].info = info;
  }),
  getInfo: thunk(async (actions, node, { injections }) => {
    const api = injections.lightningFactory.getService(node);
    const info = await api.getInfo(node);
    actions.setInfo({ node, info });
  }),
  setBalances: action((state, { node, balances }) => {
    if (!state.nodes[node.name]) state.nodes[node.name] = {};
    state.nodes[node.name].balances = balances;
  }),
  getBalances: thunk(async (actions, node, { injections }) => {
    const api = injections.lightningFactory.getService(node);
    const balances = await api.getBalances(node);
    actions.setBalances({ node, balances });
  }),
};

export default lightningModel;
github jamaljsr / polar / src / store / models / lightning.ts View on Github external
}
  }),
  setInfo: action((state, { node, info }) => {
    if (!state.nodes[node.name]) state.nodes[node.name] = {};
    state.nodes[node.name].info = info;
  }),
  getInfo: thunk(async (actions, node, { injections }) => {
    const api = injections.lightningFactory.getService(node);
    const info = await api.getInfo(node);
    actions.setInfo({ node, info });
  }),
  setWalletBalance: action((state, { node, balance }) => {
    if (!state.nodes[node.name]) state.nodes[node.name] = {};
    state.nodes[node.name].walletBalance = balance;
  }),
  getWalletBalance: thunk(async (actions, node, { injections }) => {
    const api = injections.lightningFactory.getService(node);
    const balance = await api.getBalances(node);
    actions.setWalletBalance({ node, balance });
  }),
  setChannels: action((state, { node, channels }) => {
    if (!state.nodes[node.name]) state.nodes[node.name] = {};
    state.nodes[node.name].channels = channels;
  }),
  getChannels: thunk(async (actions, node, { injections }) => {
    const api = injections.lightningFactory.getService(node);
    const channels = await api.getChannels(node);
    actions.setChannels({ node, channels });
  }),
  getAllInfo: thunk(async (actions, node) => {
    await actions.getInfo(node);
    await actions.getWalletBalance(node);
github thedevs-network / kutt / client / store / settings.ts View on Github external
customDomain: payload.customDomain,
          homepage: payload.homepage
        }
      ];
    }
  }),
  getSettings: thunk(async (actions, payload, { getStoreActions }) => {
    getStoreActions().loading.show();
    const res = await axios.get(API.SETTINGS, getAxiosConfig());
    actions.setSettings(res.data);
    getStoreActions().loading.hide();
  }),
  setApiKey: action((state, payload) => {
    state.apikey = payload;
  }),
  generateApiKey: thunk(async actions => {
    const res = await axios.post(API.GENERATE_APIKEY, null, getAxiosConfig());
    actions.setApiKey(res.data.apikey);
  }),
  addDomain: action((state, payload) => {
    state.domains.push(payload);
  }),
  removeDomain: action(state => {
    state.domains = [];
  }),
  saveDomain: thunk(async (actions, payload) => {
    const res = await axios.post(API.CUSTOM_DOMAIN, payload, getAxiosConfig());
    actions.addDomain({
      customDomain: res.data.customDomain,
      homepage: res.data.homepage
    });
  }),