mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-07-16 09:34:28 +02:00
BIG move towards backbone models and collections for everything
This commit is contained in:
parent
95277013e5
commit
5ac6b035f5
9 changed files with 290 additions and 234 deletions
|
@ -3,3 +3,75 @@ var CommitCollection = Backbone.Collection.extend({
|
|||
});
|
||||
|
||||
var commitCollection = new CommitCollection();
|
||||
|
||||
var CommandCollection = Backbone.Collection.extend({
|
||||
model: Command
|
||||
});
|
||||
|
||||
var CommandBuffer = Backbone.Model.extend({
|
||||
initialize: function() {
|
||||
events.on('gitCommandReady', _.bind(
|
||||
this.addCommand, this
|
||||
));
|
||||
|
||||
this.collection = new CommandCollection();
|
||||
this.buffer = [];
|
||||
this.timeout = null;
|
||||
this.delay = 300;
|
||||
},
|
||||
|
||||
addCommand: function(command) {
|
||||
this.collection.add(command);
|
||||
this.buffer.push(command);
|
||||
this.touchBuffer();
|
||||
},
|
||||
|
||||
touchBuffer: function() {
|
||||
// touch buffer just essentially means we just check if our buffer is being
|
||||
// processed. if it's not, we immediately process the first item
|
||||
// and then set the timeout.
|
||||
if (this.timeout) {
|
||||
// timeout existence implies its being processed
|
||||
return;
|
||||
}
|
||||
|
||||
// process first element now
|
||||
this.popAndProcess();
|
||||
// always set the timeout, regardless of buffer size
|
||||
this.setTimeout();
|
||||
},
|
||||
|
||||
|
||||
setTimeout: function() {
|
||||
this.timeout = setTimeout(_.bind(function() {
|
||||
this.sipFromBuffer();
|
||||
}, this), 300);
|
||||
},
|
||||
|
||||
popAndProcess: function() {
|
||||
var popped = this.buffer.pop();
|
||||
events.trigger('processCommand', popped);
|
||||
},
|
||||
|
||||
clear: function() {
|
||||
clearTimeout(this.timeout);
|
||||
this.timeout = null;
|
||||
},
|
||||
|
||||
sipFromBuffer: function() {
|
||||
if (!this.buffer.length) {
|
||||
this.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
this.popAndProcess();
|
||||
if (this.buffer.length) {
|
||||
this.setTimeout();
|
||||
} else {
|
||||
this.clear();
|
||||
}
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
var commandBuffer = new CommandBuffer();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue