d3-dispatch
Advanced tools
Comparing version 0.2.6 to 0.3.0
@@ -1,1 +0,1 @@ | ||
var version = "0.2.6"; export * from "../index"; export {version}; | ||
var version = "0.3.0"; export * from "../index"; export {version}; |
@@ -7,2 +7,4 @@ (function (global, factory) { | ||
var slice = Array.prototype.slice; | ||
function dispatch() { | ||
@@ -12,92 +14,73 @@ return new Dispatch(arguments); | ||
function Dispatch(types) { | ||
var i = -1, | ||
n = types.length, | ||
callbacksByType = {}, | ||
callbackByName = {}, | ||
type, | ||
that = this; | ||
function Dispatch(typenames) { | ||
for (var i = 0, n = typenames.length, types = this._ = new Array(n), t; i < n; ++i) { | ||
if (!(t = typenames[i] + "") || (t in this)) throw new Error("illegal or duplicate type: " + t); | ||
this[types[i] = t] = new Type; | ||
} | ||
} | ||
that.on = function(type, callback) { | ||
type = parseType(type); | ||
dispatch.prototype = Dispatch.prototype = { | ||
on: function(typename, callback) { | ||
var t = typename + "", name = "", i = t.indexOf("."), n; | ||
if (i >= 0) name = t.slice(i + 1), t = t.slice(0, i); | ||
if (t && !((t = this[t]) instanceof Type)) throw new Error("unknown type: " + typename); | ||
// Return the current callback, if any. | ||
if (arguments.length < 2) { | ||
return (callback = callbackByName[type.name]) && callback.value; | ||
} | ||
// If no callback was specified, return the callback of the given type and name. | ||
if (arguments.length < 2) return t ? t._get(name) : undefined; | ||
// If a type was specified… | ||
if (type.type) { | ||
var callbacks = callbacksByType[type.type], | ||
callback0 = callbackByName[type.name], | ||
i; | ||
// If a type was specified, set the callback for the given type and name. | ||
if (t) t._set(name, callback); | ||
// Remove the current callback, if any, using copy-on-remove. | ||
if (callback0) { | ||
callback0.value = null; | ||
i = callbacks.indexOf(callback0); | ||
callbacksByType[type.type] = callbacks = callbacks.slice(0, i).concat(callbacks.slice(i + 1)); | ||
delete callbackByName[type.name]; | ||
} | ||
// Add the new callback, if any. | ||
if (callback) { | ||
callback = {value: callback}; | ||
callbackByName[type.name] = callback; | ||
callbacks.push(callback); | ||
} | ||
} | ||
// Otherwise, if a null callback was specified, remove all callbacks with the given name. | ||
// Otherwise, if a null callback was specified, remove callbacks of the given name. | ||
else if (callback == null) { | ||
for (var otherType in callbacksByType) { | ||
if (callback = callbackByName[otherType + type.name]) { | ||
callback.value = null; | ||
callbacks = callbacksByType[otherType]; | ||
i = callbacks.indexOf(callback); | ||
callbacksByType[otherType] = callbacks.slice(0, i).concat(callbacks.slice(i + 1)); | ||
delete callbackByName[callback.name]; | ||
} | ||
for (t = this._, i = 0, n = t.length; i < n; ++i) { | ||
this[t[i]]._set(name, null); | ||
} | ||
} | ||
return that; | ||
}; | ||
while (++i < n) { | ||
type = types[i] + ""; | ||
if (!type || (type in that)) throw new Error("illegal or duplicate type: " + type); | ||
callbacksByType[type] = []; | ||
that[type] = applier(type); | ||
return this; | ||
} | ||
}; | ||
function parseType(type) { | ||
var i = (type += "").indexOf("."), name = type; | ||
if (i >= 0) type = type.slice(0, i); else name += "."; | ||
if (type && !callbacksByType.hasOwnProperty(type)) throw new Error("unknown type: " + type); | ||
return {type: type, name: name}; | ||
} | ||
function Type() { | ||
this._ = []; | ||
} | ||
function applier(type) { | ||
return function() { | ||
var callbacks = callbacksByType[type], // Defensive reference; copy-on-remove. | ||
callbackValue, | ||
i = -1, | ||
n = callbacks.length; | ||
Type.prototype = { | ||
_get: function(name) { | ||
for (var callbacks = this._, i = 0, n = callbacks.length, c; i < n; ++i) { | ||
if ((c = callbacks[i]).name === name) { | ||
return c.value; | ||
} | ||
} | ||
}, | ||
_set: function(name, callback) { | ||
for (var callbacks = this._, i = 0, n = callbacks.length, c; i < n; ++i) { | ||
if ((c = callbacks[i]).name === name) { | ||
this._ = callbacks = callbacks.slice(0, i).concat(callbacks.slice(i + 1)); | ||
c.value = null; | ||
break; | ||
} | ||
} | ||
if (callback != null) callbacks.push({name: name, value: callback}); | ||
}, | ||
call: function(that) { | ||
this.apply(that, slice.call(arguments, 1)); | ||
}, | ||
apply: function(that, args) { | ||
var callbacks = this._, // Defensive reference; copy-on-remove. | ||
callback, | ||
i = -1, | ||
n = callbacks.length; | ||
while (++i < n) { | ||
if (callbackValue = callbacks[i].value) { | ||
callbackValue.apply(this, arguments); | ||
} | ||
while (++i < n) { | ||
if (callback = callbacks[i].value) { | ||
callback.apply(that, args); | ||
} | ||
return that; | ||
}; | ||
} | ||
} | ||
} | ||
}; | ||
dispatch.prototype = Dispatch.prototype; | ||
var version = "0.3.0"; | ||
var version = "0.2.6"; | ||
exports.version = version; | ||
@@ -104,0 +87,0 @@ exports.dispatch = dispatch; |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.d3_dispatch={})}(this,function(e){"use strict";function n(){return new t(arguments)}function t(e){function n(e){var n=(e+="").indexOf("."),t=e;if(n>=0?e=e.slice(0,n):t+=".",e&&!u.hasOwnProperty(e))throw new Error("unknown type: "+e);return{type:e,name:t}}function t(e){return function(){for(var n,t=u[e],r=-1,i=t.length;++r<i;)(n=t[r].value)&&n.apply(this,arguments);return l}}var r,i=-1,o=e.length,u={},a={},l=this;for(l.on=function(e,t){if(e=n(e),arguments.length<2)return(t=a[e.name])&&t.value;if(e.type){var r,i=u[e.type],o=a[e.name];o&&(o.value=null,r=i.indexOf(o),u[e.type]=i=i.slice(0,r).concat(i.slice(r+1)),delete a[e.name]),t&&(t={value:t},a[e.name]=t,i.push(t))}else if(null==t)for(var f in u)(t=a[f+e.name])&&(t.value=null,i=u[f],r=i.indexOf(t),u[f]=i.slice(0,r).concat(i.slice(r+1)),delete a[t.name]);return l};++i<o;){if(r=e[i]+"",!r||r in l)throw new Error("illegal or duplicate type: "+r);u[r]=[],l[r]=t(r)}}n.prototype=t.prototype;var r="0.2.6";e.version=r,e.dispatch=n}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.d3_dispatch={})}(this,function(t){"use strict";function e(){return new n(arguments)}function n(t){for(var e,n=0,r=t.length,o=this._=new Array(r);r>n;++n){if(!(e=t[n]+"")||e in this)throw new Error("illegal or duplicate type: "+e);this[o[n]=e]=new i}}function i(){this._=[]}var r=Array.prototype.slice;e.prototype=n.prototype={on:function(t,e){var n,r=t+"",o="",l=r.indexOf(".");if(l>=0&&(o=r.slice(l+1),r=r.slice(0,l)),r&&!((r=this[r])instanceof i))throw new Error("unknown type: "+t);if(arguments.length<2)return r?r._get(o):void 0;if(r)r._set(o,e);else if(null==e)for(r=this._,l=0,n=r.length;n>l;++l)this[r[l]]._set(o,null);return this}},i.prototype={_get:function(t){for(var e,n=this._,i=0,r=n.length;r>i;++i)if((e=n[i]).name===t)return e.value},_set:function(t,e){for(var n,i=this._,r=0,o=i.length;o>r;++r)if((n=i[r]).name===t){this._=i=i.slice(0,r).concat(i.slice(r+1)),n.value=null;break}null!=e&&i.push({name:t,value:e})},call:function(t){this.apply(t,r.call(arguments,1))},apply:function(t,e){for(var n,i=this._,r=-1,o=i.length;++r<o;)(n=i[r].value)&&n.apply(t,e)}};var o="0.3.0";t.version=o,t.dispatch=e}); |
{ | ||
"name": "d3-dispatch", | ||
"version": "0.2.6", | ||
"version": "0.3.0", | ||
"description": "Register named callbacks and call them with arguments.", | ||
@@ -27,3 +27,3 @@ "keywords": [ | ||
"prepublish": "npm run test && uglifyjs build/d3-dispatch.js -c -m -o build/d3-dispatch.min.js && rm -f build/d3-dispatch.zip && zip -j build/d3-dispatch.zip -- LICENSE README.md build/d3-dispatch.js build/d3-dispatch.min.js", | ||
"postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git tag -am \"Release $VERSION.\" v${VERSION} && git push --tags && cp build/d3-dispatch.js ../d3.github.com/d3-dispatch.v0.2.js && cp build/d3-dispatch.min.js ../d3.github.com/d3-dispatch.v0.2.min.js && cd ../d3.github.com && git add d3-dispatch.v0.2.js d3-dispatch.v0.2.min.js && git commit -m \"d3-dispatch ${VERSION}\" && git push" | ||
"postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git tag -am \"Release $VERSION.\" v${VERSION} && git push --tags && cp build/d3-dispatch.js ../d3.github.com/d3-dispatch.v0.3.js && cp build/d3-dispatch.min.js ../d3.github.com/d3-dispatch.v0.3.min.js && cd ../d3.github.com && git add d3-dispatch.v0.3.js d3-dispatch.v0.3.min.js && git commit -m \"d3-dispatch ${VERSION}\" && git push" | ||
}, | ||
@@ -30,0 +30,0 @@ "devDependencies": { |
@@ -32,3 +32,3 @@ # d3-dispatch | ||
```html | ||
<script src="https://d3js.org/d3-dispatch.v0.2.min.js"></script> | ||
<script src="https://d3js.org/d3-dispatch.v0.3.min.js"></script> | ||
``` | ||
@@ -52,5 +52,5 @@ | ||
<a name="dispatch_type" href="#dispatch_type">#</a> *dispatch*.<b>*type*</b>(<i>arguments…</i>) | ||
<a name="dispatch_type" href="#dispatch_type">#</a> *dispatch*.<b>*type*</b> | ||
The *type* method (such as `dispatch.start` for the *start* event) invokes each registered callback for the given type, passing the callback the specified *arguments*. The `this` context will be used as the context of the registered callbacks. | ||
The *type* object (such as `dispatch.start` for the *start* event) exposes [call](#type_call) and [apply](#type_apply) methods which invoke each registered callback for the given type, passing the callback the specified *arguments*. The `this` context will be used as the context of the registered callbacks. | ||
@@ -66,1 +66,9 @@ For example, if you wanted to dispatch your *custom* callbacks after handling a native *click* event, while preserving the current `this` context and arguments, you could say: | ||
You can pass whatever arguments you want to callbacks; most commonly, you might create an object that represents an event, or pass the current datum (*d*) and index (*i*). See [function.call](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Call) and [function.apply](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Apply) for further information. | ||
<a name="type_call" href="#type_call">#</a> *type*.<b>call</b>(<i>that</i>, [<i>arguments…</i>]) | ||
Like [*function*.call](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call), invokes each registered callback for this type, passing the callback the specified *arguments*, with *that* as the `this` context. | ||
<a name="type_apply" href="#type_apply">#</a> *type*.<b>apply</b>(<i>that</i>, [<i>arguments</i>]) | ||
Like [*function*.apply](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call), invokes each registered callback for this type, passing the callback the specified *arguments*, with *that* as the `this` context. |
@@ -0,1 +1,3 @@ | ||
var slice = Array.prototype.slice; | ||
function dispatch() { | ||
@@ -5,90 +7,71 @@ return new Dispatch(arguments); | ||
function Dispatch(types) { | ||
var i = -1, | ||
n = types.length, | ||
callbacksByType = {}, | ||
callbackByName = {}, | ||
type, | ||
that = this; | ||
function Dispatch(typenames) { | ||
for (var i = 0, n = typenames.length, types = this._ = new Array(n), t; i < n; ++i) { | ||
if (!(t = typenames[i] + "") || (t in this)) throw new Error("illegal or duplicate type: " + t); | ||
this[types[i] = t] = new Type; | ||
} | ||
} | ||
that.on = function(type, callback) { | ||
type = parseType(type); | ||
dispatch.prototype = Dispatch.prototype = { | ||
on: function(typename, callback) { | ||
var t = typename + "", name = "", i = t.indexOf("."), n; | ||
if (i >= 0) name = t.slice(i + 1), t = t.slice(0, i); | ||
if (t && !((t = this[t]) instanceof Type)) throw new Error("unknown type: " + typename); | ||
// Return the current callback, if any. | ||
if (arguments.length < 2) { | ||
return (callback = callbackByName[type.name]) && callback.value; | ||
} | ||
// If no callback was specified, return the callback of the given type and name. | ||
if (arguments.length < 2) return t ? t._get(name) : undefined; | ||
// If a type was specified… | ||
if (type.type) { | ||
var callbacks = callbacksByType[type.type], | ||
callback0 = callbackByName[type.name], | ||
i; | ||
// If a type was specified, set the callback for the given type and name. | ||
if (t) t._set(name, callback); | ||
// Remove the current callback, if any, using copy-on-remove. | ||
if (callback0) { | ||
callback0.value = null; | ||
i = callbacks.indexOf(callback0); | ||
callbacksByType[type.type] = callbacks = callbacks.slice(0, i).concat(callbacks.slice(i + 1)); | ||
delete callbackByName[type.name]; | ||
} | ||
// Add the new callback, if any. | ||
if (callback) { | ||
callback = {value: callback}; | ||
callbackByName[type.name] = callback; | ||
callbacks.push(callback); | ||
} | ||
} | ||
// Otherwise, if a null callback was specified, remove all callbacks with the given name. | ||
// Otherwise, if a null callback was specified, remove callbacks of the given name. | ||
else if (callback == null) { | ||
for (var otherType in callbacksByType) { | ||
if (callback = callbackByName[otherType + type.name]) { | ||
callback.value = null; | ||
callbacks = callbacksByType[otherType]; | ||
i = callbacks.indexOf(callback); | ||
callbacksByType[otherType] = callbacks.slice(0, i).concat(callbacks.slice(i + 1)); | ||
delete callbackByName[callback.name]; | ||
} | ||
for (t = this._, i = 0, n = t.length; i < n; ++i) { | ||
this[t[i]]._set(name, null); | ||
} | ||
} | ||
return that; | ||
}; | ||
while (++i < n) { | ||
type = types[i] + ""; | ||
if (!type || (type in that)) throw new Error("illegal or duplicate type: " + type); | ||
callbacksByType[type] = []; | ||
that[type] = applier(type); | ||
return this; | ||
} | ||
}; | ||
function parseType(type) { | ||
var i = (type += "").indexOf("."), name = type; | ||
if (i >= 0) type = type.slice(0, i); else name += "."; | ||
if (type && !callbacksByType.hasOwnProperty(type)) throw new Error("unknown type: " + type); | ||
return {type: type, name: name}; | ||
} | ||
function Type() { | ||
this._ = []; | ||
} | ||
function applier(type) { | ||
return function() { | ||
var callbacks = callbacksByType[type], // Defensive reference; copy-on-remove. | ||
callbackValue, | ||
i = -1, | ||
n = callbacks.length; | ||
Type.prototype = { | ||
_get: function(name) { | ||
for (var callbacks = this._, i = 0, n = callbacks.length, c; i < n; ++i) { | ||
if ((c = callbacks[i]).name === name) { | ||
return c.value; | ||
} | ||
} | ||
}, | ||
_set: function(name, callback) { | ||
for (var callbacks = this._, i = 0, n = callbacks.length, c; i < n; ++i) { | ||
if ((c = callbacks[i]).name === name) { | ||
this._ = callbacks = callbacks.slice(0, i).concat(callbacks.slice(i + 1)); | ||
c.value = null; | ||
break; | ||
} | ||
} | ||
if (callback != null) callbacks.push({name: name, value: callback}); | ||
}, | ||
call: function(that) { | ||
this.apply(that, slice.call(arguments, 1)); | ||
}, | ||
apply: function(that, args) { | ||
var callbacks = this._, // Defensive reference; copy-on-remove. | ||
callback, | ||
i = -1, | ||
n = callbacks.length; | ||
while (++i < n) { | ||
if (callbackValue = callbacks[i].value) { | ||
callbackValue.apply(this, arguments); | ||
} | ||
while (++i < n) { | ||
if (callback = callbacks[i].value) { | ||
callback.apply(that, args); | ||
} | ||
return that; | ||
}; | ||
} | ||
} | ||
} | ||
}; | ||
dispatch.prototype = Dispatch.prototype; // allow instanceof | ||
export default dispatch; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
72
14290
138
1