Merge pull request #519 from Hongarc/underscore

Replace function of `underscore` with simple function
This commit is contained in:
Peter Cottle 2018-12-06 11:52:40 -08:00 committed by GitHub
commit 8b71c1271e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 18 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

@ -1,8 +1,8 @@
var _ = require('underscore');
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;
@ -82,7 +82,7 @@ GitVisuals.prototype.resetAll = function() {
visTag.remove();
}, this);
_.each(this.visNodeMap, function(visNode) {
Object.values(this.visNodeMap).forEach(function(visNode) {
visNode.remove();
}, this);
@ -206,7 +206,7 @@ GitVisuals.prototype.animateAllAttrKeys = function(keys, attr, speed, easing) {
this.visBranchCollection.each(animate);
this.visEdgeCollection.each(animate);
this.visTagCollection.each(animate);
_.each(this.visNodeMap, animate);
Object.values(this.visNodeMap).forEach(animate);
var time = (speed !== undefined) ? speed : GRAPHICS.defaultAnimationTime;
setTimeout(function() {
@ -321,7 +321,7 @@ GitVisuals.prototype.finishAnimation = function(speed) {
GitVisuals.prototype.explodeNodes = function(speed) {
var deferred = Q.defer();
var funcs = [];
_.each(this.visNodeMap, function(visNode) {
Object.values(this.visNodeMap).forEach(function(visNode) {
funcs.push(visNode.getExplodeStepFunc(speed));
});
@ -368,7 +368,7 @@ GitVisuals.prototype.animateAllFromAttrToAttr = function(fromSnapshot, toSnapsho
this.visBranchCollection.each(animate);
this.visEdgeCollection.each(animate);
this.visTagCollection.each(animate);
_.each(this.visNodeMap, animate);
Object.values(this.visNodeMap).forEach(animate);
};
/***************************************
@ -392,7 +392,7 @@ GitVisuals.prototype.genSnapshot = function() {
this.fullCalc();
var snapshot = {};
_.each(this.visNodeMap, function(visNode) {
Object.values(this.visNodeMap).forEach(function(visNode) {
snapshot[visNode.get('id')] = visNode.getAttributes();
}, this);
@ -632,7 +632,7 @@ GitVisuals.prototype.calcDepth = function() {
}
var depthIncrement = this.getDepthIncrement(maxDepth);
_.each(this.visNodeMap, function(visNode) {
Object.values(this.visNodeMap).forEach(function(visNode) {
visNode.setDepthBasedOn(depthIncrement, this.getHeaderOffset());
}, this);
};
@ -655,7 +655,7 @@ GitVisuals.prototype.calcDepth = function() {
**************************************/
GitVisuals.prototype.animateNodePositions = function(speed) {
_.each(this.visNodeMap, function(visNode) {
Object.values(this.visNodeMap).forEach(function(visNode) {
visNode.animateUpdatedPosition(speed);
}, this);
};
@ -795,7 +795,7 @@ GitVisuals.prototype.canvasResize = function(width, height) {
};
GitVisuals.prototype.genResizeFunc = function() {
this.resizeFunc = _.debounce(
this.resizeFunc = debounce(
function(width, height) {
this.refreshTree();
}.bind(this),
@ -853,7 +853,7 @@ GitVisuals.prototype.zIndexReflow = function() {
};
GitVisuals.prototype.visNodesFront = function() {
_.each(this.visNodeMap, function(visNode) {
Object.values(this.visNodeMap).forEach(function(visNode) {
visNode.toFront();
});
};
@ -892,7 +892,7 @@ GitVisuals.prototype.drawTreeFirstTime = function() {
this.gitReady = true;
this.calcTreeCoords();
_.each(this.visNodeMap, function(visNode) {
Object.values(this.visNodeMap).forEach(function(visNode) {
visNode.genGraphics(this.paper);
}, this);