mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-07-05 12:14:28 +02:00
Create simple debounce and throttle instead of _.
This commit is contained in:
parent
ae0030daa7
commit
e6d7edd130
7 changed files with 43 additions and 8 deletions
17
src/js/util/debounce.js
Normal file
17
src/js/util/debounce.js
Normal 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
13
src/js/util/throttle.js
Normal 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
|
@ -2,6 +2,7 @@ var _ = require('underscore');
|
||||||
var Q = require('q');
|
var Q = require('q');
|
||||||
|
|
||||||
var Views = require('../views');
|
var Views = require('../views');
|
||||||
|
var throttle = require('../util/throttle');
|
||||||
var ModalTerminal = Views.ModalTerminal;
|
var ModalTerminal = Views.ModalTerminal;
|
||||||
var ContainedBase = Views.ContainedBase;
|
var ContainedBase = Views.ContainedBase;
|
||||||
|
|
||||||
|
@ -97,7 +98,7 @@ var MarkdownGrabber = ContainedBase.extend({
|
||||||
|
|
||||||
keyup: function() {
|
keyup: function() {
|
||||||
if (!this.throttledPreview) {
|
if (!this.throttledPreview) {
|
||||||
this.throttledPreview = _.throttle(
|
this.throttledPreview = throttle(
|
||||||
this.updatePreview.bind(this),
|
this.updatePreview.bind(this),
|
||||||
500
|
500
|
||||||
);
|
);
|
||||||
|
|
|
@ -7,6 +7,8 @@ var intl = require('../intl');
|
||||||
var log = require('../log');
|
var log = require('../log');
|
||||||
var Constants = require('../util/constants');
|
var Constants = require('../util/constants');
|
||||||
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
||||||
|
var debounce = require('../util/debounce');
|
||||||
|
var throttle = require('../util/throttle');
|
||||||
|
|
||||||
var BaseView = Backbone.View.extend({
|
var BaseView = Backbone.View.extend({
|
||||||
getDestination: function() {
|
getDestination: function() {
|
||||||
|
@ -106,7 +108,7 @@ var GeneralButton = ContainedBase.extend({
|
||||||
|
|
||||||
click: function() {
|
click: function() {
|
||||||
if (!this.clickFunc) {
|
if (!this.clickFunc) {
|
||||||
this.clickFunc = _.throttle(
|
this.clickFunc = throttle(
|
||||||
this.sendClick.bind(this),
|
this.sendClick.bind(this),
|
||||||
500
|
500
|
||||||
);
|
);
|
||||||
|
@ -580,7 +582,7 @@ var CanvasTerminalHolder = BaseView.extend({
|
||||||
|
|
||||||
// If the entire window gets resized such that the terminal is outside the view, then
|
// 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.
|
// 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) {
|
if (options.additionalClass) {
|
||||||
this.$el.addClass(options.additionalClass);
|
this.$el.addClass(options.additionalClass);
|
||||||
|
|
|
@ -4,6 +4,7 @@ var Backbone = require('backbone');
|
||||||
var LocaleStore = require('../stores/LocaleStore');
|
var LocaleStore = require('../stores/LocaleStore');
|
||||||
|
|
||||||
var util = require('../util');
|
var util = require('../util');
|
||||||
|
var debounce = require('../util/debounce');
|
||||||
var intl = require('../intl');
|
var intl = require('../intl');
|
||||||
var log = require('../log');
|
var log = require('../log');
|
||||||
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
||||||
|
@ -41,7 +42,7 @@ var LevelDropdownView = ContainedBase.extend({
|
||||||
};
|
};
|
||||||
|
|
||||||
this.navEvents = Object.assign({}, Backbone.Events);
|
this.navEvents = Object.assign({}, Backbone.Events);
|
||||||
this.navEvents.on('clickedID', _.debounce(
|
this.navEvents.on('clickedID', debounce(
|
||||||
this.loadLevelID.bind(this),
|
this.loadLevelID.bind(this),
|
||||||
300,
|
300,
|
||||||
true
|
true
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
var _ = require('underscore');
|
|
||||||
var Q = require('q');
|
var Q = require('q');
|
||||||
var Backbone = require('backbone');
|
var Backbone = require('backbone');
|
||||||
|
|
||||||
|
@ -10,6 +9,7 @@ var BuilderViews = require('../views/builderViews');
|
||||||
var MarkdownPresenter = BuilderViews.MarkdownPresenter;
|
var MarkdownPresenter = BuilderViews.MarkdownPresenter;
|
||||||
|
|
||||||
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
||||||
|
var debounce = require('../util/debounce');
|
||||||
|
|
||||||
var MultiView = Backbone.View.extend({
|
var MultiView = Backbone.View.extend({
|
||||||
tagName: 'div',
|
tagName: 'div',
|
||||||
|
@ -84,13 +84,13 @@ var MultiView = Backbone.View.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
getPosFunc: function() {
|
getPosFunc: function() {
|
||||||
return _.debounce(function() {
|
return debounce(function() {
|
||||||
this.navForward();
|
this.navForward();
|
||||||
}.bind(this), this.navEventDebounce, true);
|
}.bind(this), this.navEventDebounce, true);
|
||||||
},
|
},
|
||||||
|
|
||||||
getNegFunc: function() {
|
getNegFunc: function() {
|
||||||
return _.debounce(function() {
|
return debounce(function() {
|
||||||
this.navBackward();
|
this.navBackward();
|
||||||
}.bind(this), this.navEventDebounce, true);
|
}.bind(this), this.navEventDebounce, true);
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,6 +3,7 @@ var Q = require('q');
|
||||||
|
|
||||||
var intl = require('../intl');
|
var intl = require('../intl');
|
||||||
var GRAPHICS = require('../util/constants').GRAPHICS;
|
var GRAPHICS = require('../util/constants').GRAPHICS;
|
||||||
|
var debounce = require('../util/debounce');
|
||||||
var GlobalStateStore = require('../stores/GlobalStateStore');
|
var GlobalStateStore = require('../stores/GlobalStateStore');
|
||||||
|
|
||||||
var VisNode = require('../visuals/visNode').VisNode;
|
var VisNode = require('../visuals/visNode').VisNode;
|
||||||
|
@ -795,7 +796,7 @@ GitVisuals.prototype.canvasResize = function(width, height) {
|
||||||
};
|
};
|
||||||
|
|
||||||
GitVisuals.prototype.genResizeFunc = function() {
|
GitVisuals.prototype.genResizeFunc = function() {
|
||||||
this.resizeFunc = _.debounce(
|
this.resizeFunc = debounce(
|
||||||
function(width, height) {
|
function(width, height) {
|
||||||
this.refreshTree();
|
this.refreshTree();
|
||||||
}.bind(this),
|
}.bind(this),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue