Tasks Class
A Tasks tracks general asynchronous tasks running within a Viewer.
Overview
- Each Viewer has a Tasks component, available via the Viewer's Viewer/tasks:property property, within which it will create and destroy Task components to indicate what processes it's running internally.
- You can also manage your own Task components within that, to indicate what application-level processes you are running.
Example
This example shows how to manage tasks and subscribe to their life cycles.
// Create a Viewer
var viewer = new BIMSURFER.Viewer({ element: "myDiv" });
// Get the Tasks tracker
var tasks = viewer.tasks;
// Subscribe to all task creations
tasks.on("started", function(task) {
console.log("Task started: " + task.id +", " + task.description);
});
// Subscribe to all task completions
tasks.on("completed", function(task) {
console.log("Task completed: " + task.id +", " + task.description);
});
// Subscribe to all task failures
tasks.on("failed", function(task) {
console.log("Task failed: " + task.id +", " + task.description);
});
// Create and start Task "foo"
var taskFoo = tasks.create({
id: "foo", // Optional, unique ID generated automatically when omitted
description: "Loading something"
});
// Create and start Task "bar"
var taskBar = tasks.create({
id: "bar",
description: "Loading something else"
});
// Subscribe to completion of Task "foo"
taskFoo.on("completed", function(task) {
console.log("Task completed: " + task.id +", " + task.description);
});
// Subscribe to failure of a specific task
taskFoo.on("failed", function(task) {
console.log("Task failed: " + task.id +", " + task.description);
});
// Set Task "foo" as completed, via the Tasks
// Fires the "completed" handler we registered above, also fires "completed" on the Task itself
tasks.setCompleted("foo");
// Set Task "bar" as failed, this time directly on the Task in question
myTask2.setFailed();
Constructor
Tasks
()
Item Index
Methods
create
-
params
Creates and starts a new Task instance with this Tasks.
If an ID is given for the new Task that is already in use for another, will log an error message and return null.
On success, fires a Tasks/started:event event and returns the new Task instance.
Parameters:
-
params
ObjectTask params.
-
[id]
String optionalOptional unique ID, internally generated if not supplied.
-
[description]
String optionalOptional description.
-
destroy
()
error
-
message
Logs an error for this component to the JavaScript console.
The console message will have this format: [ERROR] <component id>: <message>
Parameters:
-
message
StringThe message to log
fire
-
event
-
value
-
[forget=false]
Fires an event on this component.
Notifies existing subscribers to the event, retains the event to give to any subsequent notifications on that location as they are made.
Parameters:
-
event
StringThe event type name
-
value
ObjectThe event
-
[forget=false]
Boolean optionalWhen true, does not retain for subsequent subscribers
log
-
message
Logs a console debugging message for this component.
The console message will have this format: [LOG] <component id>: <message>
Parameters:
-
message
StringThe message to log
off
-
handle
Parameters:
-
handle
StringSubscription handle
on
-
event
-
callback
-
[scope=this]
Subscribes to an event on this component.
The callback is be called with this component as scope.
Parameters:
-
event
StringPublication event
-
callback
FunctionCalled when fresh data is available at the event
-
[scope=this]
Object optionalScope for the callback
Returns:
Handle to the subscription, which may be used to unsubscribe with {@link #off}.
once
-
event
-
callback
-
[scope=this]
Subscribes to the next occurrence of the given event, then un-subscribes as soon as the event is handled.
Parameters:
-
event
StringData event to listen to
-
callback
Function(data)Called when fresh data is available at the event
-
[scope=this]
Object optionalScope for the callback
setCompleted
-
id
Completes the Task with the given ID.
Fires a completed event, as well as separate completed event on the Task itself.
Logs an error message if no task can be found for the given ID.
Parameters:
-
id
StringID of the Task to complete.
setFailed
-
id
Fails the Task with the given ID.
Fires a failed event, as well as separate failed event on the Task itself.
Logs an error message if no task can be found for the given ID.
Parameters:
-
id
StringID of the Task to fail.
warn
-
message
Logs a warning for this component to the JavaScript console.
The console message will have this format: [WARN] <component id>: <message>
Parameters:
-
message
StringThe message to log
Properties
className
String
final
JavaScript class name for this Component.
destroyed
Boolean
True as soon as this Component has been destroyed
Items in this map
Unknown
metadata
Object
Metadata on this component.