List State Operations

The State provider has list operations for storing state.

Push Elements at the List's Start

Using a list name, you can add an element to a list at the start.

Method Signature

listPrepend<T>(list: string, value: T)

Pushing Elements at the List's Start

await state.listPrepend("list", "bar");

Push Elements at the List's End

Using a list name, you can add an element to a list at the end.

Method Signature

listAppend<T>(list: string, value: T)

Pushing Elements at the List's End

await state.listAppend("list", "foo");

Insert an Element in a Relative Position

You can add an element to a list before or after an existing element. The before boolean is used to decide to place the element before (true) or after (false) the pivot value (the existing element).

Method Signature

listInsert<T>(list: string, before: boolean, pivot: T, value: T)

Inserting an Element Relatively

await state.listInsert('list', false, 'foo', 'bar');

Overwrite the Value of an Element at a Specific Index

You can overwrite the value of an element at a specific index. There needs to be a value already at the position specified.

Method Signature

listSet<T>(list: string, position: number, value: T)

Overwriting the Value of an Element at a Specific Index

await state.listSet('list', 0, 'foo');

Get an Element at a Specific Index

You can get an element at a specific index.

Method Signature

listIndex<T>(list: string, position: number)

Getting an Element at a Specific Index

const element = await state.listIndex('list', 0);

Get a List's Length

Using a list name, you can get the length of the list.

Method Signature

listLength(list: string)

Getting a List's Length

const listLength = await state.listLength("list");

Get a List's Elements With a Range

Using a list name and a range, you can get the elements in the specified range.

Method Signature

listRange<T>(list: string, startPos: number, endPos: number)

Getting a List's Elements With a Range

const elements = await state.listRange("list", 0, 2);

Remove List Elements

You can remove elements from a list by specifying a value. You can optionally provide a count to control how many elements get removed if there are duplicates.

Method Signature

listRemove<T>(list: string, value: T, count?: number)

Removing List Elements

await state.listRemove('list', 'foo');

Pop Elements at the List's Start

You can pop elements from the start of a list and have them returned to you. You optionally pass a count to pop more than one element at a time.

Method Signature

listStartPop<T>(list: string, count?: number)

Popping Elements at the List's Start

await state.listStartPop("list");

Pop Elements at the List's End

You can pop elements from the end of a list and have them returned to you.

Method Signature

listEndPop<T>(list: string, count?: number)

Popping Elements at the List's End

await state.listEndPop("list");

Trim a List

You can remove elements from a list by specifying a range. The removed elements will not be returned to you.

Method Signature

listTrim<T>(list: string, startPos: number, endPos: number)

Trimming a List

await state.listTrim("list", 0, 1);