Workers are templates that typically:
Live 24-7 or for prolonged periods.
Supply live-data and/or change playback-flow based on external sources (WebSocket, EventSource, SystemBridge-scripts, etc).
Run in a (normally hidden) layer-playlist which only contains instances (messages) of the Worker. Usually there is only one instance, but there are cases where a sequence is useful.
Live “across” message-invocations by utilizing template-reuse.
Are restarted only when the active Template/Message is changed in the CMS (provided the Dise One Player v3 or later is used).
Implementing the Worker pattern is simplified by inheriting from Dise.WorkerViewBase. Remember to set “allowReuse“ to true in the template.json and MockData\Settings.json. The “WorkerTemplate“ Boilerplate can used as a starting point.
main() and onNewContent()
As in a SimpleTemplate, the main-method is the only requirement. In contrast to SimpleTemplate, however, main() is called from the onShift-stage in execution-flow, so there is no stage-transition to consider.
Because Workers use template-reuse, if main() lasts longer than the CMS-duration of the message, it will continue to be alive when the Player continues on to the next message in the Playlist.
If the contents (payloads) of the previous and next message are different, the onNewMessage() event will be fired (from the onPreload-stage). This can be implemented as async if necessary.
As an analogy, think of this as creating a classic executable file in C/C++/C#/Java. main() is the starting point and if it subscribes to some external interface, it will be called with new data when available.
Return values from main()
The return value instructs the BaseView how to end the template instance.
Throwing an Error will cause a Client Alarm. This will cause CMS-logging, the Player will proceed to the next Message in the Playlist and start it fresh (i.e. if the Playlist only contains a single Worker-template, it will be restarted and main() called again).
Returning falsy will skip to the next Playlist-Message immediately and, if it is an instance of this Worker, main() will be started anew (but without a full template restart, so e.g. the class-member data will remain intact)
Returning truthy will wait for the remaining duration of the current Message before ending, then behave like case 2.
Changing the restart behavior
The WorkerViewBase implements the onTemplateUpdate event. This is called by the (Dise One v3+) Player when:
The currently active content (message) is modified in the CMS.
The currently active content (message) is deleted from the CMS.
The Template is updated in the CMS.
By default, the ViewBase will restart in all three cases. If this is not suitable, this can be filtered by overriding the event-handler.
As an example, if we only want a restart when case 3 happens, but ignore 1 & 2, override onTemplateUpdate in the View and call “up” like this:
onTemplateUpdate(update: Dise.TemplateUpdate): void {
if (update.templateUpdated)
super.onTemplateUpdate(update);
}Other options would be to inform the running main() about a change and allow a clean exit or end.
On a Player older than v3, there is no event and no way of knowing when a template is changed nor any reliable way to cause a template-restart. There are possible workarounds, but they are all dependent on the use-case. Please contact us if you cannot upgrade to Dise v3+ and need this.