Foundation.OrderedUpdateDictionary

A container for Updatable elements which is both a dictionary and preserves insert order for updating (with the option to change order as needed).


Example

var dictionary = new Foundation.OrderedUpdateDictionary<Actor>();

dictionary.add("Foo", new Foundation.Actor());
dictionary.add("Bar", new Foundation.Actor());
dictionary.add("Baz", new Foundation.Actor());

// Access first item by index
console.log(dictionary["Foo"]);

// Find Bar's position in list [0 === front... 2 === end]
console.log(dictionary.orderOf("Bar")); // 1

// Call all actor updates, in the order: Foo, Bar, then Baz
dictionary.update();

// Move Bar one place toward the front
dictionary.promote('Bar')
console.log(dictionary.orderOf("Bar")); // 0

// Call all actor updates, in the order: Bar, Foo, then Baz
dictionary.update();


Implementation Details

Reference paths

  • '/foundation/_digest.ts'
  • '/foundation/core/actors/_digest.ts'
  • '/foundation/core/actors/OrderedUpdateDictionary.ts'

Extends

This type extends:

An update() call is passed down to all container entries (if the collection is active).

No other methods are overridden from the base (equals and free to be overridden at some point, I suppose).

Implements

This type does not implement any interfaces (except those in the base).


Public Properties

length: number

  • Returns the number of entries currently in the collection
  • Not settable

Constructors

()<T extends Updatable>

  • The object defaults to an active state of true.
  • The type can only store items which implement the Updatable interface.

(active: boolean)<T extends Updatable>

  • Explicitly set active state.
  • The type can only store items which implement the Updatable interface.

Instance Methods

add(name: string, entry: T, forceReplace: boolean = false, pushFront: boolean = false): boolean

  • Add an entry to the dictionary which is indexable on the collection as example[name].
  • For update ordering, the item will be ordered to the end of the list (the last item updated).
  • If name is already an index in the list, the push will fail. forceReplace can be passed as true to remove the old element and add the new element. IMPORTANT! The new element will still be ordered at the end.
  • By default, push orders new entries at the end of the list. pushFront can be passed as true to order the new element as the first in the list (first to be updated).
  • Returns true if the new element was added.

remove(name: string): T

  • Removes and returns the item indexed at name.
  • If name is not a valid key, null will be returned.
  • Technically speaking, when this method is successfully called, the name key will still exist in the collection, but will have a value of null.

orderOf(name: string): number

  • Returns name's position in the list [0 === front...n === end].
  • If the key is not valid, -1 will be returned.

promote(name: string): boolean

  • Reorder name one place toward the front of the list.
  • Returns true if the move was made, otherwise false (name did not exist, or is already ordered as 0).

demote(name: string): boolean

  • Reorder name one place toward the end of the list.
  • Returns true if the move was made, otherwise false (name did not exist, or is already ordered as length - 1).

setOrder(name: string, order: number): boolean

  • Reorder name such that it's order is order.
  • This will keep the order of all other entries (i.e. it works like calling demote/promote in a loop)
  • Returns true if the move was made, otherwise false (name did not exist, name is already ordered as order, or order is invalid (-1, >= length)).