-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-slicer.js
104 lines (88 loc) · 2.2 KB
/
render-slicer.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
var renderSlicer = (function (func) {
var _queue = [], // data to be rendered
_rate = 1000, // number of calls per frame
_invalidate = function () {}, // invalidate last render queue
_onStartCallback = function () {}, // clearing function
_isDone = false,
_doneCallback, // all done
_chunkDoneCallback, // chunk done
_startQueueSize,
_frameDoneCallback,
_dataCountDone; // percent done
var rq = function (data) {
_isDone = false;
if (data) rq.data(data);
_invalidate();
_onStartCallback();
rq.render();
};
rq.render = function () {
var valid = true;
_invalidate = rq.invalidate = function () {
valid = false;
};
function doFrame() {
if (!valid) return true;
if (!_isDone && _queue.length === 0 && _doneCallback) {
_isDone = true;
_doneCallback();
}
var chunk = _queue.splice(0, _rate);
chunk.map(function(d){
_dataCountDone++;
if(_frameDoneCallback) _frameDoneCallback();
func(d);
});
if(chunk.length > 0 && _chunkDoneCallback){
_chunkDoneCallback();
}
timer_frame(doFrame);
}
doFrame();
};
rq.onStart = function (onStartCallback) {
_onStartCallback = onStartCallback;
return rq;
};
rq.onChunkDone = function (chunkDoneCallback) {
_chunkDoneCallback = chunkDoneCallback;
return rq;
};
rq.onFrameDone = function (frameDoneCallback) {
_frameDoneCallback = frameDoneCallback;
return rq;
};
rq.onDone = function (doneCallback) {
_doneCallback = doneCallback;
return rq;
};
rq.data = function (data) {
_startQueueSize = data.length;
_dataCountDone = 0;
_invalidate();
_queue = data.slice(0); // creates a copy of the data
return rq;
};
rq.add = function (data) {
_queue = _queue.concat(data);
};
rq.rate = function (value) {
if (!arguments.length) return _rate;
_rate = value;
return rq;
};
rq.remaining = function () {
return _queue.length;
};
rq.count = function(){
return _dataCountDone;
};
rq.invalidate = _invalidate;
var timer_frame = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function (callback) { setTimeout(callback, 17); };
return rq;
});