From 965ac9dfce82e1c4ca8bf724afb4c724ea5305c6 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sat, 21 Jun 2025 14:53:52 -0400 Subject: [PATCH] [skip ci] converting more tasks to jobs --- CHANGELOG.md | 4 ++ .../components/Dialog/YouTubeLoginDialog.bs | 46 ++++++++--------- .../components/Dialog/YouTubeLoginDialog.xml | 1 + ...YouTubeLoginTask.bs => YouTubeLoginJob.bs} | 51 ++++++++----------- ...rateQrCodeTask.bs => GenerateQrCodeJob.bs} | 13 ++--- .../components/QrCodePoster/QrCodePoster.bs | 27 +++++++--- 6 files changed, 74 insertions(+), 68 deletions(-) rename playlet-lib/src/components/Dialog/{YouTubeLoginTask.bs => YouTubeLoginJob.bs} (70%) rename playlet-lib/src/components/QrCodePoster/{GenerateQrCodeTask.bs => GenerateQrCodeJob.bs} (80%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51d6b467..9343d316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +- Nothing worth mentioning yet. + ## [0.40.7] - 2025-06-21 ### Fixed diff --git a/playlet-lib/src/components/Dialog/YouTubeLoginDialog.bs b/playlet-lib/src/components/Dialog/YouTubeLoginDialog.bs index e174c30e..a2e04077 100644 --- a/playlet-lib/src/components/Dialog/YouTubeLoginDialog.bs +++ b/playlet-lib/src/components/Dialog/YouTubeLoginDialog.bs @@ -1,7 +1,7 @@ import "pkg:/components/Dialog/DialogUtils.bs" +import "pkg:/components/JobSystem/Jobs.bs" +import "pkg:/components/JobSystem/JobSystem.bs" import "pkg:/components/Screens/ProfileScreen/YouTubeAccountSelectionView/YouTubeAccountSelectionViewUtils.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" @@ -29,49 +29,45 @@ function Init() end function function OnNodeReady() - m.task = AsyncTask.Start(Tasks.YouTubeLoginTask, { + input = { dialog: m.top profilesService: m.top.profilesService - }, OnYouTubeLoginTaskResult) + } + + m.youTubeLoginJob = JobSystem.CreateCallback(OnYouTubeLoginJobDone) + JobSystem.QueueJob(m.jobQueue, Jobs.YouTubeLoginJob, input, m.youTubeLoginJob) end function -function OnYouTubeLoginTaskResult(output as object) as void - m.task = invalid +function OnYouTubeLoginJobDone(event as object) as void + JobSystem.UnobserveCallback(event.getRoSGNode()) + m.youTubeLoginJob = invalid - if output.cancelled + info = event.getInfo() + if info.cancel Close() 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) + if not info.success DialogUtils.ShowDialogEx({ - message: error + message: info.error title: Tr(Locale.Generic.Error) large: true }) return end if - accounts = output.result.accounts - accessToken = output.result.accessToken + data = info.data ' if accounts is invalid, we're going to assume a profile is already created. - if accounts = invalid or accessToken = invalid + if data = invalid or data.accounts = invalid or data.accessToken = invalid Close() return end if ' The accountSelectionView will take over the creation of the profile. accountSelectionView = YouTubeAccountSelectionViewUtils.Open(m.top.appController) - accountSelectionView.accessToken = accessToken - accountSelectionView.accounts = accounts + accountSelectionView.accessToken = data.accessToken + accountSelectionView.accounts = data.accounts Close() end function @@ -85,9 +81,9 @@ function Close() end function function OnWasClosed() - if m.task <> invalid - m.task.cancel = true - m.task = invalid + if m.youTubeLoginJob <> invalid + m.youTubeLoginJob.cancel = true + m.youTubeLoginJob = invalid end if end function diff --git a/playlet-lib/src/components/Dialog/YouTubeLoginDialog.xml b/playlet-lib/src/components/Dialog/YouTubeLoginDialog.xml index 38775351..a4246308 100644 --- a/playlet-lib/src/components/Dialog/YouTubeLoginDialog.xml +++ b/playlet-lib/src/components/Dialog/YouTubeLoginDialog.xml @@ -2,6 +2,7 @@ + diff --git a/playlet-lib/src/components/Dialog/YouTubeLoginTask.bs b/playlet-lib/src/components/Dialog/YouTubeLoginJob.bs similarity index 70% rename from playlet-lib/src/components/Dialog/YouTubeLoginTask.bs rename to playlet-lib/src/components/Dialog/YouTubeLoginJob.bs index 31f6b1a4..1931df1c 100644 --- a/playlet-lib/src/components/Dialog/YouTubeLoginTask.bs +++ b/playlet-lib/src/components/Dialog/YouTubeLoginJob.bs @@ -4,24 +4,25 @@ import "pkg:/components/Services/Innertube/InnertubeService.bs" import "pkg:/source/utils/CancellationUtils.bs" import "pkg:/source/utils/Logging.bs" -@asynctask -function YouTubeLoginTask(input as object) as object +@job("YouTubeLoginJob") +function ExecuteJob() as void + input = JobGetInput() + profilesNode = input.profilesService dialogNode = input.dialog - cancellation = m.top.cancellation + cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) ' TODO:P2 cache client identity authCode = InnertubeService.AuthGetCode(cancellation) if CancellationUtils.IsCancelled(cancellation) - return invalid + JobCancel() + return end if if authCode.error <> invalid - return { - success: false - error: authCode.error - } + JobError(authCode.error) + return end if url = InnertubeService.AuthGetActivationUrl(authCode) @@ -34,34 +35,30 @@ function YouTubeLoginTask(input as object) as object accessToken = InnertubeService.AuthPollForAccessToken(authCode, cancellation) if CancellationUtils.IsCancelled(cancellation) - return invalid + JobCancel() + return end if if accessToken.error <> invalid - return { - success: false - error: accessToken.error - } + JobError(accessToken.error) + return end if accounts = InnertubeService.AuthListAccounts(accessToken.accessToken, cancellation) if CancellationUtils.IsCancelled(cancellation) - return invalid + JobCancel() + return end if if IsAssociativeArray(accounts) and accounts.error <> invalid - return { - success: false - error: accounts.error - } + JobError(accounts.error) + return end if if not IsArray(accounts) or accounts.Count() = 0 - return { - success: false - error: Tr(Locale.Dialogs.NoValidAccountsFound) - } + JobError(Tr(Locale.Dialogs.NoValidAccountsFound)) + return end if ' If there is only one account, we can skip the selection screen @@ -69,15 +66,11 @@ function YouTubeLoginTask(input as object) as object if accounts.Count() = 1 profile = YouTubeLoginAccountItemUtils.CreateProfileContentNode(accounts[0], accessToken) profilesNode@.LoginWithProfile(profile) - - return { - success: true - } + return end if - return { - success: true + JobSuccessData({ accounts: accounts accessToken: accessToken - } + }) end function diff --git a/playlet-lib/src/components/QrCodePoster/GenerateQrCodeTask.bs b/playlet-lib/src/components/QrCodePoster/GenerateQrCodeJob.bs similarity index 80% rename from playlet-lib/src/components/QrCodePoster/GenerateQrCodeTask.bs rename to playlet-lib/src/components/QrCodePoster/GenerateQrCodeJob.bs index 37aa0a8d..111d6bff 100644 --- a/playlet-lib/src/components/QrCodePoster/GenerateQrCodeTask.bs +++ b/playlet-lib/src/components/QrCodePoster/GenerateQrCodeJob.bs @@ -1,8 +1,10 @@ import "pkg:/source/QrCode/QrImage.bs" import "pkg:/source/utils/StringUtils.bs" -@asynctask -function GenerateQrCodeTask(input as object) as object +@job("GenerateQrCodeJob") +function ExecuteJob() as void + input = JobGetInput() + poster = input.poster mode = input.mode text = input.text @@ -24,13 +26,12 @@ function GenerateQrCodeTask(input as object) as object qr = new QrCode.QRImage() imagePath = qr.Generate(text, size, padding, mode) - if m.top.cancel - return invalid + if JobIsCancelled() + JobCancel() + return end if if not StringUtils.IsNullOrEmpty(imagePath) poster.uri = imagePath end if - - return invalid end function diff --git a/playlet-lib/src/components/QrCodePoster/QrCodePoster.bs b/playlet-lib/src/components/QrCodePoster/QrCodePoster.bs index 8683af82..ca7e469c 100644 --- a/playlet-lib/src/components/QrCodePoster/QrCodePoster.bs +++ b/playlet-lib/src/components/QrCodePoster/QrCodePoster.bs @@ -1,5 +1,5 @@ -import "pkg:/source/AsyncTask/AsyncTask.bs" -import "pkg:/source/AsyncTask/Tasks.bs" +import "pkg:/components/JobSystem/Jobs.bs" +import "pkg:/components/JobSystem/JobSystem.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" @@ -44,8 +44,8 @@ function OnLoadStatusChange() as void end function function GenerateQrCode() as void - if m.qrCodeGenTask <> invalid - m.qrCodeGenTask.cancel = true + if m.generateQrCodeJob <> invalid + m.generateQrCodeJob.cancel = true end if text = m.top.text @@ -64,16 +64,27 @@ function GenerateQrCode() as void m.top.isBusy = true - m.qrCodeGenTask = AsyncTask.Start(Tasks.GenerateQrCodeTask, { + if m.jobQueue = invalid + m.jobQueue = m.top.getScene().findNode("JobQueue") + end if + + input = { poster: m.top text: text size: width padding: m.top.padding - }, OnGenerateQrCodeTaskResult) + } + + m.generateQrCodeJob = JobSystem.CreateCallback(OnGenerateQrCodeJobDone) + JobSystem.QueueJob(m.jobQueue, Jobs.GenerateQrCodeJob, input, m.generateQrCodeJob) end function -function OnGenerateQrCodeTaskResult(output as object) as void - if output.cancelled +function OnGenerateQrCodeJobDone(event as object) as void + JobSystem.UnobserveCallback(event.getRoSGNode()) + m.generateQrCodeJob = invalid + + info = event.getInfo() + if info.cancel return end if