-
Notifications
You must be signed in to change notification settings - Fork 456
/
microevent.coffee
128 lines (103 loc) · 2.97 KB
/
microevent.coffee
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
# Tests for event framework
assert = require 'assert'
MicroEvent = require '../../lib/client/microevent'
makePassPart = require('../helpers').makePassPart
tests = ->
it 'does nothing when you emit an event with no listeners', ->
@e.emit 'a'
@e.emit 'a', 1, 2, 3
@e.emit 'b', 1, 2, 3
it 'fires the event listener', (done) ->
@e.on 'foo', -> done()
@e.emit 'foo'
it 'does not fire a removed event listener', ->
fn = -> throw new Error 'event listener fired'
@e.on 'foo', fn
@e.removeListener 'foo', fn
@e.emit 'foo'
it 'passes arguments to event listeners', (done) ->
@e.on 'foo', (a, b, c) ->
assert.strictEqual a, 1
assert.strictEqual b, 2
assert.strictEqual c, 3
done()
@e.emit 'foo', 1, 2, 3
it 'fires multiple event listeners', (done) ->
passPart = makePassPart 2, done
@e.on 'foo', passPart
@e.on 'foo', passPart
@e.emit 'foo'
it 'does nothing when you remove a missing event listener', (done) ->
@e.removeListener 'foo', ->
@e.emit 'foo' # Does nothing.
@e.on 'foo', -> done()
@e.removeListener 'foo', ->
@e.emit 'foo'
it 'removes an event listener while handling an event', (done) ->
passPart = makePassPart 3, done
fn = -> passPart()
@e.on 'foo', fn
@e.on 'foo', =>
@e.removeListener 'foo', fn
passPart()
@e.on 'foo', passPart
@e.emit 'foo'
it 'will fire an event if you remove it and add it back', (done) ->
fn = -> done()
@e.on 'foo', fn
@e.removeListener 'foo', fn
@e.on 'foo', fn
@e.emit 'foo'
it 'fires an event listener that was removed from a different event', (done) ->
fn = -> done()
@e.on 'foo', fn
@e.on 'bar', fn
@e.removeListener 'foo', fn
@e.emit 'bar'
it 'fires a listener registered with once', (done) ->
@e.once 'foo', (x, y) ->
assert.strictEqual x, 1
assert.strictEqual y, 2
done()
@e.emit 'foo', 1, 2
it 'only fires a listener registered with once once', ->
calls = 0
@e.once 'foo', -> calls++
@e.emit 'foo'
@e.emit 'foo'
assert.strictEqual calls, 1
it 'calls listeners in the proper context', (done) ->
passPart = makePassPart 2, done
e = @e
@e.once 'foo', ->
assert.strictEqual this, e
passPart()
@e.on 'foo', ->
assert.strictEqual this, e
passPart()
@e.emit 'foo'
it 'is ok with emitting before anything is registered', ->
@e.emit 'blah'
# The tests above are run both with a new MicroEvent and with an object with
# microevent mixed in.
describe 'raw', ->
beforeEach ->
@e = new MicroEvent
tests()
describe 'mixinObj', ->
beforeEach ->
@e = {}
MicroEvent.mixin @e
tests()
describe 'mixinClass', ->
beforeEach ->
class Foo
bar: ->
MicroEvent.mixin Foo
@e = new Foo
tests()
# Test that the same behaviour holds with nodejs's event emitters.
describe 'eventEmitter', ->
beforeEach ->
@e = new (require 'events').EventEmitter
tests()