This repository was archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.js
More file actions
149 lines (115 loc) · 4.39 KB
/
queue.js
File metadata and controls
149 lines (115 loc) · 4.39 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var Node = require('./Node');
var rxjs = require('rxjs');
class Queue {
// var head = null;
// var tail;
// var observer: Observer<Node> = null;
// var this$PiFrame = null;
// var timerSubscription = null;
// // A timer subscription that keeps sending new images to the observer
// executeSubscription() {
// // Check if there is an element in the list
// if (this.head != null) {
// // If the current node at head is a folder, unsubscribe the listener
// if (this.head.data['id'].startsWith('folder')) {
// this.timerSubscription.unsubscribe();
// }
// // Pop a node from the list and pass on to observer
// this.observer.next(this.this$PiFrame.pop());
// } else {
// // If no nodes are left, unsubscribe from the timer
// this.timerSubscription.unsubscribe();
// console.log('No items left on the queue. Deactivating timer subscription.');
// }
// }
constructor() {
this.head = null;
this.tail = null;
this.timer = rxjs.timer(1000, 5000);
// Create a new observable for the head that is subscribed to. The observer is extracted and handled manually
this.observable = new rxjs.Observable(observer => {
console.log('A new observer has subscribed to the queue.');
// Store the observer in a variable to call .next() manually.
this.observer = observer;
});
}
/**
* Adds a new item to the queue
* @param newItem The new item to be pushed to the queue
*/
add(newItem) {
// Create a new Node from the item received
const node = new Node();
node.set(newItem);
// If the queue head is empty, add the new item received to the head and the tail
if (this.head == null) {
this.head = node;
this.tail = node;
// Log out the operation
console.log('Item with ID : ' + this.head.data['id'] + ' and name ' + this.head.data['name'] + ' added to head. Total number in the queue: ' + this.count());
// Begin emitting objects for the attached observer
if (this.observer != null) {
// Setup a timer to pop every 300 ms
// this.timer.subscribe(this.this$PiFrame.timerSubscription);
this.timerSubscription = this.timer.subscribe(() => this.executeSubscription(), () => {}, () => {});
}
} else {
// Set the new received node as the 'next' node of the current node
this.tail.next = node;
// Update the current tail with the one received
this.tail = node;
// Log out the operation
console.log('Item with ID : ' + this.tail.data['id'] + ' and name ' + this.tail.data['name'] + ' added to tail. Total number in the queue: ' + this.count());
}
}
/**
* Retrieves the first item from the queue.
*/
pop() {
// If head is null, there's nothing on the list
if (this.head == null) {
return null;
} else {
// Get the item that will be popped from the queue
const extractedItem = this.head;
// Set the next item in the queue as the new head
this.head = this.head.next;
// Returned the previous head
return extractedItem;
}
}
/**
* Count the number of items in the queue and return to the caller
*/
count() {
if (this.head == null) {
return 0;
} else {
let intCounter = 1;
let countHead = this.head;
while (countHead.next != null) {
intCounter++;
countHead = countHead.next;
}
return intCounter;
}
}
// A timer subscription that keeps sending new images to the observer
executeSubscription() {
// Check if there is an element in the list
if (this.head != null) {
// If the current node at head is a folder, unsubscribe the listener
if (this.head.data['folder']) {
this.timerSubscription.unsubscribe();
}
// Pop a node from the list and pass on to observer
this.observer.next(this.pop());
} else {
// If no nodes are left, unsubscribe from the timer
this.timerSubscription.unsubscribe();
console.log('No items left on the queue. Deactivating timer subscription.');
}
}
}
//Export to module to make it available globally
module.exports = Queue;