本文整理汇总了TypeScript中src/observer/Observable.Observable.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Observable.on方法的具体用法?TypeScript Observable.on怎么用?TypeScript Observable.on使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类src/observer/Observable.Observable
的用法示例。
在下文中一共展示了Observable.on方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('Should be able to unregister all listeners to a specific event', (): void => {
let count1: number = 0;
let count2: number = 0;
const listener1: Function = (): void => {
count1 += 1;
};
const listener2: Function = (): void => {
count2 += 1;
};
o.on('change', listener1);
o.on('change', listener2);
expect(count1).toBe(0);
expect(count2).toBe(0);
o.trigger('change');
expect(count1).toBe(1);
expect(count2).toBe(1);
o.off('change');
o.trigger('change');
expect(count1).toBe(1);
expect(count2).toBe(1);
});
示例2: Observable
export const watcher: IWatcher = ((): IWatcher => {
const observable: Observable = new Observable();
let count: number = 0;
let invoked: IWatchableRegistry = {};
return {
/**
* Register a key.
*
* @param {string} key
* @returns {IWatchable}
*/
register(key: string): IWatchable {
count += 1;
return {
identifier: `observable-${count}-${key}`,
key
};
},
/**
* @param {IWatchable} watchable
*/
invoke(watchable: IWatchable): void {
invoked[watchable.identifier] = watchable;
},
/**
* Trigger a change.
*
* @param {IWatchable} watchable
*/
trigger(watchable: IWatchable): void {
observable.trigger(`change:${watchable.identifier}`);
},
/**
* @param {IWatchable} watchable
* @param {Function} callback
*/
listen(watchable: IWatchable, callback: Function): void {
observable.on(`change:${watchable.identifier}`, callback);
},
/**
* @param {Function} callback
* @returns {IWatchableRegistry}
*/
capture(callback: Function): {[id: string]: IWatchable} {
invoked = {};
callback();
return invoked;
}
};
})();
示例3: expect
expect((): void => {
o.on('test', listener);
}).toThrow();