Remove debounce

This commit is contained in:
Ben Thomson 2022-01-20 13:18:44 +08:00
parent 17f77fd673
commit 104b567179
No known key found for this signature in database
GPG Key ID: E2B9C73B52D15AA0
5 changed files with 38 additions and 24 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -61,6 +61,12 @@ export default class Transition extends Snowboard.PluginBase {
this.doTransition();
}
/**
* Maps event classes to the given transition state.
*
* @param {...any} args
* @returns {Array}
*/
eventClasses(...args) {
const eventClasses = {
in: `${this.transition}-in`,
@ -84,6 +90,11 @@ export default class Transition extends Snowboard.PluginBase {
return returnClasses;
}
/**
* Executes the transition.
*
* @returns {void}
*/
doTransition() {
// Add duration override
if (this.duration !== null) {
@ -120,6 +131,13 @@ export default class Transition extends Snowboard.PluginBase {
});
}
/**
* Callback function when the transition ends.
*
* When a transition ends, the instance of the transition is automatically destructed.
*
* @returns {void}
*/
onTransitionEnd() {
this.eventClasses('active', (!this.trailTo) ? 'out' : '').forEach((eventClass) => {
this.element.classList.remove(eventClass);
@ -137,6 +155,11 @@ export default class Transition extends Snowboard.PluginBase {
this.destructor();
}
/**
* Cancels a transition.
*
* @returns {void}
*/
cancel() {
this.element.removeEventListener('transitionend', () => this.onTransitionEnd, {
once: true,
@ -152,12 +175,23 @@ export default class Transition extends Snowboard.PluginBase {
this.destructor();
}
/**
* Resets the classes, removing any transition classes.
*
* @returns {void}
*/
resetClasses() {
this.eventClasses().forEach((eventClass) => {
this.element.classList.remove(eventClass);
});
}
/**
* Parses a given duration and converts it to a "ms" value.
*
* @param {String} duration
* @returns {String}
*/
parseDuration(duration) {
const parsed = /^([0-9]+(\.[0-9]+)?)(m?s)?$/.exec(duration);
const amount = Number(parsed[1]);

View File

@ -3,7 +3,6 @@ import Singleton from '../abstracts/Singleton';
import PluginLoader from './PluginLoader';
import Cookie from '../utilities/Cookie';
import Debounce from '../utilities/Debounce';
import JsonParser from '../utilities/JsonParser';
import Sanitizer from '../utilities/Sanitizer';
@ -44,7 +43,6 @@ export default class Snowboard {
loadUtilities() {
this.addPlugin('cookie', Cookie);
this.addPlugin('debounce', Debounce);
this.addPlugin('jsonParser', JsonParser);
this.addPlugin('sanitizer', Sanitizer);
}

View File

@ -1,18 +0,0 @@
export default (fn) => {
// This holds the requestAnimationFrame reference, so we can cancel it if we wish
let frame;
// The debounce function returns a new function that can receive a variable number of arguments
return (...params) => {
// If the frame variable has been defined, clear it now, and queue for next frame
if (frame) {
cancelAnimationFrame(frame);
}
// Queue our function call for the next frame
frame = requestAnimationFrame(() => {
// Call our function and pass any params we received
fn(...params);
});
};
};