Skip to content

Commit 0d1418e

Browse files
authored
fix: Provide correct return value from source.emit() (#17)
This rebases the work from #8 with minor modifications, and tests the return values directly (rather than adding a wide-bracket integration test). Adopts arrow functions in the test file. This was identified as the root cause of nock/nock#1485. Thanks to @rbrtribeiro for the fix! Close #8 Close #9
1 parent 7815925 commit 0d1418e

File tree

2 files changed

+78
-47
lines changed

2 files changed

+78
-47
lines changed

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ function propagate(events, source, dest) {
1515
return explicitPropagate(events, source, dest)
1616
}
1717

18+
const shouldPropagate = eventName =>
19+
events === undefined || events.includes(eventName)
20+
1821
const oldEmit = source.emit
1922

20-
source.emit = function(eventType) {
21-
oldEmit.apply(source, arguments)
23+
// Returns true if the event had listeners, false otherwise.
24+
// https://nodejs.org/api/events.html#events_emitter_emit_eventname_args
25+
source.emit = (eventName, ...args) => {
26+
const oldEmitHadListeners = oldEmit.call(source, eventName, ...args)
2227

23-
if (!events || ~events.indexOf(eventType)) {
24-
dest.emit.apply(dest, arguments)
28+
let destEmitHadListeners = false
29+
if (shouldPropagate(eventName)) {
30+
destEmitHadListeners = dest.emit(eventName, ...args)
2531
}
32+
33+
return oldEmitHadListeners || destEmitHadListeners
2634
}
2735

2836
function end() {

tests/index.js

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,64 @@ const { test } = require('tap')
44
const { EventEmitter } = require('events')
55
const propagate = require('..')
66

7-
test('propagates events', function(t) {
8-
t.plan(12)
7+
test('propagates events', t => {
8+
t.plan(16)
99
const ee1 = new EventEmitter()
1010
const ee2 = new EventEmitter()
1111
propagate(ee1, ee2)
1212

13-
ee2.on('event-1', function(a, b, c) {
13+
ee2.on('event-1', (a, b, c) => {
1414
t.equal(a, 'a')
1515
t.equal(b, 'b')
1616
t.equal(c, undefined)
1717
})
1818

19-
ee2.on('event-2', function(a, b, c) {
19+
ee2.on('event-2', (a, b, c) => {
2020
t.equal(a, 'c')
2121
t.equal(b, 'd')
2222
t.equal(c, undefined)
2323
})
2424

25-
ee1.emit('event-1', 'a', 'b')
26-
ee1.emit('event-1', 'a', 'b')
27-
ee1.emit('event-2', 'c', 'd')
28-
ee1.emit('event-2', 'c', 'd')
25+
t.true(ee1.emit('event-1', 'a', 'b'))
26+
t.true(ee1.emit('event-1', 'a', 'b'))
27+
t.true(ee1.emit('event-2', 'c', 'd'))
28+
t.true(ee1.emit('event-2', 'c', 'd'))
2929
})
3030

31-
test('propagates can end', function(t) {
32-
t.plan(1)
31+
test('propagates can end', t => {
32+
t.plan(3)
3333

3434
const ee1 = new EventEmitter()
3535
const ee2 = new EventEmitter()
3636
const prop = propagate(ee1, ee2)
3737

38-
ee2.on('event', function() {
38+
ee2.on('event', () => {
3939
t.ok('true', 'propagated')
4040
})
4141

42-
ee1.emit('event')
42+
t.true(ee1.emit('event'))
4343
prop.end()
44-
ee1.emit('event')
44+
t.false(ee1.emit('event'))
4545
})
4646

47-
test('after propagation old one still emits', function(t) {
48-
t.plan(2)
47+
test('after propagation old one still emits', t => {
48+
t.plan(4)
4949

5050
const ee1 = new EventEmitter()
5151
const ee2 = new EventEmitter()
5252
const prop = propagate(ee1, ee2)
5353

54-
ee1.on('event', function() {
54+
ee1.on('event', () => {
5555
t.ok('true', 'propagated')
5656
})
5757

58-
ee1.emit('event')
58+
t.true(ee1.emit('event'))
5959
prop.end()
60-
ee1.emit('event')
60+
t.true(ee1.emit('event'))
6161
})
6262

63-
test('emit on source before destination', function(t) {
64-
t.plan(1)
63+
test('emit on source before destination', t => {
64+
t.plan(2)
6565

6666
const source = new EventEmitter()
6767
const dest = new EventEmitter()
@@ -70,47 +70,47 @@ test('emit on source before destination', function(t) {
7070
// `count` should have been incremented by handler on source when handler on dest is invoked
7171
let count = 0
7272
propagate(source, dest)
73-
source.on('event', function() {
73+
source.on('event', () => {
7474
count++
7575
})
76-
dest.on('event', function() {
76+
dest.on('event', () => {
7777
t.equal(count, 1, 'emit on source first')
7878
})
7979

8080
// Emit the events for assertion
81-
source.emit('event')
81+
t.true(source.emit('event'))
8282
})
8383

84-
test('is able to propagate only certain events', function(t) {
85-
t.plan(2)
84+
test('is able to propagate only certain events', t => {
85+
t.plan(6)
8686
const ee1 = new EventEmitter()
8787
const ee2 = new EventEmitter()
8888
// propagate only event-1 and event-2, leaving out
8989
const p = propagate(['event-1', 'event-2'], ee1, ee2)
9090

91-
ee2.on('event-1', function() {
91+
ee2.on('event-1', () => {
9292
t.ok(true, 'event 1 received')
9393
})
9494

95-
ee2.on('event-2', function(a, b, c) {
95+
ee2.on('event-2', (a, b, c) => {
9696
t.ok(true, 'event 2 received')
9797
})
9898

99-
ee2.on('event-3', function(a, b, c) {
100-
t.ok(false, 'event 3 should not have been received')
99+
ee2.on('event-3', (a, b, c) => {
100+
t.fail('event 3 should not have been received')
101101
})
102102

103-
ee1.emit('event-1')
104-
ee1.emit('event-2')
105-
ee1.emit('event-3')
103+
t.true(ee1.emit('event-1'))
104+
t.true(ee1.emit('event-2'))
105+
t.false(ee1.emit('event-3'))
106106

107107
p.end()
108108

109-
ee1.emit('event-1')
109+
t.false(ee1.emit('event-1'))
110110
})
111111

112-
test('is able to propagate and map certain events', function(t) {
113-
t.plan(2)
112+
test('is able to propagate and map certain events', t => {
113+
t.plan(6)
114114
const ee1 = new EventEmitter()
115115
const ee2 = new EventEmitter()
116116
// propagate only event-1 and event-2, leaving out
@@ -123,23 +123,46 @@ test('is able to propagate and map certain events', function(t) {
123123
ee2
124124
)
125125

126-
ee2.on('other-event-1', function() {
126+
ee2.on('other-event-1', () => {
127127
t.ok(true, 'event 1 received')
128128
})
129129

130-
ee2.on('other-event-2', function(a, b, c) {
130+
ee2.on('other-event-2', (a, b, c) => {
131131
t.ok(true, 'event 2 received')
132132
})
133133

134-
ee2.on('event-3', function(a, b, c) {
135-
t.ok(false, 'event 3 should not have been received')
134+
ee2.on('event-3', (a, b, c) => {
135+
t.fail('event 3 should not have been received')
136+
})
137+
138+
t.true(ee1.emit('event-1'))
139+
t.true(ee1.emit('event-2'))
140+
t.false(ee1.emit('event-3'))
141+
142+
p.end()
143+
144+
t.false(ee1.emit('event-1'))
145+
})
146+
147+
test('is able to propagate a single event', t => {
148+
t.plan(5)
149+
const ee1 = new EventEmitter()
150+
const ee2 = new EventEmitter()
151+
const p = propagate('event-1', ee1, ee2)
152+
153+
ee2.on('event-1', () => {
154+
t.ok(true, 'event 1 received')
155+
})
156+
157+
ee2.on('event-2', (a, b, c) => {
158+
t.fail('event 3 should not have been received')
136159
})
137160

138-
ee1.emit('event-1')
139-
ee1.emit('event-2')
140-
ee1.emit('event-3')
161+
t.true(ee1.emit('event-1'))
162+
t.false(ee1.emit('event-2'))
141163

142164
p.end()
143165

144-
ee1.emit('event-1')
166+
t.false(ee1.emit('event-1'))
167+
t.false(ee1.emit('event-2'))
145168
})

0 commit comments

Comments
 (0)