-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
180 lines (166 loc) · 4.83 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*!
* compose-emitter <https://github.com/tunnckoCore/compose-emitter>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var extend = require('extend-shallow')
var AppBase = require('app-base').AppBase
var isObject = require('is-extendable')
/**
* > Initialize ComposeEmitter with `options`. Pass your EventEmitter
* instance and optionally pass `options.context` to bind to listeners.
*
* **Example**
*
* ```js
* var ComposeEmitter = require('compose-emitter').ComposeEmitter
* var Emitter = require('component-emitter')
*
* var ee = new ComposeEmitter({
* emitter: new Emitter()
* })
*
* ee
* .compose('on')('foo', console.log) // => 1, 2, 3
* .compose('emit')('foo', 1, 2, 3)
* ```
*
* @param {Object} `options` Pass event emitter instance to `options.emitter`.
* @api public
*/
function ComposeEmitter (options) {
if (!(this instanceof ComposeEmitter)) {
return new ComposeEmitter(options)
}
AppBase.call(this)
this.options = isObject(options)
? options
: isObject(this.options) ? this.options : {}
}
/**
* > Extend your application with ComposeEmitter static and prototype methods.
* See [static-extend][] or [tunnckoCore/app-base](https://github.com/tunnckoCore/app-base)
* for more info and what's static and prototype methods are added.
*
* **Example**
*
* ```js
* var ComposeEmitter = require('compose-emitter').ComposeEmitter
* var Emitter = require('eventemitter3')
*
* function App (options) {
* if (!(this instanceof App)) {
* return new App(options)
* }
* ComposeEmitter.call(this, options)
* }
*
* ComposeEmitter.extend(App)
*
* App.prototype.on = function on (name, fn, context) {
* return this.compose('on')(name, fn, context)
* }
*
* App.prototype.once = function once (name, fn, context) {
* return this.compose('once')(name, fn, context)
* }
*
* App.prototype.off = function off (name, fn, context) {
* return this.compose('off')(name, fn, context)
* }
*
* App.prototype.emit = function emit () {
* return this.compose('emit').apply(null, arguments)
* }
*
* var app = new App({
* context: {foo: 'bar'},
* emitter: new Emitter()
* })
*
* app
* .on('foo', function (a) {
* console.log('foo:', a, this) // => 123, {foo: 'bar'}
* })
* .once('bar', function (b) {
* console.log('bar:', b) // => 456
* })
* .emit('foo', 123)
* .emit('bar', 456)
* .emit('bar', 789)
* ```
*
* @name ComposeEmitter.extend
* @param {Function} `Parent` The constructor to extend, using [static-extend][].
* @api public
*/
AppBase.extend(ComposeEmitter)
/**
* > Compose different `type` of emitter methods. You can use this to create
* the usual `.on`, `.emit` and other methods. Pass as `type` name of the method
* that your emitter have and optional `options` to pass context for the listeners.
*
* **Example**
*
* ```js
* var emitter = require('compose-emitter')
* var Emitter = require('eventemitter3')
*
* var on = emitter.compose('on', {
* context: {a: 'b'},
* emitter: new Emitter()
* })
* var emit = emitter.compose('emit')
*
* on('foo', function (a, b) {
* console.log('foo:', a, b, this) // => 1, 2, {a: 'b', c: 'd'}
* }, {c: 'd'})
*
* emit('foo', 1, 2)
* ```
*
* @name .compose
* @param {String} `type` Name of the emitter method that you want to mirror.
* @param {Object} `options` Optionally pass `context` that will be bind to listeners.
* @return {Function} Method that accept as many arguments as you want or emitter method need.
* @api public
*/
AppBase.define(ComposeEmitter.prototype, 'compose', function compose (type, options) {
if (typeof type !== 'string') {
throw new TypeError('.compose: expect `type` be string')
}
// @todo
var opts = this.options
var ctx = options ? extend({}, opts.context, options.context) : opts.context
this.options = options ? extend({}, opts, options) : opts
this.options.context = ctx
if (!isObject(this.options.emitter)) {
throw new TypeError('.compose: expect `options.emitter` be extendable object')
}
this.define('emitter', this.options.emitter)
var self = this
return function onOffEmitOnce (name, fn, context) {
if (type !== 'on' && type !== 'once' && type !== 'off') {
self.emitter[type].apply(self.emitter, arguments)
return self
}
self.options.context = context ? extend({}, self.options.context, context) : self.options.context
self.options.context = isObject(self.options.context) ? self.options.context : self
self.emitter[type](name, fn, self.options.context)
return self
}
})
/**
* Expose `ComposeEmitter` instance
* @type {ComposeEmitter}
* @private
*/
module.exports = new ComposeEmitter()
/**
* Expose `ComposeEmitter` constructor
* @type {ComposeEmitter}
* @private
*/
module.exports.ComposeEmitter = ComposeEmitter