From 909b72a39e884518062de7caa5fc34e59cd9c998 Mon Sep 17 00:00:00 2001 From: Kushan Joshi <0o3ko0@gmail.com> Date: Thu, 24 Aug 2017 16:53:40 +0530 Subject: [PATCH] cleaning up --- modules/core/context.js | 1 - modules/renderer/map.js | 115 ++++++--------------------------- modules/svg/labels.js | 5 +- modules/ui/background.js | 4 +- modules/util/call_when_idle.js | 6 +- modules/util/index.js | 2 +- 6 files changed, 26 insertions(+), 107 deletions(-) diff --git a/modules/core/context.js b/modules/core/context.js index ed1d77f34f..0c4032e984 100644 --- a/modules/core/context.js +++ b/modules/core/context.js @@ -16,7 +16,6 @@ import { utilRebind } from '../util/rebind'; import { utilCallWhenIdle } from '../util/index'; - export var areaKeys = {}; export function setAreaKeys(value) { diff --git a/modules/renderer/map.js b/modules/renderer/map.js index 6350405f2c..8fbad68e32 100644 --- a/modules/renderer/map.js +++ b/modules/renderer/map.js @@ -32,7 +32,6 @@ import { utilGetDimensions } from '../util/dimensions'; - export function rendererMap(context) { var dimensions = [1, 1], @@ -58,20 +57,6 @@ export function rendererMap(context) { mouse, mousemove; - // var debouncedTransformStart = () => requestAnimationFrame(() => transformStart = projection.transform()); - // var setProjectionTransformStart = () => debSetTransformStart(projection.transform()); - - // var debSetTransformStart = _.debounce((val) => transformStart = val, 50); - - // const debouncedTransform - // var projectIdleCb; - var savedToDrawVertices; - var saveToDrawLines; - var saveToDrawArea; - var saveToDrawMidpoints; - var saveToDrawLabels; - var saveToDrawPoints; - var zoom = d3.zoom() .scaleExtent([ztok(2), ztok(24)]) .interpolate(d3.interpolate) @@ -79,25 +64,24 @@ export function rendererMap(context) { .on('zoom', zoomPan); var _selection = d3.select(null); - var isRenderScheduled = false; + var isRedrawScheduled = false; var pendingRedrawCall; - function initiateRedraw() { - // Reset the boolean so future redraws can be set. - isRenderScheduled = false; - redraw.apply(this, arguments); - } - + function scheduleRedraw() { // Only schedule the redraw if one has not already been set. - if (isRenderScheduled) return; - - isRenderScheduled = true; - - pendingRedrawCall = requestIdleCallback(() => initiateRedraw.apply(this, arguments), { timeout: 1400 }); + if (isRedrawScheduled) return; + isRedrawScheduled = true; + var that = this; + var args = arguments; + pendingRedrawCall = requestIdleCallback(function () { + // Reset the boolean so future redraws can be set. + isRedrawScheduled = false; + redraw.apply(that, args); + }, { timeout: 1400 }); } function cancelPendingRedraw() { - isRenderScheduled = false; + isRedrawScheduled = false; window.cancelIdleCallback(pendingRedrawCall); } @@ -301,67 +285,6 @@ export function rendererMap(context) { .call(drawLabels, graph, data, filter, dimensions, !difference && !extent) .call(drawPoints, graph, data, filter); - // var toDrawVertices = (selection) => { - // // window.cancelIdleCallback(savedToDrawVertices); - // savedToDrawVertices = requestIdleCallback(() => { - // drawVertices(selection, graph, data, filter, map.extent(), map.zoom()); - // setProjectionTransformStart(); - // toDrawLines(selection); - // }, {timeout: 150}); - // }; - - // var toDrawLines = (selection) => { - // // window.cancelIdleCallback(saveToDrawLines); - // saveToDrawLines = requestIdleCallback(() => { - // drawLines(selection, graph, data, filter); - // // setProjectionTransformStart(); - // toDrawAreas(selection); - // }, {timeout: 150}); - // }; - - // var toDrawAreas = (selection) => { - // // window.cancelIdleCallback(saveToDrawArea); - // saveToDrawArea = requestIdleCallback(() => { - // drawAreas(selection, graph, data, filter); - // // setProjectionTransformStart(); - // toDrawMidpoints(selection); - // }, {timeout: 150}); - // }; - - // var toDrawMidpoints = (selection) => { - // // window.cancelIdleCallback(saveToDrawMidpoints); - // saveToDrawMidpoints = requestIdleCallback(() => { - // drawMidpoints(selection,graph, data, filter, map.trimmedExtent()); - // // setProjectionTransformStart(); - // toDrawLabels(selection); - // }, {timeout: 150}); - // }; - - // var toDrawLabels = (selection) => { - // // window.cancelIdleCallback(saveToDrawLabels); - // saveToDrawLabels = requestIdleCallback(() => { - // drawLabels(selection, graph, data, filter, dimensions, !difference && !extent); - // // setProjectionTransformStart(); - // toDrawPoints(selection); - // }, {timeout: 150}); - // }; - - // var toDrawPoints = (selection) => { - // // window.cancelIdleCallback(saveToDrawPoints); - // saveToDrawPoints = requestIdleCallback(() => { - // drawPoints(selection, graph, data, filter); - // setProjectionTransformStart(); - // }, {timeout: 150}); - // }; - - // surface.selectAll('.data-layer-osm') - // .call(toDrawVertices) - // .call(toDrawLines) - // .call(toDrawAreas) - // .call(toDrawMidpoints) - // .call(toDrawLabels) - // .call(toDrawPoints); - dispatch.call('drawn', this, {full: true}); } @@ -419,7 +342,7 @@ export function rendererMap(context) { surface.interrupt(); uiFlash().text(t('cannot_zoom')); setZoom(context.minEditableZoom(), true); - queueRedraw(); + scheduleRedraw(); dispatch.call('move', this, map); return; } @@ -442,7 +365,7 @@ export function rendererMap(context) { transformed = true; transformLast = eventTransform; utilSetTransform(supersurface, tX, tY, scale); - queueRedraw(); + scheduleRedraw(); dispatch.call('move', this, map); } @@ -499,8 +422,6 @@ export function rendererMap(context) { } - var queueRedraw = scheduleRedraw; - var immediateRedraw = function(difference, extent) { if (!difference && !extent) cancelPendingRedraw(); @@ -672,7 +593,7 @@ export function rendererMap(context) { mouse = utilFastMouse(supersurface.node()); setCenter(center); - queueRedraw(); + scheduleRedraw(); return map; }; @@ -701,7 +622,7 @@ export function rendererMap(context) { dispatch.call('move', this, map); } - queueRedraw(); + scheduleRedraw(); return map; }; @@ -721,7 +642,7 @@ export function rendererMap(context) { dispatch.call('move', this, map); } - queueRedraw(); + scheduleRedraw(); return map; }; @@ -744,7 +665,7 @@ export function rendererMap(context) { dispatch.call('move', this, map); } - queueRedraw(); + scheduleRedraw(); return map; }; diff --git a/modules/svg/labels.js b/modules/svg/labels.js index 90a3ea7f26..7b5fdb8e03 100644 --- a/modules/svg/labels.js +++ b/modules/svg/labels.js @@ -18,7 +18,8 @@ import { utilDetect } from '../util/detect'; import { utilDisplayName, utilDisplayNameForPath, - utilEntitySelector + utilEntitySelector, + utilCallWhenIdle } from '../util/index'; @@ -652,7 +653,7 @@ export function svgLabels(projection, context) { } - var throttleFilterLabels = _.throttle(filterLabels, 100); + var throttleFilterLabels = _.throttle(utilCallWhenIdle(filterLabels), 100); drawLabels.observe = function(selection) { diff --git a/modules/ui/background.js b/modules/ui/background.js index a9686fc161..419931fe07 100644 --- a/modules/ui/background.js +++ b/modules/ui/background.js @@ -4,7 +4,7 @@ import { d3keybinding } from '../lib/d3.keybinding.js'; import { t, textDirection } from '../util/locale'; import { geoMetersToOffset, geoOffsetToMeters } from '../geo/index'; import { utilDetect } from '../util/detect'; -import { utilSetTransform } from '../util/index'; +import { utilSetTransform, utilCallWhenIdle } from '../util/index'; import { svgIcon } from '../svg/index'; import { uiMapInMap } from './map_in_map'; import { uiCmd } from './cmd'; @@ -540,7 +540,7 @@ export function uiBackground(context) { ); context.map() - .on('move.background-update', _.debounce(update, 1000)); + .on('move.background-update', _.debounce(utilCallWhenIdle(update), 1000)); context.background() .on('change.background-update', update); diff --git a/modules/util/call_when_idle.js b/modules/util/call_when_idle.js index e86de47471..1b8ae482a0 100644 --- a/modules/util/call_when_idle.js +++ b/modules/util/call_when_idle.js @@ -1,13 +1,11 @@ // note the function should be of low priority // and should not be returning a value. -export function utilCallWhenIdle(func, timeout, name) { +export function utilCallWhenIdle(func, timeout) { return function() { var args = arguments; var that = this; - console.log('called ', name); window.requestIdleCallback(function() { - console.log('idle succeed ', name); func.apply(that, args); }, {timeout: timeout}); }; -} \ No newline at end of file +} diff --git a/modules/util/index.js b/modules/util/index.js index b71c2fd4ed..f3bf74842c 100644 --- a/modules/util/index.js +++ b/modules/util/index.js @@ -23,4 +23,4 @@ export { utilTagText } from './util'; export { utilTriggerEvent } from './trigger_event'; export { utilWrap } from './util'; export { utilIdleWorker} from './idle_worker'; -export { utilCallWhenIdle } from './call_when_idle'; \ No newline at end of file +export { utilCallWhenIdle } from './call_when_idle';