Skip to content

Commit 7da4620

Browse files
committed
Tweak API, add (de)hydrate
1 parent 6954066 commit 7da4620

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/Dispatcher.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ function dispatch(store, atom, action) {
33
}
44

55
export default class Dispatcher {
6-
constructor(store, atom) {
6+
constructor(store) {
77
this.store = store;
8+
this.hydrate();
9+
}
10+
11+
hydrate({ atom, subscriptions = [] } = {}) {
812
this.atom = atom;
9-
this.subscriptions = [];
13+
this.subscriptions = subscriptions;
1014
this.dispatch({});
1115
}
1216

13-
receive(dispatcher) {
14-
this.atom = dispatcher.atom;
15-
this.subscriptions = dispatcher.subscriptions;
16-
this.dispatch({});
17+
dehydrate() {
18+
const { atom, subscriptions } = this;
19+
delete this.atom;
20+
this.subscriptions = [];
21+
return { atom, subscriptions };
1722
}
1823

1924
dispatch(action) {
@@ -40,9 +45,4 @@ export default class Dispatcher {
4045
const { atom, subscriptions } = this;
4146
subscriptions.forEach(listener => listener(atom));
4247
}
43-
44-
dispose() {
45-
this.atom = undefined;
46-
this.subscriptions = [];
47-
}
4848
}

src/components/Provider.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ export default class Provider {
2020
}
2121

2222
componentWillReceiveProps(nextProps) {
23-
nextProps.dispatcher.receive(this.props.dispatcher);
24-
this.props.dispatcher.dispose();
25-
}
26-
27-
componentWillUnmount() {
28-
this.props.dispatcher.dispose();
23+
nextProps.dispatcher.hydrate(
24+
this.props.dispatcher.dehydrate()
25+
);
2926
}
3027

3128
subscribe(listener) {

0 commit comments

Comments
 (0)