[skip ci] converting more tasks to jobs

This commit is contained in:
Brahim Hadriche committed 2025-06-21 14:53:52 -04:00
1 parent 9dbc0a7e66
commit 965ac9dfce
6 files changed
+74 -68

No files matched your search

+4
View File
@@ -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
@@ -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
@@ -2,6 +2,7 @@
<interface>
<field id="profilesService" type="node" bind="/ProfilesService" />
<field id="appController" type="node" bind="/AppController" />
<field id="jobQueue" type="node" bind="/JobQueue" />
<field id="code" type="string" alias="codeLabel.text" />
<field id="url" type="string" onChange="OnUrlSet" />
<field id="alwaysOnTop" type="boolean" />
@@ -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
@@ -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
@@ -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