Skip to content

Commit

Permalink
feat(scheduler): adds animationFrame scheduler.
Browse files Browse the repository at this point in the history
  • Loading branch information
trxcllnt authored and benlesh committed Jan 13, 2016
1 parent a411673 commit e637b78
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Rx.DOM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,18 @@ import {ArgumentOutOfRangeError} from './util/ArgumentOutOfRangeError';
import {ObjectUnsubscribedError} from './util/ObjectUnsubscribedError';
import {asap} from './scheduler/asap';
import {queue} from './scheduler/queue';
import {animationFrame} from './scheduler/animationFrame';
import {AsapScheduler} from './scheduler/AsapScheduler';
import {QueueScheduler} from './scheduler/QueueScheduler';
import {AnimationFrameScheduler} from './scheduler/AnimationFrameScheduler';
import {rxSubscriber} from './symbol/rxSubscriber';
/* tslint:enable:no-unused-variable */

/* tslint:disable:no-var-keyword */
var Scheduler = {
asap,
queue
queue,
animationFrame
};

var Symbol = {
Expand Down
39 changes: 39 additions & 0 deletions src/scheduler/AnimationFrameAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {Action} from './Action';
import {FutureAction} from './FutureAction';
import {AnimationFrame} from '../util/AnimationFrame';

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

_schedule(state?: any, delay: number = 0): Action {
if (delay > 0) {
return super._schedule(state, delay);
}
this.delay = delay;
this.state = state;
const {scheduler} = this;
scheduler.actions.push(this);
if (!scheduler.scheduledId) {
scheduler.scheduledId = AnimationFrame.requestAnimationFrame(() => {
scheduler.scheduledId = null;
scheduler.flush();
});
}
return this;
}

_unsubscribe(): void {

const {scheduler} = this;
const {scheduledId, actions} = scheduler;

super._unsubscribe();

if (actions.length === 0) {
scheduler.active = false;
if (scheduledId != null) {
scheduler.scheduledId = null;
AnimationFrame.cancelAnimationFrame(scheduledId);
}
}
}
}
10 changes: 10 additions & 0 deletions src/scheduler/AnimationFrameScheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Action} from './Action';
import {Subscription} from '../Subscription';
import {QueueScheduler} from './QueueScheduler';
import {AnimationFrameAction} from './AnimationFrameAction';

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

export class AsapScheduler extends QueueScheduler {
public scheduledId: number = null;
scheduleNow<T>(work: (x?: any) => Subscription, state?: any): Action {
return new AsapAction(this, work).schedule(state);
}
Expand Down
3 changes: 3 additions & 0 deletions src/scheduler/animationFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {AnimationFrameScheduler} from './AnimationFrameScheduler';

export const animationFrame = new AnimationFrameScheduler();
29 changes: 29 additions & 0 deletions src/util/AnimationFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { root } from './root';

export class RequestAnimationFrameDefinition {
cancelAnimationFrame: (handle: number) => void;
requestAnimationFrame: (cb: () => void) => number;
constructor(root: any) {
if (root.requestAnimationFrame) {
this.cancelAnimationFrame = root.cancelAnimationFrame;
this.requestAnimationFrame = root.requestAnimationFrame;
} else if (root.mozRequestAnimationFrame) {
this.cancelAnimationFrame = root.mozCancelAnimationFrame;
this.requestAnimationFrame = root.mozRequestAnimationFrame;
} else if (root.webkitRequestAnimationFrame) {
this.cancelAnimationFrame = root.webkitCancelAnimationFrame;
this.requestAnimationFrame = root.webkitRequestAnimationFrame;
} else if (root.msRequestAnimationFrame) {
this.cancelAnimationFrame = root.msCancelAnimationFrame;
this.requestAnimationFrame = root.msRequestAnimationFrame;
} else if (root.oRequestAnimationFrame) {
this.cancelAnimationFrame = root.oCancelAnimationFrame;
this.requestAnimationFrame = root.oRequestAnimationFrame;
} else {
this.cancelAnimationFrame = root.clearTimeout;
this.requestAnimationFrame = function(cb) { return root.setTimeout(cb, 1000 / 60); };
}
}
}

export const AnimationFrame = new RequestAnimationFrameDefinition(root);

0 comments on commit e637b78

Please sign in to comment.