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