Skip to content

Commit 868db88

Browse files
committed
Have mergeLocationSets work on Objects, add locationSetID property
This means less work for the other parts of the code, now they don't need to chain that extra step after the promise.
1 parent c4daf1b commit 868db88

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

modules/core/locations.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function coreLocations() {
2626
let _loco = new LocationConflation(); // instance of a location-conflation resolver
2727
let _wp; // instance of a which-polygon index
2828

29-
resolveLocationSet({ include: ['Q2'] }); // pre-resolve the worldwide locationSet
29+
// pre-resolve the worldwide locationSet
30+
const world = { locationSet: { include: ['Q2'] } };
31+
resolveLocationSet(world);
3032
rebuildIndex();
3133

3234
let _queue = [];
@@ -53,8 +55,14 @@ export function coreLocations() {
5355
.then(() => processQueue());
5456
}
5557

56-
function resolveLocationSet(locationSet) {
58+
function resolveLocationSet(obj) {
59+
if (obj.locationSetID) return; // work was done already
60+
5761
try {
62+
const locationSet = obj.locationSet;
63+
if (!locationSet) {
64+
throw new Error('object missing locationSet property');
65+
}
5866
const resolved = _loco.resolveLocationSet(locationSet);
5967
const locationSetID = resolved.id;
6068
if (!resolved.feature.geometry.coordinates.length || !resolved.feature.properties.area) {
@@ -66,7 +74,10 @@ export function coreLocations() {
6674
feature.properties.id = locationSetID;
6775
_resolvedFeatures[locationSetID] = feature; // insert into cache
6876
}
69-
} catch (err) { /* ignore? */ }
77+
} catch (err) {
78+
obj.locationSet = { include: ['Q2'] }; // default worldwide
79+
obj.locationSetID = '+[Q2]';
80+
}
7081
}
7182

7283
function rebuildIndex() {
@@ -119,12 +130,28 @@ export function coreLocations() {
119130

120131
//
121132
// `mergeLocationSets`
122-
// Accepts an Array of locationSets to merge into the index
133+
// Accepts an Array of Objects containing `locationSet` properties.
134+
// The locationSets will be resolved and indexed in the background.
135+
// [
136+
// { id: 'preset1', locationSet: {…} },
137+
// { id: 'preset2', locationSet: {…} },
138+
// { id: 'preset3', locationSet: {…} },
139+
// …
140+
// ]
141+
// After resolving and indexing, the Objects will be decorated with a
142+
// `locationSetID` property.
143+
// [
144+
// { id: 'preset1', locationSet: {…}, locationSetID: '+[Q2]' },
145+
// { id: 'preset2', locationSet: {…}, locationSetID: '+[Q30]' },
146+
// { id: 'preset3', locationSet: {…}, locationSetID: '+[Q2]' },
147+
// …
148+
// ]
149+
//
123150
// Returns a Promise fullfilled when the resolving/indexing has been completed
124151
// This will take some seconds but happen in the background during browser idle time
125152
//
126-
_this.mergeLocationSets = (locationSets) => {
127-
if (!Array.isArray(locationSets)) return Promise.reject('nothing to do');
153+
_this.mergeLocationSets = (objects) => {
154+
if (!Array.isArray(objects)) return Promise.reject('nothing to do');
128155

129156
// Resolve all locationSets -> geojson, processing data in chunks
130157
//
@@ -136,7 +163,7 @@ export function coreLocations() {
136163
// Some discussion and performance results on these tickets:
137164
// https://github.com/ideditor/location-conflation/issues/26
138165
// https://github.com/osmlab/name-suggestion-index/issues/4784#issuecomment-742003434
139-
_queue = _queue.concat(utilArrayChunk(locationSets, 200));
166+
_queue = _queue.concat(utilArrayChunk(objects, 200));
140167

141168
// Everything after here will be deferred.
142169
if (!_inProcess) {

modules/presets/index.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ export function presetIndex() {
8888
// featureCollection: {}
8989
//}
9090
_this.merge = (d) => {
91-
let newFields = [];
92-
let newPresets = [];
9391
let newLocationSets = [];
9492

9593
// Merge Fields
@@ -100,8 +98,7 @@ export function presetIndex() {
10098
if (f) { // add or replace
10199
f = presetField(fieldID, f);
102100
if (f.locationSet) {
103-
newFields.push(f);
104-
newLocationSets.push(f.locationSet);
101+
newLocationSets.push(f);
105102
} else {
106103
f.locationSet = { include: ['Q2'] }; // default worldwide
107104
f.locationSetID = '+[Q2]';
@@ -123,8 +120,7 @@ export function presetIndex() {
123120
const isAddable = !_addablePresetIDs || _addablePresetIDs.has(presetID);
124121
p = presetPreset(presetID, p, isAddable, _fields, _presets);
125122
if (p.locationSet) {
126-
newPresets.push(p);
127-
newLocationSets.push(p.locationSet);
123+
newLocationSets.push(p);
128124
} else {
129125
p.locationSet = { include: ['Q2'] }; // default worldwide
130126
p.locationSetID = '+[Q2]';
@@ -195,13 +191,8 @@ export function presetIndex() {
195191
}
196192

197193
// Resolve all locationSet features.
198-
// When done, assign the locationSetIDs (we use these to quickly test where the preset/field is valid).
199194
if (newLocationSets.length) {
200-
locationManager.mergeLocationSets(newLocationSets)
201-
.then(() => {
202-
newFields.forEach(f => f.locationSetID = locationManager.locationSetID(f.locationSet));
203-
newPresets.forEach(p => p.locationSetID = locationManager.locationSetID(p.locationSet));
204-
});
195+
locationManager.mergeLocationSets(newLocationSets);
205196
}
206197

207198
return _this;

modules/ui/success.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ export function uiSuccess(context) {
3434
locationManager.mergeCustomGeoJSON(vals[0]);
3535
}
3636

37-
const ociResources = Object.values(vals[1].resources);
38-
const locationSets = ociResources.map(r => r.locationSet);
39-
40-
// Resolve all locationSet features.
41-
// When done, assign the locationSetIDs (we use these to quickly test where each community is valid).
42-
if (locationSets.length) {
43-
return locationManager.mergeLocationSets(locationSets)
37+
let ociResources = Object.values(vals[1].resources);
38+
if (ociResources.length) {
39+
// Resolve all locationSet features.
40+
return locationManager.mergeLocationSets(ociResources)
4441
.then(() => {
45-
ociResources.forEach(r => r.locationSetID = locationManager.locationSetID(r.locationSet));
4642
_oci = { resources: ociResources };
4743
return _oci;
4844
});

0 commit comments

Comments
 (0)