@umbraco-cms/backoffice

    Class UmbArrayState<T, U>

    UmbArrayState

    • A RxJS BehaviorSubject which deepFreezes the object-data to ensure its not manipulated from any implementations. Additionally the Subject ensures the data is unique, not updating any Observes unless there is an actual change of the content.

    The ArrayState provides methods to append data when the data is an Object.

    Type Parameters

    • T
    • U = unknown

    Hierarchy (View Summary, Expand)

    Index

    Constructors

    Properties

    _subject: BehaviorSubject<T[]>
    getUniqueMethod: (entry: T) => U

    Accessors

    • get value(): T

      Returns T

      Observable that

      • Holds the current data of this state.
      const myState = new UmbArrayState('Hello world');
      console.log("Value is: ", myState.value);

    Methods

    • Function

      append

      Parameters

      • entries: T[]

        A array of new data to be added in this Subject.

      Returns UmbArrayState<T, U>

      Reference to it self.

      • Append some new data to this Subject, if it compares to existing data it will replace it.
      const data = [
      { key: 1, value: 'foo'},
      { key: 2, value: 'bar'}
      ];
      const myState = new UmbArrayState(data);
      myState.append([
      { key: 1, value: 'replaced-foo'},
      { key: 3, value: 'another-bla'}
      ]);
    • Function

      appendOne

      Parameters

      • entry: T

        new data to be added in this Subject.

      Returns UmbArrayState<T, U>

      Reference to it self.

      • Append some new data to this Subject.
      const data = [
      { key: 1, value: 'foo'},
      { key: 2, value: 'bar'}
      ];
      const myState = new UmbArrayState(data);
      myState.append({ key: 1, value: 'replaced-foo'});
    • Function

      appendOneAt

      Parameters

      • entry: T

        new data to be added in this Subject.

      • index: number

        index of where to append this data into the Subject.

      Returns UmbArrayState<T, U>

      Reference to it self.

      • Append some new data to this Subject.
      const data = [
      { key: 1, value: 'foo'},
      { key: 3, value: 'bar'}
      ];
      const myState = new UmbArrayState(data);
      myState.appendOneAt({ key: 2, value: 'in-between'}, 1);
    • Function

      asObservable

      Returns Observable<T[]>

      Observable that the State casts to.

      • Creates a new Observable with this State as the source. Observe this to subscribe to its value and future changes.
      const myState = new UmbArrayState('Hello world');

      this.observe(myState, (latestStateValue) => console.log("Value is: ", latestStateValue));
    • Function

      filter

      Parameters

      • predicate: (value: T, index: number, array: T[]) => boolean

      Returns UmbArrayState<T, U>

      Reference to it self.

      • Remove some new data of this Subject.

      Example: Example remove entry with key '1'

      const data = [
      { key: 1, value: 'foo'},
      { key: 2, value: 'bar'},
      { key: 3, value: 'poo'}
      ];
      const myState = new UmbArrayState(data, (x) => x.key);
      myState.filter((entry) => entry.key !== 1);

      Result:
      [
      { key: 2, value: 'bar'},
      { key: 3, value: 'poo'}
      ]
    • Function

      getHasOne

      Parameters

      • unique: U

        the unique value to compare with.

      Returns boolean

      Wether it existed

      • Check if a unique value exists in the current data of this Subject.
      const data = [
      { key: 1, value: 'foo'},
      { key: 2, value: 'bar'}
      ];
      const myState = new UmbArrayState(data, (x) => x.key);
      myState.hasOne(1);
    • Function

      getValue

      Returns T[]

      The current data of this state.

      • Provides the current data of this state.
      const myState = new UmbArrayState('Hello world');
      console.log("Value is: ", myState.value);
    • Function

      remove

      Parameters

      • uniques: U[]

        The unique values to remove.

      Returns UmbArrayState<T, U>

      Reference to it self.

      • Remove some new data of this Subject.

      Example: Example remove entry with id '1' and '2'

      const data = [
      { id: 1, value: 'foo'},
      { id: 2, value: 'bar'}
      ];
      const myState = new UmbArrayState(data, (x) => x.id);
      myState.remove([1, 2]);
    • Function

      removeOne

      Parameters

      • unique: U

        The unique value to remove.

      Returns UmbArrayState<T, U>

      Reference to it self.

      • Remove some new data of this Subject.

      Example: Example remove entry with id '1'

      const data = [
      { id: 1, value: 'foo'},
      { id: 2, value: 'bar'}
      ];
      const myState = new UmbArrayState(data, (x) => x.id);
      myState.removeOne(1);
    • Function

      setValue

      Parameters

      • value: T[]

      Returns void

      • Set the data of this state, if sortBy has been defined for this state the data will be sorted before set. If data is different than current this will trigger observations to update.
      const myState = new UmbArrayState('Good morning');
      // myState.value is equal 'Good morning'.
      myState.setValue('Goodnight')
      // myState.value is equal 'Goodnight'.
    • Function

      sortBy

      Parameters

      • OptionalsortMethod: (a: T, b: T) => number

        A method to be used for sorting every time data is set.

      Returns UmbArrayState<T, U>

      Reference to it self.

      • A sort method to this Subject.
      const data = [
      { key: 1, value: 'foo'},
      { key: 2, value: 'bar'}
      ];
      const myState = new UmbArrayState(data, (x) => x.key);
      myState.sortBy((a, b) => (a.sortOrder || 0) - (b.sortOrder || 0));
    • Function

      updateOne

      Parameters

      • unique: U

        Unique value to find entry to update.

      • entry: Partial<T>

        new data to be added in this Subject.

      Returns UmbArrayState<T, U>

      Reference to it self.

      • Update a item with some new data, requires the ArrayState to be constructed with a getUnique method.
      const data = [
      { key: 1, value: 'foo'},
      { key: 2, value: 'bar'}
      ];
      const myState = new UmbArrayState(data, (x) => x.key);
      myState.updateOne(2, {value: 'updated-bar'});
    MMNEPVFCICPMFPCPTTAAATR