From 5b8cb3e4e19c9f4a10f89d9db2fad09d69f76877 Mon Sep 17 00:00:00 2001 From: Tetsuharu OHZEKI Date: Wed, 23 Mar 2016 18:55:56 +0900 Subject: [PATCH] refactor(Action): add 'T' type param to Action. --- src/Scheduler.ts | 2 +- src/operator/bufferTime.ts | 15 +++++++++++---- src/operator/windowTime.ts | 13 ++++++++++--- src/scheduler/Action.ts | 8 ++++---- src/scheduler/AnimationFrameAction.ts | 2 +- src/scheduler/AnimationFrameScheduler.ts | 2 +- src/scheduler/AsapAction.ts | 2 +- src/scheduler/AsapScheduler.ts | 2 +- src/scheduler/AsyncScheduler.ts | 2 +- src/scheduler/FutureAction.ts | 6 +++--- src/scheduler/QueueAction.ts | 2 +- src/scheduler/QueueScheduler.ts | 9 +++++---- src/scheduler/VirtualTimeScheduler.ts | 10 +++++----- 13 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/Scheduler.ts b/src/Scheduler.ts index 00e8fa0739..e2477f4d0a 100644 --- a/src/Scheduler.ts +++ b/src/Scheduler.ts @@ -6,6 +6,6 @@ export interface Scheduler { schedule(work: (state?: T) => Subscription | void, delay?: number, state?: T): Subscription; flush(): void; active: boolean; - actions: Action[]; + actions: Action[]; // XXX: use `any` to remove type param `T` from `Scheduler`. scheduledId: number; } diff --git a/src/operator/bufferTime.ts b/src/operator/bufferTime.ts index 6c3487ab2c..fed4bce35b 100644 --- a/src/operator/bufferTime.ts +++ b/src/operator/bufferTime.ts @@ -68,6 +68,13 @@ class BufferTimeOperator implements Operator { } } +type CreationState = { + bufferTimeSpan: number; + bufferCreationInterval: number, + subscriber: BufferTimeSubscriber; + scheduler: Scheduler; +}; + class BufferTimeSubscriber extends Subscriber { private buffers: Array = []; @@ -79,7 +86,7 @@ class BufferTimeSubscriber extends Subscriber { const buffer = this.openBuffer(); if (bufferCreationInterval !== null && bufferCreationInterval >= 0) { const closeState = { subscriber: this, buffer }; - const creationState = { bufferTimeSpan, bufferCreationInterval, subscriber: this, scheduler }; + const creationState: CreationState = { bufferTimeSpan, bufferCreationInterval, subscriber: this, scheduler }; this.add(scheduler.schedule(dispatchBufferClose, bufferTimeSpan, closeState)); this.add(scheduler.schedule(dispatchBufferCreation, bufferCreationInterval, creationState)); } else { @@ -140,10 +147,10 @@ function dispatchBufferTimeSpanOnly(state: any) { } } -function dispatchBufferCreation(state: any) { +function dispatchBufferCreation(state: CreationState) { const { bufferCreationInterval, bufferTimeSpan, subscriber, scheduler } = state; const buffer = subscriber.openBuffer(); - const action = this; + const action = >>this; if (!subscriber.isUnsubscribed) { action.add(scheduler.schedule(dispatchBufferClose, bufferTimeSpan, { subscriber, buffer })); action.schedule(state, bufferCreationInterval); @@ -152,4 +159,4 @@ function dispatchBufferCreation(state: any) { function dispatchBufferClose({ subscriber, buffer }) { subscriber.closeBuffer(buffer); -} +} \ No newline at end of file diff --git a/src/operator/windowTime.ts b/src/operator/windowTime.ts index 129ea7a378..7aa9bd5f0b 100644 --- a/src/operator/windowTime.ts +++ b/src/operator/windowTime.ts @@ -79,6 +79,13 @@ class WindowTimeOperator implements Operator> { } } +type CreationState = { + windowTimeSpan: number; + windowCreationInterval: number; + subscriber: WindowTimeSubscriber; + scheduler: Scheduler; +}; + class WindowTimeSubscriber extends Subscriber { private windows: Subject[] = []; @@ -90,7 +97,7 @@ class WindowTimeSubscriber extends Subscriber { if (windowCreationInterval !== null && windowCreationInterval >= 0) { let window = this.openWindow(); const closeState = { subscriber: this, window, context: null }; - const creationState = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler }; + const creationState: CreationState = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler }; this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState)); this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState)); } else { @@ -161,10 +168,10 @@ function dispatchWindowTimeSpanOnly(state: TimeSpanOnlyState) { (this).schedule(state, windowTimeSpan); } -function dispatchWindowCreation(state: any) { +function dispatchWindowCreation(state: CreationState) { let { windowTimeSpan, subscriber, scheduler, windowCreationInterval } = state; let window = subscriber.openWindow(); - let action = this; + let action = >>this; let context = { action, subscription: null }; const timeSpanState = { subscriber, window, context }; context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState); diff --git a/src/scheduler/Action.ts b/src/scheduler/Action.ts index e434c7f29f..a294e76b87 100644 --- a/src/scheduler/Action.ts +++ b/src/scheduler/Action.ts @@ -1,11 +1,11 @@ import {Subscription} from '../Subscription'; import {Scheduler} from '../Scheduler'; -export interface Action extends Subscription { - work: (state?: any) => void|Subscription; - state?: any; +export interface Action extends Subscription { + work: (state?: T) => void|Subscription; + state?: T; delay?: number; - schedule(state?: any, delay?: number): void; + schedule(state?: T, delay?: number): void; execute(): void; scheduler: Scheduler; error: any; diff --git a/src/scheduler/AnimationFrameAction.ts b/src/scheduler/AnimationFrameAction.ts index 4a391e643f..f0c4a78405 100644 --- a/src/scheduler/AnimationFrameAction.ts +++ b/src/scheduler/AnimationFrameAction.ts @@ -4,7 +4,7 @@ import {AnimationFrame} from '../util/AnimationFrame'; export class AnimationFrameAction extends FutureAction { - protected _schedule(state?: T, delay: number = 0): Action { + protected _schedule(state?: T, delay: number = 0): Action { if (delay > 0) { return super._schedule(state, delay); } diff --git a/src/scheduler/AnimationFrameScheduler.ts b/src/scheduler/AnimationFrameScheduler.ts index 28beffd71c..5ca4a9ef40 100644 --- a/src/scheduler/AnimationFrameScheduler.ts +++ b/src/scheduler/AnimationFrameScheduler.ts @@ -4,7 +4,7 @@ import {QueueScheduler} from './QueueScheduler'; import {AnimationFrameAction} from './AnimationFrameAction'; export class AnimationFrameScheduler extends QueueScheduler { - scheduleNow(work: (x?: T) => Subscription, state?: T): Action { + scheduleNow(work: (x?: T) => Subscription, state?: T): Action { return new AnimationFrameAction(this, work).schedule(state); } } diff --git a/src/scheduler/AsapAction.ts b/src/scheduler/AsapAction.ts index 85f2eb23bc..c08e43a660 100644 --- a/src/scheduler/AsapAction.ts +++ b/src/scheduler/AsapAction.ts @@ -4,7 +4,7 @@ import {FutureAction} from './FutureAction'; export class AsapAction extends FutureAction { - protected _schedule(state?: T, delay: number = 0): Action { + protected _schedule(state?: T, delay: number = 0): Action { if (delay > 0) { return super._schedule(state, delay); } diff --git a/src/scheduler/AsapScheduler.ts b/src/scheduler/AsapScheduler.ts index e829628c5c..b09bf286d4 100644 --- a/src/scheduler/AsapScheduler.ts +++ b/src/scheduler/AsapScheduler.ts @@ -4,7 +4,7 @@ import {Subscription} from '../Subscription'; import {QueueScheduler} from './QueueScheduler'; export class AsapScheduler extends QueueScheduler { - scheduleNow(work: (x?: T) => Subscription, state?: T): Action { + scheduleNow(work: (x?: T) => Subscription, state?: T): Action { return new AsapAction(this, work).schedule(state); } } diff --git a/src/scheduler/AsyncScheduler.ts b/src/scheduler/AsyncScheduler.ts index a92a2a01c9..db5af54f55 100644 --- a/src/scheduler/AsyncScheduler.ts +++ b/src/scheduler/AsyncScheduler.ts @@ -4,7 +4,7 @@ import {Subscription} from '../Subscription'; import {QueueScheduler} from './QueueScheduler'; export class AsyncScheduler extends QueueScheduler { - scheduleNow(work: (x?: any) => Subscription, state?: any): Action { + scheduleNow(work: (x?: T) => Subscription, state?: T): Action { return new FutureAction(this, work).schedule(state, 0); } } diff --git a/src/scheduler/FutureAction.ts b/src/scheduler/FutureAction.ts index 7cd21456d9..f340ee867f 100644 --- a/src/scheduler/FutureAction.ts +++ b/src/scheduler/FutureAction.ts @@ -3,7 +3,7 @@ import {Action} from './Action'; import {Scheduler} from '../Scheduler'; import {Subscription} from '../Subscription'; -export class FutureAction extends Subscription implements Action { +export class FutureAction extends Subscription implements Action { public id: number; public state: T; @@ -30,14 +30,14 @@ export class FutureAction extends Subscription implements Action { } } - schedule(state?: T, delay: number = 0): Action { + schedule(state?: T, delay: number = 0): Action { if (this.isUnsubscribed) { return this; } return this._schedule(state, delay); } - protected _schedule(state?: T, delay: number = 0): Action { + protected _schedule(state?: T, delay: number = 0): Action { // Always replace the current state with the new state. this.state = state; diff --git a/src/scheduler/QueueAction.ts b/src/scheduler/QueueAction.ts index bb89bb12f3..ee2a4cc287 100644 --- a/src/scheduler/QueueAction.ts +++ b/src/scheduler/QueueAction.ts @@ -2,7 +2,7 @@ import {Action} from './Action'; import {FutureAction} from './FutureAction'; export class QueueAction extends FutureAction { - protected _schedule(state?: T, delay: number = 0): Action { + protected _schedule(state?: T, delay: number = 0): Action { if (delay > 0) { return super._schedule(state, delay); } diff --git a/src/scheduler/QueueScheduler.ts b/src/scheduler/QueueScheduler.ts index 97829015f3..84e4237159 100644 --- a/src/scheduler/QueueScheduler.ts +++ b/src/scheduler/QueueScheduler.ts @@ -6,7 +6,7 @@ import {Action} from './Action'; export class QueueScheduler implements Scheduler { public active: boolean = false; - public actions: QueueAction[] = []; + public actions: QueueAction[] = []; // XXX: use `any` to remove type param `T` from `VirtualTimeScheduler`. public scheduledId: number = null; now() { @@ -19,7 +19,8 @@ export class QueueScheduler implements Scheduler { } this.active = true; const actions = this.actions; - for (let action: QueueAction; action = actions.shift(); ) { + // XXX: use `any` to remove type param `T` from `VirtualTimeScheduler`. + for (let action: QueueAction = null; action = actions.shift(); ) { action.execute(); if (action.error) { this.active = false; @@ -35,11 +36,11 @@ export class QueueScheduler implements Scheduler { this.scheduleLater(work, delay, state); } - scheduleNow(work: (x?: T) => Subscription | void, state?: T): Action { + scheduleNow(work: (x?: T) => Subscription | void, state?: T): Action { return new QueueAction(this, work).schedule(state); } - scheduleLater(work: (x?: T) => Subscription | void, delay: number, state?: T): Action { + scheduleLater(work: (x?: T) => Subscription | void, delay: number, state?: T): Action { return new FutureAction(this, work).schedule(state, delay); } } diff --git a/src/scheduler/VirtualTimeScheduler.ts b/src/scheduler/VirtualTimeScheduler.ts index af0af7899a..b72a748a22 100644 --- a/src/scheduler/VirtualTimeScheduler.ts +++ b/src/scheduler/VirtualTimeScheduler.ts @@ -3,7 +3,7 @@ import {Subscription} from '../Subscription'; import {Action} from './Action'; export class VirtualTimeScheduler implements Scheduler { - actions: Action[] = []; + actions: Action[] = []; // XXX: use `any` to remove type param `T` from `VirtualTimeScheduler`. active: boolean = false; scheduledId: number = null; index: number = 0; @@ -38,8 +38,8 @@ export class VirtualTimeScheduler implements Scheduler { this.frame = 0; } - addAction(action: Action) { - const actions = this.actions; + addAction(action: Action): void { + const actions: Action[] = this.actions; actions.push(action); @@ -66,7 +66,7 @@ export class VirtualTimeScheduler implements Scheduler { } } -class VirtualAction extends Subscription implements Action { +class VirtualAction extends Subscription implements Action { state: T; delay: number; calls = 0; @@ -83,7 +83,7 @@ class VirtualAction extends Subscription implements Action { return this; } const scheduler = this.scheduler; - let action: Action; + let action: Action = null; if (this.calls++ === 0) { // the action is not being rescheduled. action = this;