Skip to content

Commit

Permalink
fix: fix bug caused by autoload switch kernel
Browse files Browse the repository at this point in the history
related to #133
  • Loading branch information
toxic-johann committed Jun 19, 2018
1 parent 8ec4062 commit 44b5549
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
21 changes: 21 additions & 0 deletions __tests__/situation/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,25 @@ describe('load', () => {
expect(player.__dispatcher.kernel).not.toBe(oldKernel);
expect(player.src).toBe('');
});

test('should not trigger switch kernel at the first auto load', () => {
const fn = jest.fn();
const plugin = {
name: 'test',
events: {
play() {
fn();
},
},
};
Chimee.install(plugin);
const player = new Chimee({
wrapper: document.createElement('div'),
src: 'http://cdn.toxicjohann.com/lostStar.mp4',
plugins: [ plugin.name ],
});
player.$video.dispatchEvent(new Event('play'));
expect(fn).toHaveBeenCalledTimes(1);
player.destroy();
});
});
4 changes: 4 additions & 0 deletions demo/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const player = new Chimee({
controls: true,
// noDefaultContextMenu: true,
noDefaultContextMenu: 'wrapper',
muted: true,
});
// [ 'touchstart', 'touchmove', 'touchend' ].forEach(key => {
// player.$on(key, evt => console.log(evt, key));
Expand All @@ -81,4 +82,7 @@ const player = new Chimee({
// player.$on('mouseenter', event => console.log(event), { target: 'container' });
// player.$on('mouseenter', event => console.log(event, 'mouseenter'));

player.$on('play', evt => console.warn(evt));
player.$video.addEventListener('play', evt => console.error(evt));
player.play();
window.player = player;
25 changes: 17 additions & 8 deletions src/dispatcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,14 @@ export default class Dispatcher {
isLive?: boolean,
box?: string,
preset?: Object,
kernels?: UserKernelsConfig
kernels?: UserKernelsConfig,
isFirst?: boolean,
}, option: {
isLive?: boolean,
box?: string,
preset?: Object,
kernels?: UserKernelsConfig
kernels?: UserKernelsConfig,
isFirst?: boolean,
} = {}) {
const src: string = isString(srcOrOption)
? srcOrOption
Expand All @@ -416,12 +418,13 @@ export default class Dispatcher {
box = videoConfig.box,
preset = videoConfig.preset,
kernels = videoConfig.kernels,
isFirst,
} = option;
if (box !== 'native' || box !== oldBox || !isEmpty(option)) {
const video = document.createElement('video');
const config = { isLive, box, preset, src, kernels };
const kernel = this._createKernel(video, config);
this.switchKernel({ video, kernel, config });
this.switchKernel({ video, kernel, config, notifyChange: isFirst });
}
const originAutoLoad = this.videoConfig.autoload;
this._changeUnwatchable(this.videoConfig, 'autoload', false);
Expand All @@ -430,7 +433,7 @@ export default class Dispatcher {
this._changeUnwatchable(this.videoConfig, 'autoload', originAutoLoad);
}

switchKernel({ video, kernel, config }: {
switchKernel({ video, kernel, config, notifyChange }: {
video: HTMLVideoElement,
kernel: ChimeeKernel,
config: {
Expand All @@ -439,7 +442,8 @@ export default class Dispatcher {
box: string,
kernels: UserKernelsConfig,
preset: Object,
}
},
notifyChange?: boolean,
}) {
const oldKernel = this.kernel;
const originVideoConfig = deepClone(this.videoConfig);
Expand All @@ -466,9 +470,14 @@ export default class Dispatcher {
oldKernel.destroy();
// delay video event binding
// so that people can't feel the default value change
setTimeout(() => {
// unless it's caused by autoload
if (notifyChange) {
this.binder && this.binder.bindEventOnVideo && this.binder.bindEventOnVideo(video);
});
} else {
setTimeout(() => {
this.binder && this.binder.bindEventOnVideo && this.binder.bindEventOnVideo(video);
});
}
}

/**
Expand Down Expand Up @@ -548,7 +557,7 @@ export default class Dispatcher {
name: 'load',
target: 'plugin',
id: 'dispatcher',
}, this.videoConfig.src);
}, { src: this.videoConfig.src, isFirst: true });
}
}
_changeUnwatchable(object: Object, property: string, value: any) {
Expand Down

0 comments on commit 44b5549

Please sign in to comment.