Record
Inherits Observable
Writeable observable which holds a record table with predefined keys and values.
Contains utility functions for observing and updating specific keys within this table, as well as cheaply oobserving specific keys within this table.
Constructor
Dex.Record
Dex.Record<T>(
initialValue T
) -> Record<T>
Creates a new Record observable with the given initial value.
Functions
Set
Record:
Set
(
key:
string
,
value:
any
) →
(
)
Sets the value at a specific key.
Current
Record:
Current
(
key:
any?
) →
any
|
T
Gets the current value at a specific key, or the current value of the whole record if no first argument is provided.
CAUTION
Currently, the value returned by StateRecord:Current()
with no first
parameter is mutable! Modifying this value directly may cause unexpected
behavior!
Replace
Record:
Replace
(
newRecord:
T
) →
(
)
Replaces entire record with a new value
Index
Creates a new observable which observes only a specific key within the record. Changes in other keys will not affect subscribers to this indexed state.
The returned observable is also a State object, and setting values in this state will set values in the original Record object.
Example:
local record = Dex.Record({
coins = 0,
items = {},
})
local coins = record:Index("coins") :: Dex.State<number>
local items = record:Index("items") :: Dex.State<number>
-- . . . Updates in stateRecord will be reflected in coins/items:
record:Replace({
coins = 100,
items = {"Sword"},
})
print(coins:Current()) -- 100
print(items:Current()[1]) -- "Sword"
-- . . . And vice versa!
coins:Set(42)
print(record:Current().coins) -- 42