CBuffer

Tinode. CBuffer

(protected) new CBuffer(compare, unique)

In-memory sorted cache of objects.
Source:
Parameters:
Name Type Description
compare function custom comparator of objects. Returns -1 if a < b, 0 if a == b, 1 otherwise.
unique boolean enforce element uniqueness: replace existing element with a new one on conflict.

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).

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 function `callback` to all elements of the buffer.
Source:
Parameters:
Name Type Description
callback Tinode.ForEachCallbackType Function to call for each element.
startIdx integer Optional index to start iterating from (inclusive).
beforeIdx integer 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() → {Object}

Convenience method for getting the last element of the buffer.
Source:
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.

reset()

Reset the buffer discarding all elements
Source:

Type Definitions

ForEachCallbackType(elem, index)

Callback for iterating contents of buffer. See Tinode.CBuffer#forEach.
Source:
Parameters:
Name Type Description
elem Object Element of the buffer.
index number Index of the current element.