本文整理匯總了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();