components_api_ApiTask.bs
import "pkg:/source/api/baseRequest.bs"
import "pkg:/source/utils/misc.bs"
sub init()
m.top.functionName = "runApiLoop"
end sub
' Continuous-server event loop (Architecture C).
' Waits for request field writes, executes HTTP via roku-requests, writes response.
' Never exits — lives for the lifetime of the app alongside the global pool.
sub runApiLoop()
port = CreateObject("roMessagePort")
m.top.observeField("request", port)
' Signal that our observer is registered. The ApiQueueTask coordinator waits
' for all pool slots to be ready before dispatching — without this, the
' coordinator could write to our request field before we're listening,
' causing a permanently dead slot.
m.top.isReady = true
while true
msg = wait(0, port)
if type(msg) = "roSGNodeEvent" and msg.getField() = "request"
' Safe to read m.top.request directly: the coordinator only dispatches
' one request per slot at a time (m.inFlight prevents reuse until we
' write the response), so the field always reflects our pending request.
req = m.top.request
if isValid(req)
m.top.response = executeRequest(req)
end if
end if
end while
end sub
' Executes a single HTTP request via the shared executeHttpRequest() helper and shapes
' the roku-requests result into the pool's response AA (invalid URL → an error AA so the
' waiting orchestrator still unblocks). Auth/timeout/Content-Type live in the helper.
' @param req - request AA: { requestId, method, url, headers?, body?, timeout? }
' @return response AA: { requestId, ok, statusCode, json, text }
function executeRequest(req as object) as object
r = executeHttpRequest(req, "GET", "[ApiTask]")
if not isValid(r)
return {
requestId: req.requestId,
ok: false,
statusCode: 0,
json: invalid,
text: ""
}
end if
return {
requestId: req.requestId,
ok: r.ok,
statusCode: r.statusCode,
json: r.json,
text: r.text
}
end function