diff --git a/modules/core/validator.js b/modules/core/validator.js index 4c2364af7..9926cc86b 100644 --- a/modules/core/validator.js +++ b/modules/core/validator.js @@ -3,7 +3,7 @@ import { dispatch as d3_dispatch } from 'd3-dispatch'; import { coreDifference } from './difference'; import { geoExtent } from '../geo'; import { osmEntity } from '../osm'; -import { utilArrayGroupBy, utilRebind } from '../util'; +import { utilArrayGroupBy, utilCallWhenIdle, utilRebind } from '../util'; import * as Validations from '../validations/index'; @@ -306,9 +306,11 @@ export function coreValidator(context) { // re-run validation upon merging fetched data context.history().on('merge.validator', function(entities) { - if (!entities) return; - var ids = entities.map(function(entity) { return entity.id; }); - validator.validateEntities(ids); + utilCallWhenIdle(function() { + if (!entities) return; + var ids = entities.map(function(entity) { return entity.id; }); + validator.validateEntities(ids); + })(); }); // // re-run validation on history change (frequent) diff --git a/modules/util/call_when_idle.js b/modules/util/call_when_idle.js deleted file mode 100644 index 1b8ae482a..000000000 --- a/modules/util/call_when_idle.js +++ /dev/null @@ -1,11 +0,0 @@ -// note the function should be of low priority -// and should not be returning a value. -export function utilCallWhenIdle(func, timeout) { - return function() { - var args = arguments; - var that = this; - window.requestIdleCallback(function() { - func.apply(that, args); - }, {timeout: timeout}); - }; -} diff --git a/modules/util/idle_worker.js b/modules/util/idle.js similarity index 59% rename from modules/util/idle_worker.js rename to modules/util/idle.js index 5e75ef324..103e0eded 100644 --- a/modules/util/idle_worker.js +++ b/modules/util/idle.js @@ -1,10 +1,32 @@ +// call a single function while idle. +// note the function should be of low priority +// and should not be returning a value. +export function utilCallWhenIdle(func, timeout) { + return function() { + var args = arguments; + var that = this; + window.requestIdleCallback(function() { + func.apply(that, args); + }, { timeout: timeout }); + }; +} + +// process queue while idle export function utilIdleWorker(tasks, processor, callback) { - var results = [], result; - for (var i = 0; i < tasks.length; i++) { - result = processor(tasks[i]); - if (result) results.push(result); - } - callback(results); + + // all tasks in single deferral + utilCallWhenIdle(function() { + var results = []; + var result; + for (var i = 0; i < tasks.length; i++) { + result = processor(tasks[i]); + if (result) results.push(result); + } + callback(results); + })(); + + // alternatively, each task deferred in its own callback + // seems to not work because it // var processed = []; // var currentPos = 0; @@ -36,7 +58,7 @@ window.requestIdleCallback = window.requestIdleCallback || function(cb) { var start = Date.now(); - return setTimeout(function() { + return window.setTimeout(function() { cb({ didTimeout: false, timeRemaining: function() { @@ -49,5 +71,5 @@ window.requestIdleCallback = window.cancelIdleCallback = window.cancelIdleCallback || function(id) { - clearTimeout(id); + window.clearTimeout(id); }; diff --git a/modules/util/index.js b/modules/util/index.js index 3ca8460cb..b7d51e668 100644 --- a/modules/util/index.js +++ b/modules/util/index.js @@ -8,7 +8,7 @@ export { utilArrayUniq } from './array'; export { utilArrayUniqBy } from './array'; export { utilAsyncMap } from './util'; -export { utilCallWhenIdle } from './call_when_idle'; +export { utilCallWhenIdle } from './idle'; export { utilCleanTags } from './clean_tags'; export { utilDisplayName } from './util'; export { utilDisplayNameForPath } from './util'; @@ -25,7 +25,7 @@ export { utilGetAllNodes } from './util'; export { utilGetSetValue } from './get_set_value'; export { utilHashcode } from './util'; export { utilHighlightEntities } from './util'; -export { utilIdleWorker } from './idle_worker'; +export { utilIdleWorker } from './idle'; export { utilKeybinding } from './keybinding'; export { utilNoAuto } from './util'; export { utilObjectOmit } from './object';