Repository navigation
Expand file tree
/
Copy paththreads.js
More file actions
79 lines (71 loc) · 1.67 KB
/
Copy paththreads.js
File metadata and controls
79 lines (71 loc) · 1.67 KB
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
(function(exports) {
'use strict';
var threads = new Map();
var rthread = /\bthread=(.+)$/;
var currentId, lastId;
function cacheId() {
var matches = rthread.exec(window.location.hash);
currentId = (matches && matches.length) ? +(matches[1].trim()) : null;
if (currentId !== null && currentId !== lastId) {
lastId = currentId;
}
return currentId;
}
function Thread(thread) {
for (var p in thread) {
this[p] = thread[p];
}
this.messages = [];
}
exports.Threads = {
set: function(id, thread) {
var old;
id = +id;
if (threads.has(id)) {
// Updates the reference
old = threads.get(id);
for (var p in thread) {
old[p] = thread[p];
}
return threads;
}
return threads.set(id, new Thread(thread));
},
get: function(id) {
return threads.get(+id);
},
has: function(id) {
return threads.has(+id);
},
delete: function(id) {
return threads.delete(+id);
},
clear: function() {
threads = new Map();
},
get size() {
// support: gecko 18 - size might be a function
if (typeof threads.size === 'function') {
return +threads.size();
}
return +threads.size;
},
get currentId() {
if (window.location.hash.startsWith('#thread=')) {
if (!currentId) {
currentId = cacheId();
}
} else {
currentId = null;
}
return currentId;
},
get lastId() {
return lastId;
},
get active() {
return Threads.get(Threads.currentId);
}
};
window.addEventListener('hashchange', cacheId);
}(this));