-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
128 lines (104 loc) · 2.22 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* jshint node:true */
/**
* Ensures only jobs created via the instance cleaned up when #clear is called
*/
'use strict';
/**
* Module Dependencies
*/
var fastdom = require('fastdom');
// uid for jobs
var uid = 0;
/**
* constructor
*/
function Fastdom(_fastdom) {
// Mainly used for testing.
this._fastdom = _fastdom || fastdom;
this._jobs = {
read: {},
write: {},
defer: {}
};
}
/**
* Wraps the fastdom.read API.
*
* Keeps track of the job and then removes the refernece when the job is run.
*
* @param {Function} fn
* @param {Object} ctx
* @api public
*/
Fastdom.prototype.read = function(fn, ctx) {
var self = this;
var id = uid++;
var job = this._fastdom.read(function() {
delete self._jobs.read[id];
fn.call(ctx);
});
this._jobs.read[id] = job;
};
/**
* Wraps the fastdom.write API.
*
* Keeps track of the job and then removes the reference when the job is run.
*
* @param {Function} fn
* @param {Object} ctx
* @api public
*/
Fastdom.prototype.write = function(fn, ctx) {
var self = this;
var id = uid++;
var job = this._fastdom.write(function() {
delete self._jobs.write[id];
fn.call(ctx);
});
this._jobs.write[id] = job;
};
/**
* Wraps the fastdom.defer API.
*
* Keeps track of the job and then removes the reference when the job is run.
*
* @param {Function} fn
* @param {Object} ctx
* @api public
*/
Fastdom.prototype.defer = function(frames, fn, ctx) {
var self = this;
var id = uid++;
// Frames argument is optional
if (typeof frames === 'function') {
ctx = fn;
fn = frames;
frames = 1;
}
var job = this._fastdom.defer(frames, function() {
delete self._jobs.defer[id];
fn.call(ctx);
});
this._jobs.defer[id] = job;
};
/**
* Wraps the fastdom.clear API.
*/
Fastdom.prototype.clear = function() {
var self = this;
var clearItems = function(queue) {
for (var key in queue) {
if (queue.hasOwnProperty(key)) {
self._fastdom.clear(queue[key]);
}
}
};
clearItems(this._jobs.read);
clearItems(this._jobs.write);
clearItems(this._jobs.defer);
// Clear the queues
this._jobs.read = {};
this._jobs.write = {};
this._jobs.defer = {};
};
module.exports = Fastdom;