From 6197a3565e15c042e905569afd5b456509ee71f4 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Fri, 13 Jan 2017 23:44:03 -0800 Subject: [PATCH] refactor(windowTime): clarify type definition of windowTime --- src/operator/windowTime.ts | 68 +++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/operator/windowTime.ts b/src/operator/windowTime.ts index 52c7b7dddb..4e8b2746d4 100644 --- a/src/operator/windowTime.ts +++ b/src/operator/windowTime.ts @@ -83,13 +83,30 @@ interface CreationState { scheduler: IScheduler; } +interface TimeSpanOnlyState { + window: Subject; + windowTimeSpan: number; + subscriber: WindowTimeSubscriber; + } + +interface CloseWindowContext { + action: Action>; + subscription: Subscription; +} + +interface CloseState { + subscriber: WindowTimeSubscriber; + window: Subject; + context: CloseWindowContext; +} + /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class WindowTimeSubscriber extends Subscriber { - private windows: Subject[] = []; + private windows: Array> = []; constructor(protected destination: Subscriber>, private windowTimeSpan: number, @@ -98,18 +115,18 @@ class WindowTimeSubscriber extends Subscriber { super(destination); if (windowCreationInterval !== null && windowCreationInterval >= 0) { let window = this.openWindow(); - const closeState = { subscriber: this, window, context: null }; + const closeState: CloseState = { subscriber: this, window, context: null }; const creationState: CreationState = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler }; this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState)); this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState)); } else { let window = this.openWindow(); - const timeSpanOnlyState = { subscriber: this, window, windowTimeSpan }; + const timeSpanOnlyState: TimeSpanOnlyState = { subscriber: this, window, windowTimeSpan }; this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState)); } } - protected _next(value: T) { + protected _next(value: T): void { const windows = this.windows; const len = windows.length; for (let i = 0; i < len; i++) { @@ -120,7 +137,7 @@ class WindowTimeSubscriber extends Subscriber { } } - protected _error(err: any) { + protected _error(err: any): void { const windows = this.windows; while (windows.length > 0) { windows.shift().error(err); @@ -128,7 +145,7 @@ class WindowTimeSubscriber extends Subscriber { this.destination.error(err); } - protected _complete() { + protected _complete(): void { const windows = this.windows; while (windows.length > 0) { const window = windows.shift(); @@ -139,7 +156,7 @@ class WindowTimeSubscriber extends Subscriber { this.destination.complete(); } - openWindow(): Subject { + public openWindow(): Subject { const window = new Subject(); this.windows.push(window); const destination = this.destination; @@ -147,20 +164,14 @@ class WindowTimeSubscriber extends Subscriber { return window; } - closeWindow(window: Subject) { + public closeWindow(window: Subject): void { window.complete(); const windows = this.windows; windows.splice(windows.indexOf(window), 1); } } -interface TimeSpanOnlyState { - window: Subject; - windowTimeSpan: number; - subscriber: WindowTimeSubscriber; -} - -function dispatchWindowTimeSpanOnly(this: Action>, state: TimeSpanOnlyState) { +function dispatchWindowTimeSpanOnly(this: Action>, state: TimeSpanOnlyState): void { const { subscriber, windowTimeSpan, window } = state; if (window) { window.complete(); @@ -169,30 +180,19 @@ function dispatchWindowTimeSpanOnly(this: Action>, state this.schedule(state, windowTimeSpan); } -interface Context { - action: Action>; - subscription: Subscription; -} - -interface DispatchArg { - subscriber: WindowTimeSubscriber; - window: Subject; - context: Context; -} - -function dispatchWindowCreation(this: Action>, state: CreationState) { - let { windowTimeSpan, subscriber, scheduler, windowCreationInterval } = state; - let window = subscriber.openWindow(); - let action = this; - let context: Context = { action, subscription: null }; - const timeSpanState: DispatchArg = { subscriber, window, context }; +function dispatchWindowCreation(this: Action>, state: CreationState): void { + const { windowTimeSpan, subscriber, scheduler, windowCreationInterval } = state; + const window = subscriber.openWindow(); + const action = this; + let context: CloseWindowContext = { action, subscription: null }; + const timeSpanState: CloseState = { subscriber, window, context }; context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState); action.add(context.subscription); action.schedule(state, windowCreationInterval); } -function dispatchWindowClose(arg: DispatchArg) { - const { subscriber, window, context } = arg; +function dispatchWindowClose(state: CloseState): void { + const { subscriber, window, context } = state; if (context && context.action && context.subscription) { context.action.remove(context.subscription); }