Skip to content

Commit 35e4e2a

Browse files
authored
Merge pull request #191 from livingdocsIO/keyboard-chaining
Eventable: listen for multiple events
2 parents 594dc21 + e3cae37 commit 35e4e2a

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

spec/eventable.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,45 @@ describe('eventable', function () {
8383
this.obj.notify('publish', 'success')
8484
expect(called).toEqual(1)
8585
})
86+
87+
it('accepts multiple whitespace separated event names', function () {
88+
let called = 0
89+
this.obj.on('publish unpublish', () => {
90+
called++
91+
})
92+
93+
this.obj.notify('publish')
94+
this.obj.notify('unpublish')
95+
this.obj.notify('foo') // should do nothing
96+
expect(called).toEqual(2)
97+
})
98+
99+
it('accepts an object to register multiple events', function () {
100+
let published = 0
101+
let unpublished = 0
102+
103+
this.obj.on({
104+
publish: () => { published++ },
105+
unpublish: () => { unpublished++ }
106+
})
107+
108+
this.obj.notify('publish')
109+
this.obj.notify('unpublish')
110+
expect(published).toEqual(1)
111+
expect(unpublished).toEqual(1)
112+
})
113+
114+
it('accepts multiple event names in object form', function () {
115+
let called = 0
116+
117+
this.obj.on({
118+
'publish unpublish': () => { called++ }
119+
})
120+
121+
this.obj.notify('publish')
122+
this.obj.notify('unpublish')
123+
expect(called).toEqual(2)
124+
})
86125
})
87126

88127
describe('off()', function () {

src/eventable.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ export default function eventable (obj, notifyContext) {
3535
function getEventableModule (notifyContext) {
3636
let listeners = {}
3737

38-
function addListener (event, listener) {
39-
listeners[event] = listeners[event] || []
40-
listeners[event].unshift(listener)
38+
function addListener (events, listener) {
39+
events.split(' ').forEach(event => {
40+
listeners[event] = listeners[event] || []
41+
listeners[event].unshift(listener)
42+
})
4143
}
4244

4345
function removeListener (event, listener) {

0 commit comments

Comments
 (0)