forked from peterbraden/linkchecker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource-queue.js
91 lines (69 loc) · 1.7 KB
/
resource-queue.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
// Transactional Queue
module.exports = function(){
var items = {}
, queue = [] // List of keys to 'items'
var res = {
// Add a URI to the queue
push: function(uri, meta){
//console.log("QUEUE push", uri, meta)
if (items.hasOwnProperty(uri)){
// Existing URL
//console.log("QUEUE contains ", uri)
items[uri].references++;
return
}
items[uri] = {
meta: meta
, stat: 'queued'
, references: 1
, attempts : 0
}
queue.push(uri)
}
// Pop a URI from the queue
, pop: function(){
var uri = queue.pop()
, data = items[uri]
data.stat = 'pending'
data.attempts ++
//console.log("QUEUE pop", uri, JSON.stringify(data))
return [uri, data.meta];
}
// Resolve a URI as complete, or incomplete (repushed)
, resolve: function(uri, success){
//console.log("QUEUE resolve", uri, success ? 'success' : 'failure')
if (success){
items[uri].stat = 'resolved'
} else {
items[uri].stat = 'pending'
queue.push(uri)
}
}
// Reolved items
, resolved: function(){
return res.filter('resolved')
}
, queued: function(){
return res.filter('queued')
}
// Length of 'queued' resources
, len: function(){
return res.queued().length
}
, filter: function(stat){
var out = Object.keys(items)
.filter(function(uri){
return items[uri].stat === stat
})
.map(function(x){
return [x, items[x].meta]
})
//console.log("QUEUE filter", stat, out.length)
return out
}
, summary: function(){
return items
}
}
return res
}