@@ -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 ) {
0 commit comments