mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Add documentation and tests of EventEmitter implementation
This commit is contained in:
parent
b0349af21d
commit
152c816b5c
2 changed files with 74 additions and 0 deletions
|
@ -1,4 +1,11 @@
|
|||
define [], () ->
|
||||
# Simple event emitter implementation, but has a slightly unusual API for
|
||||
# removing specific listeners. If a specific listener needs to be removed
|
||||
# (instead of all listeners), then it needs to use a "namespace":
|
||||
# Create a listener on the foo event with bar namespace: .on 'foo.bar'
|
||||
# Trigger all events for the foo event (including namespaces): .trigger 'foo'
|
||||
# Remove all listeners for the foo event (including namespaces): .off 'foo'
|
||||
# Remove a listener for the foo event with the bar namespace: .off 'foo.bar'
|
||||
class EventEmitter
|
||||
on: (event, callback) ->
|
||||
@events ||= {}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
define ['utils/EventEmitter'], (EventEmitter) ->
|
||||
describe 'EventEmitter', () ->
|
||||
beforeEach () ->
|
||||
@eventEmitter = new EventEmitter
|
||||
|
||||
it 'calls listeners', () ->
|
||||
cb1 = sinon.stub()
|
||||
cb2 = sinon.stub()
|
||||
@eventEmitter.on 'foo', cb1
|
||||
@eventEmitter.on 'bar', cb2
|
||||
|
||||
@eventEmitter.trigger 'foo'
|
||||
|
||||
expect(cb1).to.have.been.called
|
||||
expect(cb2).to.not.have.been.called
|
||||
|
||||
it 'calls multiple listeners', () ->
|
||||
cb1 = sinon.stub()
|
||||
cb2 = sinon.stub()
|
||||
@eventEmitter.on 'foo', cb1
|
||||
@eventEmitter.on 'foo', cb2
|
||||
|
||||
@eventEmitter.trigger 'foo'
|
||||
|
||||
expect(cb1).to.have.been.called
|
||||
expect(cb2).to.have.been.called
|
||||
|
||||
it 'calls listeners with namespace', () ->
|
||||
cb1 = sinon.stub()
|
||||
cb2 = sinon.stub()
|
||||
@eventEmitter.on 'foo', cb1
|
||||
@eventEmitter.on 'foo.bar', cb2
|
||||
|
||||
@eventEmitter.trigger 'foo'
|
||||
|
||||
expect(cb1).to.have.been.called
|
||||
expect(cb2).to.have.been.called
|
||||
|
||||
it 'removes listeners', () ->
|
||||
cb = sinon.stub()
|
||||
@eventEmitter.on 'foo', cb
|
||||
@eventEmitter.off 'foo'
|
||||
|
||||
@eventEmitter.trigger 'foo'
|
||||
|
||||
expect(cb).to.not.have.been.called
|
||||
|
||||
it 'removes namespaced listeners', () ->
|
||||
cb = sinon.stub()
|
||||
@eventEmitter.on 'foo.bar', cb
|
||||
@eventEmitter.off 'foo.bar'
|
||||
|
||||
@eventEmitter.trigger 'foo'
|
||||
|
||||
expect(cb).to.not.have.been.called
|
||||
|
||||
it 'does not remove unnamespaced listeners if off called with namespace', () ->
|
||||
cb1 = sinon.stub()
|
||||
cb2 = sinon.stub()
|
||||
@eventEmitter.on 'foo', cb1
|
||||
@eventEmitter.on 'foo.bar', cb2
|
||||
@eventEmitter.off 'foo.bar'
|
||||
|
||||
@eventEmitter.trigger 'foo'
|
||||
|
||||
expect(cb1).to.have.been.called
|
||||
expect(cb2).to.not.have.been.called
|
Loading…
Reference in a new issue