Skip to content

Commit

Permalink
[update] remove kernel event in this layer
Browse files Browse the repository at this point in the history
  • Loading branch information
toxic-johann committed Apr 24, 2018
1 parent e459a35 commit 23f9640
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
6 changes: 3 additions & 3 deletions __tests__/index.js
Expand Up @@ -78,14 +78,11 @@ describe('chimee-kernel index.js', () => {
});

test('seek without number', () => {
const fn = jest.fn();
kernel.on('error', fn);
expect(kernel.videoElement.currentTime).toBe(0);
kernel.load();
expect(kernel.videoElement.currentTime).toBe(0);
kernel.seek(true);
expect(Log.data.error[0]).toEqual([ 'chimee-kernel', 'When you try to seek, you must offer us a number, but not boolean' ]);
expect(fn).toHaveBeenCalledTimes(1);
expect(kernel.videoElement.currentTime).toBe(0);
});

Expand Down Expand Up @@ -153,6 +150,9 @@ describe('chimee-kernel index.js', () => {
kernel.on('error', fn);
kernel.videoKernel.emit('error', 'test');
expect(fn).toHaveBeenCalledTimes(1);
kernel.off('error', fn);
kernel.videoKernel.emit('error', 'test');
expect(fn).toHaveBeenCalledTimes(1);
});

test('getMp4Kernel, provide nothing and got native', () => {
Expand Down
40 changes: 13 additions & 27 deletions src/index.js
@@ -1,17 +1,16 @@
// @flow
import { Log, CustEvent, isNumber, deepAssign, isString, isFunction, isElement } from 'chimee-helper';
import { Log, isNumber, deepAssign, isString, isFunction, isElement } from 'chimee-helper';
import NativeVideoKernel from './native/index';
import defaultConfig from './config';

const LOG_TAG = 'chimee-kernel';
const kernelEvents = [ 'mediaInfo', 'heartbeat', 'error' ];
const boxSuffixMap = {
flv: '.flv',
hls: '.m3u8',
mp4: '.mp4',
};

export default class ChimeeKernel extends CustEvent {
export default class ChimeeKernel {
box: string;
boxConfig: Object;
config: KernelConfig;
Expand All @@ -26,18 +25,15 @@ export default class ChimeeKernel extends CustEvent {
* @class kernel
*/
constructor(videoElement: HTMLVideoElement, config: KernelConfig) {
super();
if (!isElement(videoElement)) throw new Error('You must pass in an video element to the chimee-kernel');
// copy and maintain only one config for chimee-kernel
// actually kernel is disposable in most situation nowaday
this.config = deepAssign({}, defaultConfig, config);
this.videoElement = videoElement;
this.initVideoKernel();
this.bindEvents(this.videoKernel);
}

destroy() {
this.bindEvents(this.videoKernel, true);
this.videoKernel.destroy();
}

Expand Down Expand Up @@ -96,31 +92,12 @@ export default class ChimeeKernel extends CustEvent {
const supportMp4Kernel = hasLegalMp4Kernel && mp4Kernel.isSupport();
// $FlowFixMe: we have make sure it's an kernel now
if (supportMp4Kernel) return mp4Kernel;
if (hasLegalMp4Kernel) this.warnLog('mp4 decode is not support in this browser, we will switch to the native video kernel');
if (hasLegalMp4Kernel) Log.warn(LOG_TAG, 'mp4 decode is not support in this browser, we will switch to the native video kernel');
this.box = 'native';
// $FlowFixMe: it's the same as videoKernel
return NativeVideoKernel;
}

errorLog(...args: Array<string>) {
this.emit('error', new Error(args[0]));
return Log.error(LOG_TAG, ...args);
}

warnLog(...args: Array<string>) {
return Log.warn(LOG_TAG, ...args);
}

bindEvents(videoKernel: VideoKernel, remove: boolean = false) {
kernelEvents.forEach(eventName => {
/* istanbul ignore next */
// $FlowFixMe: we have make sure it's legal now
videoKernel[remove ? 'off' : 'on'](eventName, ({ data } = {}) => {
this.emit(eventName, data);
});
});
}

attachMedia() {
this.videoKernel.attachMedia();
}
Expand Down Expand Up @@ -155,12 +132,21 @@ export default class ChimeeKernel extends CustEvent {

seek(seconds: number) {
if (!isNumber(seconds)) {
this.errorLog(`When you try to seek, you must offer us a number, but not ${typeof seconds}`);
Log.error(LOG_TAG, `When you try to seek, you must offer us a number, but not ${typeof seconds}`);
return;
}
this.videoKernel.seek(seconds);
}

refresh() {
this.videoKernel.refresh();
}

on(key: string, fn: Function) {
this.videoKernel.on(key, fn);
}

off(key: string, fn: Function) {
this.videoKernel.off(key, fn);
}
}

0 comments on commit 23f9640

Please sign in to comment.