unirakun / k-ramel

State manager for your components apps, the safe and easy way
MIT License
21 stars 5 forks source link

Add a reset store function #72

Closed bpetetot closed 5 years ago

bpetetot commented 6 years ago

In different use cases, sometimes, I need to reset the store or a part of the store.

For example, when I switch from a connected user to another I need to reset a huge part of the store and its quite exhaustive to do. Because I have to call each reset() methods of my reducers, or create RESET actions to my custom reducers. And, in the future, if I have to add a new reducer, I dont must forget to reset it when needed.

I was thinking about a method :

fabienjuif commented 6 years ago

What do you call an initial store ?

  1. The init parameter (given to createStore) ?
  2. The defaultData from k-redux-factory reducers ?

Implementation

Right now I think about:

  1. trigger an action : @@krml/RESET${path ? '>' : ''}${path} for custom reducers and reactions
  2. calls reset in the tree when reducers are from k-redux-factory, but I don't know if I keep the store declaration at runtime (after the store is created)
bpetetot commented 6 years ago

For me initial store, means the state of the store when I have started it for the first times.

The equivalent in redux is calling each reducers with an undefined state and an empty action. (acting like redux executing them for the first time.)

fabienjuif commented 6 years ago

I am speaking about redux behaviour. If you pass an initState to redux, I believe it inject that initial state on each subreducer at init (not always valued undefined)

This is the reason of my question

bpetetot commented 6 years ago

Ok so, I think I waiting to have the given initial state. (like redux do when it starts for the first time)

fabienjuif commented 6 years ago

Mini algorithm

  1. Retrieve path parameter
  2. Retrieve description of the store (so we can know if a path is a an object description, or a function (reducer))
  3. For each leaf a. find the initState into createStore((), { init }) parameter (init) for the same path b. if this is a krf reducer, calls reset -> 🚧 we have to accept a state into reset payload on krf 🚧 b)bis. if this is a custom reducer, calls ??? 🆘
bpetetot commented 6 years ago

b) bis. execute the custom reducer with the init for the same path : custom(init, {}) and I think it's could be the same with krf reducers.

fabienjuif commented 6 years ago

How do you mutate the redux store ? :) This is not as easy as you say. Maybe we have to pass by an enhancer

bpetetot commented 6 years ago

Of course, it's not as easy as I say 😄

But... maybe we could try to use replaceReducer doing something like that :

store.replaceReducer(combine(store[pathInReducerTree]))
fabienjuif commented 6 years ago

Yes maybe, I don't really know how it behave. Is it synchronous ? In this case this is ok for me. Is it asynchronous ? In this case, how Redux handles action meanwhile reducer is being replaced ?

bpetetot commented 6 years ago

For me, redux is always synchronous.

bpetetot commented 6 years ago

I tried but replaceReducer doesn't work as I expected. If the reducer already exists, it doesn't seems to update it :(

fabienjuif commented 5 years ago

Mouahaha

➜  k-ramel git:(master) ✗ node reset.js
Data
 { data:
   { nested: { input: 'example' },
     shows: { data: [Array], initialized: true } },
  ui: true }
reset "ui"
 { data:
   { nested: { input: 'example' },
     shows: { data: [Array], initialized: true } },
  ui: false }
Data
 { data:
   { nested: { input: 'example' },
     shows: { data: [Array], initialized: true } },
  ui: true }
reset ""
 { data:
   { nested: { input: '' },
     shows: { data: [], initialized: false } },
  ui: false }
Data
 { data:
   { nested: { input: 'example' },
     shows: { data: [Array], initialized: true } },
  ui: true }
reset "data.nested"
 { data:
   { nested: { input: '' },
     shows: { data: [Array], initialized: true } },
  ui: true }
➜  k-ramel git:(master) ✗
fabienjuif commented 5 years ago

310