can-event-queue
Advanced tools
Comparing version 0.5.1 to 0.6.0
@@ -7,2 +7,3 @@ var QUnit = require('steal-qunit'); | ||
var canReflect = require("can-reflect"); | ||
var addBoundChange = require("./bound-change/bound-change"); | ||
@@ -173,31 +174,37 @@ QUnit.module('can-event-queue',{ | ||
QUnit.ok(!obj[canSymbol.for("can.isBound")](), "Object is not bound after removing listener"); | ||
}); | ||
obj[canSymbol.for("can.onBoundChange")](handler); | ||
QUnit.ok(!obj[canSymbol.for("can.isBound")](), "Object is not bound after adding bound change listener"); | ||
test("Events when object is bound/unbound", function() { | ||
expect(1); | ||
var Type = function(){}; | ||
eventQueue(Type.prototype); | ||
addBoundChange( Type ); | ||
}); | ||
var obj1 = new Type(), | ||
obj2 = new Type(); | ||
test("Events when object is bound/unbound", function() { | ||
expect(2); | ||
var obj = eventQueue({}); | ||
var trueCaseHit = false; | ||
var metaHandler = function(newVal) { | ||
if(!trueCaseHit) { | ||
QUnit.ok(newVal, "new value reflects other events bound"); | ||
trueCaseHit = true; | ||
} else { | ||
QUnit.ok(!newVal, "new value reflects all events unbound"); | ||
} | ||
var calls = []; | ||
var metaHandler = function(obj, newVal) { | ||
calls.push([obj, newVal]); | ||
}; | ||
var handler = function() {}; | ||
obj[canSymbol.for("can.onBoundChange")](metaHandler); | ||
Type[canSymbol.for("can.onBoundChange")](metaHandler); | ||
obj.on("first", handler); | ||
obj.off("first", handler); | ||
obj1.on("first", handler); | ||
obj1.off("first", handler); | ||
obj2.on("second", handler); | ||
obj2.off("second", handler); | ||
// Sanity check. Ensure that no more events fire after offBoundChange | ||
obj[canSymbol.for("can.offBoundChange")](metaHandler); | ||
obj.on("first", handler); | ||
obj.off("first", handler); | ||
Type[canSymbol.for("can.offBoundChange")](metaHandler); | ||
obj1.on("first", handler); | ||
obj1.off("first", handler); | ||
QUnit.deepEqual(calls,[ | ||
[obj1,true], | ||
[obj1,false], | ||
[obj2,true], | ||
[obj2,false] | ||
]); | ||
}); | ||
@@ -204,0 +211,0 @@ |
@@ -10,2 +10,11 @@ var canDev = require("can-util/js/dev/dev"); | ||
var metaSymbol = canSymbol.for("can.meta"), | ||
dispatchBoundChangeSymbol = canSymbol.for("can.dispatchBoundChange"), | ||
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"); | ||
var eventQueue; | ||
@@ -18,3 +27,2 @@ | ||
var ensureMeta = function ensureMeta(obj) { | ||
var metaSymbol = canSymbol.for("can.meta"); | ||
var meta = obj[metaSymbol]; | ||
@@ -26,4 +34,3 @@ | ||
} | ||
var handlers = meta.handlers; | ||
if (!handlers) { | ||
if (!meta.handlers) { | ||
// Handlers are organized by: | ||
@@ -34,3 +41,3 @@ // event name - the type of event bound to | ||
// handlers - the handlers. | ||
handlers = meta.handlers = new KeyTree([Object, Object, Object, Array], { | ||
meta.handlers = new KeyTree([Object, Object, Object, Array], { | ||
onFirst: function() { | ||
@@ -40,3 +47,6 @@ if (obj._eventSetup) { | ||
} | ||
queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [true]); | ||
if(obj.constructor[dispatchBoundChangeSymbol]) { | ||
obj.constructor[dispatchBoundChangeSymbol](obj, true); | ||
} | ||
//queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [true]); | ||
}, | ||
@@ -47,3 +57,6 @@ onEmpty: function() { | ||
} | ||
queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [false]); | ||
if(obj.constructor[dispatchBoundChangeSymbol]) { | ||
obj.constructor[dispatchBoundChangeSymbol](obj, false); | ||
} | ||
//queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [false]); | ||
} | ||
@@ -53,9 +66,2 @@ }); | ||
if (!meta.lifecycleHandlers) { | ||
// lifecycleHandlers are organized by: | ||
// queue name - mutate, queue, etc | ||
// lifecycleHandlers - the lifecycleHandlers. | ||
meta.lifecycleHandlers = new KeyTree([Object, Array]); | ||
} | ||
if (!meta.listenHandlers) { | ||
@@ -68,14 +74,2 @@ meta.listenHandlers = new KeyTree([Map, Object, Array]); | ||
// getHandlers returns a KeyTree used for event handling. | ||
// `handlers` will be on the `can.meta` symbol on the object. | ||
function getHandlers(obj) { | ||
return ensureMeta(obj).handlers; | ||
} | ||
// getLifecycleHandlers returns a KeyTree used for handling first binding and last unbinding events | ||
// `lifecycleHandlers` will be on the `can.meta` symbol on the object. | ||
function getLifecycleHandlers(obj) { | ||
return ensureMeta(obj).lifecycleHandlers; | ||
} | ||
// These are the properties we are going to add to objects | ||
@@ -141,3 +135,3 @@ var props = { | ||
addEventListener: function(key, handler, queueName) { | ||
getHandlers(this).add([key, "event", queueName || "mutate", handler]); | ||
ensureMeta(this).handlers.add([key, "event", queueName || "mutate", handler]); | ||
}, | ||
@@ -148,3 +142,3 @@ removeEventListener: function(key, handler, queueName) { | ||
// We could re-arrange the tree so it would be faster. | ||
var handlers = getHandlers(this); | ||
var handlers = ensureMeta(this).handlers; | ||
var keyHandlers = handlers.getNode([]); | ||
@@ -155,7 +149,7 @@ Object.keys(keyHandlers).forEach(function(key){ | ||
} else if (!handler && !queueName) { | ||
getHandlers(this).delete([key, "event"]); | ||
ensureMeta(this).handlers.delete([key, "event"]); | ||
} else if (!handler) { | ||
getHandlers(this).delete([key, "event", queueName || "mutate"]); | ||
ensureMeta(this).handlers.delete([key, "event", queueName || "mutate"]); | ||
} else { | ||
getHandlers(this).delete([key, "event", queueName || "mutate", handler]); | ||
ensureMeta(this).handlers.delete([key, "event", queueName || "mutate", handler]); | ||
} | ||
@@ -217,76 +211,63 @@ }, | ||
return this; | ||
} | ||
}; | ||
props.bind = props.addEventListener; | ||
props.unbind = props.removeEventListener; | ||
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"); | ||
props.on = function(eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === "string" ? "addDelegateListener" : "addEventListener"; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if ("addEventListener" in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
}, | ||
on: function(eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === "string" ? "addDelegateListener" : "addEventListener"; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
if ("addEventListener" in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
} else { | ||
throw new Error("can-event-queue: Unable to bind " + eventName); | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
} else { | ||
throw new Error("can-event-queue: Unable to bind " + eventName); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
props.off = function(eventName, handler, queue) { | ||
}, | ||
off: function(eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === "string" ? "removeDelegateListener" : "removeEventListener"; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === "string" ? "removeDelegateListener" : "removeEventListener"; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if ("removeEventListener" in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
} else { | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
if ("removeEventListener" in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
} else { | ||
throw new Error("can-control: Unable to unbind " + eventName); | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
} else { | ||
throw new Error("can-event-queue: Unable to unbind " + eventName); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
props.bind = props.addEventListener; | ||
props.unbind = props.removeEventListener; | ||
// The symbols we'll add to objects | ||
var symbols = { | ||
"can.onKeyValue": function(key, handler, queueName) { | ||
getHandlers(this).add([key, "onKeyValue", queueName || "mutate", handler]); | ||
ensureMeta(this).handlers.add([key, "onKeyValue", queueName || "mutate", handler]); | ||
}, | ||
"can.offKeyValue": function(key, handler, queueName) { | ||
getHandlers(this).delete([key, "onKeyValue", queueName || "mutate", handler]); | ||
ensureMeta(this).handlers.delete([key, "onKeyValue", queueName || "mutate", handler]); | ||
}, | ||
"can.onBoundChange": function(handler, queueName) { | ||
getLifecycleHandlers(this).add([queueName || "mutate", handler]); | ||
}, | ||
"can.offBoundChange": function(handler, queueName) { | ||
getLifecycleHandlers(this).delete([queueName || "mutate", handler]); | ||
}, | ||
"can.isBound": function() { | ||
return getHandlers(this).size() > 0; | ||
return ensureMeta(this).handlers.size() > 0; | ||
} | ||
@@ -300,4 +281,3 @@ }; | ||
// add symbols | ||
canReflect.assignSymbols(obj, symbols); | ||
return obj; | ||
return canReflect.assignSymbols(obj, symbols); | ||
}; | ||
@@ -316,2 +296,3 @@ | ||
assign(eventQueue, props); | ||
defineNonEnumerable(eventQueue, "start", function() { | ||
@@ -318,0 +299,0 @@ console.warn("use can-queues.batch.start()"); |
@@ -1,2 +0,2 @@ | ||
/*can-event-queue@0.5.0#can-event-queue*/ | ||
/*can-event-queue@0.5.1#can-event-queue*/ | ||
define([ | ||
@@ -21,5 +21,5 @@ 'require', | ||
var domEvents = require('can-util/dom/events'); | ||
var metaSymbol = canSymbol.for('can.meta'), dispatchBoundChangeSymbol = canSymbol.for('can.dispatchBoundChange'), 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'); | ||
var eventQueue; | ||
var ensureMeta = function ensureMeta(obj) { | ||
var metaSymbol = canSymbol.for('can.meta'); | ||
var meta = obj[metaSymbol]; | ||
@@ -30,5 +30,4 @@ if (!meta) { | ||
} | ||
var handlers = meta.handlers; | ||
if (!handlers) { | ||
handlers = meta.handlers = new KeyTree([ | ||
if (!meta.handlers) { | ||
meta.handlers = new KeyTree([ | ||
Object, | ||
@@ -43,3 +42,5 @@ Object, | ||
} | ||
queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [true]); | ||
if (obj.constructor[dispatchBoundChangeSymbol]) { | ||
obj.constructor[dispatchBoundChangeSymbol](obj, true); | ||
} | ||
}, | ||
@@ -50,12 +51,8 @@ onEmpty: function () { | ||
} | ||
queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [false]); | ||
if (obj.constructor[dispatchBoundChangeSymbol]) { | ||
obj.constructor[dispatchBoundChangeSymbol](obj, false); | ||
} | ||
} | ||
}); | ||
} | ||
if (!meta.lifecycleHandlers) { | ||
meta.lifecycleHandlers = new KeyTree([ | ||
Object, | ||
Array | ||
]); | ||
} | ||
if (!meta.listenHandlers) { | ||
@@ -70,8 +67,2 @@ meta.listenHandlers = new KeyTree([ | ||
}; | ||
function getHandlers(obj) { | ||
return ensureMeta(obj).handlers; | ||
} | ||
function getLifecycleHandlers(obj) { | ||
return ensureMeta(obj).lifecycleHandlers; | ||
} | ||
var props = { | ||
@@ -101,3 +92,3 @@ dispatch: function (event, args) { | ||
addEventListener: function (key, handler, queueName) { | ||
getHandlers(this).add([ | ||
ensureMeta(this).handlers.add([ | ||
key, | ||
@@ -111,3 +102,3 @@ 'event', | ||
if (key === undefined) { | ||
var handlers = getHandlers(this); | ||
var handlers = ensureMeta(this).handlers; | ||
var keyHandlers = handlers.getNode([]); | ||
@@ -121,3 +112,3 @@ Object.keys(keyHandlers).forEach(function (key) { | ||
} else if (!handler && !queueName) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -127,3 +118,3 @@ 'event' | ||
} else if (!handler) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -134,3 +125,3 @@ 'event', | ||
} else { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -204,45 +195,42 @@ 'event', | ||
return this; | ||
} | ||
}; | ||
props.bind = props.addEventListener; | ||
props.unbind = props.removeEventListener; | ||
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'); | ||
props.on = function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'addDelegateListener' : 'addEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if ('addEventListener' in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
}, | ||
on: function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'addDelegateListener' : 'addEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
if ('addEventListener' in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
} else { | ||
throw new Error('can-event-queue: Unable to bind ' + eventName); | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
} else { | ||
throw new Error('can-event-queue: Unable to bind ' + eventName); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
props.off = function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'removeDelegateListener' : 'removeEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if ('removeEventListener' in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
}, | ||
off: function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'removeDelegateListener' : 'removeEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
if ('removeEventListener' in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
} else { | ||
throw new Error('can-control: Unable to unbind ' + eventName); | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
} else { | ||
throw new Error('can-event-queue: Unable to unbind ' + eventName); | ||
} | ||
} | ||
@@ -252,5 +240,7 @@ } | ||
}; | ||
props.bind = props.addEventListener; | ||
props.unbind = props.removeEventListener; | ||
var symbols = { | ||
'can.onKeyValue': function (key, handler, queueName) { | ||
getHandlers(this).add([ | ||
ensureMeta(this).handlers.add([ | ||
key, | ||
@@ -263,3 +253,3 @@ 'onKeyValue', | ||
'can.offKeyValue': function (key, handler, queueName) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -271,16 +261,4 @@ 'onKeyValue', | ||
}, | ||
'can.onBoundChange': function (handler, queueName) { | ||
getLifecycleHandlers(this).add([ | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
}, | ||
'can.offBoundChange': function (handler, queueName) { | ||
getLifecycleHandlers(this).delete([ | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
}, | ||
'can.isBound': function () { | ||
return getHandlers(this).size() > 0; | ||
return ensureMeta(this).handlers.size() > 0; | ||
} | ||
@@ -290,4 +268,3 @@ }; | ||
assign(obj, props); | ||
canReflect.assignSymbols(obj, symbols); | ||
return obj; | ||
return canReflect.assignSymbols(obj, symbols); | ||
}; | ||
@@ -294,0 +271,0 @@ function defineNonEnumerable(obj, prop, value) { |
@@ -1,2 +0,2 @@ | ||
/*can-event-queue@0.5.0#can-event-queue*/ | ||
/*can-event-queue@0.5.1#can-event-queue*/ | ||
var canDev = require('can-util/js/dev/dev'); | ||
@@ -9,5 +9,5 @@ var assign = require('can-util/js/assign/assign'); | ||
var domEvents = require('can-util/dom/events/events'); | ||
var metaSymbol = canSymbol.for('can.meta'), dispatchBoundChangeSymbol = canSymbol.for('can.dispatchBoundChange'), 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'); | ||
var eventQueue; | ||
var ensureMeta = function ensureMeta(obj) { | ||
var metaSymbol = canSymbol.for('can.meta'); | ||
var meta = obj[metaSymbol]; | ||
@@ -18,5 +18,4 @@ if (!meta) { | ||
} | ||
var handlers = meta.handlers; | ||
if (!handlers) { | ||
handlers = meta.handlers = new KeyTree([ | ||
if (!meta.handlers) { | ||
meta.handlers = new KeyTree([ | ||
Object, | ||
@@ -31,3 +30,5 @@ Object, | ||
} | ||
queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [true]); | ||
if (obj.constructor[dispatchBoundChangeSymbol]) { | ||
obj.constructor[dispatchBoundChangeSymbol](obj, true); | ||
} | ||
}, | ||
@@ -38,12 +39,8 @@ onEmpty: function () { | ||
} | ||
queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [false]); | ||
if (obj.constructor[dispatchBoundChangeSymbol]) { | ||
obj.constructor[dispatchBoundChangeSymbol](obj, false); | ||
} | ||
} | ||
}); | ||
} | ||
if (!meta.lifecycleHandlers) { | ||
meta.lifecycleHandlers = new KeyTree([ | ||
Object, | ||
Array | ||
]); | ||
} | ||
if (!meta.listenHandlers) { | ||
@@ -58,8 +55,2 @@ meta.listenHandlers = new KeyTree([ | ||
}; | ||
function getHandlers(obj) { | ||
return ensureMeta(obj).handlers; | ||
} | ||
function getLifecycleHandlers(obj) { | ||
return ensureMeta(obj).lifecycleHandlers; | ||
} | ||
var props = { | ||
@@ -89,3 +80,3 @@ dispatch: function (event, args) { | ||
addEventListener: function (key, handler, queueName) { | ||
getHandlers(this).add([ | ||
ensureMeta(this).handlers.add([ | ||
key, | ||
@@ -99,3 +90,3 @@ 'event', | ||
if (key === undefined) { | ||
var handlers = getHandlers(this); | ||
var handlers = ensureMeta(this).handlers; | ||
var keyHandlers = handlers.getNode([]); | ||
@@ -109,3 +100,3 @@ Object.keys(keyHandlers).forEach(function (key) { | ||
} else if (!handler && !queueName) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -115,3 +106,3 @@ 'event' | ||
} else if (!handler) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -122,3 +113,3 @@ 'event', | ||
} else { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -192,45 +183,42 @@ 'event', | ||
return this; | ||
} | ||
}; | ||
props.bind = props.addEventListener; | ||
props.unbind = props.removeEventListener; | ||
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'); | ||
props.on = function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'addDelegateListener' : 'addEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if ('addEventListener' in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
}, | ||
on: function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'addDelegateListener' : 'addEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
if ('addEventListener' in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
} else { | ||
throw new Error('can-event-queue: Unable to bind ' + eventName); | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
} else { | ||
throw new Error('can-event-queue: Unable to bind ' + eventName); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
props.off = function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'removeDelegateListener' : 'removeEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if ('removeEventListener' in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
}, | ||
off: function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'removeDelegateListener' : 'removeEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
if ('removeEventListener' in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
} else { | ||
throw new Error('can-control: Unable to unbind ' + eventName); | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
} else { | ||
throw new Error('can-event-queue: Unable to unbind ' + eventName); | ||
} | ||
} | ||
@@ -240,5 +228,7 @@ } | ||
}; | ||
props.bind = props.addEventListener; | ||
props.unbind = props.removeEventListener; | ||
var symbols = { | ||
'can.onKeyValue': function (key, handler, queueName) { | ||
getHandlers(this).add([ | ||
ensureMeta(this).handlers.add([ | ||
key, | ||
@@ -251,3 +241,3 @@ 'onKeyValue', | ||
'can.offKeyValue': function (key, handler, queueName) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -259,16 +249,4 @@ 'onKeyValue', | ||
}, | ||
'can.onBoundChange': function (handler, queueName) { | ||
getLifecycleHandlers(this).add([ | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
}, | ||
'can.offBoundChange': function (handler, queueName) { | ||
getLifecycleHandlers(this).delete([ | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
}, | ||
'can.isBound': function () { | ||
return getHandlers(this).size() > 0; | ||
return ensureMeta(this).handlers.size() > 0; | ||
} | ||
@@ -278,4 +256,3 @@ }; | ||
assign(obj, props); | ||
canReflect.assignSymbols(obj, symbols); | ||
return obj; | ||
return canReflect.assignSymbols(obj, symbols); | ||
}; | ||
@@ -282,0 +259,0 @@ function defineNonEnumerable(obj, prop, value) { |
@@ -130,3 +130,3 @@ /*[global-shim-start]*/ | ||
/*can-event-queue@0.5.0#can-event-queue*/ | ||
/*can-event-queue@0.5.1#can-event-queue*/ | ||
define('can-event-queue', [ | ||
@@ -151,5 +151,5 @@ 'require', | ||
var domEvents = require('can-util/dom/events/events'); | ||
var metaSymbol = canSymbol.for('can.meta'), dispatchBoundChangeSymbol = canSymbol.for('can.dispatchBoundChange'), 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'); | ||
var eventQueue; | ||
var ensureMeta = function ensureMeta(obj) { | ||
var metaSymbol = canSymbol.for('can.meta'); | ||
var meta = obj[metaSymbol]; | ||
@@ -160,5 +160,4 @@ if (!meta) { | ||
} | ||
var handlers = meta.handlers; | ||
if (!handlers) { | ||
handlers = meta.handlers = new KeyTree([ | ||
if (!meta.handlers) { | ||
meta.handlers = new KeyTree([ | ||
Object, | ||
@@ -173,3 +172,5 @@ Object, | ||
} | ||
queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [true]); | ||
if (obj.constructor[dispatchBoundChangeSymbol]) { | ||
obj.constructor[dispatchBoundChangeSymbol](obj, true); | ||
} | ||
}, | ||
@@ -180,12 +181,8 @@ onEmpty: function () { | ||
} | ||
queues.enqueueByQueue(getLifecycleHandlers(obj).getNode([]), obj, [false]); | ||
if (obj.constructor[dispatchBoundChangeSymbol]) { | ||
obj.constructor[dispatchBoundChangeSymbol](obj, false); | ||
} | ||
} | ||
}); | ||
} | ||
if (!meta.lifecycleHandlers) { | ||
meta.lifecycleHandlers = new KeyTree([ | ||
Object, | ||
Array | ||
]); | ||
} | ||
if (!meta.listenHandlers) { | ||
@@ -200,8 +197,2 @@ meta.listenHandlers = new KeyTree([ | ||
}; | ||
function getHandlers(obj) { | ||
return ensureMeta(obj).handlers; | ||
} | ||
function getLifecycleHandlers(obj) { | ||
return ensureMeta(obj).lifecycleHandlers; | ||
} | ||
var props = { | ||
@@ -231,3 +222,3 @@ dispatch: function (event, args) { | ||
addEventListener: function (key, handler, queueName) { | ||
getHandlers(this).add([ | ||
ensureMeta(this).handlers.add([ | ||
key, | ||
@@ -241,3 +232,3 @@ 'event', | ||
if (key === undefined) { | ||
var handlers = getHandlers(this); | ||
var handlers = ensureMeta(this).handlers; | ||
var keyHandlers = handlers.getNode([]); | ||
@@ -251,3 +242,3 @@ Object.keys(keyHandlers).forEach(function (key) { | ||
} else if (!handler && !queueName) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -257,3 +248,3 @@ 'event' | ||
} else if (!handler) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -264,3 +255,3 @@ 'event', | ||
} else { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -334,45 +325,42 @@ 'event', | ||
return this; | ||
} | ||
}; | ||
props.bind = props.addEventListener; | ||
props.unbind = props.removeEventListener; | ||
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'); | ||
props.on = function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'addDelegateListener' : 'addEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if ('addEventListener' in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
}, | ||
on: function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'addDelegateListener' : 'addEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
if ('addEventListener' in this) { | ||
this.addEventListener(eventName, handler, queue); | ||
} else if (this[onKeyValueSymbol]) { | ||
canReflect.onKeyValue(this, eventName, handler, queue); | ||
} else if (this[onEventSymbol]) { | ||
this[onEventSymbol](eventName, handler, queue); | ||
} else { | ||
throw new Error('can-event-queue: Unable to bind ' + eventName); | ||
if (!eventName && this[onValueSymbol]) { | ||
canReflect.onValue(this, handler); | ||
} else { | ||
throw new Error('can-event-queue: Unable to bind ' + eventName); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
props.off = function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'removeDelegateListener' : 'removeEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if ('removeEventListener' in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
}, | ||
off: function (eventName, handler, queue) { | ||
var listenWithDOM = domEvents.canAddEventListener.call(this); | ||
if (listenWithDOM) { | ||
var method = typeof handler === 'string' ? 'removeDelegateListener' : 'removeEventListener'; | ||
domEvents[method].call(this, eventName, handler, queue); | ||
} else { | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
if ('removeEventListener' in this) { | ||
this.removeEventListener(eventName, handler, queue); | ||
} else if (this[offKeyValueSymbol]) { | ||
canReflect.offKeyValue(this, eventName, handler, queue); | ||
} else if (this[offEventSymbol]) { | ||
this[offEventSymbol](eventName, handler, queue); | ||
} else { | ||
throw new Error('can-control: Unable to unbind ' + eventName); | ||
if (!eventName && this[offValueSymbol]) { | ||
canReflect.offValue(this, handler); | ||
} else { | ||
throw new Error('can-event-queue: Unable to unbind ' + eventName); | ||
} | ||
} | ||
@@ -382,5 +370,7 @@ } | ||
}; | ||
props.bind = props.addEventListener; | ||
props.unbind = props.removeEventListener; | ||
var symbols = { | ||
'can.onKeyValue': function (key, handler, queueName) { | ||
getHandlers(this).add([ | ||
ensureMeta(this).handlers.add([ | ||
key, | ||
@@ -393,3 +383,3 @@ 'onKeyValue', | ||
'can.offKeyValue': function (key, handler, queueName) { | ||
getHandlers(this).delete([ | ||
ensureMeta(this).handlers.delete([ | ||
key, | ||
@@ -401,16 +391,4 @@ 'onKeyValue', | ||
}, | ||
'can.onBoundChange': function (handler, queueName) { | ||
getLifecycleHandlers(this).add([ | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
}, | ||
'can.offBoundChange': function (handler, queueName) { | ||
getLifecycleHandlers(this).delete([ | ||
queueName || 'mutate', | ||
handler | ||
]); | ||
}, | ||
'can.isBound': function () { | ||
return getHandlers(this).size() > 0; | ||
return ensureMeta(this).handlers.size() > 0; | ||
} | ||
@@ -420,4 +398,3 @@ }; | ||
assign(obj, props); | ||
canReflect.assignSymbols(obj, symbols); | ||
return obj; | ||
return canReflect.assignSymbols(obj, symbols); | ||
}; | ||
@@ -424,0 +401,0 @@ function defineNonEnumerable(obj, prop, value) { |
{ | ||
"name": "can-event-queue", | ||
"main": "./can-event-queue.js", | ||
"version": "0.5.1", | ||
"version": "0.6.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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
1
62334
1547