Skip to content

Commit

Permalink
Merge branch 'master' into mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Krueger committed Aug 2, 2017
2 parents 35dee50 + 4f250f1 commit af9d8e8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/emitter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EmitterError } from './error'

export type callback = (input: mixed) => void
export type callback = (input?: mixed) => void

/**
* Microscopic and speedy event emitter.
Expand Down Expand Up @@ -29,16 +29,16 @@ export default class Emitter {
* @throws {EmitterError} throw error when fn isn't a function
*/
on(name: string, fn: callback): void {
if (typeof fn !== 'function') throw new EmitterError('requires function')
if (typeof fn !== 'function') throw new EmitterError('requires callback')
this._event(name).add(fn)
}

/**
* Disassociate a callback from an event name.
* @param {string} name - name of event
* @param {function(input: *)} fn - callback
* @param {function(input: *)} [fn] - callback
*/
off(name: string, fn: callback): void {
off(name: string, fn?: callback): void {
if (!this._events[name]) return
if (fn) this._event(name).delete(fn)
else this._event(name).clear()
Expand All @@ -47,9 +47,9 @@ export default class Emitter {
/**
* Emit an event with the supplied input.
* @param {string} name - name of event
* @param input - input given to the callbacks
* @param {*} [input] - input given to the callbacks
*/
emit(name: string, input: mixed): void {
emit(name: string, input?: mixed): void {
if (!this._events[name]) return
this._event(name).forEach(fn => fn(input))
}
Expand Down
8 changes: 4 additions & 4 deletions src/emitter.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Emitter from '../src/emitter'
import { EmitterError } from '../src/error'
import Emitter from './emitter'
import { EmitterError } from './error'

describe('Emitter', function() {
beforeEach(() => {
Expand All @@ -16,11 +16,11 @@ describe('Emitter', function() {

it('#on should throw an EmitterError if called without a function', () => {
expect(() => this.emitter.on('event')).toThrowError(
new EmitterError('requires function'),
new EmitterError('requires callback'),
)

expect(() => this.emitter.on('event', 100)).toThrowError(
new EmitterError('requires function'),
new EmitterError('requires callback'),
)
})

Expand Down
2 changes: 1 addition & 1 deletion src/error.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EmitterError } from '../src/error'

describe('Emitter', function() {
it('#constructor should call super() and set this.name', () => {
it('#constructor should call super(message) and set this.name', () => {
const err = new EmitterError('honk')
expect(err.message).toBe('honk')
})
Expand Down

0 comments on commit af9d8e8

Please sign in to comment.