Skip to content

Asynchronous Processing

BobDickinson edited this page Oct 4, 2014 · 1 revision

Synchro supports asynchronous processing in two ways.

First, if a page needs to perform an asynchronous or long-running task when it is loaded, it may establish an initial view model in InitializeViewModel and then further populate that view model asynchronously in LoadViewModel. The initial view model and corresponding view will be returned to the client at the completion of InitializeViewModel (and subsequent view rendering), so that the client can render the initial user interface. Then LoadViewModel will be called to complete subsequent view model loading/population.

Second, if a page needs to perform an asynchronous or long-running task, typically in a command, it may use Synchro.interimUpdate() to provide periodic updates of the view model back to the client.

Any time a Synchro module calls into an asynchronous function, it should use Synchro.waitFor(), which will both streamline the module design and automatically share the view model state between potentially multiple processors running against a given module instance.

It is also good practice to call Synchro.isActiveInstance() periodically in any asynchronous loop, in order to determine if it will be productive to continue (if it returns false, the page has been navigated away from, possibly in response to a separate update/command, so no further updates to the view model being processed will be communicated back to the client).

Following is an example of a simple asynchronous loop demonstrating these principles:

while (Synchro.isActiveInstance(context) && !viewModel.cancelled)
{
    Synchro.interimUpdate(context);
    Synchro.waitFor(someAsyncFunction);
    viewModel.progress++;
} 

!!! Need demo, like countdown, showing all of this