Skip to content

Commit 31127ac

Browse files
Node 4 compatibility
1 parent 235d635 commit 31127ac

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

EventProxy.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ Object.assign(EventProxy.prototype, {
4949
this._initEventProxy(eventName)
5050
return this
5151
},
52-
prependListener: function (eventName, listener) {
53-
EventEmitter.prototype.prependListener.call(this, eventName, listener)
54-
this._initEventProxy(eventName)
55-
return this
56-
},
5752
removeListener: function (eventName, listener) {
5853
EventEmitter.prototype.removeListener.call(this, eventName, listener)
5954
this._exitEventProxy(eventName)
@@ -76,18 +71,29 @@ Object.assign(EventProxy.prototype, {
7671
}
7772
return this.addListener(eventName, wrapper)
7873
},
79-
prependOnceListener: function (eventName, listener) {
80-
var self = this
81-
var wrapper = function (payload) {
82-
listener(payload)
83-
self.removeListener(eventName, wrapper)
84-
}
85-
return this.prependListener(eventName, wrapper)
86-
},
8774
on: function (eventName, listener) {
8875
return this.addListener(eventName, listener)
8976
},
9077
off: function (eventName, listener) {
9178
return this.removeListener(eventName, listener)
9279
}
9380
})
81+
82+
// Node 4 compatibility
83+
if (EventEmitter.prototype.prependListener) {
84+
Object.assign(EventProxy.prototype, {
85+
prependListener: function (eventName, listener) {
86+
EventEmitter.prototype.prependListener.call(this, eventName, listener)
87+
this._initEventProxy(eventName)
88+
return this
89+
},
90+
prependOnceListener: function (eventName, listener) {
91+
var self = this
92+
var wrapper = function (payload) {
93+
listener(payload)
94+
self.removeListener(eventName, wrapper)
95+
}
96+
return this.prependListener(eventName, wrapper)
97+
}
98+
})
99+
}

EventProxy.test.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,30 @@ tape('once', function (t) {
3636
t.end()
3737
})
3838

39-
tape('prepend once', function (t) {
40-
var target = new events.EventEmitter()
41-
var proxy = new EventProxy(target)
42-
var count = 0
43-
target.addListener('hello', function () {
44-
count += 1
45-
})
46-
proxy.addListener('hello', function () {
47-
count += 2
48-
})
49-
var addResult = proxy.prependOnceListener('hello', function (data) {
50-
t.equals(count, 1, 'event called once, before the listeners on proxy, after the listeners on target')
51-
t.equals(data, 'world', 'data passed through')
52-
count += 1
39+
// Node 4 compatibility
40+
if (events.EventEmitter.prototype.prependOnceListener) {
41+
tape('prepend once', function (t) {
42+
var target = new events.EventEmitter()
43+
var proxy = new EventProxy(target)
44+
var count = 0
45+
target.addListener('hello', function () {
46+
count += 1
47+
})
48+
proxy.addListener('hello', function () {
49+
count += 2
50+
})
51+
var addResult = proxy.prependOnceListener('hello', function (data) {
52+
t.equals(count, 1, 'event called once, before the listeners on proxy, after the listeners on target')
53+
t.equals(data, 'world', 'data passed through')
54+
count += 1
55+
})
56+
t.equals(addResult, proxy, 'Result of prepend once is proxy instance')
57+
target.emit('hello', 'world')
58+
target.emit('hello', 'mundo')
59+
t.equals(count, 7)
60+
t.end()
5361
})
54-
t.equals(addResult, proxy, 'Result of prepend once is proxy instance')
55-
target.emit('hello', 'world')
56-
target.emit('hello', 'mundo')
57-
t.equals(count, 7)
58-
t.end()
59-
})
62+
}
6063

6164
tape('remove all listeners', function (t) {
6265
var target = new events.EventEmitter()

0 commit comments

Comments
 (0)