From fb4d65886082cc0545b70f7ff760d164bc69a9cb Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Wed, 29 Jan 2020 15:24:26 -0500 Subject: [PATCH] Don't bundle the community index anymore, fetch from CDN instead Start fetching and preparing it just before the changeset gets sent --- data/index.js | 30 ----------------- modules/core/data.js | 2 ++ modules/ui/success.js | 78 ++++++++++++++++++++++++++++++++----------- package.json | 1 - 4 files changed, 60 insertions(+), 51 deletions(-) diff --git a/data/index.js b/data/index.js index d69487324..ed86501d4 100644 --- a/data/index.js +++ b/data/index.js @@ -1,43 +1,13 @@ export { dataLocales } from './locales.json'; export { en as dataEn } from '../dist/locales/en.json'; -import { - features as ociCustomFeatures, - resources as ociResources -} from 'osm-community-index'; - import { dataImagery } from './imagery.json'; import { presets } from './presets/presets.json'; import { defaults } from './presets/defaults.json'; import { categories } from './presets/categories.json'; import { fields } from './presets/fields.json'; -import LocationConflation from '@ideditor/location-conflation'; -import whichPolygon from 'which-polygon'; - - -// index the osm-community-index -let ociFeatures = {}; -const loco = new LocationConflation({ type: 'FeatureCollection', features: ociCustomFeatures }); - -Object.values(ociResources).forEach(resource => { - const feature = loco.resolveLocationSet(resource.locationSet); - let ociFeature = ociFeatures[feature.id]; - if (!ociFeature) { - ociFeature = JSON.parse(JSON.stringify(feature)); // deep clone - ociFeature.properties.resourceIDs = new Set(); - ociFeatures[feature.id] = ociFeature; - } - ociFeature.properties.resourceIDs.add(resource.id); -}); - - export let data = { - community: { - features: ociFeatures, - resources: ociResources, - query: whichPolygon({ type: 'FeatureCollection', features: Object.values(ociFeatures) }) - }, imagery: dataImagery, //legacy presets: { presets: presets, diff --git a/modules/core/data.js b/modules/core/data.js index 42a8ae1db..731ae5feb 100644 --- a/modules/core/data.js +++ b/modules/core/data.js @@ -16,6 +16,8 @@ export function coreData(context) { 'intro_graph': 'data/intro_graph.min.json', 'languages': 'data/languages.min.json', 'locales': 'data/locales.min.json', + 'oci_features': 'https://cdn.jsdelivr.net/npm/osm-community-index@2/dist/features.min.json', + 'oci_resources': 'https://cdn.jsdelivr.net/npm/osm-community-index@2/dist/resources.min.json', 'phone_formats': 'data/phone_formats.min.json', 'shortcuts': 'data/shortcuts.min.json', 'territory_languages': 'data/territory_languages.min.json', diff --git a/modules/ui/success.js b/modules/ui/success.js index 3ab20c4a5..989e975ff 100644 --- a/modules/ui/success.js +++ b/modules/ui/success.js @@ -1,20 +1,55 @@ import { dispatch as d3_dispatch } from 'd3-dispatch'; import { select as d3_select } from 'd3-selection'; +import LocationConflation from '@ideditor/location-conflation'; +import whichPolygon from 'which-polygon'; + import { t, languageName } from '../util/locale'; -import { data } from '../../data'; import { svgIcon } from '../svg/icon'; import { uiDisclosure } from '../ui/disclosure'; import { utilDetect } from '../util/detect'; import { utilRebind } from '../util/rebind'; +let _oci = null; + export function uiSuccess(context) { const MAXEVENTS = 2; const detected = utilDetect(); const dispatch = d3_dispatch('cancel'); let _changeset; let _location; + ensureOSMCommunityIndex(); // start fetching the data + + + function ensureOSMCommunityIndex() { + const data = context.data(); + return Promise.all([ data.get('oci_resources'), data.get('oci_features') ]) + .then(vals => { + if (_oci) return _oci; + + const ociResources = vals[0]; + const loco = new LocationConflation(vals[1]); + let ociFeatures = {}; + + Object.values(ociResources).forEach(resource => { + const feature = loco.resolveLocationSet(resource.locationSet); + let ociFeature = ociFeatures[feature.id]; + if (!ociFeature) { + ociFeature = JSON.parse(JSON.stringify(feature)); // deep clone + ociFeature.properties.resourceIDs = new Set(); + ociFeatures[feature.id] = ociFeature; + } + ociFeature.properties.resourceIDs.add(resource.id); + }); + + return _oci = { + features: ociFeatures, + resources: ociResources, + query: whichPolygon({ type: 'FeatureCollection', features: Object.values(ociFeatures) }) + }; + }); + } // string-to-date parsing in JavaScript is weird @@ -114,28 +149,31 @@ export function uiSuccess(context) { })); - // Get community index features intersecting the map.. - let communities = []; - const properties = data.community.query(context.map().center(), true) || []; + // Get OSM community index features intersecting the map.. + ensureOSMCommunityIndex() + .then(oci => { + let communities = []; + const properties = oci.query(context.map().center(), true) || []; - // Gather the communities from the result - properties.forEach(props => { - const resourceIDs = Array.from(props.resourceIDs); - resourceIDs.forEach(resourceID => { - const resource = data.community.resources[resourceID]; - communities.push({ - area: props.area || Infinity, - order: resource.order || 0, - resource: resource + // Gather the communities from the result + properties.forEach(props => { + const resourceIDs = Array.from(props.resourceIDs); + resourceIDs.forEach(resourceID => { + const resource = oci.resources[resourceID]; + communities.push({ + area: props.area || Infinity, + order: resource.order || 0, + resource: resource + }); + }); }); + + // sort communities by feature area ascending, community order descending + communities.sort((a, b) => a.area - b.area || b.order - a.order); + + body + .call(showCommunityLinks, communities.map(c => c.resource)); }); - }); - - // sort communities by feature area ascending, community order descending - communities.sort((a, b) => a.area - b.area || b.order - a.order); - - body - .call(showCommunityLinks, communities.map(c => c.resource)); } diff --git a/package.json b/package.json index 3eb53a575..760d26600 100644 --- a/package.json +++ b/package.json @@ -95,7 +95,6 @@ "name-suggestion-index": "3.7.0", "npm-run-all": "^4.0.0", "object-inspect": "0.4.0", - "osm-community-index": "2.0.0", "phantomjs-prebuilt": "~2.1.11", "request": "^2.88.0", "rollup": "~1.29.1",