components_GetPlaybackInfoTask.bs

import "pkg:/source/api/ApiClient.bs"
import "pkg:/source/api/apiPool.bs"
import "pkg:/source/roku_modules/log/LogMixin.brs"
import "pkg:/source/utils/config.bs"
import "pkg:/source/utils/misc.bs"

sub init()
  m.log = new log.Logger("GetPlaybackInfoTask")
  m.top.functionName = "getPlaybackInfoTask"
end sub

' Fetch this device's live session. That is ALL this task does.
'
' It used to build the whole report here — reading user settings, choosing
' labels, calling translate() — which was the wrong split three times over. The
' work is pure computation that needed no Task thread; every `m.global` read it
' made cost ~93 µs from here against ~2 µs from the render thread; and the built
' report was then cached for the life of the player, so a DoVi buffer-overflow
' fallback (transcode -> direct play) left the "i" button still reporting a
' transcode that had stopped.
'
' Now the network call is the only thing on this thread and PlayerHostView
' composes the report per press from this plus the cached PlaybackInfo. Rebuilding
' is cheap enough to do every time, which is what makes the report honest about a
' stream that changed underneath it.
'
' `session` is left invalid when the server returns nothing — that is a real
' state (the server has not registered playback yet), and the renderer says so
' rather than the task inventing an error section.
sub getPlaybackInfoTask()
  sessions = fetchJson(GetApi().BuildGetSessionsRequest({ "deviceId": m.global.device.serverDeviceName }), "sessions")

  session = invalid
  if isValidAndNotEmpty(sessions) then session = sessions[0]

  if not isValid(session)
    m.log.warn("No session returned for this device; the playback report will have no transcode detail")
  end if

  ' alwaysNotify on `data` — the observer has to fire on a refresh that returns
  ' an identical session, or the live rows would stop updating the moment the
  ' transcode settled into a steady state.
  m.top.data = { session: session }
end sub