(protected) new CBuffer(compare, unique)
In-memory sorted cache of objects.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
compare |
function
|
custom comparator of objects. Takes two parameters a and b ;
returns -1 if a < b , 0 if a == b , 1 otherwise.
|
unique |
boolean
|
enforce element uniqueness: when true replace existing element with a new
one on conflict; when false keep both elements.
|
Methods
delAt(at) → {Object}
Remove element at the given position.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
at |
number
|
Position to delete at. |
Returns:
- Type:
-
Object
Element at the given position or
undefined
.
delRange(since, before) → {Array}
Remove elements between two positions.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
since |
number
|
Position to delete from (inclusive). |
before |
number
|
Position to delete to (exclusive). |
Returns:
- Type:
-
Array
array of removed elements (could be zero length).
filter(callback, context)
Remove all elements that do not pass the test implemented by the provided callback function.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
callback |
Tinode.FilterCallbackType
|
Function to call for each element. |
context |
Object
|
calling context (i.e. value of this in the callback)
|
find(elem, nearestopt) → {number}
Find element in buffer using buffer's comparison function.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
elem |
Object
|
element to find. | |
nearest |
boolean
|
<optional> |
when true and exact match is not found, return the nearest element (insertion point). |
Returns:
- Type:
-
number
index of the element in the buffer or -1.
forEach(callback, startIdx, beforeIdx, context)
Apply given
callback
to all elements of the buffer.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
callback |
Tinode.ForEachCallbackType
|
Function to call for each element. |
startIdx |
number
|
Optional index to start iterating from (inclusive). |
beforeIdx |
number
|
Optional index to stop iterating before (exclusive). |
context |
Object
|
calling context (i.e. value of this in callback)
|
getAt(at) → {Object}
Get an element at the given position.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
at |
number
|
Position to fetch from. |
Returns:
- Type:
-
Object
Element at the given position or
undefined
.
getLast(at) → {Object}
Convenience method for getting the element from the end of the buffer.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
at |
number
|
position to fetch from, counting from the end;
undefined or null mean "last".
|
Returns:
- Type:
-
Object
The last element in the buffer or
undefined
if buffer is empty.
length() → {number}
Return the number of elements the buffer holds.
- Source:
Returns:
- Type:
-
number
Number of elements in the buffer.
put()
Add new element(s) to the buffer. Variadic: takes one or more arguments. If an array is passed as a single
argument, its elements are inserted individually.
- Source:
Parameters:
Type | Description |
---|---|
Object
|
Array
|
One or more objects to insert. |
Type Definitions
FilterCallbackType(elem, index) → {boolen}
Callback for filtering the buffer. See Tinode.CBuffer#filter.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
elem |
Object
|
Current element of the buffer. |
index |
number
|
Index of the current element. |
Returns:
- Type:
-
boolen
true
to keep the element, false
to remove.
ForEachCallbackType(elem, prev, next, index)
Callback for iterating contents of buffer. See Tinode.CBuffer#forEach.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
elem |
Object
|
Current element of the buffer. |
prev |
Object
|
Previous element of the buffer. |
next |
Object
|
Next element of the buffer. |
index |
number
|
Index of the current element. |