source: node_modules/d3-dispatch/src/dispatch.js@ ba17441

Last change on this file since ba17441 was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 2.7 KB
Line 
1var noop = {value: () => {}};
2
3function dispatch() {
4 for (var i = 0, n = arguments.length, _ = {}, t; i < n; ++i) {
5 if (!(t = arguments[i] + "") || (t in _) || /[\s.]/.test(t)) throw new Error("illegal type: " + t);
6 _[t] = [];
7 }
8 return new Dispatch(_);
9}
10
11function Dispatch(_) {
12 this._ = _;
13}
14
15function parseTypenames(typenames, types) {
16 return typenames.trim().split(/^|\s+/).map(function(t) {
17 var name = "", i = t.indexOf(".");
18 if (i >= 0) name = t.slice(i + 1), t = t.slice(0, i);
19 if (t && !types.hasOwnProperty(t)) throw new Error("unknown type: " + t);
20 return {type: t, name: name};
21 });
22}
23
24Dispatch.prototype = dispatch.prototype = {
25 constructor: Dispatch,
26 on: function(typename, callback) {
27 var _ = this._,
28 T = parseTypenames(typename + "", _),
29 t,
30 i = -1,
31 n = T.length;
32
33 // If no callback was specified, return the callback of the given type and name.
34 if (arguments.length < 2) {
35 while (++i < n) if ((t = (typename = T[i]).type) && (t = get(_[t], typename.name))) return t;
36 return;
37 }
38
39 // If a type was specified, set the callback for the given type and name.
40 // Otherwise, if a null callback was specified, remove callbacks of the given name.
41 if (callback != null && typeof callback !== "function") throw new Error("invalid callback: " + callback);
42 while (++i < n) {
43 if (t = (typename = T[i]).type) _[t] = set(_[t], typename.name, callback);
44 else if (callback == null) for (t in _) _[t] = set(_[t], typename.name, null);
45 }
46
47 return this;
48 },
49 copy: function() {
50 var copy = {}, _ = this._;
51 for (var t in _) copy[t] = _[t].slice();
52 return new Dispatch(copy);
53 },
54 call: function(type, that) {
55 if ((n = arguments.length - 2) > 0) for (var args = new Array(n), i = 0, n, t; i < n; ++i) args[i] = arguments[i + 2];
56 if (!this._.hasOwnProperty(type)) throw new Error("unknown type: " + type);
57 for (t = this._[type], i = 0, n = t.length; i < n; ++i) t[i].value.apply(that, args);
58 },
59 apply: function(type, that, args) {
60 if (!this._.hasOwnProperty(type)) throw new Error("unknown type: " + type);
61 for (var t = this._[type], i = 0, n = t.length; i < n; ++i) t[i].value.apply(that, args);
62 }
63};
64
65function get(type, name) {
66 for (var i = 0, n = type.length, c; i < n; ++i) {
67 if ((c = type[i]).name === name) {
68 return c.value;
69 }
70 }
71}
72
73function set(type, name, callback) {
74 for (var i = 0, n = type.length; i < n; ++i) {
75 if (type[i].name === name) {
76 type[i] = noop, type = type.slice(0, i).concat(type.slice(i + 1));
77 break;
78 }
79 }
80 if (callback != null) type.push({name: name, value: callback});
81 return type;
82}
83
84export default dispatch;
Note: See TracBrowser for help on using the repository browser.