-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetWrapper.js
62 lines (49 loc) · 1.2 KB
/
NetWrapper.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
import { PureComponent } from 'react';
import Proptypes from 'prop-types';
export default class NetWrapper extends PureComponent {
static contextTypes = {
app: Proptypes.object
}
static childContextTypes = {
renderStage: Proptypes.func
};
constructor(props) {
super(props);
this.stageNeedsRender = false;
this.raf = null;
this.renderStage = this.renderStage.bind(this);
this.tick = this.tick.bind(this);
}
getChildContext() {
return {
renderStage: this.renderStage
};
}
componentDidMount() {
this.renderStage();
// Start render loop here
this.tick();
}
componentDidUpdate() {
this.renderStage();
}
componentWillUnmount() {
window.cancelAnimationFrame(this.raf);
this.stageNeedsRender = false;
}
// This externalizes the standard PIXI ticker for greater control
tick() {
if (this.stageNeedsRender) {
this.context.app.render();
this.stageNeedsRender = false;
}
this.raf = window.requestAnimationFrame(this.tick);
}
renderStage() {
// Don't invoke setState as we're managing pixi re-render not React
this.stageNeedsRender = true;
}
render() {
return this.props.children;
}
}