Skip to main content

Record

Observable

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(
keystring,
valueany
) → ()

Sets the value at a specific key.

Current

Record:Current(keyany?) → 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(newRecordT) → ()

Replaces entire record with a new value

Index

Record:Index(keyany) → State<any>

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
Show raw api
{
    "functions": [
        {
            "name": "Set",
            "desc": "Sets the value at a specific key.",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 85,
                "path": "src/Observables/StateRecord.luau"
            }
        },
        {
            "name": "Current",
            "desc": "Gets the current value at a specific key, or the current value of the whole\nrecord if no first argument is provided.\n\n:::caution\nCurrently, the value returned by `StateRecord:Current()` with no first\nparameter is mutable! Modifying this value directly may cause unexpected\nbehavior!\n:::",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "any?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any | T"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 132,
                "path": "src/Observables/StateRecord.luau"
            }
        },
        {
            "name": "Replace",
            "desc": "Replaces entire record with a new value",
            "params": [
                {
                    "name": "newRecord",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 166,
                "path": "src/Observables/StateRecord.luau"
            }
        },
        {
            "name": "Index",
            "desc": "Creates a new observable which observes only a specific key within the\nrecord. Changes in other keys will not affect subscribers to this indexed\nstate.\n\nThe returned observable is also a State object, and setting values in this\nstate will set values in the original Record object.\n\nExample:\n```lua\nlocal record = Dex.Record({\n    coins = 0,\n    items = {},\n})\n\nlocal coins = record:Index(\"coins\") :: Dex.State<number>\nlocal items = record:Index(\"items\") :: Dex.State<number>\n\n-- . . . Updates in stateRecord will be reflected in coins/items:\nrecord:Replace({\n    coins = 100,\n    items = {\"Sword\"},\n})\nprint(coins:Current()) -- 100\nprint(items:Current()[1]) -- \"Sword\"\n\n-- . . . And vice versa!\ncoins:Set(42)\nprint(record:Current().coins) -- 42\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "State<any>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 247,
                "path": "src/Observables/StateRecord.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Record",
    "desc": "#### Inherits [Observable]\n\nWriteable observable which holds a record table with predefined keys and\nvalues.\n\nContains utility functions for observing and updating specific keys within\nthis table, as well as cheaply oobserving specific keys within this table.\n\n## Constructor\n\n---\n\n### Dex.Record\n```ts\nDex.Record<T>(\n    initialValue T\n) -> Record<T>\n```\n\nCreates a new [Record] observable with the given initial value.",
    "tags": [
        "Observable"
    ],
    "source": {
        "line": 36,
        "path": "src/Observables/StateRecord.luau"
    }
}