-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStore.js
70 lines (60 loc) · 1.9 KB
/
Store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as React from 'react';
/**
* If you want to share data between multiple root components, you'll need a
* global store like Redux. This is similar to building a web app where you
* want to synchronize data between a sidebar and a main view - just extended
* into three dimensions.
* To simplify this sample, we implement a trivial Redux-like store that will
* ensure all of our elements are synchronized.
*/
const PokeDesk = {
showPokeballs: false,
showPikachu: false,
showCubone: false,
showCharmander: false,
showHaunter: false
};
const listeners = new Set();
function updateComponents() {
for (const cb of listeners.values()) {
cb();
}
}
export function setPokemonVisibility(pokemon) {
let key = `show${pokemon}`;
PokeDesk[key] = !PokeDesk[key];
updateComponents();
}
export function setPokeballVisibility(status) {
PokeDesk['showPokeballs'] = status;
updateComponents();
}
export function connect(Component) {
return class Wrapper extends React.Component {
state = {
showPokeballs: PokeDesk.showPokeballs,
showPikachu: PokeDesk.showPikachu,
showCubone: PokeDesk.showCubone,
showCharmander: PokeDesk.showCharmander,
showHaunter: PokeDesk.showHaunter
};
_listener = () => {
this.setState({
showPokeballs: PokeDesk.showPokeballs,
showPikachu: PokeDesk.showPikachu,
showCubone: PokeDesk.showCubone,
showCharmander: PokeDesk.showCharmander,
showHaunter: PokeDesk.showHaunter
});
};
componentDidMount() {
listeners.add(this._listener);
}
componentWillUnmount() {
listeners.delete(this._listener);
}
render() {
return <Component {...this.props} PokeDesk={this.state} />;
}
};
}