can-event-queue
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -5,2 +5,3 @@ var QUnit = require('steal-qunit'); | ||
var domEvents = require("can-util/dom/events/events"); | ||
var canSymbol = require("can-symbol"); | ||
@@ -129,1 +130,13 @@ QUnit.module('can-event-queue',{ | ||
} | ||
QUnit.test("empty unbind", function(){ | ||
var obj = eventQueue({}); | ||
obj.addEventListener("first", function(){}); | ||
obj.addEventListener("first", function(){},"notify"); | ||
var handlers = obj[canSymbol.for("can.meta")].handlers; | ||
QUnit.equal(handlers.get(["first"]).length, 2, "2 first handlers"); | ||
obj.removeEventListener("first"); | ||
QUnit.equal(handlers.get(["first"]).length, 0, "first handlers removed"); | ||
}); |
@@ -28,25 +28,25 @@ var canDev = require("can-util/js/dev/dev"); | ||
function getHandlers(obj) { | ||
var meta = ensureMeta(obj); | ||
var meta = ensureMeta(obj); | ||
var handlers = meta.handlers; | ||
if(!handlers) { | ||
// Handlers are organized by: | ||
// event name - the type of event bound to | ||
// binding type - "event" for things that expect an event object (legacy), "onKeyValue" for reflective bindings. | ||
// queue name - mutate, queue, etc | ||
// handlers - the handlers. | ||
handlers = meta.handlers = new KeyTree([Object, Object, Object, Array],{ | ||
onFirst: function(){ | ||
if( obj._eventSetup ) { | ||
obj._eventSetup(); | ||
} | ||
}, | ||
onEmpty: function(){ | ||
if( obj._eventTeardown ) { | ||
obj._eventTeardown(); | ||
} | ||
} | ||
}); | ||
} | ||
return handlers; | ||
var handlers = meta.handlers; | ||
if (!handlers) { | ||
// Handlers are organized by: | ||
// event name - the type of event bound to | ||
// binding type - "event" for things that expect an event object (legacy), "onKeyValue" for reflective bindings. | ||
// queue name - mutate, queue, etc | ||
// handlers - the handlers. | ||
handlers = meta.handlers = new KeyTree([Object, Object, Object, Array], { | ||
onFirst: function() { | ||
if (obj._eventSetup) { | ||
obj._eventSetup(); | ||
} | ||
}, | ||
onEmpty: function() { | ||
if (obj._eventTeardown) { | ||
obj._eventTeardown(); | ||
} | ||
} | ||
}); | ||
} | ||
return handlers; | ||
} | ||
@@ -56,3 +56,3 @@ | ||
var props = { | ||
dispatch: function(event, args){ | ||
dispatch: function(event, args) { | ||
//!steal-remove-start | ||
@@ -66,3 +66,3 @@ if (arguments.length > 2) { | ||
canDev.warn('Arguments to dispatch should be an array.'); | ||
args = [ args ]; | ||
args = [args]; | ||
} | ||
@@ -73,3 +73,3 @@ //!steal-remove-end | ||
if (!this.__inSetup) { | ||
if(typeof event === 'string') { | ||
if (typeof event === 'string') { | ||
event = { | ||
@@ -82,3 +82,3 @@ type: event | ||
if (!event.reasonLog) { | ||
event.reasonLog = [ canReflect.getName(this), "dispatched", '"' + event + '"', "with" ].concat(args); | ||
event.reasonLog = [canReflect.getName(this), "dispatched", '"' + event + '"', "with"].concat(args); | ||
} | ||
@@ -88,3 +88,3 @@ if (!event.makeMeta) { | ||
return { | ||
log: [ canReflect.getName(handler) ] | ||
log: [canReflect.getName(handler)] | ||
}; | ||
@@ -102,8 +102,8 @@ }; | ||
var handlersByType = handlers.getNode([event.type]); | ||
if(handlersByType) { | ||
if (handlersByType) { | ||
queues.batch.start(); | ||
if(handlersByType.onKeyValue) { | ||
if (handlersByType.onKeyValue) { | ||
queues.enqueueByQueue(handlersByType.onKeyValue, this, args, event.makeMeta, event.reasonLog); | ||
} | ||
if(handlersByType.event) { | ||
if (handlersByType.event) { | ||
@@ -119,33 +119,39 @@ event.batchNum = queues.batch.number(); | ||
addEventListener: function(key, handler, queueName) { | ||
getHandlers(this).add([key, "event",queueName || "mutate", handler]); | ||
getHandlers(this).add([key, "event", queueName || "mutate", handler]); | ||
}, | ||
removeEventListener: function(key, handler, queueName) { | ||
getHandlers(this).delete([key, "event", queueName || "mutate", handler]); | ||
} | ||
if (!handler && !queueName) { | ||
getHandlers(this).delete([key, "event"]); | ||
} else if (!handler) { | ||
getHandlers(this).delete([key, "event", queueName || "mutate"]); | ||
} else { | ||
getHandlers(this).delete([key, "event", queueName || "mutate", handler]); | ||
} | ||
} | ||
}; | ||
var onKeyValueSymbol = canSymbol.for("can.onKeyValue"), | ||
offKeyValueSymbol = canSymbol.for("can.offKeyValue"), | ||
onEventSymbol = canSymbol.for("can.onEvent"), | ||
offEventSymbol = canSymbol.for("can.offEvent"), | ||
onValueSymbol = canSymbol.for("can.onValue"), | ||
offValueSymbol = canSymbol.for("can.offValue"); | ||
offKeyValueSymbol = canSymbol.for("can.offKeyValue"), | ||
onEventSymbol = canSymbol.for("can.onEvent"), | ||
offEventSymbol = canSymbol.for("can.offEvent"), | ||
onValueSymbol = canSymbol.for("can.onValue"), | ||
offValueSymbol = canSymbol.for("can.offValue"); | ||
props.on = function(eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if(listenWithDOM) { | ||
if (listenWithDOM) { | ||
var method = typeof handler === "string" ? "addDelegateListener" : "addEventListener"; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if("addEventListener" in this) { | ||
if ("addEventListener" in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if(this[onKeyValueSymbol]) { | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if(this[onEventSymbol]) { | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
} else { | ||
if(!eventName && this[onValueSymbol]) { | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
} else { | ||
throw new Error("can-control: Unable to bind "+eventName); | ||
throw new Error("can-control: Unable to bind " + eventName); | ||
} | ||
@@ -158,3 +164,3 @@ } | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if(listenWithDOM) { | ||
if (listenWithDOM) { | ||
var method = typeof handler === "string" ? "removeDelegateListener" : "removeEventListener"; | ||
@@ -164,13 +170,13 @@ domEvents[method].call(this, eventName, handler, queue); | ||
if("removeEventListener" in this) { | ||
if ("removeEventListener" in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if(this[offKeyValueSymbol]) { | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if(this[offEventSymbol]) { | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
} else { | ||
if(!eventName && this[offValueSymbol]) { | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
} else { | ||
throw new Error("can-control: Unable to unbind "+eventName); | ||
throw new Error("can-control: Unable to unbind " + eventName); | ||
} | ||
@@ -184,8 +190,8 @@ | ||
var symbols = { | ||
"can.onKeyValue": function(key, handler, queueName) { | ||
getHandlers(this).add([key, "onKeyValue",queueName || "mutate", handler]); | ||
}, | ||
"can.offKeyValue": function(key, handler, queueName) { | ||
getHandlers(this).delete([key, "onKeyValue", queueName || "mutate", handler]); | ||
} | ||
"can.onKeyValue": function(key, handler, queueName) { | ||
getHandlers(this).add([key, "onKeyValue", queueName || "mutate", handler]); | ||
}, | ||
"can.offKeyValue": function(key, handler, queueName) { | ||
getHandlers(this).delete([key, "onKeyValue", queueName || "mutate", handler]); | ||
} | ||
}; | ||
@@ -195,49 +201,47 @@ | ||
var eventQueue = function(obj) { | ||
// add properties | ||
assign(obj, props); | ||
// add symbols | ||
canReflect.assignSymbols(obj, symbols); | ||
return obj; | ||
// add properties | ||
assign(obj, props); | ||
// add symbols | ||
canReflect.assignSymbols(obj, symbols); | ||
return obj; | ||
}; | ||
// The following is for compatability with the old can-event/batch | ||
// This can be removed in a future version. | ||
function defineNonEnumerable(obj, prop, value) { | ||
Object.defineProperty(obj, prop, { | ||
enumerable: false, | ||
value: value | ||
}); | ||
Object.defineProperty(obj, prop, { | ||
enumerable: false, | ||
value: value | ||
}); | ||
} | ||
assign(eventQueue, props); | ||
defineNonEnumerable(eventQueue,"start", function(){ | ||
console.warn("use can-queues.batch.start()"); | ||
queues.batch.start(); | ||
defineNonEnumerable(eventQueue, "start", function() { | ||
console.warn("use can-queues.batch.start()"); | ||
queues.batch.start(); | ||
}); | ||
defineNonEnumerable(eventQueue,"stop", function(){ | ||
console.warn("use can-queues.batch.stop()"); | ||
queues.batch.stop(); | ||
defineNonEnumerable(eventQueue, "stop", function() { | ||
console.warn("use can-queues.batch.stop()"); | ||
queues.batch.stop(); | ||
}); | ||
defineNonEnumerable(eventQueue,"flush", function(){ | ||
console.warn("use can-queues.flush()"); | ||
queues.flush(); | ||
defineNonEnumerable(eventQueue, "flush", function() { | ||
console.warn("use can-queues.flush()"); | ||
queues.flush(); | ||
}); | ||
defineNonEnumerable(eventQueue,"afterPreviousEvents", function(handler){ | ||
console.warn("don't use afterPreviousEvents"); | ||
queues.mutateQueue.enqueue(function afterPreviousEvents(){ | ||
queues.mutateQueue.enqueue(handler); | ||
}); | ||
queues.flush(); | ||
defineNonEnumerable(eventQueue, "afterPreviousEvents", function(handler) { | ||
console.warn("don't use afterPreviousEvents"); | ||
queues.mutateQueue.enqueue(function afterPreviousEvents() { | ||
queues.mutateQueue.enqueue(handler); | ||
}); | ||
queues.flush(); | ||
}); | ||
defineNonEnumerable(eventQueue,"after", function(handler){ | ||
console.warn("don't use after"); | ||
queues.mutateQueue.enqueue(handler); | ||
queues.flush(); | ||
defineNonEnumerable(eventQueue, "after", function(handler) { | ||
console.warn("don't use after"); | ||
queues.mutateQueue.enqueue(handler); | ||
queues.flush(); | ||
}); | ||
module.exports = eventQueue; |
@@ -1,2 +0,2 @@ | ||
/*can-event-queue@0.2.2#can-event-queue*/ | ||
/*can-event-queue@0.3.0#can-event-queue*/ | ||
define([ | ||
@@ -85,8 +85,21 @@ 'require', | ||
removeEventListener: function (key, handler, queueName) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
if (!handler && !queueName) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event' | ||
]); | ||
} else if (!handler) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate' | ||
]); | ||
} else { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
} | ||
} | ||
@@ -93,0 +106,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
/*can-event-queue@0.2.2#can-event-queue*/ | ||
/*can-event-queue@0.3.0#can-event-queue*/ | ||
var canDev = require('can-util/js/dev/dev'); | ||
@@ -73,8 +73,21 @@ var assign = require('can-util/js/assign/assign'); | ||
removeEventListener: function (key, handler, queueName) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
if (!handler && !queueName) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event' | ||
]); | ||
} else if (!handler) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate' | ||
]); | ||
} else { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
} | ||
} | ||
@@ -81,0 +94,0 @@ }; |
@@ -130,3 +130,3 @@ /*[global-shim-start]*/ | ||
/*can-event-queue@0.2.2#can-event-queue*/ | ||
/*can-event-queue@0.3.0#can-event-queue*/ | ||
define('can-event-queue', [ | ||
@@ -215,8 +215,21 @@ 'require', | ||
removeEventListener: function (key, handler, queueName) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
if (!handler && !queueName) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event' | ||
]); | ||
} else if (!handler) { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate' | ||
]); | ||
} else { | ||
getHandlers(this).delete([ | ||
key, | ||
'event', | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
} | ||
} | ||
@@ -223,0 +236,0 @@ }; |
{ | ||
"name": "can-event-queue", | ||
"main": "./can-event-queue.js", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A event mixin that uses queues to dispatch handlers", | ||
@@ -6,0 +6,0 @@ "homepage": "", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44152
1067