Skip to content

Commit

Permalink
refactor: combine implementation of adding/removing hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jan 16, 2018
1 parent fbfd9ad commit 9d76b12
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 103 deletions.
24 changes: 8 additions & 16 deletions index.js
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
}
13 changes: 13 additions & 0 deletions lib/add.js
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)
}
14 changes: 0 additions & 14 deletions lib/after.js

This file was deleted.

14 changes: 0 additions & 14 deletions lib/before.js

This file was deleted.

14 changes: 0 additions & 14 deletions lib/error.js

This file was deleted.

7 changes: 7 additions & 0 deletions lib/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function register (state, name, options, method) {
}

var hooks = state.registry[name]

if (!hooks) {
return invokeMethod(options, method)
}
Expand All @@ -29,12 +30,17 @@ function register (state, name, options, method) {
var errorHooks = hooks.error
var afterHooks = hooks.after

// 1. run "before hooks" which may mutate options
return Promise.all(beforeHooks.map(invokeBeforeHook.bind(null, options)))

// 2. Once all finish without error, call the method with the (mutated) options
.then(function () {
return method(options)
})

// 3. If an error occurs in 1. or 2. run the "error hooks" which may mutate
// the error object. If one of them does not return an error then set the
// result to that. Otherwise throw (mutated) error.
.catch(function (error) {
return Promise.all(errorHooks.map(invokeErrorHook.bind(null, error, options)))

Expand All @@ -49,6 +55,7 @@ function register (state, name, options, method) {
})
})

// 4. Run the "after hooks". They may mutate the result
.then(function (result) {
return Promise.all(afterHooks.map(invokeAfterHook.bind(null, result, options)))

Expand Down
15 changes: 0 additions & 15 deletions lib/remove-after.js

This file was deleted.

15 changes: 0 additions & 15 deletions lib/remove-before.js

This file was deleted.

15 changes: 0 additions & 15 deletions lib/remove-error.js

This file was deleted.

15 changes: 15 additions & 0 deletions lib/remove.js
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)
}

0 comments on commit 9d76b12

Please sign in to comment.