-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (70 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*!
* dush-no-chaining <https://github.com/tunnckoCore/dush-no-chaining>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
/**
* > A plugin that removes support for emitter methods to be chainable. Basically, by
* default [dush][]'s methods (and most of other eventemitters) are chainable
* and some user don't like that feature that I need.
*
* **Example**
*
* ```js
* const dush = require('dush')
* const noChaining = require('dush-no-chaining')
*
* const app = dush()
*
* // by default they are chainable
* app
* .on('foo', () => {})
* .once('bar', () => {})
* .emit('bar', 123)
* .off('foo')
* .emit('bar', 333)
* .emit('foo', 1)
*
* // but when add this plugin
* // they are not chainable
* const emitter = dush().use(noChaining())
*
* const noop = () => {}
* const on = emitter.on('foo', noop)
* console.log(on) // => undefined
*
* const off = emitter.off('foo', noop)
* console.log(off) // => undefined
*
* const once = emitter.once('bar', noop)
* console.log(once) // => undefined
*
* const emit = emitter.emit('bar', 123)
* console.log(emit) // => undefined
* ```
*
* @name noChaining
* @return {Function} A plugin function that can be passed to [dush][], [minibase][] or [base][]
* @api public
*/
module.exports = function dushNoChaining () {
return function noChaining (app) {
var map = {
on: app.on,
off: app.off,
once: app.once,
emit: app.emit
}
Object.keys(map).forEach(function (name) {
app[name] = factory(map[name])
})
return app
}
}
function factory (fn) {
return function factory_ () {
fn.apply(null, arguments)
}
}