forked from mapbox/mapbox-studio-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
85 lines (75 loc) · 2.22 KB
/
task.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
module.exports = {
get: get,
set: set,
del: del,
Task: Task,
Done: Done
};
var stream = require('stream');
var task = undefined;
// Get the currently active task.
function get() {
return task;
}
// Set a task to be active.
function set(t) {
if (task) throw new Error('Active task in progress');
if (!(t instanceof Task)) throw new Error('Invalid task object');
task = t;
t.progress.on('finished', function() {
if (task !== t) return;
task = undefined;
});
t.progress.on('error', function(err) {
if (task !== t) return;
t.err = err;
});
}
// Clear the current active task.
function del() {
if (!task) return;
task.progress.unpipe();
task = undefined;
}
// A Task object, representing a stream pipe process which is not yet finished.
function Task(id, type, progress) {
if (typeof id !== 'string') throw new Error('Task id must be a string');
if (type !== 'export' && type !== 'upload') throw new Error('Task type must be one of [export, upload]');
if (!progress || typeof progress.progress !== 'function') throw new Error('Task progress must be a progress stream');
this.id = id;
this.type = type;
this.progress = progress;
}
Task.prototype.toJSON = function() {
return {
id: this.id,
type: this.type,
progress: this.progress.progress(),
size: null,
url: null,
mapid: null
};
};
// A Done object, a record of the completed result of a Task.
function Done(id, type, url, size, mapid) {
if (typeof id !== 'string') throw new Error('Done id must be a string');
if (type !== 'export' && type !== 'upload') throw new Error('Done type must be one of [export, upload]');
if (typeof url !== 'string') throw new Error('Done url must be a string');
if (typeof size !== 'number') throw new Error('Done size must be a number');
this.id = id;
this.type = type;
this.url = url;
this.size = size;
this.progress = null;
this.mapid = mapid || null;
}
Done.prototype.toJSON = function() {
return {
id: this.id,
type: this.type,
progress: null,
size: this.size,
url: this.url,
mapid: this.mapid || null
};
};