BufferQueue
This is the main object of the BufferQueue library.
Emitted events:
- 'added': when a new element to download is added to the queue
- 'removed': when an element is removed (and will not be downloaded)
- 'reseted': when the whole queue is reseted
- 'downloading': when a file is starting to be downloading (after being popped from the queue)
- 'failed': when a file could not be downloaded properly (status above 2xx)
- 'aborted': when a file download is aborted by an explicit abort() call
- 'success': when a file has been successfully downloaded and converted into an ArrayBuffer. Callback arguments are URL, ArrayBuffer, downloadTimeMs
new BufferQueue(options:
Object)
Extends
EventManager
Parameters
options (Object)
the option object
Instance Members
Get the level of priority of a given file
Parameters
str (string)
URL of the file to check
Returns
number
:
zero is the highest priority, -1 means the element is NOT
in the queue
Checks if a string is in the queue. Optionally, we can verify a specific
level only.
Parameters
str (string)
string to verify the presence in the queue
priority (number
= -1
)
OPTIONAL the priority (must be in
[0, level-1]
)
Returns
boolean
:
▸
isDownloadInProcess(str)
Checks if a given url is currently being downloaded
Parameters
Returns
boolean
:
true if being downloaded, false if not
▸
add(str, priority, priorityScore, true)
Add a string to the queue with a given priority.
If the string is already present in the queue with the same level of priority or higher, then nothing is done.
If the string is already present but with a different level of priority, then
it is removed and added with the provided level or priority.
Emits the event 'added' with the str as argument if properly added.
Parameters
str (string)
the string to add
priority (number)
the priority (must be in
[0, level-1]
)
priorityScore (any
= Infinity
)
true (boolean)
if added, false if not (because already in with a higher priority)
Check if the priority queue is empty (= if all the per-level-queues are all empty)
Returns
boolean
:
true if empty, false if not empty
Get the total number of elements in the priority queue, or optionnaly, for a specific
level of priority
Parameters
priority (number)
OPTIONAL level of priority
Get the size of the queue for each priority level
Returns
array
:
Note: this should be used as rarely as possible since it does not respect the logic
of a queue.
Remove an element. If null is returned, this means the element was not in the queue.
Emits the event 'removed' if the str as argument if properly removed
Parameters
null-null (string)
str, the element to remove
Returns
(string | null)
:
the element that was jsut removed
Reset the whole priority queue, empty it all. No value returned.
Emits the event 'reseted' without any argument
reset()
Abort the download of a specific file. If the file was actually being downloaded,
the aborted
event will be emitted.
Note: once a file is aborted, it is no longer in a queue.
Parameters
str (string)
the URL of the file to abort
Abort all the current downloads. aborted
event will be emitted.
abortAll()
Get a string status of the prioirty queue size per level and the number of
files currently being downloaded.
Returns
string
:
If priority scores are given, elements can be sorted based on that.
sortWithinLevel()
PriorityQueue
This priority queue works with levels of priority, 0 beeing the highest priority
and following level will be of decreasing priority.
In term of implementation, PriorityQueue instanciates N Queues, where N is the
number of priority levels. The number of priority levels has to be given at
the creation of a PriorityQueue instance.
new PriorityQueue(levels: any)
Parameters
Instance Members
Get the level of priority of a given string
Parameters
Returns
number
:
zero is the highest priority, -1 means the element is NOT
in the queue
Checks if a string is in the queue. Optionally, we can verify a specific
level only.
Parameters
str (string)
string to verify the presence in the queue
priority (number
= -1
)
OPTIONAL the priority (must be in
[0, level-1]
)
Returns
boolean
:
▸
add(str, priority, priorityScore, true)
Add a string to the queue with a given priority.
If the string is already present in the queue with the same level of priority or higher, then nothing is done.
If the string is already present but with a different level of priority, then
it is removed and added with the provided level or priority.
Parameters
str (string)
the string to add
priority (number)
the priority (must be in
[0, level-1]
)
priorityScore (any
= Infinity
)
true (boolean)
if added, false if not (because already in with a higher priority)
Get the the element with the highest priority and remove it from the queue
Returns
(string | null)
:
can return null if the queue is empty
This version of pop relies on the probability map to make sure that some lower
priorities items are getting popped every now and then, even if there are still
elements in higher priority queues
pop()
Check if the priority queue is empty (= if all the per-level-queues are all empty)
Returns
boolean
:
true if empty, false if not empty
Get the total number of elements in the priority queue, or optionnaly, for a specific
level of priority
Parameters
priority (number
= -1
)
OPTIONAL level of priority
Get the size of the queue for each priority level
Returns
array
:
Note: this should be used as rarely as possible since it does not respect the logic
of a queue.
Remove an element. If null is returned, this means the element was not in the queue.
Parameters
null-null (string)
str, the element to remove
Returns
(string | null)
:
the element that was jsut removed
Reset the whole priority queue, empty it all. No value returned.
reset()
Get a string status about the queue length per level
Returns
string
:
If a priotrity score is given to the elements, then each level of priority can
have their element sorted within.
sortWithinLevel()
Queue
A queue to add and pop string. It also provides an arbitrary remove function.
A queue is first-in-first-out.
Elements in this queue must be strings
new Queue()
Instance Members
▸
add(str, priorityScore)
Add a string at the end of the queue. Not added again if already in there.
add(str:
string, priorityScore: any)
Parameters
str (string)
some string to add
priorityScore (any
= Infinity
)
Check if this queue contains a given string
Parameters
Returns
boolean
:
true if this queue has a given string, false if not
Extract the first element
Returns
string
:
the first element
Is the queue empty?
Returns
boolean
:
true if empty, false if not
Get the number of element in the queue
size(number: any)
Parameters
Get the first element of the queue without removing it
(Not sure how useful is that)
Returns
string
:
Get the last element of the queue without removing it
(Not sure how useful is that)
Returns
string
:
Remove an element from the queue and returns it
Parameters
str (string)
an element to remove
Returns
(string | null)
:
Remove all the elements of the queue
reset()
If a priority score is given to the elements, then we can sort the queue based on that
sortByPriorityScore()
EventManager
The EventManager deals with events, create them, call them.
This class is mostly for being inherited from.
new EventManager()
Instance Members
▸
on(eventName, callback)
Define an event, with a name associated with a function
Parameters
eventName (String)
Name to give to the event
callback (Function)
function associated to the even
Emit the event(s). If multiple callbacks are tied to this event,
they will be called in the order they were declared.
Parameters
eventName (string)
name of the event to fire.
args (array
= []
)
argument to call the callback with.
Note that within the callback, those arguments will be "flattened" and not as an array.