easy-peasy icon indicating copy to clipboard operation
easy-peasy copied to clipboard

Access to persist via thunk helpers

Open mkuklis opened this issue 6 years ago • 4 comments

I'm not sure if this makes sense (perhaps there is a better way to handle this). I ran into a situation where I need to run:

store.persist.flush();

from one of the models. It's a thunk used from couple different places. It would be really nice if we could access persist:

https://github.com/ctrlplusb/easy-peasy/blob/72e60706bbf23cfab8b4ef97d6af43e96f5fad75/src/create-store.js#L155

via thunk helpers. In a similar fashion to dispatch, getActions, getStoreActions, etc.

mkuklis avatar May 06 '20 15:05 mkuklis

It looks like I was able to accomplish what I needed via injections so I'm going to close this one.

mkuklis avatar May 08 '20 02:05 mkuklis

@mkuklis We are running in a similar situation: Would you mind explaining how you managed to do it via injections?

mwhnrt avatar Jun 29 '20 06:06 mwhnrt

@mwehnert sure. Here is our current setup:

store.js

import { createStore as createRootStore } from 'easy-peasy';
import rootModel from './models';

let store;

export const getStore = () => store;

export const createStore = () => {
  store = createRootStore(rootModel, { injections: { getStore } });
  return store;
};

one of the models:

const userModel = {
   // ...
   replace: thunk(async (actions, payload, { injections }) => {
    const { getStore } = injections;
    const store = getStore();
    // ...
  }),
  // ....
}

mkuklis avatar Jun 29 '20 15:06 mkuklis

Love it! Thanks.

mwhnrt avatar Jun 29 '20 15:06 mwhnrt