rxjs-zone-less
Version:
A set of wrappers for RxJS to avoid unnecessary change detection and zone interference in Angular.
35 lines (34 loc) • 1.03 kB
JavaScript
import { Promise } from './zone-less';
var nextHandle = 1;
// The promise needs to be created lazily otherwise it won't be patched by Zones
var resolved;
var activeHandles = {};
/**
* Finds the handle in the list of active handles, and removes it.
* Returns `true` if found, `false` otherwise. Used both to clear
* Immediate scheduled tasks, and to identify if a task should be scheduled.
*/
function findAndClearHandle(handle) {
if (handle in activeHandles) {
delete activeHandles[handle];
return true;
}
return false;
}
/**
* Helper functions to schedule and unschedule microtasks.
*/
export var Immediate = {
setImmediate: function (cb) {
var handle = nextHandle++;
activeHandles[handle] = true;
if (!resolved) {
resolved = Promise.resolve();
}
resolved.then(function () { return findAndClearHandle(handle) && cb(); });
return handle;
},
clearImmediate: function (handle) {
findAndClearHandle(handle);
},
};