mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-29 09:20:03 +02:00
event baton now working
This commit is contained in:
parent
17ac79d052
commit
0779bdeadb
6 changed files with 230 additions and 21 deletions
|
@ -5,8 +5,9 @@ var Backbone = require('backbone');
|
|||
* Globals
|
||||
*/
|
||||
var events = _.clone(Backbone.Events);
|
||||
var ui = null;
|
||||
var mainVis = null;
|
||||
var ui;
|
||||
var mainVis;
|
||||
var eventBaton;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
@ -17,6 +18,8 @@ var init = function(){
|
|||
mainVis = new Visualization({
|
||||
el: $('#canvasWrapper')[0]
|
||||
});
|
||||
var EventBaton = require('../util/eventBaton').EventBaton;
|
||||
eventBaton = new EventBaton();
|
||||
|
||||
if (/\?demo/.test(window.location.href)) {
|
||||
setTimeout(function() {
|
||||
|
@ -78,5 +81,9 @@ exports.getMainVis = function() {
|
|||
return mainVis;
|
||||
};
|
||||
|
||||
exports.getEventBaton = function() {
|
||||
return eventBaton;
|
||||
};
|
||||
|
||||
exports.init = init;
|
||||
|
||||
|
|
|
@ -36,7 +36,6 @@ InputWaterfall.prototype.mute = function() {
|
|||
};
|
||||
|
||||
InputWaterfall.prototype.process = function(command, callback) {
|
||||
console.log('processing', command.get('rawStr'));
|
||||
|
||||
if (this.checkDisabledMap(command)) {
|
||||
callback();
|
||||
|
|
|
@ -43,14 +43,11 @@ ParseWaterfall.prototype.processAllInstants = function(commandStr) {
|
|||
};
|
||||
|
||||
ParseWaterfall.prototype.processInstant = function(commandStr, instantCommands) {
|
||||
console.log('processing', commandStr, 'with', instantCommands);
|
||||
_.each(instantCommands, function(tuple) {
|
||||
var regex = tuple[0];
|
||||
console.log('the regex', regex);
|
||||
var results = regex.exec(commandStr);
|
||||
if (results) {
|
||||
console.log('results', results);
|
||||
// this will throw a result
|
||||
// this will throw a result because it's an instant
|
||||
tuple[1](results);
|
||||
}
|
||||
});
|
||||
|
@ -69,3 +66,4 @@ ParseWaterfall.prototype.parseAll = function(commandStr) {
|
|||
};
|
||||
|
||||
exports.ParseWaterfall = ParseWaterfall;
|
||||
|
||||
|
|
63
src/js/util/eventBaton.js
Normal file
63
src/js/util/eventBaton.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
var _ = require('underscore');
|
||||
|
||||
function EventBaton() {
|
||||
this.eventMap = {};
|
||||
}
|
||||
|
||||
// this method steals the "baton" -- aka, only this method will now
|
||||
// get called. analogous to events.on
|
||||
// EventBaton.prototype.on = function(name, func, context) {
|
||||
EventBaton.prototype.stealBaton = function(name, func, context) {
|
||||
if (!name) { throw new Error('need name'); }
|
||||
|
||||
var listeners = this.eventMap[name] || [];
|
||||
listeners.push({
|
||||
func: func,
|
||||
context: context
|
||||
});
|
||||
this.eventMap[name] = listeners;
|
||||
};
|
||||
|
||||
EventBaton.prototype.trigger = function(name) {
|
||||
var argsToApply = [];
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
argsToApply.push(arguments[i]);
|
||||
}
|
||||
|
||||
// get the last one
|
||||
var listeners = this.eventMap[name];
|
||||
if (!listeners) {
|
||||
console.warn('no listeners for that event', name);
|
||||
return;
|
||||
}
|
||||
// call the top most listener with context and such
|
||||
var toCall = listeners.slice(-1)[0];
|
||||
toCall.func.apply(toCall.context, argsToApply);
|
||||
};
|
||||
|
||||
EventBaton.prototype.releaseBaton = function(name, func, context) {
|
||||
if (!name) { throw new Error('need name'); }
|
||||
// might be in the middle of the stack
|
||||
var listeners = this.eventMap[name];
|
||||
if (!listeners || !listeners.length) {
|
||||
throw new Error('no one has that baton!' + name);
|
||||
}
|
||||
|
||||
var newListeners = [];
|
||||
var found = false;
|
||||
_.each(listeners, function(listenerObj) {
|
||||
if (listenerObj.func === func) {
|
||||
found = true;
|
||||
} else {
|
||||
newListeners.push(listenerObj);
|
||||
}
|
||||
}, this);
|
||||
|
||||
if (!found) {
|
||||
throw new Error('did not find that function');
|
||||
}
|
||||
this.eventMap[name] = newListeners;
|
||||
};
|
||||
|
||||
exports.EventBaton = EventBaton;
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue