This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
animationscheduler.js
75 lines (62 loc) · 2.53 KB
/
animationscheduler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
var Scheduler = require('./scheduler');
var disposables = require('rx.disposables');
var BinaryDisposable = disposables.BinaryDisposable;
var Disposable = disposables.Disposable;
var SingleAssignmentDisposable = disposables.SingleAssignmentDisposable;
var inherits = require('inherits');
var requestAnimFrame, cancelAnimFrame;
if (global.requestAnimationFrame) {
requestAnimFrame = global.requestAnimationFrame;
cancelAnimFrame = global.cancelAnimationFrame;
} else if (global.mozRequestAnimationFrame) {
requestAnimFrame = global.mozRequestAnimationFrame;
cancelAnimFrame = global.mozCancelAnimationFrame;
} else if (global.webkitRequestAnimationFrame) {
requestAnimFrame = global.webkitRequestAnimationFrame;
cancelAnimFrame = global.webkitCancelAnimationFrame;
} else if (global.msRequestAnimationFrame) {
requestAnimFrame = global.msRequestAnimationFrame;
cancelAnimFrame = global.msCancelAnimationFrame;
} else if (global.oRequestAnimationFrame) {
requestAnimFrame = global.oRequestAnimationFrame;
cancelAnimFrame = global.oCancelAnimationFrame;
} else {
requestAnimFrame = function(cb) { global.setTimeout(cb, 1000 / 60); };
cancelAnimFrame = global.clearTimeout;
}
/**
* Gets a scheduler that schedules schedules work on the requestAnimationFrame for immediate actions.
*/
function AnimationScheduler() {
Scheduler.call(this);
}
inherits(AnimationScheduler, Scheduler);
function scheduleAction(disposable, action, scheduler, state) {
return function schedule() {
!disposable.isDisposed && disposable.setDisposable(Disposable._fixup(action(scheduler, state)));
};
}
function ClearDisposable(method, id) {
this._id = id;
this._method = method;
this.isDisposed = false;
}
ClearDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
this._method.call(null, this._id);
}
};
AnimationScheduler.prototype.schedule = function (state, action) {
var disposable = new SingleAssignmentDisposable(),
id = requestAnimFrame(scheduleAction(disposable, action, this, state));
return new BinaryDisposable(disposable, new ClearDisposable(cancelAnimFrame, id));
};
AnimationScheduler.prototype._scheduleFuture = function (state, dueTime, action) {
if (dueTime === 0) { return this.schedule(state, action); }
var disposable = new SingleAssignmentDisposable(),
id = global.setTimeout(scheduleAction(disposable, action, this, state), dueTime);
return new BinaryDisposable(disposable, new ClearDisposable(global.clearTimeout, id));
};
module.exports = AnimationScheduler;