-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
98 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,107 @@ | ||
let loopIsActive = false; | ||
let animationFrame = null; | ||
const loopRegister = []; | ||
|
||
class LoopController | ||
{ | ||
constructor() { | ||
this.rafStep = this.rafStep.bind(this); | ||
} | ||
|
||
getRegisteredItems() { | ||
return [...loopRegister]; | ||
} | ||
|
||
registerScrollbar(scrollbar) { | ||
if (!loopRegister.includes(scrollbar)) { | ||
loopRegister.push(scrollbar); | ||
|
||
this.start(); | ||
function LoopControllerClass() { | ||
/** | ||
* @typedef {Object} Scrollbar | ||
* @property {function} update | ||
*/ | ||
|
||
/** | ||
* @type {Scrollbar[]} | ||
*/ | ||
const scrollbarsRegister = []; | ||
|
||
/** | ||
* true if loop is active | ||
* @type {boolean} | ||
*/ | ||
let isActive = false; | ||
/** | ||
* ID of requested animation frame | ||
* @type {null|number} | ||
*/ | ||
let animationFrameId = null; | ||
|
||
/** | ||
* Function that called in animation frame | ||
*/ | ||
const animationFrameCallback = () => { | ||
if (!isActive) {return;} | ||
|
||
for (let scrollbar of scrollbarsRegister) { | ||
scrollbar.update(); | ||
} | ||
|
||
return this; | ||
requestAnimationFrame(animationFrameCallback); | ||
}; | ||
|
||
unregisterScrollbar(scrollbar) { | ||
let index = loopRegister.indexOf(scrollbar); | ||
/** | ||
* Stop the loop if it wasn't active | ||
* @return {LoopControllerClass} | ||
*/ | ||
this.start = () => { | ||
if (isActive) {return this;} | ||
|
||
if (index !== -1) { | ||
loopRegister.length === 1 && this.stop(); | ||
isActive = true; | ||
|
||
loopRegister.splice(index, 1); | ||
} | ||
|
||
return this; | ||
animationFrameId && cancelAnimationFrame(animationFrameId); | ||
requestAnimationFrame(animationFrameCallback); | ||
}; | ||
/** | ||
* Stop the loop if it is active | ||
* @return {LoopControllerClass} | ||
*/ | ||
this.stop = () => { | ||
if (!isActive) {return this;} | ||
|
||
start() { | ||
if (!loopIsActive) { | ||
loopIsActive = true; | ||
isActive = false; | ||
|
||
animationFrame && cancelAnimationFrame(animationFrame); | ||
animationFrame = requestAnimationFrame(this.rafStep); | ||
} | ||
|
||
return this; | ||
animationFrameId && cancelAnimationFrame(animationFrameId); | ||
animationFrameId = null; | ||
}; | ||
|
||
rafStep() { | ||
if (!loopIsActive) {return;} | ||
/** | ||
* Return the array pf registered scrollbars | ||
* @return {Scrollbar[]} | ||
*/ | ||
this.getRegisteredScrollbars = () => { | ||
return [...scrollbarsRegister]; | ||
}; | ||
/** | ||
* Add the scrollbar to list to iterate each loop | ||
* @param {Scrollbar} scrollbar | ||
* @return {LoopControllerClass} | ||
*/ | ||
this.registerScrollbar = (scrollbar) => { | ||
if (scrollbarsRegister.indexOf(scrollbar) === -1) { | ||
scrollbarsRegister.push(scrollbar); | ||
|
||
for (let i = 0; i < loopRegister.length; i++) { | ||
loopRegister[i].update(); | ||
this.start(); | ||
} | ||
|
||
animationFrame = requestAnimationFrame(this.rafStep); | ||
return this; | ||
}; | ||
/** | ||
* Remove the scrollbar from list to iterate each loop | ||
* @param {Scrollbar} scrollbar | ||
* @return {LoopControllerClass} | ||
*/ | ||
this.unregisterScrollbar = (scrollbar) => { | ||
const index = scrollbarsRegister.indexOf(scrollbar); | ||
|
||
stop() { | ||
if (loopIsActive) { | ||
loopIsActive = false; | ||
|
||
animationFrame && cancelAnimationFrame(animationFrame); | ||
if (index !== -1) { | ||
scrollbarsRegister.splice(index, 1); | ||
} | ||
|
||
return this; | ||
}; | ||
} | ||
|
||
const instance = new LoopController(); | ||
export const LoopController = new LoopControllerClass(); | ||
export default LoopController; | ||
|
||
export default instance; | ||
/** | ||
* Return new instance of LoopControllerClass | ||
* @return {LoopControllerClass} | ||
*/ | ||
export function createLoopController() { | ||
return new LoopControllerClass(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters