-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlrucache.js
More file actions
81 lines (66 loc) · 1.97 KB
/
lrucache.js
File metadata and controls
81 lines (66 loc) · 1.97 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
80
81
const ClickNode = function(element) {
this.element = element;
this.clickCount = 1;
};
const ClickHistoryQueue = function(duration) {
this.elementsMap = new WeakMap || {};
this.recentClickCount = 0;
this.duration = duration;
this.idCounter = 1;
};
ClickHistoryQueue.prototype.elementClicked = function (element) {
this.recentClickCount += 1;
setTimeout(() => {
this.recentClickCount -= 1;
}, this.duration);
if (this.elementsMap[element._uniqueId]) {
this.updateElement(element._uniqueId);
} else {
element._uniqueId = this.idCounter++;
this.addElement(element);
}
};
ClickHistoryQueue.prototype.updateElement = function (elementId) {
const clickNode = this.elementsMap[elementId];
clickNode.clickCount += 1;
if (!clickNode.next) {
return;
}
if (clickNode.prev) {
clickNode.prev.next = clickNode.next;
}
clickNode.next.prev = clickNode.prev;
clickNode.prev = this.tail;
clickNode.next = undefined;
this.tail = clickNode;
};
ClickHistoryQueue.prototype.addElement = function (element) {
const newClickNode = new ClickNode(element);
this.elementsMap[element._uniqueId] = newClickNode;
if (!this.head) {
this.head = this.tail = newClickNode;
return;
}
newClickNode.prev = this.tail;
this.tail.next = newClickNode;
this.tail = newClickNode;
};
ClickHistoryQueue.prototype.mostRecentlyClicked = function (count) {
count = count || this.recentClickCount;
const mostRecent = new Array(count);
let currNode = this.tail;
for (let i = 0; i < count; i++) {
mostRecent[i] = currNode;
if (!currNode.prev) { break; }
currNode = currNode.prev;
}
return mostRecent.map((node) => { return node.element; });
};
ClickHistoryQueue.prototype.getClickCount = function (element) {
this.elementsMap[element._uniqueId].clickCount;
};
ClickHistoryQueue.getUnclickedLike = function(element) {
return Array.from(document.getElementsByClassName(element.className)).filter(el => {
return !el._uniqueId;
});
};