Skip to content

Commit

Permalink
refactor(Action): add 'T' type param to Action.
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuharuohzeki authored and kwonoj committed Mar 29, 2016
1 parent a152e95 commit 5b8cb3e
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export interface Scheduler {
schedule<T>(work: (state?: T) => Subscription | void, delay?: number, state?: T): Subscription;
flush(): void;
active: boolean;
actions: Action[];
actions: Action<any>[]; // XXX: use `any` to remove type param `T` from `Scheduler`.
scheduledId: number;
}
15 changes: 11 additions & 4 deletions src/operator/bufferTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class BufferTimeOperator<T> implements Operator<T, T[]> {
}
}

type CreationState<T> = {
bufferTimeSpan: number;
bufferCreationInterval: number,
subscriber: BufferTimeSubscriber<T>;
scheduler: Scheduler;
};

class BufferTimeSubscriber<T> extends Subscriber<T> {
private buffers: Array<T[]> = [];

Expand All @@ -79,7 +86,7 @@ class BufferTimeSubscriber<T> extends Subscriber<T> {
const buffer = this.openBuffer();
if (bufferCreationInterval !== null && bufferCreationInterval >= 0) {
const closeState = { subscriber: this, buffer };
const creationState = { bufferTimeSpan, bufferCreationInterval, subscriber: this, scheduler };
const creationState: CreationState<T> = { bufferTimeSpan, bufferCreationInterval, subscriber: this, scheduler };
this.add(scheduler.schedule(dispatchBufferClose, bufferTimeSpan, closeState));
this.add(scheduler.schedule(dispatchBufferCreation, bufferCreationInterval, creationState));
} else {
Expand Down Expand Up @@ -140,10 +147,10 @@ function dispatchBufferTimeSpanOnly(state: any) {
}
}

function dispatchBufferCreation(state: any) {
function dispatchBufferCreation<T>(state: CreationState<T>) {
const { bufferCreationInterval, bufferTimeSpan, subscriber, scheduler } = state;
const buffer = subscriber.openBuffer();
const action = <Action>this;
const action = <Action<CreationState<T>>>this;
if (!subscriber.isUnsubscribed) {
action.add(scheduler.schedule(dispatchBufferClose, bufferTimeSpan, { subscriber, buffer }));
action.schedule(state, bufferCreationInterval);
Expand All @@ -152,4 +159,4 @@ function dispatchBufferCreation(state: any) {

function dispatchBufferClose({ subscriber, buffer }) {
subscriber.closeBuffer(buffer);
}
}
13 changes: 10 additions & 3 deletions src/operator/windowTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class WindowTimeOperator<T> implements Operator<T, Observable<T>> {
}
}

type CreationState<T> = {
windowTimeSpan: number;
windowCreationInterval: number;
subscriber: WindowTimeSubscriber<T>;
scheduler: Scheduler;
};

class WindowTimeSubscriber<T> extends Subscriber<T> {
private windows: Subject<T>[] = [];

Expand All @@ -90,7 +97,7 @@ class WindowTimeSubscriber<T> extends Subscriber<T> {
if (windowCreationInterval !== null && windowCreationInterval >= 0) {
let window = this.openWindow();
const closeState = { subscriber: this, window, context: <any>null };
const creationState = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler };
const creationState: CreationState<T> = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler };
this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState));
this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState));
} else {
Expand Down Expand Up @@ -161,10 +168,10 @@ function dispatchWindowTimeSpanOnly<T>(state: TimeSpanOnlyState<T>) {
(<any>this).schedule(state, windowTimeSpan);
}

function dispatchWindowCreation(state: any) {
function dispatchWindowCreation<T>(state: CreationState<T>) {
let { windowTimeSpan, subscriber, scheduler, windowCreationInterval } = state;
let window = subscriber.openWindow();
let action = <Action>this;
let action = <Action<CreationState<T>>>this;
let context = { action, subscription: <any>null };
const timeSpanState = { subscriber, window, context };
context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState);
Expand Down
8 changes: 4 additions & 4 deletions src/scheduler/Action.ts
Original file line number Diff line number Diff line change
@@ -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<T> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler/AnimationFrameAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {AnimationFrame} from '../util/AnimationFrame';

export class AnimationFrameAction<T> extends FutureAction<T> {

protected _schedule(state?: T, delay: number = 0): Action {
protected _schedule(state?: T, delay: number = 0): Action<T> {
if (delay > 0) {
return super._schedule(state, delay);
}
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler/AnimationFrameScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {QueueScheduler} from './QueueScheduler';
import {AnimationFrameAction} from './AnimationFrameAction';

export class AnimationFrameScheduler extends QueueScheduler {
scheduleNow<T>(work: (x?: T) => Subscription, state?: T): Action {
scheduleNow<T>(work: (x?: T) => Subscription, state?: T): Action<T> {
return new AnimationFrameAction(this, work).schedule(state);
}
}
2 changes: 1 addition & 1 deletion src/scheduler/AsapAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {FutureAction} from './FutureAction';

export class AsapAction<T> extends FutureAction<T> {

protected _schedule(state?: T, delay: number = 0): Action {
protected _schedule(state?: T, delay: number = 0): Action<T> {
if (delay > 0) {
return super._schedule(state, delay);
}
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler/AsapScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Subscription} from '../Subscription';
import {QueueScheduler} from './QueueScheduler';

export class AsapScheduler extends QueueScheduler {
scheduleNow<T>(work: (x?: T) => Subscription, state?: T): Action {
scheduleNow<T>(work: (x?: T) => Subscription, state?: T): Action<T> {
return new AsapAction(this, work).schedule(state);
}
}
2 changes: 1 addition & 1 deletion src/scheduler/AsyncScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Subscription} from '../Subscription';
import {QueueScheduler} from './QueueScheduler';

export class AsyncScheduler extends QueueScheduler {
scheduleNow<T>(work: (x?: any) => Subscription, state?: any): Action {
scheduleNow<T>(work: (x?: T) => Subscription, state?: T): Action<T> {
return new FutureAction(this, work).schedule(state, 0);
}
}
6 changes: 3 additions & 3 deletions src/scheduler/FutureAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Action} from './Action';
import {Scheduler} from '../Scheduler';
import {Subscription} from '../Subscription';

export class FutureAction<T> extends Subscription implements Action {
export class FutureAction<T> extends Subscription implements Action<T> {

public id: number;
public state: T;
Expand All @@ -30,14 +30,14 @@ export class FutureAction<T> extends Subscription implements Action {
}
}

schedule(state?: T, delay: number = 0): Action {
schedule(state?: T, delay: number = 0): Action<T> {
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<T> {

// Always replace the current state with the new state.
this.state = state;
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler/QueueAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Action} from './Action';
import {FutureAction} from './FutureAction';

export class QueueAction<T> extends FutureAction<T> {
protected _schedule(state?: T, delay: number = 0): Action {
protected _schedule(state?: T, delay: number = 0): Action<T> {
if (delay > 0) {
return super._schedule(state, delay);
}
Expand Down
9 changes: 5 additions & 4 deletions src/scheduler/QueueScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Action} from './Action';

export class QueueScheduler implements Scheduler {
public active: boolean = false;
public actions: QueueAction<any>[] = [];
public actions: QueueAction<any>[] = []; // XXX: use `any` to remove type param `T` from `VirtualTimeScheduler`.
public scheduledId: number = null;

now() {
Expand All @@ -19,7 +19,8 @@ export class QueueScheduler implements Scheduler {
}
this.active = true;
const actions = this.actions;
for (let action: QueueAction<any>; action = actions.shift(); ) {
// XXX: use `any` to remove type param `T` from `VirtualTimeScheduler`.
for (let action: QueueAction<any> = null; action = actions.shift(); ) {
action.execute();
if (action.error) {
this.active = false;
Expand All @@ -35,11 +36,11 @@ export class QueueScheduler implements Scheduler {
this.scheduleLater(work, delay, state);
}

scheduleNow<T>(work: (x?: T) => Subscription | void, state?: T): Action {
scheduleNow<T>(work: (x?: T) => Subscription | void, state?: T): Action<T> {
return new QueueAction(this, work).schedule(state);
}

scheduleLater<T>(work: (x?: T) => Subscription | void, delay: number, state?: T): Action {
scheduleLater<T>(work: (x?: T) => Subscription | void, delay: number, state?: T): Action<T> {
return new FutureAction(this, work).schedule(state, delay);
}
}
10 changes: 5 additions & 5 deletions src/scheduler/VirtualTimeScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Subscription} from '../Subscription';
import {Action} from './Action';

export class VirtualTimeScheduler implements Scheduler {
actions: Action[] = [];
actions: Action<any>[] = []; // XXX: use `any` to remove type param `T` from `VirtualTimeScheduler`.
active: boolean = false;
scheduledId: number = null;
index: number = 0;
Expand Down Expand Up @@ -38,8 +38,8 @@ export class VirtualTimeScheduler implements Scheduler {
this.frame = 0;
}

addAction<T>(action: Action) {
const actions = this.actions;
addAction<T>(action: Action<T>): void {
const actions: Action<T>[] = this.actions;

actions.push(action);

Expand All @@ -66,7 +66,7 @@ export class VirtualTimeScheduler implements Scheduler {
}
}

class VirtualAction<T> extends Subscription implements Action {
class VirtualAction<T> extends Subscription implements Action<T> {
state: T;
delay: number;
calls = 0;
Expand All @@ -83,7 +83,7 @@ class VirtualAction<T> extends Subscription implements Action {
return this;
}
const scheduler = this.scheduler;
let action: Action;
let action: Action<T> = null;
if (this.calls++ === 0) {
// the action is not being rescheduled.
action = this;
Expand Down

0 comments on commit 5b8cb3e

Please sign in to comment.