From 5335a0273b8a116235a8c7c8e463379d9d89343b Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Mon, 8 Jan 2024 19:38:39 -0500 Subject: [PATCH] Feature/optimize task imports (#257) * Prune empty code files * delete file * Restore * refactor * fix warnings * Fix tasks * Add diag * tweak * Log load times * changelog --- CHANGELOG.md | 1 + config/bsconfig.base.jsonc | 3 +- docs/plugins.md | 7 +- package-lock.json | 42 ++-- package.json | 10 +- .../BootstrapScene/BootstrapScene.bs | 6 +- .../src/components/ChannelView/ChannelView.bs | 6 +- .../ContentNode/ChannelContentNode.bs | 8 +- .../src/components/Dialog/LoginDialog.bs | 6 +- .../src/components/PlayQueue/PlayQueue.bs | 6 +- .../components/PlaylistView/PlaylistView.bs | 7 +- .../Screens/AppInfoScreen/AppInfoScreen.bs | 2 +- .../Screens/SearchScreen/SearchScreen.bs | 7 +- .../InvidiousInstanceTesting.bs | 7 +- .../InvidiousPublicInstances.bs | 6 +- .../Screens/WebAppScreen/WebAppScreen.bs | 6 +- .../ApplicationInfo/ApplicationInfo.bs | 7 +- .../Services/Invidious/Invidious.bs | 6 +- .../Services/Invidious/InvidiousService.bs | 6 +- .../src/components/VideoFeed/VideoRowList.bs | 11 +- .../VideoFeed/VideoRowListContentTask.bs | 2 +- .../components/VideoPlayer/SponsorBlock.bs | 10 +- .../src/components/VideoPlayer/VideoPlayer.bs | 9 +- playlet-lib/src/source/AsyncTask/AsyncTask.bs | 48 +++++ playlet-lib/src/source/asyncTask/asyncTask.bs | 56 ----- tools/bs-plugins/asynctask-plugin.ts | 192 +++++++----------- 26 files changed, 221 insertions(+), 256 deletions(-) create mode 100644 playlet-lib/src/source/AsyncTask/AsyncTask.bs delete mode 100644 playlet-lib/src/source/asyncTask/asyncTask.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index 26b15e3c..c7c88c34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 - 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 diff --git a/config/bsconfig.base.jsonc b/config/bsconfig.base.jsonc index b368a3f5..0a446d70 100644 --- a/config/bsconfig.base.jsonc +++ b/config/bsconfig.base.jsonc @@ -77,5 +77,6 @@ "require": [ "ts-node/register" ], - "sourceMap": true + "sourceMap": true, + "pruneEmptyCodeFiles": true } \ No newline at end of file diff --git a/docs/plugins.md b/docs/plugins.md index 3e58e599..40aedc34 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -470,7 +470,10 @@ end function And use the function like so ```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 print(output.result.value) 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. -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 diff --git a/package-lock.json b/package-lock.json index 804aaa6e..e0d147af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,14 +9,14 @@ "version": "0.18.1", "hasInstallScript": true, "devDependencies": { - "@rokucommunity/bslint": "^0.8.13", + "@rokucommunity/bslint": "^0.8.14", "@types/crypto-js": "^4.2.1", "@types/fs-extra": "^11.0.4", - "@types/node": "^20.10.6", + "@types/node": "^20.10.7", "@types/xml2js": "^0.4.14", "argparse": "^2.0.1", - "brighterscript": "0.65.15", - "brighterscript-formatter": "^1.6.36", + "brighterscript": "0.65.16", + "brighterscript-formatter": "^1.6.37", "convert-svg-to-png": "^0.6.4", "cross-fetch": "^4.0.0", "crypto-js": "^4.2.0", @@ -29,7 +29,7 @@ "json5": "^2.2.3", "mailparser": "^3.6.6", "roku-deploy": "^3.11.2", - "rooibos-roku": "^5.9.1", + "rooibos-roku": "^5.10.0", "semver": "^7.5.4", "shelljs": "^0.8.5", "ts-node": "^10.9.2", @@ -276,9 +276,9 @@ "dev": true }, "node_modules/@rokucommunity/bslint": { - "version": "0.8.13", - "resolved": "https://registry.npmjs.org/@rokucommunity/bslint/-/bslint-0.8.13.tgz", - "integrity": "sha512-EC5tQdtRFTJxOo+9lnZz6yLcsyctuchCzUNJX391ZgCN0EKJTjGigyqTsdsFprINmr0pBojUiys/ZpS/Bv3ROQ==", + "version": "0.8.14", + "resolved": "https://registry.npmjs.org/@rokucommunity/bslint/-/bslint-0.8.14.tgz", + "integrity": "sha512-vKD2weW5I8jOXe+2xHN9FZ9GUCkbE/FOTbWcziLXrObraXJZcNRqk+fWDvi7go79ourQ/9DmYfmFH0bocjOMqg==", "dev": true, "dependencies": { "fs-extra": "^10.0.0", @@ -370,9 +370,9 @@ } }, "node_modules/@types/node": { - "version": "20.10.6", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.6.tgz", - "integrity": "sha512-Vac8H+NlRNNlAmDfGUP7b5h/KA+AtWIzuXy0E6OyP8f1tCLYAtPvKRRDJjAPqhpCb0t6U2j7/xqAuLEebW2kiw==", + "version": "20.10.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.7.tgz", + "integrity": "sha512-fRbIKb8C/Y2lXxB5eVMj4IU7xpdox0Lh8bUPEdtLysaylsml1hOOx1+STloRs/B9nf7C6kPRmmg/V7aQW7usNg==", "dev": true, "dependencies": { "undici-types": "~5.26.4" @@ -728,9 +728,9 @@ } }, "node_modules/brighterscript": { - "version": "0.65.15", - "resolved": "https://registry.npmjs.org/brighterscript/-/brighterscript-0.65.15.tgz", - "integrity": "sha512-WDnHaeCQclDG8wic1SCLYavnZDSjjzHcWxo+efwYkvyTjZivPoGWXbEulsYM3Nm9VawPuWWwzVqzHiN0Y4RvxQ==", + "version": "0.65.16", + "resolved": "https://registry.npmjs.org/brighterscript/-/brighterscript-0.65.16.tgz", + "integrity": "sha512-2RJMF8itkrPXtZ92JkJf3emvzcCl5ETXGVyPsdO/hMKbUGinEGMRFG07FR0bzjRotbALzsozGE/Hjzw412Sjww==", "dev": true, "dependencies": { "@rokucommunity/bslib": "^0.1.1", @@ -771,12 +771,12 @@ } }, "node_modules/brighterscript-formatter": { - "version": "1.6.36", - "resolved": "https://registry.npmjs.org/brighterscript-formatter/-/brighterscript-formatter-1.6.36.tgz", - "integrity": "sha512-f9Y3Re6egB1MCUftph8AT8RxWTtXmeMjwigRv1biNGmEuGeSOipoBnCbwY4U1CFz46L7W39/TL4n1JMV9URD6w==", + "version": "1.6.37", + "resolved": "https://registry.npmjs.org/brighterscript-formatter/-/brighterscript-formatter-1.6.37.tgz", + "integrity": "sha512-jIlqRjPVIp2WwB9Pe0YwK4KU4G3WqhRsqRluuOFY1sTDVW0R/AKXMvCfTsu99L2W7CbpeWA4UjOPzl9JOqN5hw==", "dev": true, "dependencies": { - "brighterscript": "^0.65.12", + "brighterscript": "^0.65.16", "glob-all": "^3.3.0", "jsonc-parser": "^3.0.0", "source-map": "^0.7.3", @@ -3994,9 +3994,9 @@ } }, "node_modules/rooibos-roku": { - "version": "5.9.1", - "resolved": "https://registry.npmjs.org/rooibos-roku/-/rooibos-roku-5.9.1.tgz", - "integrity": "sha512-Ez+/nDJ2WJuXtr+HF80lCPQNIQC+kVf26Yz2kLXy+YrOg1GYYTjA8S8lrWA4HUI65yTtD+vCGV2xMlRBqsYIHg==", + "version": "5.10.0", + "resolved": "https://registry.npmjs.org/rooibos-roku/-/rooibos-roku-5.10.0.tgz", + "integrity": "sha512-Idiv3nWQOEr5V8z8oHcIovjcoBffgqRtTQ5B6K+x/cGAouKxyVJp0fsZ80xQvUul9IwHQ0XIPUYbNWCR3RQ3sA==", "dev": true, "dependencies": { "source-map": "^0.7.3", diff --git a/package.json b/package.json index c3ddad63..d0c99fc5 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,14 @@ "version": "0.18.1", "description": "Unofficial Youtube client for Roku", "devDependencies": { - "@rokucommunity/bslint": "^0.8.13", + "@rokucommunity/bslint": "^0.8.14", "@types/crypto-js": "^4.2.1", "@types/fs-extra": "^11.0.4", - "@types/node": "^20.10.6", + "@types/node": "^20.10.7", "@types/xml2js": "^0.4.14", "argparse": "^2.0.1", - "brighterscript": "0.65.15", - "brighterscript-formatter": "^1.6.36", + "brighterscript": "0.65.16", + "brighterscript-formatter": "^1.6.37", "convert-svg-to-png": "^0.6.4", "cross-fetch": "^4.0.0", "crypto-js": "^4.2.0", @@ -23,7 +23,7 @@ "json5": "^2.2.3", "mailparser": "^3.6.6", "roku-deploy": "^3.11.2", - "rooibos-roku": "^5.9.1", + "rooibos-roku": "^5.10.0", "semver": "^7.5.4", "shelljs": "^0.8.5", "ts-node": "^10.9.2", diff --git a/playlet-app/src/components/BootstrapScene/BootstrapScene.bs b/playlet-app/src/components/BootstrapScene/BootstrapScene.bs index 4e5e2df0..3dbdb675 100644 --- a/playlet-app/src/components/BootstrapScene/BootstrapScene.bs +++ b/playlet-app/src/components/BootstrapScene/BootstrapScene.bs @@ -59,7 +59,8 @@ function OnLoadStatusChanged() as void if m.lib.loadStatus = "ready" RemoveComponentLibrary() ' 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 container = m.scene.findNode("MainSceneContainer") container.createChild("PlayletLib:MainScene") @@ -86,6 +87,9 @@ function RemoveComponentLibrary() as void end function function LoadComponentLibrary(uri as string) + m.timer = CreateObject("roTimespan") + m.timer.mark() + m.lib = m.scene.createChild("ComponentLibrary") ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK m.lib.observeField("loadStatus", "OnLoadStatusChanged") diff --git a/playlet-lib/src/components/ChannelView/ChannelView.bs b/playlet-lib/src/components/ChannelView/ChannelView.bs index 6c090bf2..d4ff4f01 100644 --- a/playlet-lib/src/components/ChannelView/ChannelView.bs +++ b/playlet-lib/src/components/ChannelView/ChannelView.bs @@ -1,11 +1,11 @@ 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/FocusManagement.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" -import "SubscribeTask.bs" function Init() m.container = m.top.findNode("container") @@ -134,7 +134,7 @@ function OnSubscribeButtonSelected() end if isSubbed = IsSubscribed() - m.subscribeTask = StartAsyncTask(SubscribeTask, { + m.subscribeTask = AsyncTask.Start(Tasks.SubscribeTask, { invidious: m.invidious ucid: m.authorId subscribe: not isSubbed diff --git a/playlet-lib/src/components/ContentNode/ChannelContentNode.bs b/playlet-lib/src/components/ContentNode/ChannelContentNode.bs index d19e36c4..bff24f53 100644 --- a/playlet-lib/src/components/ContentNode/ChannelContentNode.bs +++ b/playlet-lib/src/components/ContentNode/ChannelContentNode.bs @@ -1,7 +1,9 @@ -import "pkg:/components/ContentNode/ChannelContentTask.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/Logging.bs" function Init() end function @@ -17,7 +19,7 @@ function LoadChannel(invidiousNode as object) as void end if m.top.loadState = FeedLoadState.Loading - m.contentTask = StartAsyncTask(ChannelContentTask, { + m.contentTask = AsyncTask.Start(Tasks.ChannelContentTask, { content: m.top invidious: invidiousNode }, OnChannelContentTaskResult) diff --git a/playlet-lib/src/components/Dialog/LoginDialog.bs b/playlet-lib/src/components/Dialog/LoginDialog.bs index b15c5993..b5fc59ce 100644 --- a/playlet-lib/src/components/Dialog/LoginDialog.bs +++ b/playlet-lib/src/components/Dialog/LoginDialog.bs @@ -1,6 +1,6 @@ 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/Types.bs" @@ -33,7 +33,7 @@ function OnUrlSet() url = m.top.url m.urlLabel.text = url - StartAsyncTask(GenerateQrCodeTask, { + AsyncTask.Start(Tasks.GenerateQrCodeTask, { poster: m.qrCode text: url size: m.qrCode.loadWidth diff --git a/playlet-lib/src/components/PlayQueue/PlayQueue.bs b/playlet-lib/src/components/PlayQueue/PlayQueue.bs index 3f5df1c7..5a008e75 100644 --- a/playlet-lib/src/components/PlayQueue/PlayQueue.bs +++ b/playlet-lib/src/components/PlayQueue/PlayQueue.bs @@ -1,10 +1,10 @@ import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/NodeByRef/NodeByRef.bs" -import "pkg:/components/PlaylistView/PlaylistContentTask.bs" import "pkg:/components/PlayQueue/Notifications/PlayQueueNotificationUtils.bs" import "pkg:/components/VideoFeed/FeedLoadState.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/ErrorUtils.bs" import "pkg:/source/utils/Logging.bs" @@ -188,7 +188,7 @@ function LoadPlaylist(playlist as object) as void playlist.loadState = FeedLoadState.Loading - task = StartAsyncTask(PlaylistContentTask, { + task = AsyncTask.Start(Tasks.PlaylistContentTask, { content: playlist invidious: m.top.invidious }, OnPlaylistContentTaskResult) diff --git a/playlet-lib/src/components/PlaylistView/PlaylistView.bs b/playlet-lib/src/components/PlaylistView/PlaylistView.bs index b183e57d..1157ef60 100644 --- a/playlet-lib/src/components/PlaylistView/PlaylistView.bs +++ b/playlet-lib/src/components/PlaylistView/PlaylistView.bs @@ -2,10 +2,11 @@ import "pkg:/components/ChannelView/ChannelUtils.bs" import "pkg:/components/ContextMenu/ContextMenuUtils.bs" import "pkg:/components/Dialog/DialogUtils.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/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/FocusManagement.bs" import "pkg:/source/utils/Logging.bs" @@ -222,7 +223,7 @@ function LoadPlaylist() as void end if m.top.content.loadState = FeedLoadState.Loading - m.playlistLoadTask = StartAsyncTask(PlaylistContentTask, { + m.playlistLoadTask = AsyncTask.Start(Tasks.PlaylistContentTask, { content: m.top.content invidious: m.top.invidious }, OnPlaylistContentTaskResult) diff --git a/playlet-lib/src/components/Screens/AppInfoScreen/AppInfoScreen.bs b/playlet-lib/src/components/Screens/AppInfoScreen/AppInfoScreen.bs index cfa26629..79574869 100644 --- a/playlet-lib/src/components/Screens/AppInfoScreen/AppInfoScreen.bs +++ b/playlet-lib/src/components/Screens/AppInfoScreen/AppInfoScreen.bs @@ -1,4 +1,4 @@ -import "pkg:/source/asyncTask/asyncTask.bs" +import "pkg:/source/AsyncTask/AsyncTask.bs" function Init() ' TODO:P2 Add tooltip to each field with description of what it means diff --git a/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs b/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs index 811d4de7..ff4d2ee3 100644 --- a/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs +++ b/playlet-lib/src/components/Screens/SearchScreen/SearchScreen.bs @@ -1,7 +1,8 @@ import "pkg:/components/Navigation/Navigation.bs" -import "pkg:/components/Screens/SearchScreen/SearchSuggestionsTask.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/FocusManagement.bs" import "pkg:/source/utils/LoadingScreen.bs" @@ -98,7 +99,7 @@ function OnTextChange() as void m.searchSuggestionsTask.cancel = true end if - m.searchSuggestionsTask = StartAsyncTask(SearchSuggestionsTask, { + m.searchSuggestionsTask = AsyncTask.Start(Tasks.SearchSuggestionsTask, { q: m.keyboard.text invidious: m.invidious preferences: m.preferences diff --git a/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTesting.bs b/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTesting.bs index f7e7bd3a..0770232b 100644 --- a/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTesting.bs +++ b/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTesting.bs @@ -1,9 +1,10 @@ import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Navigation/Navigation.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/StringUtils.bs" import "pkg:/source/utils/Types.bs" function Init() @@ -58,7 +59,7 @@ function RunTestsTask() m.testingTask.cancel = true end if - m.testingTask = StartAsyncTask(InvidiousInstanceTestingTask, { + m.testingTask = AsyncTask.Start(Tasks.InvidiousInstanceTestingTask, { instance: m.top.instance listNode: m.testList }) diff --git a/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousPublicInstances/InvidiousPublicInstances.bs b/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousPublicInstances/InvidiousPublicInstances.bs index 8d81a94f..c6e2a78e 100644 --- a/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousPublicInstances/InvidiousPublicInstances.bs +++ b/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousPublicInstances/InvidiousPublicInstances.bs @@ -1,6 +1,6 @@ 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/FocusManagement.bs" import "pkg:/source/utils/Logging.bs" @@ -43,7 +43,7 @@ function Refresh() m.invidiousPublicInstancesTask.cancel = true end if - m.invidiousPublicInstancesTask = StartAsyncTask(InvidiousPublicInstancesTask, { + m.invidiousPublicInstancesTask = AsyncTask.Start(Tasks.InvidiousPublicInstancesTask, { listNode: m.instances }, OnInvidiousPublicInstancesTaskResults) diff --git a/playlet-lib/src/components/Screens/WebAppScreen/WebAppScreen.bs b/playlet-lib/src/components/Screens/WebAppScreen/WebAppScreen.bs index 3ae91a45..457bae91 100644 --- a/playlet-lib/src/components/Screens/WebAppScreen/WebAppScreen.bs +++ b/playlet-lib/src/components/Screens/WebAppScreen/WebAppScreen.bs @@ -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" function Init() @@ -22,7 +22,7 @@ function OnUrlSet() urlLabel.text = url qrCodePoster = m.top.findNode("QrCodePoster") - StartAsyncTask(GenerateQrCodeTask, { + AsyncTask.Start(Tasks.GenerateQrCodeTask, { poster: qrCodePoster text: url size: qrCodePoster.loadWidth diff --git a/playlet-lib/src/components/Services/ApplicationInfo/ApplicationInfo.bs b/playlet-lib/src/components/Services/ApplicationInfo/ApplicationInfo.bs index 300ea5d6..f7c7235a 100644 --- a/playlet-lib/src/components/Services/ApplicationInfo/ApplicationInfo.bs +++ b/playlet-lib/src/components/Services/ApplicationInfo/ApplicationInfo.bs @@ -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" function Init() @@ -32,7 +31,7 @@ function GetLatestPlayletLibVersion(_unused as dynamic) as dynamic return invalid 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 if output.success and output.result.latest <> invalid m.top.lib_version_latest = output.result.latest diff --git a/playlet-lib/src/components/Services/Invidious/Invidious.bs b/playlet-lib/src/components/Services/Invidious/Invidious.bs index dff233e0..08da3450 100644 --- a/playlet-lib/src/components/Services/Invidious/Invidious.bs +++ b/playlet-lib/src/components/Services/Invidious/Invidious.bs @@ -1,6 +1,6 @@ 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() m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml")) @@ -32,5 +32,5 @@ function GetSubscriptions() as void m.top.subscriptions = invalid return end if - m.subscriptionsTask = StartAsyncTask(InvidiousSubscriptionsTask, { invidious: m.top }) + m.subscriptionsTask = AsyncTask.Start(Tasks.InvidiousSubscriptionsTask, { invidious: m.top }) end function diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs index aab29245..5d799a2d 100644 --- a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs +++ b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs @@ -1,7 +1,7 @@ import "pkg:/components/Dialog/DialogUtils.bs" -import "pkg:/components/Services/Invidious/InvidiousUnregisterTokenTask.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/utils/CryptoUtils.bs" import "pkg:/source/utils/Logging.bs" @@ -474,7 +474,7 @@ namespace Invidious if not m.VerifyTokenScope(authToken.token) ' Right now we're probably in a UI thread, and we can't unregister the token ' Without a Task - StartAsyncTask(InvidiousUnregisterTokenTask, { + AsyncTask.Start(Tasks.InvidiousUnregisterTokenTask, { invidious: m.node authToken: authToken }) diff --git a/playlet-lib/src/components/VideoFeed/VideoRowList.bs b/playlet-lib/src/components/VideoFeed/VideoRowList.bs index 2170afc7..88177962 100644 --- a/playlet-lib/src/components/VideoFeed/VideoRowList.bs +++ b/playlet-lib/src/components/VideoFeed/VideoRowList.bs @@ -2,10 +2,11 @@ import "pkg:/components/ChannelView/ChannelUtils.bs" import "pkg:/components/ContextMenu/ContextMenuUtils.bs" import "pkg:/components/Navigation/LongPress.bs" import "pkg:/components/PlaylistView/PlaylistUtils.bs" -import "pkg:/components/VideoFeed/VideoRowListContentTask.bs" -import "pkg:/components/VideoFeed/VideoRowListRowContentTask.bs" +import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" +import "pkg:/components/VideoFeed/FeedLoadState.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/ErrorUtils.bs" import "pkg:/source/utils/Logging.bs" @@ -213,7 +214,7 @@ function InitContent() as void return end if - task = StartAsyncTask(VideoRowListContentTask, { + task = AsyncTask.Start(Tasks.VideoRowListContentTask, { node: m.top feeds: m.top.feeds }, OnVideoRowListContentTaskResult) @@ -286,7 +287,7 @@ function LoadRowContent(contentNode as object) LogInfo("Loading row content:", contentNode.title) contentNode.loadState = FeedLoadState.Loading - task = StartAsyncTask(VideoRowListRowContentTask, { + task = AsyncTask.Start(Tasks.VideoRowListRowContentTask, { rowList: m.top feedContentNode: contentNode invidious: m.invidious diff --git a/playlet-lib/src/components/VideoFeed/VideoRowListContentTask.bs b/playlet-lib/src/components/VideoFeed/VideoRowListContentTask.bs index 190f2658..d816b426 100644 --- a/playlet-lib/src/components/VideoFeed/VideoRowListContentTask.bs +++ b/playlet-lib/src/components/VideoFeed/VideoRowListContentTask.bs @@ -1,4 +1,4 @@ -import "FeedLoadState.bs" +import "pkg:/components/VideoFeed/FeedLoadState.bs" import "pkg:/source/utils/StringUtils.bs" @asynctask diff --git a/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs b/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs index afa16255..38ad7e62 100644 --- a/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs +++ b/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs @@ -1,7 +1,7 @@ import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotificationUtils.bs" -import "pkg:/components/Services/SponsorBlock/SponsorBlockSegmentViewedTask.bs" -import "pkg:/components/Services/SponsorBlock/SponsorBlockTask.bs" -import "pkg:/source/asyncTask/asyncTask.bs" +import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs" +import "pkg:/source/AsyncTask/AsyncTask.bs" +import "pkg:/source/AsyncTask/Tasks.bs" namespace SponsorBlock @@ -84,7 +84,7 @@ namespace SponsorBlock m.sponsorBlockTask.cancel = true end if - m.sponsorBlockTask = StartAsyncTask(SponsorBlockTask, { metadata: metadata }, OnSponsorBlockTaskResults) + m.sponsorBlockTask = AsyncTask.Start(Tasks.SponsorBlockTask, { metadata: metadata }, OnSponsorBlockTaskResults) end function function OnSponsorBlockTaskResults(output as object) as void @@ -172,7 +172,7 @@ namespace SponsorBlock end function function SendSkippedSectionEvent(segmentId as string) - StartAsyncTask(SponsorBlockSegmentViewedTask, { segmentId: segmentId }) + AsyncTask.Start(Tasks.SponsorBlockSegmentViewedTask, { segmentId: segmentId }) end function end namespace diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index 2f3f919a..3093717d 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -1,11 +1,10 @@ import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" -import "pkg:/components/VideoPlayer/MarkVideoWatchedTask.bs" import "pkg:/components/VideoPlayer/SponsorBlock.bs" -import "pkg:/components/VideoPlayer/VideoContentTask.bs" import "pkg:/components/VideoPlayer/VideoPlayerStyle.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/Logging.bs" @@ -77,7 +76,7 @@ function StartVideoContentTask(videoContentNode as object) m.top.content = videoContentNode - m.videoContentTask = StartAsyncTask(VideoContentTask, { + m.videoContentTask = AsyncTask.Start(Tasks.VideoContentTask, { content: videoContentNode invidious: m.top.invidious preferences: m.top.preferences @@ -118,7 +117,7 @@ end function function MarkVideoWatched(videoId as string) if m.top.invidious.authToken <> invalid - StartAsyncTask(MarkVideoWatchedTask, { + AsyncTask.Start(Tasks.MarkVideoWatchedTask, { videoId: videoId invidious: m.top.invidious }) diff --git a/playlet-lib/src/source/AsyncTask/AsyncTask.bs b/playlet-lib/src/source/AsyncTask/AsyncTask.bs new file mode 100644 index 00000000..dd8d48fd --- /dev/null +++ b/playlet-lib/src/source/AsyncTask/AsyncTask.bs @@ -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 diff --git a/playlet-lib/src/source/asyncTask/asyncTask.bs b/playlet-lib/src/source/asyncTask/asyncTask.bs deleted file mode 100644 index 6b7a9836..00000000 --- a/playlet-lib/src/source/asyncTask/asyncTask.bs +++ /dev/null @@ -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 diff --git a/tools/bs-plugins/asynctask-plugin.ts b/tools/bs-plugins/asynctask-plugin.ts index 929933bc..f6e266ad 100644 --- a/tools/bs-plugins/asynctask-plugin.ts +++ b/tools/bs-plugins/asynctask-plugin.ts @@ -3,89 +3,24 @@ import { CompilerPlugin, BscFile, - BeforeFileTranspileEvent, isBrsFile, WalkMode, createVisitor, - DiagnosticSeverity, Program, FunctionStatement, - BeforeFileValidateEvent, } from 'brighterscript'; -declare type FunctionInFile = { - functionStatement: FunctionStatement, - file: BscFile -} - export class AsyncTaskPlugin implements CompilerPlugin { public name = 'AsyncTaskPlugin'; - private asyncTaskFunctions: FunctionInFile[] = [] - 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) { + afterFileParse(file: BscFile) { if (!isBrsFile(file)) { return } - const functionsWithDuplicates = this.getFunctionsWithDuplicatesForFile(file); + const program = file.program - for (let index = 0; index < functionsWithDuplicates.length; index++) { - 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({ + file.ast.walk(createVisitor({ FunctionExpression: (func) => { if (!this.isAsyncTask(func.functionStatement)) { return @@ -93,22 +28,51 @@ export class AsyncTaskPlugin implements CompilerPlugin { const functionName = func.functionStatement!.name.text 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 bsFile = `components/asynctasks/generated/${taskName}.bs` + const bs = this.generateBsTask(functionName, hasParams, file) + const bsFile = `components/AsyncTask/generated/${taskName}.bs` 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) - event.program.setFile(bsFile, bs) + if (program.hasFile(xmlFile)) { + 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 }); } + beforeProgramValidate(program: Program) { + this.generateTaskListEnum(program); + } + isAsyncTask(functionStatement: FunctionStatement | undefined) { const annotations = functionStatement?.annotations if (!annotations || annotations.length === 0) { @@ -123,39 +87,6 @@ export class AsyncTaskPlugin implements CompilerPlugin { 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 { return ` import "pkg:/${file.pkgPath}" @@ -164,8 +95,8 @@ import "pkg:/source/utils/ErrorUtils.bs" function Init() m.top.functionName = "TaskMain" m.top.cancellation = { - node: m.top, - field: "cancel", + node: m.top + field: "cancel" value: true } end function @@ -174,9 +105,9 @@ function TaskMain() try result = ${functionName}(${(hasInput ? "m.top.input" : "")}) m.top.setField("output", { - success: true, - task: m.top, - cancelled: m.top.cancel, + success: true + task: m.top + cancelled: m.top.cancel result: result }) catch e @@ -185,9 +116,9 @@ function TaskMain() print ErrorUtils.Format(e) #end if m.top.setField("output", { - success: false, - task: m.top, - cancelled: m.top.cancel, + success: false + task: m.top + cancelled: m.top.cancel error: e }) end try @@ -206,9 +137,38 @@ end function -