mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Merge branch 'issue-7'
This commit is contained in:
Vendored
+34
-20
@@ -6864,30 +6864,44 @@ define('events',[],function() {
|
||||
subscription.callback.call(null, data);
|
||||
}
|
||||
}
|
||||
|
||||
function subscribe(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";
|
||||
}
|
||||
}
|
||||
|
||||
getEvent(event).subscribers.push(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";
|
||||
}
|
||||
}
|
||||
|
||||
getEvent(event).subscribers.push(subscription);
|
||||
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);
|
||||
|
||||
Vendored
+1
-1
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
+52
-18
@@ -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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
getEvent(event).subscribers.push(subscription);
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
+2
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user