-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robin Pokorny
committed
Jul 7, 2019
1 parent
ea9fef2
commit fc7e797
Showing
6 changed files
with
173 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
const pubsub = require('./pubsub') | ||
const pubsub = require("./pubsub"); | ||
|
||
module.exports = () => { | ||
const events = new Map() | ||
const events = new Map(); | ||
|
||
const on = (name, fn) => { | ||
if (!events.has(name)) events.set(name, pubsub()) | ||
if (!events.has(name)) events.set(name, pubsub()); | ||
|
||
return events.get(name).sub(fn) | ||
} | ||
return events.get(name).sub(fn); | ||
}; | ||
|
||
const emit = (name, data) => | ||
events.has(name) && events.get(name).pub(data) | ||
const emit = (name, data) => events.has(name) && events.get(name).pub(data); | ||
|
||
return Object.freeze({ on, emit }) | ||
} | ||
return Object.freeze({ on, emit }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,64 @@ | ||
/* eslint-env jest */ | ||
const eventEmitter = require('./eventEmitter') | ||
const eventEmitter = require("./eventEmitter"); | ||
|
||
describe('Event Emitter', () => { | ||
let emitter | ||
const callback1 = jest.fn() | ||
const callback2 = jest.fn() | ||
describe("Event Emitter", () => { | ||
let emitter; | ||
const callback1 = jest.fn(); | ||
const callback2 = jest.fn(); | ||
|
||
beforeEach(() => { | ||
emitter = eventEmitter() | ||
callback1.mockClear() | ||
callback2.mockClear() | ||
}) | ||
|
||
it('has simple API', () => { | ||
const keys = Object.keys(emitter).sort().join() | ||
expect(keys).toBe('emit,on') | ||
}) | ||
|
||
it('passes data to registered callbacks', () => { | ||
emitter.on('event', callback1) | ||
emitter.on('click', callback2) | ||
|
||
emitter.emit('event', 'foo') | ||
emitter.emit('click', 'bar') | ||
|
||
expect(callback1).toHaveBeenCalledTimes(1) | ||
expect(callback1).toHaveBeenCalledWith('foo') | ||
expect(callback2).toHaveBeenCalledTimes(1) | ||
expect(callback2).toHaveBeenCalledWith('bar') | ||
}) | ||
|
||
it('removes callback when it is unsubscibed', () => { | ||
const unSub = emitter.on('event', callback1) | ||
unSub() | ||
emitter.emit('event', 'foo') | ||
|
||
expect(callback1).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('does not call a callback twice', () => { | ||
emitter.on('event', callback1) | ||
emitter.on('event', callback1) | ||
emitter.emit('event', 'foo') | ||
|
||
expect(callback1).toHaveBeenCalledTimes(1) | ||
expect(callback1).toHaveBeenCalledWith('foo') | ||
}) | ||
|
||
it('does not leak on sucessfull emit', () => { | ||
emitter.on('event', callback1) | ||
const result = emitter.emit('event', 'foo') | ||
|
||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('returns false on empty emit', () => { | ||
const result = emitter.emit('event', 'foo') | ||
|
||
expect(result).toBe(false) | ||
}) | ||
}) | ||
emitter = eventEmitter(); | ||
callback1.mockClear(); | ||
callback2.mockClear(); | ||
}); | ||
|
||
it("has simple API", () => { | ||
const keys = Object.keys(emitter) | ||
.sort() | ||
.join(); | ||
expect(keys).toBe("emit,on"); | ||
}); | ||
|
||
it("passes data to registered callbacks", () => { | ||
emitter.on("event", callback1); | ||
emitter.on("click", callback2); | ||
|
||
emitter.emit("event", "foo"); | ||
emitter.emit("click", "bar"); | ||
|
||
expect(callback1).toHaveBeenCalledTimes(1); | ||
expect(callback1).toHaveBeenCalledWith("foo"); | ||
expect(callback2).toHaveBeenCalledTimes(1); | ||
expect(callback2).toHaveBeenCalledWith("bar"); | ||
}); | ||
|
||
it("removes callback when it is unsubscibed", () => { | ||
const unSub = emitter.on("event", callback1); | ||
unSub(); | ||
emitter.emit("event", "foo"); | ||
|
||
expect(callback1).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("does not call a callback twice", () => { | ||
emitter.on("event", callback1); | ||
emitter.on("event", callback1); | ||
emitter.emit("event", "foo"); | ||
|
||
expect(callback1).toHaveBeenCalledTimes(1); | ||
expect(callback1).toHaveBeenCalledWith("foo"); | ||
}); | ||
|
||
it("does not leak on sucessfull emit", () => { | ||
emitter.on("event", callback1); | ||
const result = emitter.emit("event", "foo"); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it("returns false on empty emit", () => { | ||
const result = emitter.emit("event", "foo"); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const pubsub = require('./pubsub') | ||
const eventEmitter = require('./eventEmitter') | ||
const pubsub = require("./pubsub"); | ||
const eventEmitter = require("./eventEmitter"); | ||
|
||
module.exports = { pubsub, eventEmitter } | ||
module.exports = { pubsub, eventEmitter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
module.exports = () => { | ||
const subscribers = new Set() | ||
const subscribers = new Set(); | ||
|
||
const sub = (fn) => { | ||
subscribers.add(fn) | ||
const sub = fn => { | ||
subscribers.add(fn); | ||
|
||
return () => subscribers.delete(fn) | ||
} | ||
return () => subscribers.delete(fn); | ||
}; | ||
|
||
const pub = (data) => subscribers.forEach((fn) => fn(data)) | ||
const pub = data => subscribers.forEach(fn => fn(data)); | ||
|
||
return Object.freeze({ pub, sub }) | ||
} | ||
return Object.freeze({ pub, sub }); | ||
}; |
Oops, something went wrong.