Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"type": "brightscript",
"request": "launch",
"rootDir": "${workspaceFolder}/dist",
"files": [
"**/*"
],
"preLaunchTask": "build-tests",
"enableDebuggerAutoRecovery": true,
"stopDebuggerOnAppExit": true,
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"brightscript.bsdk": "./node_modules/brighterscript"
"brightscript.bsdk": "./node_modules/brighterscript",
"brightscript.projects": [
"bsconfig.json",
"bsconfig.tests.json"
]
}
2 changes: 1 addition & 1 deletion bsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": [
"**/*",
"!**/*.spec.bs"
"!**/*.spec.*"
],
"plugins": [
"@rokucommunity/bslint"
Expand Down
63 changes: 33 additions & 30 deletions bsconfig.tests.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
{
"stagingDir": "./dist",
"extends": "./bsconfig.json",
"retainStagingDir": true,
"createPackage": false,
"files": [
"**/*"
],
"plugins": [
"rooibos-roku"
],
"rooibos": {
"isRecordingCodeCoverage": false,
"isGlobalMethodMockingEnabled": true,
"testsFilePattern": null,
"tags": [
"!integration",
"!deprecated",
"!fixme"
],
"showOnlyFailures": true,
"catchCrashes": true,
"lineWidth": 70,
"failFast": false,
"sendHomeOnFinish": false,
"colorizeOutput": true,
"reporters": [
"mocha"
]
}
}
"files": [
"**/*"
],
"rootDir": "./src",
"stagingDir": "./dist",
"emitDefinitions": true,
"sourceMap": true,
"autoImportComponentScript": true,
"retainStagingDir": true,
"createPackage": false,
"plugins": [
"rooibos-roku"
],
"rooibos": {
"isRecordingCodeCoverage": false,
"isGlobalMethodMockingEnabled": true,
"testsFilePattern": null,
"tags": [
"!integration",
"!deprecated",
"!fixme"
],
"showOnlyFailures": true,
"catchCrashes": true,
"lineWidth": 70,
"failFast": false,
"sendHomeOnFinish": false,
"colorizeOutput": true,
"reporters": [
"mocha"
]
}
}
50 changes: 26 additions & 24 deletions demos/simple-brightscript/components/MainScene.bs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ sub init()

'Uncomment the example below to run...

'Simple network request example (passing in the `m` scope as context)
'Simple network request example
' promise = networkRequest("http://ip-api.com/json/", "GET")
' promises_onThen(promise, sub(response as object, context = {} as dynamic)
' promises_onThen(promise, sub(response as object)
' print "Your timezone is " + response.timezone
' end sub, m)
' end sub)

'Simple single request using method
' separateCallbacksExample("http://ip-api.com/json/")
Expand All @@ -23,6 +23,19 @@ sub init()

'Simple single request using method with error handling
' separateCallbacksExample("http://invalid--url.com")

runTaskWithInternalPromises()
end sub

function runTaskWithInternalPromises()
task = createObject("roSGNode", "TaskWithInternalPromises")
task.observeFieldScoped("response", "onTaskWithInternalPromisesResponseChange")
task.control = "RUN"
end function

sub onTaskWithInternalPromisesResponseChange(event as object)
print "onTaskWithInternalPromisesResponseChange:"
print event.getData()
end sub

function networkRequest(url as string, method = "GET" as string, body = {} as object) as object
Expand Down Expand Up @@ -74,7 +87,7 @@ sub chainExample()
promises_chain(promise, context).then(function(ipApiData, context)
if (ipApiData.error = invalid)
print "Promises chain first call completed!!!"
return getTimeApiTimeToFiji(ipApiData.timezone)
return validateTimezoneInformation(ipApiData.timezone)
else
print "Promises chain first call failed!!!"
return {
Expand All @@ -94,12 +107,11 @@ sub chainExample()
end if

m.top.chainResult = context

end function).catch(function(error, context)
print "Caught an error with the chain promise!!!", error

end function).finally(function(error, context)
print "Chain promise completed!!!", error
end function).finally(function(context)
print "Chain promise completed!!!", context
end function)
end sub

Expand Down Expand Up @@ -158,18 +170,11 @@ function getIpApiTimeZoneByDomain(domain as string) as object
return networkRequest(url)
end function

function getTimeApiTimeToFiji(fromTimeZone as object) as object
function validateTimezoneInformation(fromTimeZone as object) as object
print "Your timezone is " + fromTimeZone
url = "https://timeapi.io/api/Conversion/ConvertTimeZone"
method = "POST"

body = {
"fromTimeZone": fromTimeZone
"dateTime": getFullDate() + " 00:00:00"
"toTimeZone": "Pacific/Fiji"
"dstAmbiguity": ""
}
return networkRequest(url, method, body)
url = "http://worldtimeapi.org/api/timezone/" + fromTimeZone
method = "GET"
return networkRequest(url, method)
end function

function getFullDate() as string
Expand All @@ -190,12 +195,9 @@ function getFullDate() as string
end function

function getChainResultString(data as object) as object
currentDay = 25
fijiDay = data.conversionResult.day
fijiHour = data.conversionResult.hour
aheadOrBehind = "ahead"
if (fijiDay > currentDay) then aheadOrBehind = "behind"
return "Fiji is " + fijiHour.toStr() + " hours " + aheadOrBehind + " of your timezone!"
timezone = data.timezone
doy = data.day_of_year
return "Confirming your timezone is is " + timezone + " and the day of year is " + doy.toStr() + "!"
end function

sub initOnscreenImage()
Expand Down
27 changes: 27 additions & 0 deletions demos/simple-brightscript/components/TaskWithInternalPromises.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
sub init()
m.port = CreateObject("roMessagePort")
m.top.functionName = "startTask"
end sub

sub startTask()
promises.initTaskFunctionality(m.port)
promise = doNetworkRequest("http://ip-api.com/json/", "GET", {})
promises.chain(promise).then(function(response)
m.top.response = response
end function).catch(function(error)
print "Error in TaskWithInternalPromises:", error
end function)

promises.runEventLoop(m.port, sub(message)
print "Task with internal promises received message:", message
end sub)
end sub


function doNetworkRequest(url as string, method as string, body as object) as object
promise = promises.create()
promises.resolve({
"something": "OF VALUE"
}, promise)
return promise
end function
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?rokuml version="1.0" encoding="utf-8"?>
<component name="TaskWithInternalPromises" extends="Task">
<script type="text/brightscript" uri="TaskWithInternalPromises.bs"/>
<script type="text/brightscript" uri="pkg:/source/promises.bs"/>
<interface>
<field id="response" type="assocarray"/>
</interface>
</component>
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@rokucommunity/bslint": "^0.8.38",
"@types/fs-extra": "^11.0.1",
"@types/node": "^20.6.0",
"brighterscript": "0.69.10",
"brighterscript": "0.69.11",
"dotenv": "^16.3.1",
"fs-extra": "^11.1.1",
"roku-deploy": "^3.14.4",
Expand Down
1 change: 1 addition & 0 deletions src/components/Test.spec.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<component name="Test" extends="group" />
69 changes: 69 additions & 0 deletions src/components/TestTask.spec.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import "pkg:/source/promises.bs"

function doAction(actionName as string) as dynamic
m.port = createObject("roMessagePort")
m.top.promise = promises.create()
m.top.functionName = actionName
m.top.control = "RUN"
return m.top.promise
end function

sub test1()
promises.setMessagePort(m.port)
promises.chain(promises.resolve(true)).then(sub(response as object)
promises.resolve("task completed", m.top.promise)
end sub).finally(sub(response as object)
m.top.control = "STOP"
end sub)

while true
message = promises.wait2(20, m.port)
end while
end sub

sub test2()
promises.setMessagePort(m.port)
promises.chain(promises.resolve(true)).then(function(response as object) as dynamic
return doNetworkRequest("http://ip-api.com/json/")
end function).then(sub(response as object)
promises.resolve(response, m.top.promise)
end sub).finally(sub()
m.top.control = "STOP"
end sub)

m.top.observeField("request", m.port)
while true
message = promises.wait2(0, m.port)
if type(message) = "roSGNodeEvent" then
requestId = message.getData()
promises.resolve({ ipInfo: "hello" }, m.requestStorage[requestId].promise)
end if
end while
end sub

sub test3()
promises.setMessagePort(m.port)
promises.chain(promises.resolve(true), {}).then(sub(response as object, context as object) as dynamic
promises.reject("Simulated error", m.top.promise)
end sub).finally(sub(context as object)
m.top.control = "STOP"
end sub)

while true
message = promises.wait2(20, m.port)
end while
end sub

function doNetworkRequest(url as string) as dynamic
promise = promises.create()
if m.requestStorage = invalid then
m.requestStorage = {}
end if

m.requestStorage[url] = {
promise: promise
}

m.top.request = url
return promise
end function
7 changes: 7 additions & 0 deletions src/components/TestTask.spec.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="TestTask" extends="Task">
<interface>
<field id="promise" type="node" />
<field id="request" type="string" />
<function name="doAction" />
</interface>
</component>
1 change: 0 additions & 1 deletion src/components/test.spec.xml

This file was deleted.

Loading
Loading