Feature/optimize task imports (#257)

* Prune empty code files

* delete file

* Restore

* refactor

* fix warnings

* Fix tasks

* Add diag

* tweak

* Log load times

* changelog
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2024-01-08 19:38:39 -05:00
1 parent 641c9ef5bf
commit 5335a0273b
26 files changed
+221 -256

No files matched your search

+1
View File
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `HTTP/0.9 when not allowed` errors by adding a retry to web requests - `HTTP/0.9 when not allowed` errors by adding a retry to web requests
- Issue where a bookmarked video is no longer valid (such as a video made private, or a live stream that ended) - Issue where a bookmarked video is no longer valid (such as a video made private, or a live stream that ended)
- [Perf] App loads slightly faster (~500ms)
## [0.18.1] - 2024-01-02 ## [0.18.1] - 2024-01-02
+2 -1
View File
@@ -77,5 +77,6 @@
"require": [ "require": [
"ts-node/register" "ts-node/register"
], ],
"sourceMap": true "sourceMap": true,
"pruneEmptyCodeFiles": true
} }
+5 -2
View File
@@ -470,7 +470,10 @@ end function
And use the function like so And use the function like so
```brighterscript ```brighterscript
StartAsyncTask(MyBackgroundTask, {myVar: "some input"}, function(output as object) as void import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/AsyncTask/AsyncTask.bs"
AsyncTask.Start(Tasks.MyBackgroundTask, {myVar: "some input"}, function(output as object) as void
if output.success if output.success
print(output.result.value) print(output.result.value)
end if end if
@@ -479,7 +482,7 @@ end function)
The rest will be taken care of by the plugin, which will generate an `xml` file containing the task component, and a script that handles calling the function. The rest will be taken care of by the plugin, which will generate an `xml` file containing the task component, and a script that handles calling the function.
Although very useful, this pattern might not be the best for long running tasks (like the Web Server) or other tasks that require task reuse. This is because `StartAsyncTask` create a new instance of the task every time. Although very useful, this pattern might not be the best for long running tasks (like the Web Server) or other tasks that require task reuse. This is because `AsyncTask.Start` create a new instance of the task every time.
## Tracking transpilied files ## Tracking transpilied files
+21 -21
View File
@@ -9,14 +9,14 @@
"version": "0.18.1", "version": "0.18.1",
"hasInstallScript": true, "hasInstallScript": true,
"devDependencies": { "devDependencies": {
"@rokucommunity/bslint": "^0.8.13", "@rokucommunity/bslint": "^0.8.14",
"@types/crypto-js": "^4.2.1", "@types/crypto-js": "^4.2.1",
"@types/fs-extra": "^11.0.4", "@types/fs-extra": "^11.0.4",
"@types/node": "^20.10.6", "@types/node": "^20.10.7",
"@types/xml2js": "^0.4.14", "@types/xml2js": "^0.4.14",
"argparse": "^2.0.1", "argparse": "^2.0.1",
"brighterscript": "0.65.15", "brighterscript": "0.65.16",
"brighterscript-formatter": "^1.6.36", "brighterscript-formatter": "^1.6.37",
"convert-svg-to-png": "^0.6.4", "convert-svg-to-png": "^0.6.4",
"cross-fetch": "^4.0.0", "cross-fetch": "^4.0.0",
"crypto-js": "^4.2.0", "crypto-js": "^4.2.0",
@@ -29,7 +29,7 @@
"json5": "^2.2.3", "json5": "^2.2.3",
"mailparser": "^3.6.6", "mailparser": "^3.6.6",
"roku-deploy": "^3.11.2", "roku-deploy": "^3.11.2",
"rooibos-roku": "^5.9.1", "rooibos-roku": "^5.10.0",
"semver": "^7.5.4", "semver": "^7.5.4",
"shelljs": "^0.8.5", "shelljs": "^0.8.5",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
@@ -276,9 +276,9 @@
"dev": true "dev": true
}, },
"node_modules/@rokucommunity/bslint": { "node_modules/@rokucommunity/bslint": {
"version": "0.8.13", "version": "0.8.14",
"resolved": "https://registry.npmjs.org/@rokucommunity/bslint/-/bslint-0.8.13.tgz", "resolved": "https://registry.npmjs.org/@rokucommunity/bslint/-/bslint-0.8.14.tgz",
"integrity": "sha512-EC5tQdtRFTJxOo+9lnZz6yLcsyctuchCzUNJX391ZgCN0EKJTjGigyqTsdsFprINmr0pBojUiys/ZpS/Bv3ROQ==", "integrity": "sha512-vKD2weW5I8jOXe+2xHN9FZ9GUCkbE/FOTbWcziLXrObraXJZcNRqk+fWDvi7go79ourQ/9DmYfmFH0bocjOMqg==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
@@ -370,9 +370,9 @@
} }
}, },
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "20.10.6", "version": "20.10.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.6.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.7.tgz",
"integrity": "sha512-Vac8H+NlRNNlAmDfGUP7b5h/KA+AtWIzuXy0E6OyP8f1tCLYAtPvKRRDJjAPqhpCb0t6U2j7/xqAuLEebW2kiw==", "integrity": "sha512-fRbIKb8C/Y2lXxB5eVMj4IU7xpdox0Lh8bUPEdtLysaylsml1hOOx1+STloRs/B9nf7C6kPRmmg/V7aQW7usNg==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"undici-types": "~5.26.4" "undici-types": "~5.26.4"
@@ -728,9 +728,9 @@
} }
}, },
"node_modules/brighterscript": { "node_modules/brighterscript": {
"version": "0.65.15", "version": "0.65.16",
"resolved": "https://registry.npmjs.org/brighterscript/-/brighterscript-0.65.15.tgz", "resolved": "https://registry.npmjs.org/brighterscript/-/brighterscript-0.65.16.tgz",
"integrity": "sha512-WDnHaeCQclDG8wic1SCLYavnZDSjjzHcWxo+efwYkvyTjZivPoGWXbEulsYM3Nm9VawPuWWwzVqzHiN0Y4RvxQ==", "integrity": "sha512-2RJMF8itkrPXtZ92JkJf3emvzcCl5ETXGVyPsdO/hMKbUGinEGMRFG07FR0bzjRotbALzsozGE/Hjzw412Sjww==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"@rokucommunity/bslib": "^0.1.1", "@rokucommunity/bslib": "^0.1.1",
@@ -771,12 +771,12 @@
} }
}, },
"node_modules/brighterscript-formatter": { "node_modules/brighterscript-formatter": {
"version": "1.6.36", "version": "1.6.37",
"resolved": "https://registry.npmjs.org/brighterscript-formatter/-/brighterscript-formatter-1.6.36.tgz", "resolved": "https://registry.npmjs.org/brighterscript-formatter/-/brighterscript-formatter-1.6.37.tgz",
"integrity": "sha512-f9Y3Re6egB1MCUftph8AT8RxWTtXmeMjwigRv1biNGmEuGeSOipoBnCbwY4U1CFz46L7W39/TL4n1JMV9URD6w==", "integrity": "sha512-jIlqRjPVIp2WwB9Pe0YwK4KU4G3WqhRsqRluuOFY1sTDVW0R/AKXMvCfTsu99L2W7CbpeWA4UjOPzl9JOqN5hw==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"brighterscript": "^0.65.12", "brighterscript": "^0.65.16",
"glob-all": "^3.3.0", "glob-all": "^3.3.0",
"jsonc-parser": "^3.0.0", "jsonc-parser": "^3.0.0",
"source-map": "^0.7.3", "source-map": "^0.7.3",
@@ -3994,9 +3994,9 @@
} }
}, },
"node_modules/rooibos-roku": { "node_modules/rooibos-roku": {
"version": "5.9.1", "version": "5.10.0",
"resolved": "https://registry.npmjs.org/rooibos-roku/-/rooibos-roku-5.9.1.tgz", "resolved": "https://registry.npmjs.org/rooibos-roku/-/rooibos-roku-5.10.0.tgz",
"integrity": "sha512-Ez+/nDJ2WJuXtr+HF80lCPQNIQC+kVf26Yz2kLXy+YrOg1GYYTjA8S8lrWA4HUI65yTtD+vCGV2xMlRBqsYIHg==", "integrity": "sha512-Idiv3nWQOEr5V8z8oHcIovjcoBffgqRtTQ5B6K+x/cGAouKxyVJp0fsZ80xQvUul9IwHQ0XIPUYbNWCR3RQ3sA==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"source-map": "^0.7.3", "source-map": "^0.7.3",
+5 -5
View File
@@ -3,14 +3,14 @@
"version": "0.18.1", "version": "0.18.1",
"description": "Unofficial Youtube client for Roku", "description": "Unofficial Youtube client for Roku",
"devDependencies": { "devDependencies": {
"@rokucommunity/bslint": "^0.8.13", "@rokucommunity/bslint": "^0.8.14",
"@types/crypto-js": "^4.2.1", "@types/crypto-js": "^4.2.1",
"@types/fs-extra": "^11.0.4", "@types/fs-extra": "^11.0.4",
"@types/node": "^20.10.6", "@types/node": "^20.10.7",
"@types/xml2js": "^0.4.14", "@types/xml2js": "^0.4.14",
"argparse": "^2.0.1", "argparse": "^2.0.1",
"brighterscript": "0.65.15", "brighterscript": "0.65.16",
"brighterscript-formatter": "^1.6.36", "brighterscript-formatter": "^1.6.37",
"convert-svg-to-png": "^0.6.4", "convert-svg-to-png": "^0.6.4",
"cross-fetch": "^4.0.0", "cross-fetch": "^4.0.0",
"crypto-js": "^4.2.0", "crypto-js": "^4.2.0",
@@ -23,7 +23,7 @@
"json5": "^2.2.3", "json5": "^2.2.3",
"mailparser": "^3.6.6", "mailparser": "^3.6.6",
"roku-deploy": "^3.11.2", "roku-deploy": "^3.11.2",
"rooibos-roku": "^5.9.1", "rooibos-roku": "^5.10.0",
"semver": "^7.5.4", "semver": "^7.5.4",
"shelljs": "^0.8.5", "shelljs": "^0.8.5",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
@@ -59,7 +59,8 @@ function OnLoadStatusChanged() as void
if m.lib.loadStatus = "ready" if m.lib.loadStatus = "ready"
RemoveComponentLibrary() RemoveComponentLibrary()
' bs:disable-next-line LINT3012 ' bs:disable-next-line LINT3012
?"Playlet lib loaded from " libUrl.link ?"Playlet lib loaded from " libUrl.link " in " m.timer.TotalMilliseconds().ToStr() "ms"
m.timer = invalid
m.scene.playletLibLoadedUrl = libUrl m.scene.playletLibLoadedUrl = libUrl
container = m.scene.findNode("MainSceneContainer") container = m.scene.findNode("MainSceneContainer")
container.createChild("PlayletLib:MainScene") container.createChild("PlayletLib:MainScene")
@@ -86,6 +87,9 @@ function RemoveComponentLibrary() as void
end function end function
function LoadComponentLibrary(uri as string) function LoadComponentLibrary(uri as string)
m.timer = CreateObject("roTimespan")
m.timer.mark()
m.lib = m.scene.createChild("ComponentLibrary") m.lib = m.scene.createChild("ComponentLibrary")
' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK
m.lib.observeField("loadStatus", "OnLoadStatusChanged") m.lib.observeField("loadStatus", "OnLoadStatusChanged")
@@ -1,11 +1,11 @@
import "pkg:/components/Navigation/Navigation.bs" import "pkg:/components/Navigation/Navigation.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/DisposableObserve.bs" import "pkg:/source/utils/DisposableObserve.bs"
import "pkg:/source/utils/FocusManagement.bs" import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/Types.bs" import "pkg:/source/utils/Types.bs"
import "SubscribeTask.bs"
function Init() function Init()
m.container = m.top.findNode("container") m.container = m.top.findNode("container")
@@ -134,7 +134,7 @@ function OnSubscribeButtonSelected()
end if end if
isSubbed = IsSubscribed() isSubbed = IsSubscribed()
m.subscribeTask = StartAsyncTask(SubscribeTask, { m.subscribeTask = AsyncTask.Start(Tasks.SubscribeTask, {
invidious: m.invidious invidious: m.invidious
ucid: m.authorId ucid: m.authorId
subscribe: not isSubbed subscribe: not isSubbed
@@ -1,7 +1,9 @@
import "pkg:/components/ContentNode/ChannelContentTask.bs"
import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/source/asyncTask/asyncTask.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/ErrorUtils.bs"
import "pkg:/source/utils/Logging.bs"
function Init() function Init()
end function end function
@@ -17,7 +19,7 @@ function LoadChannel(invidiousNode as object) as void
end if end if
m.top.loadState = FeedLoadState.Loading m.top.loadState = FeedLoadState.Loading
m.contentTask = StartAsyncTask(ChannelContentTask, { m.contentTask = AsyncTask.Start(Tasks.ChannelContentTask, {
content: m.top content: m.top
invidious: invidiousNode invidious: invidiousNode
}, OnChannelContentTaskResult) }, OnChannelContentTaskResult)
@@ -1,6 +1,6 @@
import "DialogPalette.bs" import "DialogPalette.bs"
import "pkg:/components/Screens/WebAppScreen/GenerateQrCodeTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/Types.bs" import "pkg:/source/utils/Types.bs"
@@ -33,7 +33,7 @@ function OnUrlSet()
url = m.top.url url = m.top.url
m.urlLabel.text = url m.urlLabel.text = url
StartAsyncTask(GenerateQrCodeTask, { AsyncTask.Start(Tasks.GenerateQrCodeTask, {
poster: m.qrCode poster: m.qrCode
text: url text: url
size: m.qrCode.loadWidth size: m.qrCode.loadWidth
@@ -1,10 +1,10 @@
import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/components/NodeByRef/NodeByRef.bs" import "pkg:/components/NodeByRef/NodeByRef.bs"
import "pkg:/components/PlaylistView/PlaylistContentTask.bs"
import "pkg:/components/PlayQueue/Notifications/PlayQueueNotificationUtils.bs" import "pkg:/components/PlayQueue/Notifications/PlayQueueNotificationUtils.bs"
import "pkg:/components/VideoFeed/FeedLoadState.bs" import "pkg:/components/VideoFeed/FeedLoadState.bs"
import "pkg:/components/VideoPlayer/VideoUtils.bs" import "pkg:/components/VideoPlayer/VideoUtils.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/ArrayUtils.bs" import "pkg:/source/utils/ArrayUtils.bs"
import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
@@ -188,7 +188,7 @@ function LoadPlaylist(playlist as object) as void
playlist.loadState = FeedLoadState.Loading playlist.loadState = FeedLoadState.Loading
task = StartAsyncTask(PlaylistContentTask, { task = AsyncTask.Start(Tasks.PlaylistContentTask, {
content: playlist content: playlist
invidious: m.top.invidious invidious: m.top.invidious
}, OnPlaylistContentTaskResult) }, OnPlaylistContentTaskResult)
@@ -2,10 +2,11 @@ import "pkg:/components/ChannelView/ChannelUtils.bs"
import "pkg:/components/ContextMenu/ContextMenuUtils.bs" import "pkg:/components/ContextMenu/ContextMenuUtils.bs"
import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/components/Navigation/LongPress.bs" import "pkg:/components/Navigation/LongPress.bs"
import "pkg:/components/PlaylistView/PlaylistContentTask.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
import "pkg:/components/VideoFeed/FeedLoadState.bs" import "pkg:/components/VideoFeed/FeedLoadState.bs"
import "pkg:/components/VideoPlayer/VideoUtils.bs" import "pkg:/components/VideoPlayer/VideoUtils.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/FocusManagement.bs" import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
@@ -222,7 +223,7 @@ function LoadPlaylist() as void
end if end if
m.top.content.loadState = FeedLoadState.Loading m.top.content.loadState = FeedLoadState.Loading
m.playlistLoadTask = StartAsyncTask(PlaylistContentTask, { m.playlistLoadTask = AsyncTask.Start(Tasks.PlaylistContentTask, {
content: m.top.content content: m.top.content
invidious: m.top.invidious invidious: m.top.invidious
}, OnPlaylistContentTaskResult) }, OnPlaylistContentTaskResult)
@@ -1,4 +1,4 @@
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
function Init() function Init()
' TODO:P2 Add tooltip to each field with description of what it means ' TODO:P2 Add tooltip to each field with description of what it means
@@ -1,7 +1,8 @@
import "pkg:/components/Navigation/Navigation.bs" import "pkg:/components/Navigation/Navigation.bs"
import "pkg:/components/Screens/SearchScreen/SearchSuggestionsTask.bs"
import "pkg:/components/Services/SearchHistory/SearchHistoryUtils.bs" import "pkg:/components/Services/SearchHistory/SearchHistoryUtils.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/CryptoUtils.bs"
import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/FocusManagement.bs" import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/LoadingScreen.bs" import "pkg:/source/utils/LoadingScreen.bs"
@@ -98,7 +99,7 @@ function OnTextChange() as void
m.searchSuggestionsTask.cancel = true m.searchSuggestionsTask.cancel = true
end if end if
m.searchSuggestionsTask = StartAsyncTask(SearchSuggestionsTask, { m.searchSuggestionsTask = AsyncTask.Start(Tasks.SearchSuggestionsTask, {
q: m.keyboard.text q: m.keyboard.text
invidious: m.invidious invidious: m.invidious
preferences: m.preferences preferences: m.preferences
@@ -1,9 +1,10 @@
import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/components/Navigation/Navigation.bs" import "pkg:/components/Navigation/Navigation.bs"
import "pkg:/components/parts/AutoBind/OnNodeReadyNoOp.bs" import "pkg:/components/parts/AutoBind/OnNodeReadyNoOp.bs"
import "pkg:/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTestingTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/FocusManagement.bs" import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/Types.bs" import "pkg:/source/utils/Types.bs"
function Init() function Init()
@@ -58,7 +59,7 @@ function RunTestsTask()
m.testingTask.cancel = true m.testingTask.cancel = true
end if end if
m.testingTask = StartAsyncTask(InvidiousInstanceTestingTask, { m.testingTask = AsyncTask.Start(Tasks.InvidiousInstanceTestingTask, {
instance: m.top.instance instance: m.top.instance
listNode: m.testList listNode: m.testList
}) })
@@ -1,6 +1,6 @@
import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/components/Screens/SettingsScreen/InvidiousInstance/InvidiousPublicInstances/InvidiousPublicInstancesTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/FocusManagement.bs" import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
@@ -43,7 +43,7 @@ function Refresh()
m.invidiousPublicInstancesTask.cancel = true m.invidiousPublicInstancesTask.cancel = true
end if end if
m.invidiousPublicInstancesTask = StartAsyncTask(InvidiousPublicInstancesTask, { m.invidiousPublicInstancesTask = AsyncTask.Start(Tasks.InvidiousPublicInstancesTask, {
listNode: m.instances listNode: m.instances
}, OnInvidiousPublicInstancesTaskResults) }, OnInvidiousPublicInstancesTaskResults)
@@ -1,5 +1,5 @@
import "pkg:/components/Screens/WebAppScreen/GenerateQrCodeTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/StringUtils.bs"
function Init() function Init()
@@ -22,7 +22,7 @@ function OnUrlSet()
urlLabel.text = url urlLabel.text = url
qrCodePoster = m.top.findNode("QrCodePoster") qrCodePoster = m.top.findNode("QrCodePoster")
StartAsyncTask(GenerateQrCodeTask, { AsyncTask.Start(Tasks.GenerateQrCodeTask, {
poster: qrCodePoster poster: qrCodePoster
text: url text: url
size: qrCodePoster.loadWidth size: qrCodePoster.loadWidth
@@ -1,6 +1,5 @@
import "LatestLibVersionTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/services/ApplicationInfo.bs" import "pkg:/source/services/ApplicationInfo.bs"
function Init() function Init()
@@ -32,7 +31,7 @@ function GetLatestPlayletLibVersion(_unused as dynamic) as dynamic
return invalid return invalid
end if end if
m.latestLibVersionTask = StartAsyncTask(LatestLibVersionTask, {}, function(output as object) as void m.latestLibVersionTask = AsyncTask.Start(Tasks.LatestLibVersionTask, {}, function(output as object) as void
m.latestLibVersionTask = invalid m.latestLibVersionTask = invalid
if output.success and output.result.latest <> invalid if output.success and output.result.latest <> invalid
m.top.lib_version_latest = output.result.latest m.top.lib_version_latest = output.result.latest
@@ -1,6 +1,6 @@
import "InvidiousService.bs" import "InvidiousService.bs"
import "InvidiousSubscriptionsTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs"
function Init() function Init()
m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml")) m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml"))
@@ -32,5 +32,5 @@ function GetSubscriptions() as void
m.top.subscriptions = invalid m.top.subscriptions = invalid
return return
end if end if
m.subscriptionsTask = StartAsyncTask(InvidiousSubscriptionsTask, { invidious: m.top }) m.subscriptionsTask = AsyncTask.Start(Tasks.InvidiousSubscriptionsTask, { invidious: m.top })
end function end function
@@ -1,7 +1,7 @@
import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/components/Services/Invidious/InvidiousUnregisterTokenTask.bs"
import "pkg:/components/VideoFeed/PaginationType.bs" import "pkg:/components/VideoFeed/PaginationType.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/services/HttpClient.bs" import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/CryptoUtils.bs" import "pkg:/source/utils/CryptoUtils.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
@@ -474,7 +474,7 @@ namespace Invidious
if not m.VerifyTokenScope(authToken.token) if not m.VerifyTokenScope(authToken.token)
' Right now we're probably in a UI thread, and we can't unregister the token ' Right now we're probably in a UI thread, and we can't unregister the token
' Without a Task ' Without a Task
StartAsyncTask(InvidiousUnregisterTokenTask, { AsyncTask.Start(Tasks.InvidiousUnregisterTokenTask, {
invidious: m.node invidious: m.node
authToken: authToken authToken: authToken
}) })
@@ -2,10 +2,11 @@ import "pkg:/components/ChannelView/ChannelUtils.bs"
import "pkg:/components/ContextMenu/ContextMenuUtils.bs" import "pkg:/components/ContextMenu/ContextMenuUtils.bs"
import "pkg:/components/Navigation/LongPress.bs" import "pkg:/components/Navigation/LongPress.bs"
import "pkg:/components/PlaylistView/PlaylistUtils.bs" import "pkg:/components/PlaylistView/PlaylistUtils.bs"
import "pkg:/components/VideoFeed/VideoRowListContentTask.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
import "pkg:/components/VideoFeed/VideoRowListRowContentTask.bs" import "pkg:/components/VideoFeed/FeedLoadState.bs"
import "pkg:/components/VideoPlayer/VideoUtils.bs" import "pkg:/components/VideoPlayer/VideoUtils.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/DisposableObserve.bs" import "pkg:/source/utils/DisposableObserve.bs"
import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
@@ -213,7 +214,7 @@ function InitContent() as void
return return
end if end if
task = StartAsyncTask(VideoRowListContentTask, { task = AsyncTask.Start(Tasks.VideoRowListContentTask, {
node: m.top node: m.top
feeds: m.top.feeds feeds: m.top.feeds
}, OnVideoRowListContentTaskResult) }, OnVideoRowListContentTaskResult)
@@ -286,7 +287,7 @@ function LoadRowContent(contentNode as object)
LogInfo("Loading row content:", contentNode.title) LogInfo("Loading row content:", contentNode.title)
contentNode.loadState = FeedLoadState.Loading contentNode.loadState = FeedLoadState.Loading
task = StartAsyncTask(VideoRowListRowContentTask, { task = AsyncTask.Start(Tasks.VideoRowListRowContentTask, {
rowList: m.top rowList: m.top
feedContentNode: contentNode feedContentNode: contentNode
invidious: m.invidious invidious: m.invidious
@@ -1,4 +1,4 @@
import "FeedLoadState.bs" import "pkg:/components/VideoFeed/FeedLoadState.bs"
import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/StringUtils.bs"
@asynctask @asynctask
@@ -1,7 +1,7 @@
import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotificationUtils.bs" import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotificationUtils.bs"
import "pkg:/components/Services/SponsorBlock/SponsorBlockSegmentViewedTask.bs" import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs"
import "pkg:/components/Services/SponsorBlock/SponsorBlockTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs"
namespace SponsorBlock namespace SponsorBlock
@@ -84,7 +84,7 @@ namespace SponsorBlock
m.sponsorBlockTask.cancel = true m.sponsorBlockTask.cancel = true
end if end if
m.sponsorBlockTask = StartAsyncTask(SponsorBlockTask, { metadata: metadata }, OnSponsorBlockTaskResults) m.sponsorBlockTask = AsyncTask.Start(Tasks.SponsorBlockTask, { metadata: metadata }, OnSponsorBlockTaskResults)
end function end function
function OnSponsorBlockTaskResults(output as object) as void function OnSponsorBlockTaskResults(output as object) as void
@@ -172,7 +172,7 @@ namespace SponsorBlock
end function end function
function SendSkippedSectionEvent(segmentId as string) function SendSkippedSectionEvent(segmentId as string)
StartAsyncTask(SponsorBlockSegmentViewedTask, { segmentId: segmentId }) AsyncTask.Start(Tasks.SponsorBlockSegmentViewedTask, { segmentId: segmentId })
end function end function
end namespace end namespace
@@ -1,11 +1,10 @@
import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
import "pkg:/components/VideoPlayer/MarkVideoWatchedTask.bs"
import "pkg:/components/VideoPlayer/SponsorBlock.bs" import "pkg:/components/VideoPlayer/SponsorBlock.bs"
import "pkg:/components/VideoPlayer/VideoContentTask.bs"
import "pkg:/components/VideoPlayer/VideoPlayerStyle.bs" import "pkg:/components/VideoPlayer/VideoPlayerStyle.bs"
import "pkg:/components/VideoPlayer/VideoUtils.bs" import "pkg:/components/VideoPlayer/VideoUtils.bs"
import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
@@ -77,7 +76,7 @@ function StartVideoContentTask(videoContentNode as object)
m.top.content = videoContentNode m.top.content = videoContentNode
m.videoContentTask = StartAsyncTask(VideoContentTask, { m.videoContentTask = AsyncTask.Start(Tasks.VideoContentTask, {
content: videoContentNode content: videoContentNode
invidious: m.top.invidious invidious: m.top.invidious
preferences: m.top.preferences preferences: m.top.preferences
@@ -118,7 +117,7 @@ end function
function MarkVideoWatched(videoId as string) function MarkVideoWatched(videoId as string)
if m.top.invidious.authToken <> invalid if m.top.invidious.authToken <> invalid
StartAsyncTask(MarkVideoWatchedTask, { AsyncTask.Start(Tasks.MarkVideoWatchedTask, {
videoId: videoId videoId: videoId
invidious: m.top.invidious invidious: m.top.invidious
}) })
@@ -0,0 +1,48 @@
import "pkg:/source/utils/Types.bs"
namespace AsyncTask
function Start(taskName as string, input as object, callback = invalid as object) as object
task = Create(taskName, input, callback)
task.control = "RUN"
return task
end function
function Create(taskName as string, input as object, callback = invalid as object) as object
id = StrI(rnd(2147483647), 36)
task = createObject("roSGNode", taskName)
task.id = id
task.input = input
m[`asynctask_${id}`] = task
m[`asynctask_${id}_callback`] = callback
task.ObserveFieldScoped("state", FuncName(internal_OnTaskState))
return task
end function
function internal_OnTaskState(e as object) as void
task = e.getRoSGNode()
if task.state = "init" or task.state = "run"
return
end if
task.UnobserveFieldScoped("state")
id = task.id
output = task.output
' if output is invalid, it means the task was interrupted before the result is ready
if output <> invalid
callback = m[`asynctask_${id}_callback`]
if callback <> invalid
callback(output)
end if
end if
task.input = invalid
task.output = invalid
m.delete(`asynctask_${id}`)
m.delete(`asynctask_${id}_callback`)
end function
end namespace
@@ -1,56 +0,0 @@
import "pkg:/source/utils/Types.bs"
function StartAsyncTask(taskFunc as object, input as object, callback = invalid as object) as object
task = CreateAsyncTask(taskFunc, input, callback)
task.control = "RUN"
return task
end function
function CreateAsyncTask(taskFunc as object, input as object, callback = invalid as object) as object
id = StrI(rnd(2147483647), 36)
task = createObject("roSGNode", GetComponentName(taskFunc))
task.id = id
task.input = input
m[`asynctask_${id}`] = task
m[`asynctask_${id}_callback`] = callback
task.ObserveFieldScoped("state", FuncName(OnTaskState))
return task
end function
function GetComponentName(taskFunc as object) as string
if IsString(taskFunc)
return `${taskFunc}_asynctask`
end if
' NOTE: do NOT use FuncName in here.
' we use:
' return `${FuncName(taskFunc)}_asynctask`
' The app hangs on exit, and the TV crashes and restarts. I'm not kidding.
return (LCase(`${taskFunc}_asynctask`)).replace("function:", "").trim()
end function
function OnTaskState(e as object) as void
task = e.getRoSGNode()
if task.state = "init" or task.state = "run"
return
end if
task.UnobserveFieldScoped("state")
id = task.id
output = task.output
' if output is invalid, it means the task was interrupted before the result is ready
if output <> invalid
callback = m[`asynctask_${id}_callback`]
if callback <> invalid
callback(output)
end if
end if
task.input = invalid
task.output = invalid
m.delete(`asynctask_${id}`)
m.delete(`asynctask_${id}_callback`)
end function
+76 -116
View File
@@ -3,89 +3,24 @@
import { import {
CompilerPlugin, CompilerPlugin,
BscFile, BscFile,
BeforeFileTranspileEvent,
isBrsFile, isBrsFile,
WalkMode, WalkMode,
createVisitor, createVisitor,
DiagnosticSeverity,
Program, Program,
FunctionStatement, FunctionStatement,
BeforeFileValidateEvent,
} from 'brighterscript'; } from 'brighterscript';
declare type FunctionInFile = {
functionStatement: FunctionStatement,
file: BscFile
}
export class AsyncTaskPlugin implements CompilerPlugin { export class AsyncTaskPlugin implements CompilerPlugin {
public name = 'AsyncTaskPlugin'; public name = 'AsyncTaskPlugin';
private asyncTaskFunctions: FunctionInFile[] = [] afterFileParse(file: BscFile) {
private duplicatesReporeted: FunctionInFile[] = []
beforeProgramValidate(program: Program) {
this.asyncTaskFunctions = []
this.duplicatesReporeted = []
}
beforeFileValidate(event: BeforeFileValidateEvent) {
if (!isBrsFile(event.file)) {
return
}
event.file.ast.walk(createVisitor({
FunctionExpression: (func) => {
if (!this.isAsyncTask(func.functionStatement)) {
return
}
this.asyncTaskFunctions.push({
file: event.file,
functionStatement: func.functionStatement!
})
},
}), {
walkMode: WalkMode.visitExpressionsRecursive
});
}
afterFileValidate(file: BscFile) {
if (!isBrsFile(file)) { if (!isBrsFile(file)) {
return return
} }
const functionsWithDuplicates = this.getFunctionsWithDuplicatesForFile(file); const program = file.program
for (let index = 0; index < functionsWithDuplicates.length; index++) { file.ast.walk(createVisitor({
const fn = functionsWithDuplicates[index];
if (this.duplicatesReporeted.indexOf(fn) !== -1) {
continue
}
file.addDiagnostics([{
code: 6661,
message: `Duplicate async task '${fn.functionStatement.name.text}', use a different function name`,
range: fn.functionStatement.range,
file: fn.file,
source: "AsyncTaskPlugin",
severity: DiagnosticSeverity.Error,
}])
this.duplicatesReporeted.push(fn)
}
}
beforeFileTranspile(event: BeforeFileTranspileEvent) {
if (!isBrsFile(event.file)) {
return
}
const functions = this.asyncTaskFunctions
.filter((fn) => fn.file === event.file)
if (functions.length === 0) {
return
}
event.file.ast.walk(createVisitor({
FunctionExpression: (func) => { FunctionExpression: (func) => {
if (!this.isAsyncTask(func.functionStatement)) { if (!this.isAsyncTask(func.functionStatement)) {
return return
@@ -93,22 +28,51 @@ export class AsyncTaskPlugin implements CompilerPlugin {
const functionName = func.functionStatement!.name.text const functionName = func.functionStatement!.name.text
const hasParams = func.functionStatement!.func.parameters.length > 0 const hasParams = func.functionStatement!.func.parameters.length > 0
const taskName = `${functionName}_asynctask` const taskName = `${functionName}_AsyncTask`
const bs = this.generateBsTask(functionName, hasParams, event.file) const bs = this.generateBsTask(functionName, hasParams, file)
const bsFile = `components/asynctasks/generated/${taskName}.bs` const bsFile = `components/AsyncTask/generated/${taskName}.bs`
const xml = this.generateXmlTask(taskName, bsFile) const xml = this.generateXmlTask(taskName, bsFile)
const xmlFile = `components/asynctasks/generated/${taskName}.xml` const xmlFile = `components/AsyncTask/generated/${taskName}.xml`
event.program.setFile(xmlFile, xml) if (program.hasFile(xmlFile)) {
event.program.setFile(bsFile, bs) const currentContent = program.getFile(xmlFile).fileContents
if (currentContent !== xml) {
file.addDiagnostics([{
file: file,
range: func.range,
message: `AsyncTaskPlugin: file ${xmlFile} already exists`,
severity: 1,
code: 'ASYNC_TASK_FILE_EXISTS',
}]);
}
}
file.program.setFile(xmlFile, xml)
if (program.hasFile(bsFile)) {
const currentContent = program.getFile(bsFile).fileContents
if (currentContent !== bs) {
file.addDiagnostics([{
file: file,
range: func.range,
message: `AsyncTaskPlugin: file ${bsFile} already exists`,
severity: 1,
code: 'ASYNC_TASK_FILE_EXISTS',
}]);
}
}
file.program.setFile(bsFile, bs)
}, },
}), { }), {
walkMode: WalkMode.visitExpressionsRecursive walkMode: WalkMode.visitExpressionsRecursive
}); });
} }
beforeProgramValidate(program: Program) {
this.generateTaskListEnum(program);
}
isAsyncTask(functionStatement: FunctionStatement | undefined) { isAsyncTask(functionStatement: FunctionStatement | undefined) {
const annotations = functionStatement?.annotations const annotations = functionStatement?.annotations
if (!annotations || annotations.length === 0) { if (!annotations || annotations.length === 0) {
@@ -123,39 +87,6 @@ export class AsyncTaskPlugin implements CompilerPlugin {
return false return false
} }
getFunctionsWithDuplicatesForFile(filterFile: BscFile) {
const fileFunctions = this.asyncTaskFunctions
.filter((fn) => fn.file === filterFile)
if (fileFunctions.length === 0) {
return []
}
const uniqueNames: { [name: string]: FunctionInFile | undefined } = {}
const duplicatesNames: { [name: string]: boolean } = {}
const duplicates: FunctionInFile[] = []
for (var i = 0; i < this.asyncTaskFunctions.length; i++) {
const fnName = this.asyncTaskFunctions[i].functionStatement.name.text.toLowerCase()
if (duplicatesNames[fnName]) {
duplicates.push(this.asyncTaskFunctions[i])
continue
}
if (uniqueNames[fnName]) {
duplicates.push(uniqueNames[fnName]!)
uniqueNames[fnName] = undefined
duplicatesNames[fnName] = true
duplicates.push(this.asyncTaskFunctions[i])
continue
}
uniqueNames[fnName] = this.asyncTaskFunctions[i]
}
return duplicates
}
generateBsTask(functionName: string, hasInput: boolean, file: BscFile): string { generateBsTask(functionName: string, hasInput: boolean, file: BscFile): string {
return ` return `
import "pkg:/${file.pkgPath}" import "pkg:/${file.pkgPath}"
@@ -164,8 +95,8 @@ import "pkg:/source/utils/ErrorUtils.bs"
function Init() function Init()
m.top.functionName = "TaskMain" m.top.functionName = "TaskMain"
m.top.cancellation = { m.top.cancellation = {
node: m.top, node: m.top
field: "cancel", field: "cancel"
value: true value: true
} }
end function end function
@@ -174,9 +105,9 @@ function TaskMain()
try try
result = ${functionName}(${(hasInput ? "m.top.input" : "")}) result = ${functionName}(${(hasInput ? "m.top.input" : "")})
m.top.setField("output", { m.top.setField("output", {
success: true, success: true
task: m.top, task: m.top
cancelled: m.top.cancel, cancelled: m.top.cancel
result: result result: result
}) })
catch e catch e
@@ -185,9 +116,9 @@ function TaskMain()
print ErrorUtils.Format(e) print ErrorUtils.Format(e)
#end if #end if
m.top.setField("output", { m.top.setField("output", {
success: false, success: false
task: m.top, task: m.top
cancelled: m.top.cancel, cancelled: m.top.cancel
error: e error: e
}) })
end try end try
@@ -206,9 +137,38 @@ end function
<field id="cancel" type="boolean" alwaysNotify="true" /> <field id="cancel" type="boolean" alwaysNotify="true" />
<field id="cancellation" type="assocarray" /> <field id="cancellation" type="assocarray" />
</interface> </interface>
<script type="text/brightscript" uri="pkg:/${bsFile}" />
</component>` </component>`
} }
generateTaskListEnum(program: Program) {
const asyncTasks = Object.values(program.files).reduce((acc, file) => {
if (!isBrsFile(file)) {
return acc
}
file.ast.walk(createVisitor({
FunctionExpression: (func) => {
if (!this.isAsyncTask(func.functionStatement)) {
return
}
acc.add(func.functionStatement!.name.text)
},
}), {
walkMode: WalkMode.visitExpressionsRecursive
});
return acc
}, new Set<string>());
const enumItems = Array.from(asyncTasks).map((task) => {
return `${task} = "${task}_AsyncTask"`
})
const content = 'enum Tasks\n ' + enumItems.join('\n ') + '\nend enum\n';
program.setFile('source/AsyncTask/Tasks.bs', content);
}
} }
export default () => { export default () => {