This repository was archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-fqueue.js
72 lines (60 loc) · 2.84 KB
/
angular-fqueue.js
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
(function () {
'use strict';
angular.module('fqueue', [])
.factory('FixedQueue', function () {
function FixedQueue(size, initialValues ) {
initialValues = (initialValues || []);
// Create the fixed queue array value.
var queue = Array.apply(null, initialValues);
// Store the fixed size in the queue.
queue.fixedSize = size;
// Add the class methods to the queue. Some of these have
// to override the native Array methods in order to make
// sure the queue length is maintained.
queue.push = FixedQueue.push;
queue.splice = FixedQueue.splice;
queue.unshift = FixedQueue.unshift;
// Trim any initial excess from the queue.
FixedQueue.trimTail.call(queue);
// Return the new queue.
return queue;
}
// Trims the queue down to the appropriate size, removing
// items from the beginning of the internal array.
FixedQueue.trimHead = function() {
if (this.length <= this.fixedSize) {
return;
}
Array.prototype.splice.call(this, 0, (this.length - this.fixedSize));
};
// Trims the queue down to the appropriate size, removing
// items from the end of the internal array.
FixedQueue.trimTail = function() {
if (this.length <= this.fixedSize){
return;
}
Array.prototype.splice.call(this, this.fixedSize, (this.length - this.fixedSize));
};
// Calls the native Array methods followed by a trimming method.
FixedQueue.wrapMethod = function(methodName, trimMethod) {
// Create a wrapper that calls the given method.
var wrapper = function() {
// Get the native Array method.
var method = Array.prototype[methodName];
// Call the native method first.
var result = method.apply(this, arguments);
// Trim the queue now that it's been augmented.
trimMethod.call(this);
// Return the original value.
return result;
};
// Return the wrapper method.
return wrapper;
};
FixedQueue.push = FixedQueue.wrapMethod("push", FixedQueue.trimHead);
FixedQueue.splice = FixedQueue.wrapMethod("splice", FixedQueue.trimTail);
FixedQueue.unshift = FixedQueue.wrapMethod('unshift', FixedQueue.trimTail);
/* return the constructor function */
return FixedQueue;
});
})();