当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Observable.Observable类代码示例

本文整理汇总了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;
        }
    };
})();
开发者ID:joppe,项目名称:aap,代码行数:58,代码来源:watcher.ts

示例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();
    });
});
开发者ID:joppe,项目名称:aap,代码行数:94,代码来源:Observable.spec.ts

示例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);
    });
});
开发者ID:joppe,项目名称:aap,代码行数:100,代码来源:Observer.spec.ts

示例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);
    });
开发者ID:joppe,项目名称:aap,代码行数:28,代码来源:Observable.spec.ts

示例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);
    });
开发者ID:joppe,项目名称:aap,代码行数:19,代码来源:Observer.spec.ts

示例6: expect

 expect((): void => {
     o.on('test', listener);
 }).toThrow();
开发者ID:joppe,项目名称:aap,代码行数:3,代码来源:Observable.spec.ts


注:本文中的src/observer/Observable.Observable类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。