Merge branch 'issue-7'

pull/14/head
Christoph Oberhofer 11 years ago
commit 93d92f2a6f

54
dist/quagga.js vendored

@ -6864,30 +6864,44 @@ define('events',[],function() {
subscription.callback.call(null, data);
}
}
function subscribe(event, callback, async) {
var subscription;
return {
subscribe : function(event, callback, async) {
var subscription;
if ( typeof callback === "function") {
subscription = {
callback : callback,
async : async
};
} else {
subscription = callback;
if (!subscription.callback) {
throw "Callback was not specified on options";
}
if ( typeof callback === "function") {
subscription = {
callback : callback,
async : async
};
} else {
subscription = callback;
if (!subscription.callback) {
throw "Callback was not specified on options";
}
}
getEvent(event).subscribers.push(subscription);
getEvent(event).subscribers.push(subscription);
}
return {
subscribe : function(event, callback, async) {
return subscribe(event, callback, async);
},
publish : function(eventName, data) {
var subscribers = getEvent(eventName).subscribers, i;
for ( i = 0; i < subscribers.length; i++) {
publishSubscription(subscribers[i], data);
}
var event = getEvent(eventName),
subscribers = event.subscribers;
event.subscribers = subscribers.filter(function(subscriber) {
publishSubscription(subscriber, data);
return !subscriber.once;
});
},
once: function(event, callback, async) {
subscribe(event, {
callback: callback,
async: async,
once: true
});
}
};
}();
@ -7212,7 +7226,7 @@ function(Code128Reader, EANReader, InputStream, ImageWrapper, BarcodeLocator, Ba
size: 800
};
config.readyFunc = function() {
Events.subscribe("detected", function(result) {
Events.once("detected", function(result) {
_stopped = true;
resultCallback.call(null, result);
}, true);

File diff suppressed because one or more lines are too long

@ -0,0 +1,114 @@
define(['events'], function(Events){
beforeEach(function() {
Events.unsubscribe();
});
describe("subscribe", function() {
it("should call one callback for a single event", function() {
var callbackA = sinon.stub(),
callbackB = sinon.stub();
Events.subscribe("test", callbackA);
Events.subscribe("test", callbackB);
Events.publish("test");
expect(callbackA.calledOnce).to.be.equal(true);
expect(callbackB.calledOnce).to.be.equal(true);
});
it("should call one callback twice if published twice", function() {
var callback = sinon.stub();
Events.subscribe("test", callback);
Events.publish("test");
Events.publish("test");
expect(callback.calledTwice).to.be.equal(true);
});
it("should call the callback asynchronuously", function(done) {
var test = {
callback: function() {
}
};
sinon.stub(test, "callback", function() {
expect(test.callback.calledOnce).to.be.true;
done();
});
Events.subscribe("test", test.callback, true);
Events.publish("test");
expect(test.callback.called).to.be.false;
});
});
describe("once", function() {
it("should call the callback once, even when published twice", function() {
var callbackA = sinon.stub(),
callbackB = sinon.stub();
Events.once("test", callbackA);
Events.subscribe("test", callbackB);
Events.publish("test");
Events.publish("test");
expect(callbackA.calledOnce).to.be.equal(true);
expect(callbackB.calledTwice).to.be.equal(true);
});
});
describe("unsubscribe", function() {
it("should unsubscribe all callbacks from a single event", function() {
var callbackA = sinon.stub(),
callbackB = sinon.stub(),
callbackC = sinon.stub();
Events.subscribe("test", callbackA);
Events.subscribe("test", callbackB);
Events.subscribe("testC", callbackC);
Events.publish("test");
expect(callbackC.called).to.be.equal(false);
expect(callbackA.calledOnce).to.be.equal(true);
expect(callbackB.calledOnce).to.be.equal(true);
Events.publish("testC");
expect(callbackC.calledOnce).to.be.equal(true);
expect(callbackA.calledOnce).to.be.equal(true);
expect(callbackB.calledOnce).to.be.equal(true);
Events.unsubscribe("test");
Events.publish("test");
expect(callbackC.calledOnce).to.be.equal(true);
expect(callbackA.calledOnce).to.be.equal(true);
expect(callbackB.calledOnce).to.be.equal(true);
});
it("should unsubscribe a single callback from a single event", function() {
var callbackA = sinon.stub(),
callbackB = sinon.stub();
Events.subscribe("test", callbackA);
Events.subscribe("test", callbackB);
Events.publish("test");
expect(callbackA.calledOnce).to.be.equal(true);
expect(callbackB.calledOnce).to.be.equal(true);
Events.unsubscribe("test", callbackB);
Events.publish("test");
expect(callbackA.calledTwice).to.be.equal(true);
expect(callbackB.calledOnce).to.be.equal(true);
});
});
});

@ -15,6 +15,10 @@ define(function() {
}
return events[eventName];
}
function clearEvents(){
events = {};
}
function publishSubscription(subscription, data) {
if (subscription.async) {
@ -25,29 +29,59 @@ define(function() {
subscription.callback.call(null, data);
}
}
function subscribe(event, callback, async) {
var subscription;
return {
subscribe : function(event, callback, async) {
var subscription;
if ( typeof callback === "function") {
subscription = {
callback : callback,
async : async
};
} else {
subscription = callback;
if (!subscription.callback) {
throw "Callback was not specified on options";
}
if ( typeof callback === "function") {
subscription = {
callback : callback,
async : async
};
} else {
subscription = callback;
if (!subscription.callback) {
throw "Callback was not specified on options";
}
}
getEvent(event).subscribers.push(subscription);
getEvent(event).subscribers.push(subscription);
}
return {
subscribe : function(event, callback, async) {
return subscribe(event, callback, async);
},
publish : function(eventName, data) {
var subscribers = getEvent(eventName).subscribers, i;
for ( i = 0; i < subscribers.length; i++) {
publishSubscription(subscribers[i], data);
var event = getEvent(eventName),
subscribers = event.subscribers;
event.subscribers = subscribers.filter(function(subscriber) {
publishSubscription(subscriber, data);
return !subscriber.once;
});
},
once: function(event, callback, async) {
subscribe(event, {
callback: callback,
async: async,
once: true
});
},
unsubscribe: function(eventName, callback) {
var event;
if (eventName) {
event = getEvent(eventName);
if (event && callback) {
event.subscribers = event.subscribers.filter(function(subscriber){
return subscriber.callback !== callback;
});
} else {
event.subscribers = [];
}
} else {
clearEvents();
}
}
};

@ -213,7 +213,7 @@ function(Code128Reader, EANReader, InputStream, ImageWrapper, BarcodeLocator, Ba
size: 800
};
config.readyFunc = function() {
Events.subscribe("detected", function(result) {
Events.once("detected", function(result) {
_stopped = true;
resultCallback.call(null, result);
}, true);

@ -20,7 +20,8 @@ require.config({
'typedefs': 'src/typedefs',
'glMatrixAddon': 'src/glMatrixAddon',
'cluster': 'src/cluster',
'camera_access': 'src/camera_access'
'camera_access': 'src/camera_access',
'events': 'src/events'
},
deps: allTestFiles,
callback: window.__karma__.start

Loading…
Cancel
Save