Consolidate idle functions into idle.js, defer validation after merge

This commit is contained in:
Bryan Housel
2019-04-12 10:49:21 -04:00
parent 0dd262d1dd
commit efcd6b6bc2
4 changed files with 38 additions and 25 deletions

View File

@@ -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)

View File

@@ -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});
};
}

View File

@@ -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);
};

View File

@@ -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';