clarus / redux-ship

Side effects with snapshots for Redux.
https://clarus.github.io/redux-ship/
MIT License
611 stars 19 forks source link

question: composition and Ship.getState #18

Open mattmarcello opened 7 years ago

mattmarcello commented 7 years ago

first of all, i love this lib.

this could be a sign that i need to improve the design of my software, however i've encountered a use case where it would be useful for Ship.getState to have access to parent state ( i have many layers of composition ).

i wonder if would make sense for the api to be something like Ship.getState((modelState, globalState) => ...

though perhaps this represents some sort of anti-pattern.

nevertheless, sometimes escape hatches are useful.

maybe the problem could be solved by treating calls to the redux store as effects:


export async function run(effect: Effect): Promise<any> {
  switch (effect.type) {
    case "HttpRequest":
      const response = await fetch(effect.url, effect.options);
      return await response.json();
    case "GetGlobalState":
      /**
       * presuming store is in scope
       */
       return effect.selector(store.getState())
    default:
      return;
  }
}

export function getGlobalState <Commit, State>(
  selector: func,
): Ship.Ship<Effect, Commit, State, any> {
  return Ship.call({ type: "GetGlobalState", selector  });
}

i'd appreciate any feedback you might have.

thanks!