@@ -15,7 +15,6 @@ import { TilePromise, WorkerPool } from './worker-pool';
1515import type { ColorScales , DimensionRange , Domain , TileIndex , TileJSON , Variable } from './types' ;
1616
1717interface OmProtocolInstance {
18- useSAB : boolean ;
1918 colorScales : ColorScales ;
2019 domainOptions : Domain [ ] ;
2120 variableOptions : Variable [ ] ;
@@ -77,26 +76,35 @@ export interface Data {
7776
7877setupGlobalCache ( ) ;
7978const workerPool = new WorkerPool ( ) ;
80- // FIXME: WeakMap is very problematic if users pass settings via new objects instead of reusing existing ones
81- const protocolInstances = new WeakMap < OmProtocolSettings , OmProtocolInstance > ( ) ;
79+
80+ // THIS is shared global state. The protocol can be added only once with different settings!
81+ let omProtocolInstance : OmProtocolInstance | undefined = undefined ;
8282
8383const getProtocolInstance = ( settings : OmProtocolSettings ) : OmProtocolInstance => {
84- let inst = protocolInstances . get ( settings ) ;
85- if ( inst ) return inst ;
84+ if ( omProtocolInstance ) return omProtocolInstance ;
8685
87- inst = {
88- useSAB : settings . useSAB ,
86+ const inst = {
8987 colorScales : settings . colorScales ,
9088 domainOptions : settings . domainOptions ,
9189 variableOptions : settings . variableOptions ,
9290 resolutionFactor : settings . resolutionFactor ,
9391 omFileReader : new OMapsFileReader ( { useSAB : settings . useSAB } ) ,
9492 stateByKey : new Map ( )
9593 } ;
96- protocolInstances . set ( settings , inst ) ;
94+ omProtocolInstance = inst ;
9795 return inst ;
9896} ;
9997
98+ /// needs to be called before setUrl using the old source url
99+ export const clearOmUrlData = ( url : string ) => {
100+ if ( ! omProtocolInstance ) return ;
101+ const key = getStateKeyFromUrl ( url ) ;
102+ const state = omProtocolInstance . stateByKey . get ( key ) ;
103+ if ( ! state ) return ;
104+ state . data = null ;
105+ state . dataPromise = null ;
106+ } ;
107+
100108const getStateKeyFromUrl = ( url : string ) : string => {
101109 const match = url . match ( URL_REGEX ) ;
102110 // FIXME: removing arrows=true avoids duplicate decoding for raster and vector layers (for windspeeds)
@@ -144,7 +152,6 @@ const getOrCreateUrlState = (
144152} ;
145153
146154const ensureData = async (
147- url : string ,
148155 omUrl : string ,
149156 protocol : OmProtocolInstance ,
150157 state : OmUrlState ,
@@ -176,8 +183,17 @@ export const getValueFromLatLong = (
176183 lat : number ,
177184 lon : number ,
178185 variable : Variable ,
179- state : OmUrlState
186+ omUrl : string
180187) : { value : number ; direction ?: number } => {
188+ const key = getStateKeyFromUrl ( omUrl ) ;
189+ if ( ! omProtocolInstance ) {
190+ throw new Error ( 'OmProtocolInstance is not initialized' ) ;
191+ }
192+ const state = omProtocolInstance . stateByKey . get ( key ) ;
193+ if ( ! state ) {
194+ throw new Error ( 'State not found' ) ;
195+ }
196+
181197 if ( ! state . data ?. values ) {
182198 return { value : NaN } ;
183199 }
@@ -241,7 +257,7 @@ const renderTile = async (
241257 const x = parseInt ( result [ 3 ] ) ;
242258 const y = parseInt ( result [ 4 ] ) ;
243259
244- const data = await ensureData ( url , omUrl , protocol , state , settings ) ;
260+ const data = await ensureData ( omUrl , protocol , state , settings ) ;
245261 return getTile ( { z, x, y } , protocol , state , data , omUrl , type ) ;
246262} ;
247263
0 commit comments