can-event/async/async
Makes the event system asynchronous. WARNING: This is experimental technology.
Object
The can-event/async/async
module makes the event system asynchronous. It:
- Provides an async method which converts event binding and dispatching to happen asynchronously.
- Provides sync method which converts event binding and dispatching to happen synchronously.
- Provides an asynchronous dispatch, queue, [can-event/async/async.addEventListener] and [can-event/async/async.removeEventListener].
- Provides a flush which can be used to immediately run all tasks in the task queue.
Use
Use can-event/async/async
's async
method to make event binding and
dispatching happen immediately following the current event loop.
var canEvent = require("can-event");
var canAsync = require("can-event/async/async");
canAsync.async();
var obj = {};
Object.assign(obj, canEvent);
obj.addEventListener("foo", function(){
console.log("heard foo");
});
obj.dispatch("foo");
console.log("dispatched foo");
// Logs -> "dispatched foo" then "heard foo"
This means you never have to call start and stop. Notice
that in the following example "change"
is only fired once:
var canAsync = require("can-event/async/async");
canAsync.async();
var compute = require("can-compute");
var first = compute("Justin");
var last = compute("Meyer");
var fullName = compute(function(){
return first() + " " + last();
});
fullName.on("change", function(ev, newVal, oldVal){
newVal //-> "Payal Shah"
oldVal //-> "Justin Meyer"
});
first("Payal");
last("Shah");