Skip to main content

Provider class

๐Ÿ“– Descriptionโ€‹

The Provider class provides access to a collection in the Tripetto builder.

tip

To construct new collections, use the of function.

๐Ÿ—ƒ๏ธ Fieldsโ€‹


๐Ÿท๏ธ allโ€‹

Retrieves all the items that are in the collection.

Typeโ€‹

Item[]


๐Ÿท๏ธ blockโ€‹

Retrieves the block for the collection (if available).

Typeโ€‹

NodeBlock | ConditionBlock | undefined


๐Ÿท๏ธ countโ€‹

Retrieves the number of items in the collection.

Typeโ€‹

number


๐Ÿท๏ธ firstItemโ€‹

Retrieves the first item in the collection.

Typeโ€‹

Item | undefined


๐Ÿท๏ธ lastItemโ€‹

Retrieves the last item in the collection.

Typeโ€‹

Item | undefined


๐Ÿท๏ธ refโ€‹

Retrieves a reference to the parent of the collection.

info

The reference is set using the reference argument of the of function.

Typeโ€‹

NodeBlock | ConditionBlock | Item

โ–ถ๏ธ Methodsโ€‹


๐Ÿ”ง appendโ€‹

Appends a new item to the end of the collection.

tip

The optional onCreate parameter is only used under certain conditions. In most cases, you can omit it and call append().

Signatureโ€‹

append(onCreate?: (item: Item) => void): Item

Parametersโ€‹

NameTypeOptionalDescription
onCreate(item: Item) => voidYesSpecifies a function that can be used to set item properties before @created decorated methods are invoked.

Return valueโ€‹

Returns a reference to the new Item instance.


๐Ÿ”ง duplicateโ€‹

Duplicates an item in the collection.

caution

Duplicating an item in a collection requires two steps:

  • Step 1: Call this duplicate method;
  • Step 2: Set the properties of the new duplicated item;
  • Step 3: Call the done function returned by the duplicate method to indicate the item is ready.

Signatureโ€‹

duplicate(src: Item): {
item: Item;
done: () => void;
}

Parametersโ€‹

NameTypeOptionalDescription
srcItemNoSpecifies the source item to duplicate.

Return valueโ€‹

Returns an object with a reference to the duplicate and a function that should be called when the duplicated item is ready. This allows you to configure the duplicated item.


๐Ÿ”ง eachโ€‹

Iterates through all items in the collection.

Signatureโ€‹

each(fn: (item: Item) => boolean | void): boolean

Parametersโ€‹

NameTypeOptionalDescription
fn(item: Item) => boolean | voidNoFunction that is called for each item in the collection. If you want to stop the iteration, you can return true from this function to do so.

Return valueโ€‹

Returns true if the iteration was stopped (break).


๐Ÿ”ง insertโ€‹

Inserts a new item to the collection.

tip

The optional onCreate parameter is only used under certain conditions. In most cases, you can omit it and call insert().

Signatureโ€‹

insert(index?: number, onCreate?: (item: Item) => void): Item

Parametersโ€‹

NameTypeOptionalDescription
indexnumberYesSpecifies the index of the new item (default is 0).
onCreate(item: Item) => voidYesSpecifies a function that can be used to set item properties before @created decorated methods are invoked.

Return valueโ€‹

Returns a reference to the new Item instance.


๐Ÿ”ง itemByIdentifierโ€‹

Retrieves the item with the specified identifier.

Signatureโ€‹

itemByIdentifier(id: string): Item | undefined

Parametersโ€‹

NameTypeOptionalDescription
idstringNoSpecifies the identifier.

Return valueโ€‹

Returns the Item or undefined if the item is not found.


๐Ÿ”ง refreshโ€‹

Refreshes all items in the collection.

Signatureโ€‹

refresh(): void

๐Ÿ”ง reverseEachโ€‹

Iterates through all items in the collection in reverse direction (starting with the last item).

Signatureโ€‹

reverseEach(fn: (item: Item) => boolean | void): boolean

Parametersโ€‹

NameTypeOptionalDescription
fn(item: Item) => boolean | voidNoFunction that is called for each item in the collection. If you want to stop the iteration, you can return true from this function to do so.

Return valueโ€‹

Returns true if the iteration was stopped (break).


๐Ÿ”ง sortโ€‹

Sorts the items using the supplied compare function.

Signatureโ€‹

sort(fn: (a: Item, b: Item) => number): boolean

Parametersโ€‹

NameTypeOptionalDescription
fn(a: Item, b: Item) => booleanNoSpecifies the compare function. The compare function should return a number according to the following rules:
- a is less than b return -1;
- a is greater than b return 1;
- a is equal to b return 0.

Return valueโ€‹

Returns true if the sort invoked an item position change.