|
1 | | -let loopIsActive = false; |
2 | | -let animationFrame = null; |
3 | | -const loopRegister = []; |
4 | | - |
5 | | -class LoopController |
6 | | -{ |
7 | | - constructor() { |
8 | | - this.rafStep = this.rafStep.bind(this); |
9 | | - } |
10 | | - |
11 | | - getRegisteredItems() { |
12 | | - return [...loopRegister]; |
13 | | - } |
14 | | - |
15 | | - registerScrollbar(scrollbar) { |
16 | | - if (!loopRegister.includes(scrollbar)) { |
17 | | - loopRegister.push(scrollbar); |
18 | | - |
19 | | - this.start(); |
| 1 | +function LoopControllerClass() { |
| 2 | + /** |
| 3 | + * @typedef {Object} Scrollbar |
| 4 | + * @property {function} update |
| 5 | + */ |
| 6 | + |
| 7 | + /** |
| 8 | + * @type {Scrollbar[]} |
| 9 | + */ |
| 10 | + const scrollbarsRegister = []; |
| 11 | + |
| 12 | + /** |
| 13 | + * true if loop is active |
| 14 | + * @type {boolean} |
| 15 | + */ |
| 16 | + let isActive = false; |
| 17 | + /** |
| 18 | + * ID of requested animation frame |
| 19 | + * @type {null|number} |
| 20 | + */ |
| 21 | + let animationFrameId = null; |
| 22 | + |
| 23 | + /** |
| 24 | + * Function that called in animation frame |
| 25 | + */ |
| 26 | + const animationFrameCallback = () => { |
| 27 | + if (!isActive) {return;} |
| 28 | + |
| 29 | + for (let scrollbar of scrollbarsRegister) { |
| 30 | + scrollbar.update(); |
20 | 31 | } |
21 | 32 |
|
22 | | - return this; |
| 33 | + requestAnimationFrame(animationFrameCallback); |
23 | 34 | }; |
24 | 35 |
|
25 | | - unregisterScrollbar(scrollbar) { |
26 | | - let index = loopRegister.indexOf(scrollbar); |
| 36 | + /** |
| 37 | + * Stop the loop if it wasn't active |
| 38 | + * @return {LoopControllerClass} |
| 39 | + */ |
| 40 | + this.start = () => { |
| 41 | + if (isActive) {return this;} |
27 | 42 |
|
28 | | - if (index !== -1) { |
29 | | - loopRegister.length === 1 && this.stop(); |
| 43 | + isActive = true; |
30 | 44 |
|
31 | | - loopRegister.splice(index, 1); |
32 | | - } |
33 | | - |
34 | | - return this; |
| 45 | + animationFrameId && cancelAnimationFrame(animationFrameId); |
| 46 | + requestAnimationFrame(animationFrameCallback); |
35 | 47 | }; |
| 48 | + /** |
| 49 | + * Stop the loop if it is active |
| 50 | + * @return {LoopControllerClass} |
| 51 | + */ |
| 52 | + this.stop = () => { |
| 53 | + if (!isActive) {return this;} |
36 | 54 |
|
37 | | - start() { |
38 | | - if (!loopIsActive) { |
39 | | - loopIsActive = true; |
| 55 | + isActive = false; |
40 | 56 |
|
41 | | - animationFrame && cancelAnimationFrame(animationFrame); |
42 | | - animationFrame = requestAnimationFrame(this.rafStep); |
43 | | - } |
44 | | - |
45 | | - return this; |
| 57 | + animationFrameId && cancelAnimationFrame(animationFrameId); |
| 58 | + animationFrameId = null; |
46 | 59 | }; |
47 | 60 |
|
48 | | - rafStep() { |
49 | | - if (!loopIsActive) {return;} |
| 61 | + /** |
| 62 | + * Return the array pf registered scrollbars |
| 63 | + * @return {Scrollbar[]} |
| 64 | + */ |
| 65 | + this.getRegisteredScrollbars = () => { |
| 66 | + return [...scrollbarsRegister]; |
| 67 | + }; |
| 68 | + /** |
| 69 | + * Add the scrollbar to list to iterate each loop |
| 70 | + * @param {Scrollbar} scrollbar |
| 71 | + * @return {LoopControllerClass} |
| 72 | + */ |
| 73 | + this.registerScrollbar = (scrollbar) => { |
| 74 | + if (scrollbarsRegister.indexOf(scrollbar) === -1) { |
| 75 | + scrollbarsRegister.push(scrollbar); |
50 | 76 |
|
51 | | - for (let i = 0; i < loopRegister.length; i++) { |
52 | | - loopRegister[i].update(); |
| 77 | + this.start(); |
53 | 78 | } |
54 | 79 |
|
55 | | - animationFrame = requestAnimationFrame(this.rafStep); |
| 80 | + return this; |
56 | 81 | }; |
| 82 | + /** |
| 83 | + * Remove the scrollbar from list to iterate each loop |
| 84 | + * @param {Scrollbar} scrollbar |
| 85 | + * @return {LoopControllerClass} |
| 86 | + */ |
| 87 | + this.unregisterScrollbar = (scrollbar) => { |
| 88 | + const index = scrollbarsRegister.indexOf(scrollbar); |
57 | 89 |
|
58 | | - stop() { |
59 | | - if (loopIsActive) { |
60 | | - loopIsActive = false; |
61 | | - |
62 | | - animationFrame && cancelAnimationFrame(animationFrame); |
| 90 | + if (index !== -1) { |
| 91 | + scrollbarsRegister.splice(index, 1); |
63 | 92 | } |
64 | 93 |
|
65 | 94 | return this; |
66 | 95 | }; |
67 | 96 | } |
68 | 97 |
|
69 | | -const instance = new LoopController(); |
| 98 | +export const LoopController = new LoopControllerClass(); |
| 99 | +export default LoopController; |
70 | 100 |
|
71 | | -export default instance; |
| 101 | +/** |
| 102 | + * Return new instance of LoopControllerClass |
| 103 | + * @return {LoopControllerClass} |
| 104 | + */ |
| 105 | +export function createLoopController() { |
| 106 | + return new LoopControllerClass(); |
| 107 | +} |
0 commit comments