From 50784f260082677d31a4a7f48ee47a1bfddc5a4e Mon Sep 17 00:00:00 2001 From: toby Date: Sun, 23 Apr 2017 13:41:28 -0400 Subject: [PATCH] Debounce autobake in the web app. Added debounce with guidance from the underscore.js implementation: https://github.com/jashkenas/underscore/blob/e944e0275abb3e1f366417ba8facb5754a7ad273/underscore.js#L880 --- src/core/Utils.js | 27 +++++++++++++++++++++++++++ src/web/App.js | 2 ++ 2 files changed, 29 insertions(+) diff --git a/src/core/Utils.js b/src/core/Utils.js index 2e4b354..a49565e 100755 --- a/src/core/Utils.js +++ b/src/core/Utils.js @@ -1185,6 +1185,33 @@ const Utils = { "Latin1": CryptoJS.enc.Latin1, }, + + /** + * A utility for "debouncing" functions. + * Debouncing is when you want to ensure events triggered by an event are rate-limited. + * @constant + */ + debounce(fn, delay) { + let timeout; + + return function() { + /** + * later calls the debounced function with arguments. + * If the debounced function is called again, then the timeout + * which calls later is cancelled. + */ + let later = () => { + fn.apply(this, arguments); + }; + + if (timeout) { + clearTimeout(timeout); + } + + timeout = setTimeout(later, delay); + }; + }, + }; export default Utils; diff --git a/src/web/App.js b/src/web/App.js index b4fb3a7..de8af4a 100755 --- a/src/web/App.js +++ b/src/web/App.js @@ -36,6 +36,8 @@ var App = function(categories, operations, defaultFavourites, defaultOptions) { this.ingId = 0; window.chef = this.chef; + + this.autoBake = Utils.debounce(this.autoBake, 300); };