本文整理匯總了TypeScript中src/observer/Observable.Observable類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Observable類的具體用法?TypeScript Observable怎麽用?TypeScript Observable使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Observable類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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;
}
};
})();
示例2: describe
describe('The observable', (): void => {
let o: Observable;
beforeEach((): void => {
o = new Observable();
});
it('Should notify listener when an event is triggered', (): void => {
let count: number = 0;
o.on('change', (): void => {
count += 1;
});
expect(count).toBe(0);
o.trigger('change');
expect(count).toBe(1);
o.trigger('change');
expect(count).toBe(2);
});
it('Should be able to unregister a single listener', (): 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', listener1);
o.trigger('change');
expect(count1).toBe(1);
expect(count2).toBe(2);
});
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);
});
it('Should not be possible to register the same listener twice to the same event', (): void => {
let count: number = 1;
const listener: Function = (): void => {
count += 1;
};
o.on('test', listener);
expect((): void => {
o.on('test', listener);
}).toThrow();
});
});
示例3: describe
describe('The observer', (): void => {
let observer: Observer;
let observable: Observable;
beforeEach((): void => {
observer = new Observer();
observable = new Observable();
});
it('Should register a listener with "listenTo', (): void => {
let count: number = 0;
const increment: Function = (): void => {
count += 1;
};
const decrement: Function = (): void => {
count -= 1;
};
observer.listenTo(observable, 'inc', increment);
observer.listenTo(observable, 'dec', decrement);
expect(count).toBe(0);
observable.trigger('inc');
expect(count).toBe(1);
observable.trigger('inc');
observable.trigger('dec');
observable.trigger('dec');
expect(count).toBe(0);
});
it('Should unregister a specific listener with "stopListening"', (): void => {
let count: number = 0;
const increment1: Function = (): void => {
count += 1;
};
const increment10: Function = (): void => {
count += 10;
};
observer.listenTo(observable, 'inc', increment1);
observer.listenTo(observable, 'inc', increment10);
observable.trigger('inc');
expect(count).toBe(11);
observer.stopListening(observable, 'inc', increment10);
observable.trigger('inc');
expect(count).toBe(12);
});
it('Should unregister a specific event with "stopListening"', (): void => {
let count: number = 0;
const increment: Function = (): void => {
count += 1;
};
const decrement: Function = (): void => {
count -= 1;
};
observer.listenTo(observable, 'inc', increment);
observer.listenTo(observable, 'dec', decrement);
observable.trigger('inc');
expect(count).toBe(1);
observer.stopListening(observable, 'inc');
observable.trigger('inc');
expect(count).toBe(1);
observable.trigger('dec');
expect(count).toBe(0);
});
it('Should unregister all listeners with "stopListening"', (): void => {
let count: number = 0;
const increment: Function = (): void => {
count += 1;
};
const decrement: Function = (): void => {
count -= 1;
};
observer.listenTo(observable, 'inc', increment);
observer.listenTo(observable, 'dec', decrement);
observable.trigger('inc');
observable.trigger('inc');
observable.trigger('inc');
expect(count).toBe(3);
observer.stopListening();
observable.trigger('inc');
expect(count).toBe(3);
observable.trigger('dec');
expect(count).toBe(3);
});
});
示例4: 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);
});
示例5: it
it('Should unregister a specific listener with "stopListening"', (): void => {
let count: number = 0;
const increment1: Function = (): void => {
count += 1;
};
const increment10: Function = (): void => {
count += 10;
};
observer.listenTo(observable, 'inc', increment1);
observer.listenTo(observable, 'inc', increment10);
observable.trigger('inc');
expect(count).toBe(11);
observer.stopListening(observable, 'inc', increment10);
observable.trigger('inc');
expect(count).toBe(12);
});
示例6: expect
expect((): void => {
o.on('test', listener);
}).toThrow();