diff --git a/.vscode/launch.json b/.vscode/launch.json index 5766dd9..972dae5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,6 +6,9 @@ "type": "brightscript", "request": "launch", "rootDir": "${workspaceFolder}/dist", + "files": [ + "**/*" + ], "preLaunchTask": "build-tests", "enableDebuggerAutoRecovery": true, "stopDebuggerOnAppExit": true, diff --git a/.vscode/settings.json b/.vscode/settings.json index 918ce9a..553d66e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" + ] } diff --git a/bsconfig.json b/bsconfig.json index 1c47c3a..1722651 100644 --- a/bsconfig.json +++ b/bsconfig.json @@ -1,7 +1,7 @@ { "files": [ "**/*", - "!**/*.spec.bs" + "!**/*.spec.*" ], "plugins": [ "@rokucommunity/bslint" diff --git a/bsconfig.tests.json b/bsconfig.tests.json index 64ba4b4..45555df 100644 --- a/bsconfig.tests.json +++ b/bsconfig.tests.json @@ -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" - ] - } -} \ No newline at end of file + "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" + ] + } +} diff --git a/demos/simple-brightscript/components/MainScene.bs b/demos/simple-brightscript/components/MainScene.bs index 38f9566..0b215bf 100644 --- a/demos/simple-brightscript/components/MainScene.bs +++ b/demos/simple-brightscript/components/MainScene.bs @@ -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/") @@ -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 @@ -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 { @@ -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 @@ -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 @@ -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() diff --git a/demos/simple-brightscript/components/TaskWithInternalPromises.bs b/demos/simple-brightscript/components/TaskWithInternalPromises.bs new file mode 100644 index 0000000..21728d3 --- /dev/null +++ b/demos/simple-brightscript/components/TaskWithInternalPromises.bs @@ -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 diff --git a/demos/simple-brightscript/components/TaskWithInternalPromises.xml b/demos/simple-brightscript/components/TaskWithInternalPromises.xml new file mode 100644 index 0000000..87160c9 --- /dev/null +++ b/demos/simple-brightscript/components/TaskWithInternalPromises.xml @@ -0,0 +1,8 @@ + + +