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
nameis already an index in the list, the push will fail.forceReplacecan be passed astrueto 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.
pushFrontcan be passed astrueto 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
nameis not a valid key,nullwill be returned. - Technically speaking, when this method is successfully called, the
namekey will still exist in the collection, but will have a value ofnull.
orderOf(name: string): number
- Returns
name's position in the list [0 === front...n === end]. - If the key is not valid,
-1will be returned.
promote(name: string): boolean
- Reorder
nameone place toward the front of the list. - Returns true if the move was made, otherwise false (
namedid not exist, or is already ordered as0).
demote(name: string): boolean
- Reorder
nameone place toward the end of the list. - Returns true if the move was made, otherwise false (
namedid not exist, or is already ordered aslength - 1).
setOrder(name: string, order: number): boolean
- Reorder
namesuch that it's order isorder. - 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 (
namedid not exist,nameis already ordered asorder, ororderis invalid (-1,>= length)).