From b1e64427dd63ff163735840f31115c47866d1a3f Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Wed, 18 Jun 2025 11:41:14 -0400 Subject: [PATCH] Playlist job system (#659) --- .../ChannelView/FetchSubscriptionStatusJob.bs | 6 +- .../components/ChannelView/SubscribeJob.bs | 6 +- .../ContentNode/ChannelContentJob.bs | 4 +- ...stContentTask.bs => PlaylistContentJob.bs} | 40 +++++------- .../ContentNode/PlaylistContentNode.bs | 65 +++++++++---------- playlet-lib/src/components/MainScene.xml | 3 +- .../MainScene_bindings.transpiled.brs | 3 +- .../components/PlaylistView/PlaylistView.bs | 3 +- .../components/PlaylistView/PlaylistView.xml | 1 + .../VideoFeed/VideoRowListRowContentJob.bs | 4 +- .../src/components/VideoQueue/VideoQueue.bs | 4 +- .../src/components/VideoQueue/VideoQueue.xml | 1 + 12 files changed, 66 insertions(+), 74 deletions(-) rename playlet-lib/src/components/ContentNode/{PlaylistContentTask.bs => PlaylistContentJob.bs} (78%) diff --git a/playlet-lib/src/components/ChannelView/FetchSubscriptionStatusJob.bs b/playlet-lib/src/components/ChannelView/FetchSubscriptionStatusJob.bs index 6eb4f978..b5b77ad4 100644 --- a/playlet-lib/src/components/ChannelView/FetchSubscriptionStatusJob.bs +++ b/playlet-lib/src/components/ChannelView/FetchSubscriptionStatusJob.bs @@ -16,11 +16,11 @@ function ExecuteJob() as void return end if - cancallation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) + cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) isInnertubeProfile = authToken.profileNode.type = "youtube" if isInnertubeProfile subscriptionStatus = InnertubeService.GetSubscriptionStatusForChannel(ucid, { - cancellation: cancallation + cancellation: cancellation accessToken: authToken.token onBehalfOfUser: authToken.activeIdentifyToken }) @@ -32,7 +32,7 @@ function ExecuteJob() as void channelView.isSubscribed = subscriptionStatus.isSubscribed else service = new Invidious.InvidiousService(invidiousNode) - subscriptionsRequest = service.GetSubscriptions(cancallation) + subscriptionsRequest = service.GetSubscriptions(cancellation) if not subscriptionsRequest.IsSuccess() JobError("Failed to fetch subscriptions: " + subscriptionsRequest.ErrorMessage()) return diff --git a/playlet-lib/src/components/ChannelView/SubscribeJob.bs b/playlet-lib/src/components/ChannelView/SubscribeJob.bs index 1f8ecb67..c3753300 100644 --- a/playlet-lib/src/components/ChannelView/SubscribeJob.bs +++ b/playlet-lib/src/components/ChannelView/SubscribeJob.bs @@ -14,13 +14,13 @@ function ExecuteJob() as void return end if - cancallation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) + cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) service = new Invidious.InvidiousService(invidiousNode) if subscribe - response = service.Subscribe(ucid, cancallation) + response = service.Subscribe(ucid, cancellation) else - response = service.Unsubscribe(ucid, cancallation) + response = service.Unsubscribe(ucid, cancellation) end if if not response.IsSuccess() diff --git a/playlet-lib/src/components/ContentNode/ChannelContentJob.bs b/playlet-lib/src/components/ContentNode/ChannelContentJob.bs index 00c7ecfd..03b45f1d 100644 --- a/playlet-lib/src/components/ContentNode/ChannelContentJob.bs +++ b/playlet-lib/src/components/ContentNode/ChannelContentJob.bs @@ -14,8 +14,8 @@ function ExecuteJob() as void end if service = new Invidious.InvidiousService(invidiousNode) - cancallation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) - response = service.GetChannel(contentNode.authorId, cancallation) + cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) + response = service.GetChannel(contentNode.authorId, cancellation) if JobIsCancelled() contentNode.loadState = FeedLoadState.None diff --git a/playlet-lib/src/components/ContentNode/PlaylistContentTask.bs b/playlet-lib/src/components/ContentNode/PlaylistContentJob.bs similarity index 78% rename from playlet-lib/src/components/ContentNode/PlaylistContentTask.bs rename to playlet-lib/src/components/ContentNode/PlaylistContentJob.bs index 6fe63f35..9487ff70 100644 --- a/playlet-lib/src/components/ContentNode/PlaylistContentTask.bs +++ b/playlet-lib/src/components/ContentNode/PlaylistContentJob.bs @@ -2,19 +2,22 @@ import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" import "pkg:/components/VideoFeed/FeedLoadState.bs" -@asynctask -function PlaylistContentTask(input as object) as object +@job("PlaylistContentJob") +function ExecuteJob() as void + input = JobGetInput() contentNode = input.content invidiousNode = input.invidious singlePage = input.singlePage - if m.top.cancel + if JobIsCancelled() contentNode.loadState = FeedLoadState.None - return invalid + JobCancel() + return end if service = new Invidious.InvidiousService(invidiousNode) instance = service.GetInstance() + cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) while true playlistId = contentNode.playlistId @@ -29,29 +32,26 @@ function PlaylistContentTask(input as object) as object end if end if - response = service.GetPlaylist(playlistId, index, continuation, m.top.cancellation) + response = service.GetPlaylist(playlistId, index, continuation, cancellation) - if m.top.cancel + if JobIsCancelled() contentNode.loadState = FeedLoadState.None - return invalid + JobCancel() + return end if metadata = response.Json() if not response.IsSuccess() or metadata = invalid contentNode.loadState = FeedLoadState.Error - return { - success: false - error: response.ErrorMessage() - } + JobError(response.ErrorMessage()) + return end if if metadata.DoesExist("error") contentNode.loadState = FeedLoadState.Error - return { - success: false - error: metadata.error - } + JobError(metadata.error) + return end if beforeChildCount = contentNode.getChildCount() @@ -68,18 +68,12 @@ function PlaylistContentTask(input as object) as object if noMoreContinuation or noVideosAdded or reachedVideoCount contentNode.loadState = FeedLoadState.Loaded - return { - success: true - } + return else contentNode.loadState = FeedLoadState.LoadedPage if singlePage - return { - success: true - } + return end if end if end while - - return invalid end function diff --git a/playlet-lib/src/components/ContentNode/PlaylistContentNode.bs b/playlet-lib/src/components/ContentNode/PlaylistContentNode.bs index 79d11fa7..1980ad49 100644 --- a/playlet-lib/src/components/ContentNode/PlaylistContentNode.bs +++ b/playlet-lib/src/components/ContentNode/PlaylistContentNode.bs @@ -1,63 +1,58 @@ import "pkg:/components/Dialog/DialogUtils.bs" +import "pkg:/components/JobSystem/Jobs.bs" +import "pkg:/components/JobSystem/JobSystem.bs" import "pkg:/components/VideoFeed/FeedLoadState.bs" -import "pkg:/source/AsyncTask/AsyncTask.bs" -import "pkg:/source/AsyncTask/Tasks.bs" import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/Locale.bs" import "pkg:/source/utils/Logging.bs" -function LoadPlaylistPage(invidiousNode as object) as void - LoadPlaylist(invidiousNode, true) +function LoadPlaylistPage(invidiousNode as object, jobQueue as object) as void + LoadPlaylist(invidiousNode, jobQueue, true) end function -function LoadPlaylistAll(invidiousNode as object) as void - LoadPlaylist(invidiousNode, false) +function LoadPlaylistAll(invidiousNode as object, jobQueue as object) as void + LoadPlaylist(invidiousNode, jobQueue, false) end function -function LoadPlaylist(invidiousNode as object, singlePage as boolean) as void - if m.contentTask <> invalid - m.contentTask.cancel = true - m.contentTask = invalid - m.top.isLoading = false - end if - +function LoadPlaylist(invidiousNode as object, jobQueue as object, singlePage as boolean) as void loadState = m.top.loadState if loadState = FeedLoadState.Loading or loadState = FeedLoadState.Loaded return end if + if m.playlistContentJob <> invalid + m.playlistContentJob.cancel = true + m.top.isLoading = false + end if + m.top.loadState = FeedLoadState.Loading - m.contentTask = AsyncTask.Start(Tasks.PlaylistContentTask, { + + input = { content: m.top invidious: invidiousNode singlePage: singlePage - }, OnPlaylistContentTaskResult) + } + + callback = JobSystem.CreateCallback(OnPlaylistContentJobDone) + JobSystem.QueueJob(jobQueue, Jobs.PlaylistContentJob, input, callback) + m.playlistContentJob = callback m.top.isLoading = true end function -function OnPlaylistContentTaskResult(output as object) as void - m.contentTask = invalid +function OnPlaylistContentJobDone(event as object) as void + callback = event.getRoSGNode() + JobSystem.UnobserveCallback(callback) + m.playlistContentJob = invalid m.top.isLoading = false - if output.cancelled + if callback.cancel or callback.success return end if - if not output.success or not output.result.success - ' output.error for unhandled exception - error = output.error - if error = invalid - ' output.result.error for network errors - error = output.result.error - end if - error = ErrorUtils.Format(error) - LogError(error) - playlistId = output.task.input.content.playlistId - message = Tr(Locale.Dialogs.FailedToLoadPlaylist).Replace("%1", playlistId) + `\n${error}` - DialogUtils.ShowDialogEx({ - message: message - title: Tr(Locale.Dialogs.PlaylistLoadError) - large: true - }) - end if + message = Tr(Locale.Dialogs.FailedToLoadPlaylist).Replace("%1", m.top.playlistId) + `\n${callback.error}` + DialogUtils.ShowDialogEx({ + message: message + title: Tr(Locale.Dialogs.PlaylistLoadError) + large: true + }) end function diff --git a/playlet-lib/src/components/MainScene.xml b/playlet-lib/src/components/MainScene.xml index 1915e11a..7981a6fc 100644 --- a/playlet-lib/src/components/MainScene.xml +++ b/playlet-lib/src/components/MainScene.xml @@ -61,7 +61,8 @@ invidious="bind:../Invidious" notifications="bind:../Notifications" preferences="bind:../Preferences" - loungeService="bind:../LoungeService" /> + loungeService="bind:../LoungeService" + jobQueue="bind:/JobQueue" /> diff --git a/playlet-lib/src/components/MainScene_bindings.transpiled.brs b/playlet-lib/src/components/MainScene_bindings.transpiled.brs index 8eb76cde..c5d5aca9 100644 --- a/playlet-lib/src/components/MainScene_bindings.transpiled.brs +++ b/playlet-lib/src/components/MainScene_bindings.transpiled.brs @@ -22,7 +22,8 @@ function InitializeBindings() "invidious": "../Invidious", "notifications": "../Notifications", "preferences": "../Preferences", - "loungeService": "../LoungeService" + "loungeService": "../LoungeService", + "jobQueue": "/JobQueue" }, "SearchHistory": { "preferences": "../Preferences" diff --git a/playlet-lib/src/components/PlaylistView/PlaylistView.bs b/playlet-lib/src/components/PlaylistView/PlaylistView.bs index 04122c46..8f919267 100644 --- a/playlet-lib/src/components/PlaylistView/PlaylistView.bs +++ b/playlet-lib/src/components/PlaylistView/PlaylistView.bs @@ -252,8 +252,7 @@ function LoadPlaylistIfNeeded() as void return end if - ' TODO:P1 use a field instead of function call - m.top.content@.LoadPlaylistPage(m.top.invidious) + m.top.content@.LoadPlaylistPage(m.top.invidious, m.top.jobQueue) end function function OnLoadingChanged(event as object) diff --git a/playlet-lib/src/components/PlaylistView/PlaylistView.xml b/playlet-lib/src/components/PlaylistView/PlaylistView.xml index 3a0acfaa..d0d7e99f 100644 --- a/playlet-lib/src/components/PlaylistView/PlaylistView.xml +++ b/playlet-lib/src/components/PlaylistView/PlaylistView.xml @@ -4,6 +4,7 @@ + diff --git a/playlet-lib/src/components/VideoFeed/VideoRowListRowContentJob.bs b/playlet-lib/src/components/VideoFeed/VideoRowListRowContentJob.bs index 678c075e..b4013a3e 100644 --- a/playlet-lib/src/components/VideoFeed/VideoRowListRowContentJob.bs +++ b/playlet-lib/src/components/VideoFeed/VideoRowListRowContentJob.bs @@ -11,7 +11,7 @@ function ExecuteJob() as void invidiousNode = input.invidious callbackNode = JobGetCallbackNode() - cancallation = CancellationUtils.CreateCancellation(callbackNode, "cancel", true) + cancellation = CancellationUtils.CreateCancellation(callbackNode, "cancel", true) service = new Invidious.InvidiousService(invidiousNode) instance = service.GetInstance() @@ -61,7 +61,7 @@ function ExecuteJob() as void response = service.MakeRequest(feedSource, { additionalQueryParams: feedSource.state.queryParams - cancellation: cancallation + cancellation: cancellation }) if not response.success diff --git a/playlet-lib/src/components/VideoQueue/VideoQueue.bs b/playlet-lib/src/components/VideoQueue/VideoQueue.bs index b13e8f4a..c28e43a2 100644 --- a/playlet-lib/src/components/VideoQueue/VideoQueue.bs +++ b/playlet-lib/src/components/VideoQueue/VideoQueue.bs @@ -153,7 +153,7 @@ function OnPlayPlaylist() as void end if playlistNode.ObserveFieldScoped("loadState", FuncName(OnPlaylistLoadStateChange)) - playlistNode@.LoadPlaylistAll(m.top.invidious) + playlistNode@.LoadPlaylistAll(m.top.invidious, m.top.jobQueue) end function function OnPlaylistLoadStateChange() as void @@ -311,7 +311,7 @@ function OnAddToQueuePlaylist() as void end if playlistNode.ObserveFieldScoped("loadState", FuncName(OnPlaylistLoadStateChange)) - playlistNode@.LoadPlaylistAll(m.top.invidious) + playlistNode@.LoadPlaylistAll(m.top.invidious, m.top.jobQueue) end function function OnSetQueueAndIndex() as void diff --git a/playlet-lib/src/components/VideoQueue/VideoQueue.xml b/playlet-lib/src/components/VideoQueue/VideoQueue.xml index 46fa6ba4..83c62ed3 100644 --- a/playlet-lib/src/components/VideoQueue/VideoQueue.xml +++ b/playlet-lib/src/components/VideoQueue/VideoQueue.xml @@ -29,6 +29,7 @@ +