-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: combine implementation of adding/removing hooks
- Loading branch information
Showing
10 changed files
with
43 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,22 @@ | ||
module.exports = Hook | ||
|
||
var register = require('./lib/register') | ||
var before = require('./lib/before') | ||
var error = require('./lib/error') | ||
var after = require('./lib/after') | ||
var removeBefore = require('./lib/remove-before') | ||
var removeError = require('./lib/remove-error') | ||
var removeAfter = require('./lib/remove-after') | ||
var addHook = require('./lib/add') | ||
var removeHook = require('./lib/remove') | ||
|
||
function Hook () { | ||
var state = { | ||
registry: {} | ||
} | ||
|
||
var hook = register.bind(null, state) | ||
hook.api = {} | ||
hook.remove = {} | ||
hook.api = {remove: {}} | ||
|
||
hook.api.before = hook.before = before.bind(null, state) | ||
hook.api.error = hook.error = error.bind(null, state) | ||
hook.api.after = hook.after = after.bind(null, state) | ||
|
||
hook.api.remove = hook.remove = { | ||
before: removeBefore.bind(null, state), | ||
error: removeError.bind(null, state), | ||
after: removeAfter.bind(null, state) | ||
} | ||
;['before', 'error', 'after'].forEach(function (kind) { | ||
hook[kind] = hook.api[kind] = addHook.bind(null, state, kind) | ||
hook.remove[kind] = hook.api.remove[kind] = removeHook.bind(null, state, kind) | ||
}) | ||
|
||
return hook | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = addHook | ||
|
||
function addHook (state, kind, name, hook) { | ||
if (!state.registry[name]) { | ||
state.registry[name] = { | ||
before: [], | ||
error: [], | ||
after: [] | ||
} | ||
} | ||
|
||
state.registry[name][kind][kind === 'before' ? 'unshift' : 'push'](hook) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = removeHook | ||
|
||
function removeHook (state, kind, name, method) { | ||
if (!state.registry[name]) { | ||
return | ||
} | ||
|
||
var index = state.registry[name][kind].indexOf(method) | ||
|
||
if (index === -1) { | ||
return | ||
} | ||
|
||
state.registry[name][kind].splice(index, 1) | ||
} |