Closed vslinko closed 9 years ago
Should I add a separate method like dispose
? I'm not sure what the convention is regarding (de)hydration and disposal.
It seems we just need a way to access the atom.
dispose()
seems like the better word for a method that clears the internal state. dehydrate()
in other Flux libraries more closely means "convert state to a serialized string so I can send it to the client."
I can't imagine that someone needs to dispose dispatcher. Btw I have some API proposes:
// 1. current
const hydro = dispatcher.dehydrate()
dispatcher.hydrate(hydro)
// 2. extract dispose
const hydro = dispatcher.dehydrate()
dispatcher.dispose()
dispatcher.hydrate(hydro)
// 3. create getAtom method
const atom = dispatcher.getAtom()
dispatcher.hydrate({atom})
We need to dispose the dispatcher inside componentWillReceiveProps()
to support prop changes / hot reloading. Right now we use the methods dehydrate()
and hydrate()
but I propose that dispose()
and initialize()
are closer to what those methods actually do.
See https://github.com/gaearon/redux/blob/master/src/components/Provider.js#L26-L30
:+1: for getAtom()
method
I think hydrate()
works as a way to replace existing atom, but I don't know if it should also deal with subscriptions...
Yes, the use case for disposing the dispatcher is hot reloading.
I'm done for today but I'd merge a PR that addresses this. @acdlite Can you come up with an API to satisfy both use cases? I don't mind adding more methods if it makes server rendering more convenient.
For server rendering:
getAtom(atom): atom
setAtom(atom)
For hot reloading:
initialize(atom, subscriptions)
dispose(): { atom, subsciptions }
I'll submit a PR when I get home.
Looks good to me!
Maybe initialize({ atom, subscriptions })? So it matches the shape.
Good idea!
I just added getAtom
in master (for a different purpose though): ff56611fdb3b599519fd8998dcd8d650d1e0b683
Closed by #54