Comparing version 0.0.1 to 0.1.0
@@ -16,7 +16,7 @@ var arrayize, getHook, isFunction; | ||
getHook = (obj) => { | ||
var arr, exec, getExec, hook; | ||
getHook = (obj, options) => { | ||
var arr, exec, execute, getExec, hook, register; | ||
arr = []; | ||
exec = null; | ||
hook = async(o) => { | ||
execute = async(o) => { | ||
if (exec == null) { | ||
@@ -27,3 +27,3 @@ exec = getExec(); | ||
}; | ||
hook.call = (o) => { | ||
register = (o) => { | ||
if (isFunction(o)) { | ||
@@ -39,3 +39,3 @@ o = { | ||
arr.push(o); | ||
return hook.exec = exec = null; | ||
return exec = null; | ||
}; | ||
@@ -60,3 +60,3 @@ getExec = () => { | ||
}); | ||
return hook.exec = exec = async(o) => { | ||
return exec = async(o) => { | ||
var callHooks, i, len, results; | ||
@@ -71,5 +71,16 @@ results = []; | ||
}; | ||
hook.clear = () => { | ||
if (options.register === "") { | ||
hook = register; | ||
hook[options.execute] = execute; | ||
} else if (options.execute === "") { | ||
hook = execute; | ||
hook[options.register] = register; | ||
} else { | ||
hook = {}; | ||
hook[options.execute] = execute; | ||
hook[options.register] = register; | ||
} | ||
hook[options.clear] = () => { | ||
hook._hooks = arr = []; | ||
return hook.exec = exec = null; | ||
return exec = null; | ||
}; | ||
@@ -80,31 +91,51 @@ hook._hooks = arr; | ||
module.exports = (obj, prefix) => { | ||
var i, len, prop; | ||
if (prefix == null) { | ||
prefix = ["before", "after"]; | ||
module.exports = (obj, options) => { | ||
var prefix; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
prefix = arrayize(prefix); | ||
for (i = 0, len = prefix.length; i < len; i++) { | ||
prop = prefix[i]; | ||
obj[prop] = {}; | ||
if (options.prefix != null) { | ||
prefix = arrayize(options.prefix); | ||
} | ||
if (options.register == null) { | ||
options.register = "register"; | ||
} | ||
if (options.execute == null) { | ||
options.execute = "execute"; | ||
} | ||
if (options.clear == null) { | ||
options.clear = "clear"; | ||
} | ||
return obj.registerHooks = (names) => { | ||
var j, len1, name, results, tmp; | ||
var i, j, k, len, len1, len2, name, prop, results, results1, tmp; | ||
names = arrayize(names); | ||
results = []; | ||
for (j = 0, len1 = prefix.length; j < len1; j++) { | ||
prop = prefix[j]; | ||
tmp = obj[prop]; | ||
results.push((function() { | ||
var k, len2, results1; | ||
results1 = []; | ||
for (k = 0, len2 = names.length; k < len2; k++) { | ||
name = names[k]; | ||
results1.push(tmp[name] = getHook(obj)); | ||
} | ||
return results1; | ||
})()); | ||
if (prefix) { | ||
for (i = 0, len = prefix.length; i < len; i++) { | ||
prop = prefix[i]; | ||
obj[prop] = {}; | ||
} | ||
results = []; | ||
for (j = 0, len1 = prefix.length; j < len1; j++) { | ||
prop = prefix[j]; | ||
tmp = obj[prop]; | ||
results.push((function() { | ||
var k, len2, results1; | ||
results1 = []; | ||
for (k = 0, len2 = names.length; k < len2; k++) { | ||
name = names[k]; | ||
results1.push(tmp[name] = getHook(obj, options)); | ||
} | ||
return results1; | ||
})()); | ||
} | ||
return results; | ||
} else { | ||
results1 = []; | ||
for (k = 0, len2 = names.length; k < len2; k++) { | ||
name = names[k]; | ||
results1.push(obj[name] = getHook(obj, options)); | ||
} | ||
return results1; | ||
} | ||
return results; | ||
}; | ||
}; |
{ | ||
"name": "hook-up", | ||
"description": "2d hooks - better than events or simple hooks", | ||
"version": "0.0.1", | ||
"description": "Create your own hook api with 2-dimensional hooks - better than events or simple hooks", | ||
"version": "0.1.0", | ||
"homepage": "https://github.com/paulpflug/", | ||
@@ -24,5 +24,5 @@ "author": { | ||
"devDependencies": { | ||
"coffeescript": "^2.0.0-beta3", | ||
"coffeescript": "^2.0.2", | ||
"script-runner": "^0.1.7", | ||
"mocha": "^3.5.3", | ||
"mocha": "^4.0.1", | ||
"chai": "^4.1.2" | ||
@@ -29,0 +29,0 @@ }, |
# hook-up | ||
2d hooks - better than events or simple hooks | ||
Create your own hook api with 2-dimensional hooks - better than events or simple hooks. | ||
@@ -13,6 +13,5 @@ ### Install | ||
hookUp = require("hook-up") | ||
// hookUp(obj:Object, prefix:String | Array of Strings) | ||
// prefix is optional and defaults to ["before","after"] | ||
// hookUp(obj:Object, options:Object) | ||
someObj = {} | ||
hookUp(someObj, "before") | ||
hookUp(someObj) | ||
// adds a function: | ||
@@ -23,7 +22,7 @@ // registerHooks(names:String | Array of Strings) | ||
// to add a hook | ||
// [prefix].[name].call({prio: (optional) Number, cb: Function}) | ||
// [name].register({prio: (optional) Number, cb: Function}) | ||
// to call all hooks | ||
// [prefix].[name](obj) | ||
// [name].execute(obj) | ||
// to delete all hooks | ||
// [prefix].[name].clear() | ||
// [name].clear() | ||
someArg = {} | ||
@@ -34,5 +33,26 @@ someHook = async (arg) => | ||
// do something async | ||
someObj.start.register(someHook) | ||
someObj.start.register({prio: 2, cb: someHook}) // bigger prio gets called first | ||
// hooks with the same prio get called simultaneously | ||
await someObj.start.execute(someArg) | ||
``` | ||
#### Options | ||
Name | type | default | description | ||
---:| --- | ---| --- | ||
prefix | Array or String | - | to add a namespace for all hooks | ||
register | String | `register` | name of the register prop, can be empty | ||
execute | String | `execute` | name of the execute prop, can be empty | ||
Note, that only one of register or execute can be empty | ||
### Example for custom API | ||
```js | ||
hookUp = require("hook-up") | ||
someObj = {} | ||
hookUp(someObj,{prefix:["before","after"], register: "call", execute:""}) | ||
someObj.registerHooks("start") | ||
// will result in the following api for hooks: | ||
someObj.before.start.call(someHook) | ||
someObj.before.start.call({prio: 2, cb: someHook}) // bigger prio gets called first | ||
// hooks with the same prio get called simultaneously | ||
someObj.after.start.call(someOtherHook) | ||
// to execute | ||
await someObj.before.start(someArg) | ||
@@ -42,4 +62,5 @@ ``` | ||
## License | ||
Copyright (c) 2017 Paul Pflugradt | ||
Licensed under the MIT license. |
5850
131
63