pesde goatsoft / lucy

useRequest

Lucy.useRequest asks the server a question and keeps the answer as state: a leaderboard, a shop's stock for today, a friend's profile. It asks again when what it asks about changes. While a call is out, loading() is true. An answer that comes back after the player has moved on is dropped.

Usage

local request = Lucy.useRequest(app, name, args?, options?)
ArgumentTypeDefaultWhat it is
appApprequiredThe App, made with server = true
namestringrequiredThe request's name, as the server handles it
argsfunction or tablenoneA function returning the call's arguments, read reactively, or a list of them
optionstable?{}See below

Options

OptionTypeDefaultWhat it does
enabledMaybeRef<boolean>trueNo calls while false, such as while a dialog is closed. Turning true asks
everynumberAsks again this many seconds apart while enabled, keeping the answer it has meanwhile
keepbooleanfalseKeeps the last answer while the next arguments load, instead of clearing it

Returns

NameTypeWhat it is
data()() -> anyThe server's answer. nil until the first one, and cleared when the arguments change unless keep
loading()() -> booleantrue while a call is out
error()() -> string?The last call's error message. nil again once a call succeeds. The answer it had stays in data
refresh()() -> ()Asks again now, keeping the answer meanwhile

When args reads a ref and the ref changes, it asks again. Only the newest call's answer lands, so switching tabs quickly never leaves the wrong board showing. Stopping the owner drops any answer still to come.

Example: Club Lucy's Hype board

The Hype board for the week picked in the dialog, while it is open:

local period = app.state:ref("boards.period")
local open = app.state:ref("ui.leaderboards.open")

local hype = Lucy.useRequest(app, "boards.get", function()
    return "hype", period()
end, { enabled = open, every = 30 })

Lucy.Leaderboard.new({
    rows = function()
        local data = hype.data()
        return if data then data.rows else {}
    end,
    own = function()
        local data = hype.data()
        return data and data.me
    end,
    loading = function()
        return hype.data() == nil and hype.error() == nil
    end,
    Parent = hypeDialog,
})

The server side, boards.get, comes from Lucy.Boards. The Leaderboards recipe puts the two together.