CBuffer

CBuffer

(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), default: 0.
beforeIdx number Optional index to stop iterating before (exclusive), default: length of the buffer.
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(filter) → {Object}

Convenience method for getting the last element from the buffer.
Source:
Parameters:
Name Type Description
filter function optional filter to apply to elements. If filter is provided, the search for the last element starts from the end of the buffer and goes backwards until the filter returns true.
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()

Insert new element(s) to the buffer at the correct position according to the sort method. 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.

reset()

Reset the buffer discarding all elements
Source: