MCPcopy Create free account
hub / github.com/bberak/react-native-game-engine / DefaultTimer

Class DefaultTimer

src/DefaultTimer.js:17–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15*/
16
17export default class DefaultTimer {
18 constructor() {
19 this.subscribers = [];
20 this.loopId = null;
21 }
22
23 loop = time => {
24 if (this.loopId) {
25 this.subscribers.forEach(callback => {
26 callback(time);
27 });
28 }
29
30 this.loopId = requestAnimationFrame(this.loop);
31 };
32
33 start() {
34 if (!this.loopId) {
35 this.loop();
36 }
37 }
38
39 stop() {
40 if (this.loopId) {
41 cancelAnimationFrame(this.loopId);
42 this.loopId = null;
43 }
44 }
45
46 subscribe(callback) {
47 if (this.subscribers.indexOf(callback) === -1)
48 this.subscribers.push(callback);
49 }
50
51 unsubscribe(callback) {
52 this.subscribers = this.subscribers.filter(s => s !== callback)
53 }
54}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected