Skip to content

Commit

Permalink
[update] add event options on emit and emitSync
Browse files Browse the repository at this point in the history
  • Loading branch information
toxic-johann committed Apr 24, 2018
1 parent c5eab63 commit 93d479a
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 15 deletions.
78 changes: 74 additions & 4 deletions __tests__/chimee/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,80 @@ describe('chimee event method', () => {
expect(() => player.emitSync('click')).not.toThrow();
});

test('emit', async () => {
expect(() => player.emit(1)).toThrow();
await expect(() => player.emit('click')).not.toThrow();
await expect(() => player.emit('play')).not.toThrow();
describe('emnit', () => {
let fn;
beforeEach(() => {
fn = jest.fn();
});

test('emitSync', async () => {
expect(() => player.emitSync(1)).toThrow();
expect(() => player.emitSync('click')).not.toThrow();
expect(() => player.emitSync('play')).not.toThrow();
});

test('normal emitSync test', async () => {
player.on('hello', fn);
player.emitSync('hello', 1);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).lastCalledWith(1);
});

test('emitSync with target', async () => {
const fn1 = jest.fn();
player.on('hello', fn1, { target: 'kernel' });
player.on('hello', fn);
player.emitSync('hello', 1);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).lastCalledWith(1);
expect(fn1).toHaveBeenCalledTimes(0);
player.emitSync({
name: 'hello',
target: 'kernel',
}, 2);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).lastCalledWith(1);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn1).lastCalledWith(2);
});
});

describe('emnit', () => {
let fn;
beforeEach(() => {
fn = jest.fn();
});

test('emit', async () => {
expect(() => player.emit(1)).toThrow();
await expect(() => player.emit('click')).not.toThrow();
await expect(() => player.emit('play')).not.toThrow();
});

test('normal emit test', async () => {
player.on('hello', fn);
await player.emit('hello', 1);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).lastCalledWith(1);
});

test('emit with target', async () => {
const fn1 = jest.fn();
player.on('hello', fn1, { target: 'kernel' });
player.on('hello', fn);
await player.emit('hello', 1);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).lastCalledWith(1);
expect(fn1).toHaveBeenCalledTimes(0);
await player.emit({
name: 'hello',
target: 'kernel',
}, 2);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).lastCalledWith(1);
expect(fn1).toHaveBeenCalledTimes(1);
expect(fn1).lastCalledWith(2);
});
});

test('once', () => {
Expand Down
39 changes: 38 additions & 1 deletion doc/zh-cn/api/chimee-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,18 @@ chimee 作为 video 的映射,自然也是可以监听 video 上的事件。
- fn
- 类型:`Function`
- 含义:处理函数
- options
- 类型:`Object`
- 含义:可以穿入一些事件相关的属性
- 备注:可选参数
- target
- 类型:'kernel' | 'container' | 'wrapper' | 'video' | 'video-dom' | 'plugin' | 'esFullscreen'
- 含义:标明事件需要绑定的目标对象。
- 默认:会根据事件名智能判断
- stage
- 类型:'before' | 'after' | 'main' | '_'
- 含义:标明事件监听的阶段
- 默认:会根据事件名智能判断
> 利用 on 可以直接监听任何发生在 video 上的事件。
>
Expand All @@ -800,6 +812,18 @@ chimee 作为 video 的映射,自然也是可以监听 video 上的事件。
- fn
- 类型:`Function`
- 含义:处理函数
- options
- 类型:`Object`
- 含义:可以穿入一些事件相关的属性
- 备注:可选参数
- target
- 类型:'kernel' | 'container' | 'wrapper' | 'video' | 'video-dom' | 'plugin' | 'esFullscreen'
- 含义:标明事件需要绑定的目标对象。
- 默认:会根据事件名智能判断
- stage
- 类型:'before' | 'after' | 'main' | '_'
- 含义:标明事件监听的阶段
- 默认:会根据事件名智能判断
### once
Expand All @@ -811,14 +835,27 @@ chimee 作为 video 的映射,自然也是可以监听 video 上的事件。
- fn
- 类型:`Function`
- 含义:处理函数
- options
- 类型:`Object`
- 含义:可以穿入一些事件相关的属性
- 备注:可选参数
- target
- 类型:'kernel' | 'container' | 'wrapper' | 'video' | 'video-dom' | 'plugin' | 'esFullscreen'
- 含义:标明事件需要绑定的目标对象。
- 默认:会根据事件名智能判断
- stage
- 类型:'before' | 'after' | 'main' | '_'
- 含义:标明事件监听的阶段
- 默认:会根据事件名智能判断
### emit
- 含义:触发一次由异步函数处理的事件
- 参数:
- key
- 类型:`string`
- 类型:`string | { name: string, target: string }`
- 含义:事件名称
- 备注:target 为 'kernel' | 'container' | 'wrapper' | 'video' | 'video-dom' | 'plugin' | 'esFullscreen'
- 其余自定义参数
一般用于触发如 play, pause 等行为,和直接调用`play``pause`等方法一致。也可以利用此和插件进行沟通。
Expand Down
4 changes: 2 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function(config) {

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: [ 'mocha' ],
frameworks: [ 'mocha', 'chai' ],

// list of files / patterns to load in the browser
files: [
Expand All @@ -45,7 +45,7 @@ module.exports = function(config) {

rollupPreprocessor: {
output: {
format: 'umd', // Helps prevent naming collisions.
format: 'iife', // Helps prevent naming collisions.
name: camelize(name), // Required for 'iife' format.
},
plugins: rollupConfig.plugins,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"postmerge": "sh tool/auto-npm-install.sh",
"test": "npm run karma && jest --coverage && node env-check.js",
"unit": "jest --coverage --watch",
"lint": "eslint . --fix && flow",
"flow": "flow",
"lint": "eslint . --fix && flow check",
"flow": "flow check",
"start": "rollup -c build/rollup.config.dev.js -w",
"build": "npm run b-common && npm run b-es && npm run b-umd && npm run b-min",
"b-common": "rollup -c build/rollup.config.common.js",
Expand Down Expand Up @@ -79,6 +79,7 @@
"husky": "^0.14.3",
"jest": "^22.4.3",
"karma": "^2.0.2",
"karma-chai": "^0.1.0",
"karma-firefox-launcher": "^1.1.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
Expand Down
2 changes: 2 additions & 0 deletions src/dispatcher/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export default class Dom {
this.__dispatcher = dispatcher;
if (!isElement(wrapper) && !isString(wrapper)) throw new TypeError(`Wrapper can only be string or HTMLElement, but not ${typeof wrapper}`);
const $wrapper = $(wrapper);
// TODO: we have to decalre length for wrapper
// $FlowFixMe: we have to decalre length here
if ($wrapper.length === 0) {
throw new TypeError('Can not get dom node accroding wrapper. Please check your wrapper');
}
Expand Down
4 changes: 2 additions & 2 deletions src/dispatcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class Dispatcher {
* @param {Object|string} option you can just set a plugin name or plugin config
* @return {Promise}
*/
use(option: string | PluginOption) {
use(option: string | PluginOption): Promise<*> {
if (isString(option)) option = { name: option, alias: undefined };
if (!isObject(option) || (isObject(option) && !isString(option.name))) {
throw new TypeError('pluginConfig do not match requirement');
Expand Down Expand Up @@ -464,7 +464,7 @@ export default class Dispatcher {
* @param {Array<UserPluginConfig>} configs a set of plugin config
* @return {Array<Promise>} a set of Promise indicate the plugin install stage
*/
_initUserPlugin(configs: Array<string | PluginOption> = []) {
_initUserPlugin(configs: Array<string | PluginOption> = []): Promise<*>[] {
if (!isArray(configs)) {
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') Log.warn('Dispatcher', `UserConfig.plugin can only by an Array, but not "${configs}" in ${typeof configs}`);
Expand Down
22 changes: 20 additions & 2 deletions src/dispatcher/video-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,15 @@ export default @autobindClass() class VideoWrapper {
* @param {...args} args
*/
@alias('emit')
$emit(key: string, ...args: any) {
$emit(key: string | {
target: binderTarget,
name: string,
}, ...args: any) {
let target: binderTarget | void;
if (isObject(key) && isString(key.name) && isString(key.target)) {
target = key.target;
key = key.name;
}
if (!isString(key)) throw new TypeError('emit key parameter must be String');
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production' && domEvents.indexOf(key.replace(/^\w_/, '')) > -1) {
Expand All @@ -245,6 +253,7 @@ export default @autobindClass() class VideoWrapper {
this.__dispatcher.binder.emit({
name: key,
id: this.__id,
target,
}, ...args);
}

Expand All @@ -254,11 +263,20 @@ export default @autobindClass() class VideoWrapper {
* @param {...args} args
*/
@alias('emitSync')
$emitSync(key: string, ...args: any) {
$emitSync(key: string | {
target: binderTarget,
name: string,
}, ...args: any) {
let target;
if (isObject(key) && isString(key.name) && isString(key.target)) {
target = key.target;
key = key.name;
}
if (!isString(key)) throw new TypeError('emitSync key parameter must be String');
return this.__dispatcher.binder.emitSync({
name: key,
id: this.__id,
target,
}, ...args);
}

Expand Down
3 changes: 1 addition & 2 deletions tests/switch-kernel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import Chimee from '../src/index';
import chai from 'chai';
const { expect } = chai;
describe('check for chimee switch kernel function', () => {
it('should not trigger volume change', async function() {
this.timeout(10000);
Expand All @@ -22,6 +20,7 @@ describe('check for chimee switch kernel function', () => {
} catch (error) {
console.error(error);
}
console.warn(expect);
expect(count).to.equal(0);
});
});

0 comments on commit 93d479a

Please sign in to comment.