• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3function init(list) {
4  list._idleNext = list;
5  list._idlePrev = list;
6}
7
8// Show the most idle item.
9function peek(list) {
10  if (list._idlePrev === list) return null;
11  return list._idlePrev;
12}
13
14// Remove an item from its list.
15function remove(item) {
16  if (item._idleNext) {
17    item._idleNext._idlePrev = item._idlePrev;
18  }
19
20  if (item._idlePrev) {
21    item._idlePrev._idleNext = item._idleNext;
22  }
23
24  item._idleNext = null;
25  item._idlePrev = null;
26}
27
28// Remove an item from its list and place at the end.
29function append(list, item) {
30  if (item._idleNext || item._idlePrev) {
31    remove(item);
32  }
33
34  // Items are linked  with _idleNext -> (older) and _idlePrev -> (newer).
35  // Note: This linkage (next being older) may seem counter-intuitive at first.
36  item._idleNext = list._idleNext;
37  item._idlePrev = list;
38
39  // The list _idleNext points to tail (newest) and _idlePrev to head (oldest).
40  list._idleNext._idlePrev = item;
41  list._idleNext = item;
42}
43
44function isEmpty(list) {
45  return list._idleNext === list;
46}
47
48module.exports = {
49  init,
50  peek,
51  remove,
52  append,
53  isEmpty
54};
55