Create simple debounce and throttle instead of _.

This commit is contained in:
Hongarc 2018-12-05 12:08:41 +07:00
parent ae0030daa7
commit e6d7edd130
7 changed files with 43 additions and 8 deletions

17
src/js/util/debounce.js Normal file
View file

@ -0,0 +1,17 @@
module.exports = function(func, time, immediate) {
var timeout;
return function() {
var later = function() {
timeout = null;
if (!immediate) {
func.apply(this, arguments);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, time);
if (callNow) {
func.apply(this, arguments);
}
};
};

13
src/js/util/throttle.js Normal file
View file

@ -0,0 +1,13 @@
module.exports = function(func, time) {
var wait = false;
return function() {
if (!wait) {
func.apply(this, arguments);
wait = true;
setTimeout(function() {
wait = false;
}, time);
}
};
};

View file

@ -2,6 +2,7 @@ var _ = require('underscore');
var Q = require('q');
var Views = require('../views');
var throttle = require('../util/throttle');
var ModalTerminal = Views.ModalTerminal;
var ContainedBase = Views.ContainedBase;
@ -97,7 +98,7 @@ var MarkdownGrabber = ContainedBase.extend({
keyup: function() {
if (!this.throttledPreview) {
this.throttledPreview = _.throttle(
this.throttledPreview = throttle(
this.updatePreview.bind(this),
500
);

View file

@ -7,6 +7,8 @@ var intl = require('../intl');
var log = require('../log');
var Constants = require('../util/constants');
var KeyboardListener = require('../util/keyboard').KeyboardListener;
var debounce = require('../util/debounce');
var throttle = require('../util/throttle');
var BaseView = Backbone.View.extend({
getDestination: function() {
@ -106,7 +108,7 @@ var GeneralButton = ContainedBase.extend({
click: function() {
if (!this.clickFunc) {
this.clickFunc = _.throttle(
this.clickFunc = throttle(
this.sendClick.bind(this),
500
);
@ -580,7 +582,7 @@ var CanvasTerminalHolder = BaseView.extend({
// If the entire window gets resized such that the terminal is outside the view, then
// move it back into the view, and expand/shrink it vertically as necessary.
$(window).on('resize', _.debounce(this.recalcLayout.bind(this), 300));
$(window).on('resize', debounce(this.recalcLayout.bind(this), 300));
if (options.additionalClass) {
this.$el.addClass(options.additionalClass);

View file

@ -4,6 +4,7 @@ var Backbone = require('backbone');
var LocaleStore = require('../stores/LocaleStore');
var util = require('../util');
var debounce = require('../util/debounce');
var intl = require('../intl');
var log = require('../log');
var KeyboardListener = require('../util/keyboard').KeyboardListener;
@ -41,7 +42,7 @@ var LevelDropdownView = ContainedBase.extend({
};
this.navEvents = Object.assign({}, Backbone.Events);
this.navEvents.on('clickedID', _.debounce(
this.navEvents.on('clickedID', debounce(
this.loadLevelID.bind(this),
300,
true

View file

@ -1,4 +1,3 @@
var _ = require('underscore');
var Q = require('q');
var Backbone = require('backbone');
@ -10,6 +9,7 @@ var BuilderViews = require('../views/builderViews');
var MarkdownPresenter = BuilderViews.MarkdownPresenter;
var KeyboardListener = require('../util/keyboard').KeyboardListener;
var debounce = require('../util/debounce');
var MultiView = Backbone.View.extend({
tagName: 'div',
@ -84,13 +84,13 @@ var MultiView = Backbone.View.extend({
},
getPosFunc: function() {
return _.debounce(function() {
return debounce(function() {
this.navForward();
}.bind(this), this.navEventDebounce, true);
},
getNegFunc: function() {
return _.debounce(function() {
return debounce(function() {
this.navBackward();
}.bind(this), this.navEventDebounce, true);
},

View file

@ -3,6 +3,7 @@ var Q = require('q');
var intl = require('../intl');
var GRAPHICS = require('../util/constants').GRAPHICS;
var debounce = require('../util/debounce');
var GlobalStateStore = require('../stores/GlobalStateStore');
var VisNode = require('../visuals/visNode').VisNode;
@ -795,7 +796,7 @@ GitVisuals.prototype.canvasResize = function(width, height) {
};
GitVisuals.prototype.genResizeFunc = function() {
this.resizeFunc = _.debounce(
this.resizeFunc = debounce(
function(width, height) {
this.refreshTree();
}.bind(this),