Options
All
  • Public
  • Public/Protected
  • All
Menu

This class is one of several convenience collections built on top of the Storage class exposed by the NEAR platform. It implements queue -- a persistent bidirectional queue (aka double-ended queue or deque)

To create a deque

let dq = new PersistentDeque<string>("q")  // choose a unique prefix per account

To use the deque

dq.pushFront(value)
dq.popBack()

IMPORTANT NOTE: Since all data stored on the blockchain is kept in a single key-value store under the contract account, you must always use a unique storage prefix for different collections to avoid data collision.

Type parameters

Hierarchy

  • PersistentDeque

Index

Constructors

constructor

  • Creates or restores a persistent deque with a given storage prefix. Always use a unique storage prefix for different collections.

    Example

    let dq = new PersistentDeque<string>("q") // note the prefix must be unique (per NEAR account)
    

    Parameters

    • prefix: string

      A prefix to use for every key of this deque.

    Returns PersistentDeque

Accessors

back

  • get back(): T
  • Returns the last element in the deque without changing the queue

    let dq = new PersistentDeque<string>("q")
    
    dq.pushFront("world")
    dq.pushFront("hello")
    
    assert(dq.back == "world")
    assert(dq.length == 2)
    

    Returns T

    The last/back element of the deque.

first

  • get first(): T

front

  • get front(): T
  • Returns the first element in the deque without changing the queue

    let dq = new PersistentDeque<string>("q")
    
    dq.pushFront("world")
    dq.pushFront("hello")
    
    assert(dq.front == "hello")
    assert(dq.length == 2)
    

    Returns T

    The first/front element of the deque.

isEmpty

  • get isEmpty(): bool
  • Returns a boolean indicating whether the deque is empty

    let dq = new PersistentDeque<string>("q")
    
    dq.isEmpty // true
    dq.pushFront("hello world")
    dq.isEmpty // false
    

    Returns bool

    True if the deque is empty.

last

  • get last(): T

length

  • get length(): i32

Methods

containsIndex

  • containsIndex(index: i32): bool
  • Checks whether the index is within the range of the deque indices

    let dq = new PersistentDeque<string>("q")
    
    dq.containsIndex(0) // false
    dq.pushFront("hello world")
    dq.containsIndex(0) // true
    

    Parameters

    • index: i32

      The index to check.

    Returns bool

    True if the given index is within the range of the deque indices.

popBack

  • popBack(): T
  • Removes the last/back element from the deque and returns it. Asserts that the deque is not empty. Decreases the length of the deque.

    let dq = new PersistentDeque<string>("q")
    
    dq.pushFront("world")
    dq.pushFront("hello")
    let element = dq.popBack()
    
    assert(element == "world")
    assert(dq.length == 1)
    

    Returns T

    The removed first element of the queue.

popFront

  • popFront(): T
  • Removes the first/front element from the deque and returns it. Asserts that the deque is not empty. Decreases the length of the deque.

    let dq = new PersistentDeque<string>("q")
    
    dq.pushFront("world")
    dq.pushFront("hello")
    let element = dq.popFront()
    
    assert(element == "hello")
    assert(dq.length == 1)
    

    Returns T

    The removed first element of the queue.

pushBack

  • pushBack(element: T): i32
  • Adds a new element to the end of the deque. Increases the length of the deque.

    let dq = new PersistentDeque<string>("q")
    
    dq.pushBack("hello")
    dq.pushBack("world")
    dq[0] // hello
    

    Parameters

    • element: T

      A new element to add.

    Returns i32

    The index of a newly added element

pushFront

  • pushFront(element: T): i32
  • Adds a new element in front of the deque. Increases the length of the deque.

    let dq = new PersistentDeque<string>("q")
    
    dq.pushFront("world")
    dq.pushFront("hello")
    dq[0] // "hello"
    

    Parameters

    • element: T

      A new element to add.

    Returns i32

    The index of a newly added element

Generated using TypeDoc