Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ObjList<T, ID>

A generic list of anything

The T type can be an object or a any literal.

The list is indexed using a number like a normal list. In the meantime, it offers a optional index generation for object.

The ID type will indicate how the object can be searched for. (only for object ignored for literal)

Type parameters

  • T

    The type of thing contained

  • ID

    An optional id type to be use for object idexing (default string)

Hierarchy

Implements

Index

Constructors

constructor

  • new ObjList(initialContent?: T[], idField?: string, idGenerator?: uuidv4): ObjList
  • Example:

    const myList<{name:string,id:string}>([],"id")
    myList.add$({name:'Nicolas'})

    Parameters

    • Default value initialContent: T[] = []

      An array with the initial content of the list.

    • Default value idField: string = undefined

      The field containing the object identification.

    • Default value idGenerator: uuidv4 = uuidv4

      A function that returns an id of the type compatible with ID.

    Returns ObjList

Properties

add$1

add$1: add$ = this.add$

Protected idField

idField: string

The field containing the object identification.

Private idGenerator

idGenerator: uuidv4

A function that returns an id of the type compatible with ID.

obs$

obs$: Observable<T[]>

An observable on the contained data.

Example:

const s = new State<number>(0)
s.obs$.filter(v => v%2==0 ).subscribe(stateContent => console.dir(stateContent))

push$1

push$1: add$ = this.add$1
see

add$1

Accessors

length$

  • get length$(): Observable<number>
  • Retrieve the number of element in the list.

    Returns Observable<number>

    An Observable on the number of elements.

updater$s

  • Property to retrieve a subject to be used to update the content.

    Examples:

    The following add a value to the content of a State that contain a list of number. Subscribers will receive updates. (see update for an example with more explanations)

    const s = new State<number[]>([1])
    s.obs$.subscribe(stateContent => console.dir(stateContent))
    const append => newVal => stateContent => [...stateContent, newVal]
    s.updater$s.next(append(2)); // see below the update syntax shortcut.

    It is also possible to directly set the state instead of passing a function updating the state

    const s = new State<number[]>([1])
    s.obs$.subscribe(stateContent => console.dir(stateContent))
    s.updater$s.next([3,4,5])

    In some case it is not required have any initial value

    const s = new State<number>() // no initial value
    s.obs$.subscribe(stateContent => console.dir(stateContent)) // triggered with a 1 second delay
    setTimeout(() => s.update(1), 1000) // delay the update

    Returns PartialObserver<StateUpdateFn<T[]> | T[]>

    A subject for updating the content.

    Remark: The returned observer is partial and will ignore error and complete messages.

Methods

add$

  • add$(element: T): Observable<number>
  • deprecated

    Parameters

    • element: T

    Returns Observable<number>

findById$

  • findById$(id: ID): Observable<T>

get$

  • get$(id: number): Observable<T>
  • Retrieve the element value from the list according to its possition in the list.

    Parameters

    • id: number

    Returns Observable<T>

    A hot observable to the element.

pop$1

  • pop$1(): Observable<T>
  • Remove the last element of the list.

    Returns Observable<T>

    A cold observable containing the value of the removed element in the list (observable will return undefined if the list is empty).

remove

  • remove(position: number): void
  • Remove an element using its position in the list.

    Parameters

    • position: number

      The position index of the element to remove.

    Returns void

removeById

  • removeById(id: ID): void
  • Remove all the element with a provided id. The element shall be an object, and the list be created with a proper idField

    Parameters

    • id: ID

      The id of the element to remove.

    Returns void

shift$1

  • shift$1(): Observable<T>
  • Remove an element at the start of the list.

    Returns Observable<T>

    a cold observable on the removed element (observable returns undefined if the list is empty).

unshift$1

  • unshift$1(element: T): Observable<number>
  • Add an element at the start of the list.

    Parameters

    • element: T

      The element to prepend to the list.

    Returns Observable<number>

    nothing.

update

  • Method to be used to update the content.

    Example 1:

    The following add a value to the content of a State that contain a list of number. Subscribers will receive updates.

    // Create a state with an initial value
    const s = new State<number[]>([1])
    
    // Subscribe to the state
    s.obs$.subscribe(stateContent => console.dir(stateContent))
    
    // transformation function that append a value at the end of an array and
    // return the new array.
    const append => newVal => stateContent => {return [...stateContent, newVal]}
    
    // Append 2 at the end of the array contained in the state s.
    // (Subscribers, like above will be triggered)
    s.update(append(2))

    Example 2:

    The following set the content of a State that contain a list of number. Subscribers will receive updates.

    // Create a state with an initial value
    const s = new State<number[]>([1])
    
    // Subscribe to the state
    s.obs$.subscribe(stateContent => console.dir(stateContent))
    
    // Set the new array contained in the state s.
    // (Subscribers, like above will be triggered)
    s.update([3,4])

    Example 3:

    The following exemple create a state without initial value. Later the content is set and latter it is updated

    // Create a state with an initial value
    
    const s = new State<number[]>()
    
    // Subscribe to the state
    s.obs$.subscribe(stateContent => console.dir(stateContent))
    
    // transformation function that append a value at the end of an array and
    // return the new array.
    const append => newVal => stateContent => [...stateContent, newVal]
    
    // Set an initial value
    // Set the new array contained in the state s.
    // (Subscribers, like above will be triggered)
    s.update([1])
    
    // Update the content by adding a value to the list
    // Set the new array contained in the state s.
    // (Subscribers, like above will be triggered)
    s.update(append(2));

    Parameters

    • fn: StateUpdateFn<T[]> | T[]

      A transformation function that takes as argument the current data of the state and return the updated data.

    Returns void

    nothing

Generated using TypeDoc