123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /**
- * this run the callback when CPU is idle. Will fallback to setTimeout if
- * the browser doesn't support requestIdleCallback
- */
- export var runWhenIdle;
- (function () {
- if (typeof requestIdleCallback !== 'undefined' &&
- typeof cancelIdleCallback !== 'undefined') {
- // use native requestIdleCallback
- runWhenIdle = function (runner, timeout) {
- var handle = requestIdleCallback(runner, typeof timeout === 'number' ? { timeout: timeout } : undefined);
- var disposed = false;
- return function () {
- if (disposed) {
- return;
- }
- disposed = true;
- cancelIdleCallback(handle);
- };
- };
- }
- else {
- // use setTimeout as hack
- var dummyIdle_1 = Object.freeze({
- didTimeout: true,
- timeRemaining: function () {
- return 15;
- },
- });
- runWhenIdle = function (runner) {
- var handle = setTimeout(function () { return runner(dummyIdle_1); });
- var disposed = false;
- return function () {
- if (disposed) {
- return;
- }
- disposed = true;
- clearTimeout(handle);
- };
- };
- }
- })();
- /**
- * a wrapper of a executor so it can be evaluated when it's necessary or the CPU is idle
- *
- * the type of the returned value of the executor would be T
- */
- var IdleValue = /** @class */ (function () {
- function IdleValue(executor) {
- var _this = this;
- this.didRun = false;
- this.selfExecutor = function () {
- try {
- _this.value = executor();
- }
- catch (err) {
- _this.error = err;
- }
- finally {
- _this.didRun = true;
- }
- };
- this.disposeCallback = runWhenIdle(function () { return _this.selfExecutor(); });
- }
- IdleValue.prototype.hasRun = function () {
- return this.didRun;
- };
- IdleValue.prototype.dispose = function () {
- this.disposeCallback();
- };
- IdleValue.prototype.getValue = function () {
- if (!this.didRun) {
- this.dispose();
- this.selfExecutor();
- }
- if (this.error) {
- throw this.error;
- }
- return this.value;
- };
- return IdleValue;
- }());
- export { IdleValue };
- //# sourceMappingURL=idleValue.js.map
|