| 1 |
/** |
| 2 |
* A class for adding deep extensibility to any piece of JS code |
| 3 |
*/ |
| 4 |
export class Hooks { |
| 5 |
add (name, callback, first) { |
| 6 |
if (typeof arguments[0] != "string") { |
| 7 |
// Multiple hooks |
| 8 |
for (var name in arguments[0]) { |
| 9 |
this.add(name, arguments[0][name], arguments[1]); |
| 10 |
} |
| 11 |
|
| 12 |
return; |
| 13 |
} |
| 14 |
|
| 15 |
(Array.isArray(name) ? name : [name]).forEach(function (name) { |
| 16 |
this[name] = this[name] || []; |
| 17 |
|
| 18 |
if (callback) { |
| 19 |
this[name][first ? "unshift" : "push"](callback); |
| 20 |
} |
| 21 |
}, this); |
| 22 |
} |
| 23 |
|
| 24 |
run (name, env) { |
| 25 |
this[name] = this[name] || []; |
| 26 |
this[name].forEach(function (callback) { |
| 27 |
callback.call(env && env.context ? env.context : env, env); |
| 28 |
}); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* The instance of {@link Hooks} used throughout Color.js |
| 34 |
*/ |
| 35 |
const hooks = new Hooks(); |
| 36 |
|
| 37 |
export default hooks; |