Add Job System (#656)

* Add Job System

* Remove testing stuff

* changelog

* plugin work

* Convert a couple more jobs

* simplify callback

* assert

* Logging changes and rendezvous perf

* changelog

* sb segment viewed job

* clear cache, and few changes

* Lint fix

* a couple more jobs

* reduce rendezvous in web server

* refactor

* Lint fix

* no message

* test shutdown

* comment

* free fields

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
Brahim Hadricheandgithub-action linter authored and GitHub committed 2025-06-17 20:13:48 -04:00
1 parent 45983a67af
commit d70a07049a
74 files changed
+1090 -298

No files matched your search

@@ -1,5 +1,7 @@
import "pkg:/components/ChannelView/ChannelUtils.bs"
import "pkg:/components/ContextMenu/ContextMenuUtils.bs"
import "pkg:/components/JobSystem/Jobs.bs"
import "pkg:/components/JobSystem/JobSystem.bs"
import "pkg:/components/Navigation/LongPress.bs"
import "pkg:/components/PlaylistView/PlaylistUtils.bs"
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
@@ -15,7 +17,7 @@ import "pkg:/source/utils/Types.bs"
' TODO:P1 add the option to remove a row if it does not have any content
' E.g. Subscriptions, Watch History and Playlists can be empty
function Init()
m.pendingLoadTasks = {}
m.pendingLoadJobs = {}
end function
function OnNodeReady()
@@ -213,33 +215,38 @@ function OnAuthTokenChange()
end function
function InitContent() as void
CancelCurrentTasks()
CancelCurrentJobs()
if m.top.feeds = invalid
m.top.content = invalid
return
end if
task = AsyncTask.Start(Tasks.VideoRowListContentTask, {
input = {
node: m.top
feeds: m.top.feeds
}, OnVideoRowListContentTaskResult)
m.pendingLoadTasks[task.id] = { task: task }
}
callback = JobSystem.CreateCallback(OnVideoRowListContentJobDone)
JobSystem.QueueJob(m.jobQueue, Jobs.VideoRowListContentJob, input, callback)
m.pendingLoadJobs[callback.id] = { callback: callback }
end function
function CancelCurrentTasks()
pendingLoadTasks = m.pendingLoadTasks
for each id in pendingLoadTasks
task = pendingLoadTasks[id].task
task.cancel = true
function CancelCurrentJobs()
pendingLoadJobs = m.pendingLoadJobs
for each id in pendingLoadJobs
callback = pendingLoadJobs[id].callback
callback.cancel = true
end for
m.pendingLoadTasks = {}
m.pendingLoadJobs = {}
end function
function OnVideoRowListContentTaskResult(output as object) as void
m.pendingLoadTasks.Delete(output.task.id)
function OnVideoRowListContentJobDone(event as object) as void
JobSystem.UnobserveCallback(event.getRoSGNode())
m.pendingLoadJobs.Delete(event.getNode())
if output.cancelled
info = event.getInfo()
if info.cancel
return
end if
@@ -290,10 +297,10 @@ function GetRowsToLoad() as object
end function
function LoadRowContent(contentNode as object) as void
' Do not spin up multiple tasks for the same content node
for each id in m.pendingLoadTasks
pendingTask = m.pendingLoadTasks[id]
if contentNode.isSameNode(pendingTask.content) and pendingTask.task.state = "run"
' Do not spin up multiple jobs for the same content node
for each id in m.pendingLoadJobs
job = m.pendingLoadJobs[id]
if contentNode.isSameNode(job.content) and not job.callback.done
LogInfo("Row content already loading:", contentNode.title)
return
end if
@@ -302,17 +309,23 @@ function LoadRowContent(contentNode as object) as void
LogInfo("Loading row content:", contentNode.title)
contentNode.loadState = FeedLoadState.Loading
task = AsyncTask.Start(Tasks.VideoRowListRowContentTask, {
input = {
rowList: m.top
feedContentNode: contentNode
invidious: m.invidious
}, OnVideoRowListRowContentTaskResult)
m.pendingLoadTasks[task.id] = { task: task, content: contentNode }
}
callback = JobSystem.CreateCallback(OnVideoRowListRowContentJobDone)
JobSystem.QueueJob(m.jobQueue, Jobs.VideoRowListRowContentJob, input, callback)
m.pendingLoadJobs[callback.id] = { callback: callback, content: contentNode }
end function
function OnVideoRowListRowContentTaskResult(output as object) as void
m.pendingLoadTasks.Delete(output.task.id)
if output.cancelled
function OnVideoRowListRowContentJobDone(event as object) as void
JobSystem.UnobserveCallback(event.getRoSGNode())
m.pendingLoadJobs.Delete(event.getNode())
info = event.getInfo()
if info.cancel
return
end if
@@ -347,7 +360,7 @@ function OnKeyEvent(key as string, press as boolean) as boolean
end function
function OnDispose()
CancelCurrentTasks()
CancelCurrentJobs()
DisposeObservers()
m.top.screen = invalid
m.top.content = invalid