-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert setMaxListener()
- Loading branch information
Showing
6 changed files
with
97 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import test from 'ava' | ||
|
||
import { createEvent, TestEmitter, createEventAction, errorEvent } from './index' | ||
|
||
test(`error thrown in listener is thrown`, t => { | ||
const emitter = new TestEmitter() | ||
const count = createEvent<number>('count') | ||
|
||
emitter.on(count, () => { throw new Error('thrown') }) | ||
|
||
const err = t.throws(() => emitter.emit(count(1, undefined))) | ||
t.is(err.message, 'thrown') | ||
}) | ||
|
||
test('error thrown in listener is thrown for event action', t => { | ||
const emitter = new TestEmitter() | ||
const count = createEventAction<number, number>('count', input => emit => emit(input + 1)) | ||
|
||
emitter.on(count, () => { throw new Error('thrown') }) | ||
|
||
const err = t.throws(() => count(emitter, 1, undefined)) | ||
t.is(err.message, 'thrown') | ||
}) | ||
|
||
test(`error thrown in listener is thrown for string event`, t => { | ||
const emitter = new TestEmitter() | ||
const count = createEvent<number>('count') | ||
|
||
emitter.on(count.type, () => { throw new Error('thrown') }) | ||
|
||
const err = t.throws(() => emitter.emit(count(1, undefined))) | ||
t.is(err.message, 'thrown') | ||
}) | ||
|
||
test('error thrown in listener is thrown for event action', t => { | ||
const emitter = new TestEmitter() | ||
const count = createEventAction<number, number>('count', input => emit => emit(input + 1)) | ||
|
||
emitter.on(count, () => { throw new Error('thrown') }) | ||
|
||
const err = t.throws(() => count(emitter, 1, undefined)) | ||
t.is(err.message, 'thrown') | ||
}) | ||
|
||
test('error thrown in listener is thrown for error event', t => { | ||
const emitter = new TestEmitter() | ||
|
||
emitter.on(errorEvent, () => { | ||
throw new Error('thrown') | ||
}) | ||
|
||
const err = t.throws(() => emitter.emit(errorEvent(new Error('abc'), undefined))) | ||
t.is(err.message, 'thrown') | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Emitter } from './Emitter'; | ||
import { TypedEvent } from './createEvent'; | ||
import { EventSubscription } from 'fbemitter'; | ||
import { errorEvent } from './errorEvent'; | ||
|
||
/** | ||
* Emitter used for testing. | ||
* The real emitter will capture the error thrown in listener and send it to console.error. | ||
* It is good for main code but not good for testing. | ||
*/ | ||
export class TestEmitter extends Emitter { | ||
addListener<Payload, Meta>( | ||
event: TypedEvent<Payload, Meta> | string, | ||
listener: (payload: Payload, meta: Meta, error: boolean) => void): EventSubscription { | ||
const type = typeof event === 'string' ? event : event.type | ||
if (type === errorEvent.type) | ||
return this.addErrorEventListener(listener) | ||
|
||
return this.emitter.addListener(type, listener) | ||
} | ||
|
||
protected addErrorEventListener<Payload, Meta>(listener: (payload: Payload, meta: Meta, error: boolean) => void): EventSubscription { | ||
return this.emitter.addListener(errorEvent.type, listener) | ||
} | ||
} |
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