Fork me on GitHub

DependencyWorkers Examples

PAGE IS STILL IN CONSTRUCTION

dependency-workers.js is a library for defining tree of tasks depend on each other and execute them efficiently. The library includes builtin support in Web-Workers. The library supports some simple use cases which will be detailed in this page.


Simple use case

If you have a lot of tasks that are common to some processings, you might want to reduce duplication of processing. The DependencyWorkers is an auxiliary class which will spawn the tasks for you and will ensure that there are no duplicated processings. The DependencyWorkers is especially good for siutations of some dependent tasks. The output of one task may be used for the next task.

Let's implement a Pascal Triangle, in which every cell is a sum of two ancestors. A typical use of the dependency-workers.js library includes implementation of three parts:

  • InputRetreiver - a class which manages the tasks. Each task is identified by a key. Several types of tasks may exist, each has another worker script defined by getWorkerTypeOptions method (or even no worker - a UI task). The InputRetreiver should implement a method taskStarted which provide the inputs for the task including the dependent tasks. Also a getKeyAsString method should be implemented to map each key to a unique string.
  • Web Worker(s) code in separate file(s) - which performs the actual work of a Web Worker task. See async-proxy.js library API for more information.
  • Application code - which uses the above classes: Instantiation of DependencyWorkers class with the above InputRetreiver, and use of its startTask or startTaskPromise methods.

First let's include dependency-workers.js in your page:

<script src="http://MaMazav.github.io/cdn/dependency-workers.dev.js"></script>

Following is demonstrated an InputRetreiver class of a Pascal triangle implementation:

The application can use it as follows:

And the actual work is done on a worker which is implemented as follows:


Progressiveness

The dependency-workers.js API enables the freedom to control complicated behavior of tasks according to your needs. In this section we will demonstrate how to perform a progressive calculation. In this progressive calculation, any task returns multiple results which are gradually refined over time. Such progressiveness and wide range of behaviors can be achieved by controlling the timing of the calls to task.dataReady and task.terminate methods of the task object (task is the taskStarted's argument as demonstrated above) and by using the task object's statusUpdated event which provides additional useful information:

Both application code and InputRetreiver implementation are more complex to support such custom behavior. It's recommended to use the simple form as shown in the simple example if no need for a custom behavior.


Priority and scheduling

One way to achieve control over the task scheduling is by deferring the call to taskStarted method. However it might be a complex task to maintiain the scheduling. The SchedulerDependencyWorkers will do that job for you. You may provide a scheduler object which is implemented according to the resource-scheduler.js library API, or using the SchedulerWrapperInputRetreiver class for basic scheduling of Worker count limitation and Prioritization features.

The priority of each task is determined by priorityCalculator property passed to startTask. The resource-scheduler.js scheduler calls it according to its internal algorithm (see PriorityScheduler's Scheduling algorithm for more details). For dependant tasks, dependency-workers.js accumulates the maximum priority of all the task it depends on.

To use it, only need to wrap a simple DependencyWorkers with the SchedulerDependencyWorkers (see below), and provide priorityCalculator function (already appeared on previous example).