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:
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
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<field id="invidious" type="node" bind="/Invidious" />
|
||||
<field id="videoQueue" type="node" bind="/VideoQueue" />
|
||||
<field id="bookmarksService" type="node" bind="/BookmarksService" />
|
||||
<field id="jobQueue" type="node" bind="/JobQueue" />
|
||||
<field id="screen" type="node" />
|
||||
<field id="feeds" type="array" onChange="OnFeedsChange" />
|
||||
<!--
|
||||
|
||||
+4
-2
@@ -1,8 +1,10 @@
|
||||
import "pkg:/components/VideoFeed/FeedLoadState.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
|
||||
@asynctask
|
||||
function VideoRowListContentTask(input as object)
|
||||
@job("VideoRowListContentJob")
|
||||
function ExecuteJob() as void
|
||||
input = JobGetInput()
|
||||
|
||||
feeds = input.feeds
|
||||
rowList = input.node
|
||||
|
||||
+20
-26
@@ -2,14 +2,17 @@ import "pkg:/components/Dialog/DialogUtils.bs"
|
||||
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
||||
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
|
||||
|
||||
' TODO:P1 handle case where all feedSources are empty
|
||||
@asynctask
|
||||
function VideoRowListRowContentTask(input as object) as object
|
||||
@job("VideoRowListRowContentJob")
|
||||
function ExecuteJob() as void
|
||||
try
|
||||
input = JobGetInput()
|
||||
rowList = input.rowList
|
||||
feedContentNode = input.feedContentNode
|
||||
invidiousNode = input.invidious
|
||||
|
||||
callbackNode = JobGetCallbackNode()
|
||||
cancallation = CancellationUtils.CreateCancellation(callbackNode, "cancel", true)
|
||||
|
||||
service = new Invidious.InvidiousService(invidiousNode)
|
||||
instance = service.GetInstance()
|
||||
|
||||
@@ -18,9 +21,10 @@ function VideoRowListRowContentTask(input as object) as object
|
||||
someContentReadySet = false
|
||||
|
||||
while true
|
||||
if m.top.cancel
|
||||
if JobIsCancelled()
|
||||
feedContentNode.loadState = FeedLoadState.None
|
||||
return invalid
|
||||
JobCancel()
|
||||
return
|
||||
end if
|
||||
|
||||
feedSources = feedContentNode.feedSources
|
||||
@@ -32,9 +36,8 @@ function VideoRowListRowContentTask(input as object) as object
|
||||
if not someContentReadySet
|
||||
rowList.someContentReady = false
|
||||
end if
|
||||
return {
|
||||
success: true
|
||||
}
|
||||
JobSuccess()
|
||||
return
|
||||
end if
|
||||
|
||||
feedSource = feedSources[feedSourcesIndex]
|
||||
@@ -46,9 +49,8 @@ function VideoRowListRowContentTask(input as object) as object
|
||||
if not someContentReadySet
|
||||
rowList.someContentReady = false
|
||||
end if
|
||||
return {
|
||||
success: true
|
||||
}
|
||||
JobSuccess()
|
||||
return
|
||||
end if
|
||||
feedContentNode.feedSourcesIndex = feedSourcesIndex
|
||||
continue while
|
||||
@@ -59,7 +61,7 @@ function VideoRowListRowContentTask(input as object) as object
|
||||
|
||||
response = service.MakeRequest(feedSource, {
|
||||
additionalQueryParams: feedSource.state.queryParams
|
||||
cancellation: m.top.cancellation
|
||||
cancellation: cancallation
|
||||
})
|
||||
|
||||
if not response.success
|
||||
@@ -67,9 +69,8 @@ function VideoRowListRowContentTask(input as object) as object
|
||||
if feedSources.Count() = 1
|
||||
parent = feedContentNode.getParent()
|
||||
parent.removeChild(feedContentNode)
|
||||
return {
|
||||
success: true
|
||||
}
|
||||
JobSuccess()
|
||||
return
|
||||
end if
|
||||
|
||||
continue while
|
||||
@@ -80,9 +81,8 @@ function VideoRowListRowContentTask(input as object) as object
|
||||
if feedSources.Count() = 1
|
||||
parent = feedContentNode.getParent()
|
||||
parent.removeChild(feedContentNode)
|
||||
return {
|
||||
success: true
|
||||
}
|
||||
JobSuccess()
|
||||
return
|
||||
end if
|
||||
|
||||
continue while
|
||||
@@ -170,15 +170,9 @@ function VideoRowListRowContentTask(input as object) as object
|
||||
|
||||
catch error
|
||||
HandleFeedLoadErrorDialog(error, false, rowList)
|
||||
return {
|
||||
success: false
|
||||
error: error
|
||||
}
|
||||
JobError(error)
|
||||
return
|
||||
end try
|
||||
|
||||
return {
|
||||
success: true
|
||||
}
|
||||
end function
|
||||
|
||||
function RemovePlaceHolderItems(contentNode as object)
|
||||
Reference in new issue
Block a user