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
- 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
+2 -1
View File
@@ -77,5 +77,6 @@
"require": [
"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
```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
+21 -21
View File
@@ -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",
+5 -5
View File
@@ -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",
@@ -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")
@@ -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
@@ -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)
@@ -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
@@ -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)
@@ -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)
@@ -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
@@ -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
@@ -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
})
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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
})
@@ -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
@@ -1,4 +1,4 @@
import "FeedLoadState.bs"
import "pkg:/components/VideoFeed/FeedLoadState.bs"
import "pkg:/source/utils/StringUtils.bs"
@asynctask
@@ -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
@@ -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
})
@@ -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 {
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
<field id="cancel" type="boolean" alwaysNotify="true" />
<field id="cancellation" type="assocarray" />
</interface>
<script type="text/brightscript" uri="pkg:/${bsFile}" />
</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 () => {