Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a reset store function #72

Closed
bpetetot opened this issue Feb 13, 2018 · 13 comments
Closed

Add a reset store function #72

bpetetot opened this issue Feb 13, 2018 · 13 comments
Assignees
Milestone

Comments

@bpetetot
Copy link
Collaborator

bpetetot commented Feb 13, 2018

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 :

  • store.reset() : apply the initial state to the entire store
  • store.reset('path') : apply the initial state from the given path
@fabienjuif
Copy link
Member

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
Copy link
Collaborator Author

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
Copy link
Member

fabienjuif commented Feb 14, 2018

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
Copy link
Collaborator Author

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

@fabienjuif
Copy link
Member

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
Copy link
Collaborator Author

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
Copy link
Member

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

@bpetetot
Copy link
Collaborator Author

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
Copy link
Member

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
Copy link
Collaborator Author

For me, redux is always synchronous.

@bpetetot
Copy link
Collaborator Author

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

@fabienjuif
Copy link
Member

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 fabienjuif mentioned this issue Jan 19, 2019
3 tasks
@fabienjuif fabienjuif removed the help wanted Extra attention is needed label Jan 19, 2019
@fabienjuif fabienjuif added this to the 2.0.0 milestone Jan 20, 2019
@fabienjuif
Copy link
Member

#310

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants