Hi, what software design pattern you use when you develop Roku applications, to have the best results?
It's a good question, because Roku uses 'content nodes' to obtain data which is normally done as a background task you normally have
<component />
m.component = m.top.findNode("component")
m.component.contentNode = .... use background task to obtain data and fill TV content
m.component.visible = true
.. and that's about it ...
There's not much 2-way data binding etc in Roku because nearly all the data is read-once and display data, there is not much dynamic data to speak of.
Hope that helps!
Thank you for your answer, it is very helpful. But in the case of background task, which is the better way to call, to send data between components?. If a Factory Method or Singleton design pattern maybe (or others) fits in Roku's development process, to send and receive data between components, and how it can be implemented in BrightScript/Scenegraph.
Hi
We normally use event-driven pattern here
<ParentComponent>
<ChildComponent />
</ParentComponent>
in Parent.brs
.m.childComponent = m.top.findNode("ChildComponent")
m.childComponent.observeField("watchThisField","runThisCallbackOnEvent")
in Child.xml
<component>
<interface>
<field id="watchThisField" type="string/boolean/etc" alwaysNotify="true" />
in Child.brs
m.top.watchThisField = "some data"/true/etc // triggers the event
this will notify the parent of the event and the data can be read in the parent as
sub runThisCallbackOnEvent(message as Object)
data = message.getData()
// use data
end sub
hope that helps
If you are using a background task to fetch data you can follow this roku sample
https://developer.roku.com/en-gb/docs/references/scenegraph/control-nodes/task.md
you can still watch for the event as before in the main component, which can be triggered once the background task has returned the data
TaskComponent.xml
<component name="TaskComponent" extends="Task">
<interface>
<field id="data" type="assocArray" alwaysNotify="true">
TaskComponent.brs
sub getData()
// get data from api
m.top.data = data // triggers event
end sub
Listen for event
MainComponent.brs
m.backgroundTask = m.top.findNode("TaskComponent")
m.backgroundTask.observeField("data","onDataReceived")
sub onDataReceived(message as Object)
data = message.getData()
// use data
In this way you have one background task fetching the data and then thread dies.
Or you can create background task event loop which is always running, to fetch data on demand.