Skip to content

Commit

Permalink
fix: overrided emit -> emitAsync in eventEmitter module
Browse files Browse the repository at this point in the history
  • Loading branch information
BrooklynKing committed Mar 4, 2019
1 parent 0230e8e commit ff9290f
Show file tree
Hide file tree
Showing 45 changed files with 302 additions and 254 deletions.
2 changes: 1 addition & 1 deletion src/adapters/dash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default class DashAdapter implements IPlaybackAdapter {
}

private _logError(error: string, errorEvent: any) {
this.eventEmitter.emit(VideoEvent.ERROR, {
this.eventEmitter.emitAsync(VideoEvent.ERROR, {
errorType: error,
streamType: MediaStreamType.DASH,
streamProvider: 'dash.js',
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/hls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default class HlsAdapter implements IPlaybackAdapter {
}

private _logError(error: string, errorEvent: any) {
this.eventEmitter.emit(VideoEvent.ERROR, {
this.eventEmitter.emitAsync(VideoEvent.ERROR, {
errorType: error,
streamType: MediaStreamType.HLS,
streamProvider: 'hls.js',
Expand Down Expand Up @@ -291,7 +291,7 @@ export default class HlsAdapter implements IPlaybackAdapter {
if (this._isDynamicContent) {
this._isDynamicContentEnded = true;

this.eventEmitter.emit(VideoEvent.DYNAMIC_CONTENT_ENDED);
this.eventEmitter.emitAsync(VideoEvent.DYNAMIC_CONTENT_ENDED);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/anomaly-bloodhound/anomaly-bloodhound.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('AnomalyBloodhound', () => {
it('should be based on event', async function() {
const spy = sinon.spy(anomalyBloodhound, '_processStateChange');
anomalyBloodhound._bindEvents();
await eventEmitter.emit(VideoEvent.STATE_CHANGED, {});
await eventEmitter.emitAsync(VideoEvent.STATE_CHANGED, {});
expect(spy.called).to.be.true;
anomalyBloodhound._processStateChange.restore();
});
Expand Down
3 changes: 1 addition & 2 deletions src/modules/event-emitter/event-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ class EventEmitterModule extends EventEmitter implements IEventEmitter {
};
}

//@ts-ignore
//Now emit fire events only at the end of current macrotask, as part as next microtask
emit(event: string | symbol, ...args: any[]): Promise<boolean> | void {
emitAsync(event: string | symbol, ...args: any[]): Promise<boolean> | void {
//Handle IE11
if (!isPromiseAvailable) {
if (setImmediate) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/event-emitter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface IEventMap extends Array<any> {
interface IEventEmitter {
on(event: string, fn: ListenerFn, context?: any): this;
off(event: string, fn?: ListenerFn, context?: any, once?: boolean): this;
emit(event: string | symbol, ...args: any[]): Promise<boolean> | void;
emitAsync(event: string | symbol, ...args: any[]): Promise<boolean> | void;
bindEvents(eventsMap: IEventMap[], defaultFnContext?: any): () => void;
destroy(): void;
}
Expand Down
16 changes: 8 additions & 8 deletions src/modules/full-screen-manager/full-screen-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('FullScreenManager', () => {

describe('due to reaction on fullscreen change', () => {
it('should trigger proper event', () => {
const spy: sinon.SinonSpy = sinon.spy(eventEmitter, 'emit');
const spy: sinon.SinonSpy = sinon.spy(eventEmitter, 'emitAsync');

mockedFullscreenHelper.isInFullScreen = true;
fullScreenManager._onChange();
Expand All @@ -157,7 +157,7 @@ describe('FullScreenManager', () => {
),
).to.be.true;

eventEmitter.emit.restore();
eventEmitter.emitAsync.restore();
});

it('should pause video on exit from full screen if proper config passed', () => {
Expand All @@ -176,11 +176,11 @@ describe('FullScreenManager', () => {
it('should enter full screen if proper config passed', async function() {
const spy = sinon.spy(fullScreenManager, 'enterFullScreen');

await eventEmitter.emit(VideoEvent.PLAY_REQUEST);
await eventEmitter.emitAsync(VideoEvent.PLAY_REQUEST);

fullScreenManager._enterFullScreenOnPlay = true;
mockedFullscreenHelper.isInFullScreen = false;
await eventEmitter.emit(VideoEvent.PLAY_REQUEST);
await eventEmitter.emitAsync(VideoEvent.PLAY_REQUEST);
expect(spy.calledOnce).to.be.true;

fullScreenManager.enterFullScreen.restore();
Expand All @@ -192,14 +192,14 @@ describe('FullScreenManager', () => {
it('should exit full screen if config passed', async function() {
const spy = sinon.spy(fullScreenManager, 'exitFullScreen');

await eventEmitter.emit(VideoEvent.STATE_CHANGED, {
await eventEmitter.emitAsync(VideoEvent.STATE_CHANGED, {
nextState: EngineState.ENDED,
});

fullScreenManager._exitFullScreenOnEnd = true;
mockedFullscreenHelper.isInFullScreen = true;

await eventEmitter.emit(VideoEvent.STATE_CHANGED, {
await eventEmitter.emitAsync(VideoEvent.STATE_CHANGED, {
nextState: EngineState.ENDED,
});
expect(spy.calledOnce).to.be.true;
Expand All @@ -212,14 +212,14 @@ describe('FullScreenManager', () => {
it('should exit full screen if config passed', async function() {
const spy = sinon.spy(fullScreenManager, 'exitFullScreen');

await eventEmitter.emit(VideoEvent.STATE_CHANGED, {
await eventEmitter.emitAsync(VideoEvent.STATE_CHANGED, {
nextState: EngineState.PAUSED,
});

fullScreenManager._exitFullScreenOnPause = true;
mockedFullscreenHelper.isInFullScreen = true;

await eventEmitter.emit(VideoEvent.STATE_CHANGED, {
await eventEmitter.emitAsync(VideoEvent.STATE_CHANGED, {
nextState: EngineState.PAUSED,
});
expect(spy.calledOnce).to.be.true;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/full-screen-manager/full-screen-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class FullScreenManager implements IFullScreenManager {
if (!this._helper.isInFullScreen && this._pauseVideoOnFullScreenExit) {
this._engine.pause();
}
this._eventEmitter.emit(
this._eventEmitter.emitAsync(
UIEvent.FULL_SCREEN_STATE_CHANGED,
this._helper.isInFullScreen,
);
Expand Down
67 changes: 41 additions & 26 deletions src/modules/keyboard-control/keyboard-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,31 @@ describe('KeyboardControl', () => {

describe('as a reaction on press of key', () => {
beforeEach(() => {
sinon.spy(eventEmitter, 'emit');
sinon.spy(eventEmitter, 'emitAsync');
delete keyDownEvent.keyCode;
});

afterEach(() => {
eventEmitter.emit.restore();
eventEmitter.emitAsync.restore();
keyboardControl.destroy();
});

it('should do stuff if key was TAB', () => {
keyDownEvent.keyCode = KEYCODES.TAB;
rootContainer.getElement().dispatchEvent(keyDownEvent);

expect(eventEmitter.emit.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED))
.to.be.true;
expect(
eventEmitter.emitAsync.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED),
).to.be.true;
});

it('should do stuff if key was TAB', () => {
keyDownEvent.keyCode = KEYCODES.TAB;
rootContainer.getElement().dispatchEvent(keyDownEvent);

expect(eventEmitter.emit.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED))
.to.be.true;
expect(
eventEmitter.emitAsync.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED),
).to.be.true;
});

it('should do stuff if key was SPACE_BAR', () => {
Expand All @@ -73,10 +75,13 @@ describe('KeyboardControl', () => {
rootContainer.getElement().dispatchEvent(keyDownEvent);

expect(keyDownEvent.preventDefault.called).to.be.true;
expect(eventEmitter.emit.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED))
.to.be.true;
expect(
eventEmitter.emit.calledWith(UIEvent.TOGGLE_PLAYBACK_WITH_KEYBOARD),
eventEmitter.emitAsync.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED),
).to.be.true;
expect(
eventEmitter.emitAsync.calledWith(
UIEvent.TOGGLE_PLAYBACK_WITH_KEYBOARD,
),
).to.be.true;
expect(engine.togglePlayback.called).to.be.true;

Expand All @@ -89,10 +94,12 @@ describe('KeyboardControl', () => {
rootContainer.getElement().dispatchEvent(keyDownEvent);

expect(keyDownEvent.preventDefault.called).to.be.true;
expect(eventEmitter.emit.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED))
.to.be.true;
expect(eventEmitter.emit.calledWith(UIEvent.GO_BACKWARD_WITH_KEYBOARD)).to
.be.true;
expect(
eventEmitter.emitAsync.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED),
).to.be.true;
expect(
eventEmitter.emitAsync.calledWith(UIEvent.GO_BACKWARD_WITH_KEYBOARD),
).to.be.true;
expect(engine.seekBackward.calledWith(AMOUNT_TO_SKIP_SECONDS)).to.be.true;

engine.seekBackward.restore();
Expand All @@ -104,10 +111,12 @@ describe('KeyboardControl', () => {
rootContainer.getElement().dispatchEvent(keyDownEvent);

expect(keyDownEvent.preventDefault.called).to.be.true;
expect(eventEmitter.emit.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED))
.to.be.true;
expect(eventEmitter.emit.calledWith(UIEvent.GO_FORWARD_WITH_KEYBOARD)).to
.be.true;
expect(
eventEmitter.emitAsync.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED),
).to.be.true;
expect(
eventEmitter.emitAsync.calledWith(UIEvent.GO_FORWARD_WITH_KEYBOARD),
).to.be.true;
expect(engine.seekForward.calledWith(AMOUNT_TO_SKIP_SECONDS)).to.be.true;

engine.seekForward.restore();
Expand All @@ -119,10 +128,13 @@ describe('KeyboardControl', () => {
rootContainer.getElement().dispatchEvent(keyDownEvent);

expect(keyDownEvent.preventDefault.called).to.be.true;
expect(eventEmitter.emit.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED))
.to.be.true;
expect(
eventEmitter.emit.calledWith(UIEvent.INCREASE_VOLUME_WITH_KEYBOARD),
eventEmitter.emitAsync.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED),
).to.be.true;
expect(
eventEmitter.emitAsync.calledWith(
UIEvent.INCREASE_VOLUME_WITH_KEYBOARD,
),
).to.be.true;
expect(engine.increaseVolume.calledWith(AMOUNT_TO_CHANGE_VOLUME)).to.be
.true;
Expand All @@ -136,10 +148,13 @@ describe('KeyboardControl', () => {
rootContainer.getElement().dispatchEvent(keyDownEvent);

expect(keyDownEvent.preventDefault.called).to.be.true;
expect(eventEmitter.emit.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED))
.to.be.true;
expect(
eventEmitter.emit.calledWith(UIEvent.DECREASE_VOLUME_WITH_KEYBOARD),
eventEmitter.emitAsync.calledWith(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED),
).to.be.true;
expect(
eventEmitter.emitAsync.calledWith(
UIEvent.DECREASE_VOLUME_WITH_KEYBOARD,
),
).to.be.true;
expect(engine.decreaseVolume.calledWith(AMOUNT_TO_CHANGE_VOLUME)).to.be
.true;
Expand All @@ -150,14 +165,14 @@ describe('KeyboardControl', () => {

describe('after destroy', () => {
it('should not react on key down', () => {
sinon.spy(eventEmitter, 'emit');
sinon.spy(eventEmitter, 'emitAsync');
keyboardControl.destroy();

keyDownEvent.keyCode = KEYCODES.TAB;
rootContainer.getElement().dispatchEvent(keyDownEvent);

expect(eventEmitter.emit.called).to.be.false;
eventEmitter.emit.restore();
expect(eventEmitter.emitAsync.called).to.be.false;
eventEmitter.emitAsync.restore();
});
});
});
22 changes: 11 additions & 11 deletions src/modules/keyboard-control/keyboard-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,41 @@ export default class KeyboardControl implements IKeyboardControl {
private _attachDefaultControls() {
this._keyboardInterceptor.addCallbacks({
[KEYCODES.TAB]: () => {
this._eventEmitter.emit(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emitAsync(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
},
[KEYCODES.SPACE_BAR]: e => {
e.preventDefault();
this._eventEmitter.emit(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emit(UIEvent.TOGGLE_PLAYBACK_WITH_KEYBOARD);
this._eventEmitter.emitAsync(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emitAsync(UIEvent.TOGGLE_PLAYBACK_WITH_KEYBOARD);
this._engine.togglePlayback();
},
[KEYCODES.LEFT_ARROW]: e => {
if (this._engine.isSeekAvailable) {
e.preventDefault();
this._eventEmitter.emit(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emit(UIEvent.GO_BACKWARD_WITH_KEYBOARD);
this._eventEmitter.emitAsync(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emitAsync(UIEvent.GO_BACKWARD_WITH_KEYBOARD);
this._engine.seekBackward(AMOUNT_TO_SKIP_SECONDS);
}
},
[KEYCODES.RIGHT_ARROW]: e => {
if (this._engine.isSeekAvailable) {
e.preventDefault();
this._eventEmitter.emit(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emit(UIEvent.GO_FORWARD_WITH_KEYBOARD);
this._eventEmitter.emitAsync(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emitAsync(UIEvent.GO_FORWARD_WITH_KEYBOARD);
this._engine.seekForward(AMOUNT_TO_SKIP_SECONDS);
}
},
[KEYCODES.UP_ARROW]: e => {
e.preventDefault();
this._eventEmitter.emit(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emit(UIEvent.INCREASE_VOLUME_WITH_KEYBOARD);
this._eventEmitter.emitAsync(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emitAsync(UIEvent.INCREASE_VOLUME_WITH_KEYBOARD);
this._engine.setMute(false);
this._engine.increaseVolume(AMOUNT_TO_CHANGE_VOLUME);
},
[KEYCODES.DOWN_ARROW]: e => {
e.preventDefault();
this._eventEmitter.emit(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emit(UIEvent.DECREASE_VOLUME_WITH_KEYBOARD);
this._eventEmitter.emitAsync(UIEvent.KEYBOARD_KEYDOWN_INTERCEPTED);
this._eventEmitter.emitAsync(UIEvent.DECREASE_VOLUME_WITH_KEYBOARD);
this._engine.setMute(false);
this._engine.decreaseVolume(AMOUNT_TO_CHANGE_VOLUME);
},
Expand Down
4 changes: 2 additions & 2 deletions src/modules/picture-in-picture/picture-in-picture.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ describe('PictureInPicture', () => {

describe('due to reaction on fullscreen change', () => {
it('should trigger proper event', () => {
const spy: sinon.SinonSpy = sinon.spy(eventEmitter, 'emit');
const spy: sinon.SinonSpy = sinon.spy(eventEmitter, 'emitAsync');

mockedPictureInPictureHelper.isInPictureInPicture = true;
pictureInPicture._onChange();
expect(spy.calledWith(UIEvent.PICTURE_IN_PICTURE_STATUS_CHANGE)).to.be
.true;

eventEmitter.emit.restore();
eventEmitter.emitAsync.restore();
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/modules/picture-in-picture/picture-in-picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PictureInPicture implements IPictureInPicture {
}

private _onChange() {
this._eventEmitter.emit(
this._eventEmitter.emitAsync(
UIEvent.PICTURE_IN_PICTURE_STATUS_CHANGE,
this.isInPictureInPicture,
);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/playback-engine/adapters-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'jsdom-global/register';
import { expect } from 'chai';

import * as sinon from 'sinon';
import { EventEmitter } from 'eventemitter3';
import EventEmitter from '../../modules/event-emitter/event-emitter';

import getNativeAdapterCreator from './adapters/native';
import AdapterStrategy from './adapters-strategy';
Expand Down
2 changes: 1 addition & 1 deletion src/modules/playback-engine/adapters-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class AdaptersStrategy {
if (typeof mediaSource === 'string') {
const type = getStreamType(mediaSource);
if (!type) {
this._eventEmitter.emit(VideoEvent.ERROR, {
this._eventEmitter.emitAsync(VideoEvent.ERROR, {
errorType: Error.SRC_PARSE,
streamSrc: mediaSource,
});
Expand Down
2 changes: 1 addition & 1 deletion src/modules/playback-engine/adapters/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default function getNativeAdapterCreator(
}

private _logError(error: string, errorEvent: MediaError) {
this.eventEmitter.emit(VideoEvent.ERROR, {
this.eventEmitter.emitAsync(VideoEvent.ERROR, {
errorType: error,
streamType,
streamProvider: 'native',
Expand Down
Loading

0 comments on commit ff9290f

Please sign in to comment.