Skip to content

Commit dedf81d

Browse files
committed
single loader per store
1 parent 2494cd1 commit dedf81d

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

src/initialize-store.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import zarr from 'zarr-js'
22

33
import { getPyramidMetadata } from './utils'
44

5-
// return promises for all for consistency
6-
const wrapGet = (getFn) => {
7-
return (chunkIndices) =>
8-
new Promise((resolve, reject) => {
9-
getFn(chunkIndices, (err, out) => (err ? reject(err) : resolve(out)))
10-
})
5+
const createLoader = ({ callGet, variable }) => {
6+
return {
7+
load: ({ level, chunk }) => callGet(`${level}/${variable}`, chunk),
8+
}
119
}
1210

1311
const initializeStore = async (
@@ -18,13 +16,13 @@ const initializeStore = async (
1816
metadataCache = {}
1917
) => {
2018
let metadata
21-
let loaders
2219
let dimensions
2320
let shape
2421
let chunks
2522
let fill_value
2623
let dtype
2724
let levels, maxZoom, tileSize, crs
25+
let loader
2826
const coordinates = {}
2927
const cacheKey = `${source}-${version}`
3028

@@ -85,15 +83,9 @@ const initializeStore = async (
8583
})
8684
)
8785

88-
loaders = {}
89-
levels.forEach((level) => {
90-
const key = `${level}/${variable}`
91-
loaders[key] = (chunkIndices) => callGet(key, chunkIndices)
92-
})
93-
94-
coordinateKeys.forEach((key) => {
95-
const coordKey = `${levels[0]}/${key}`
96-
loaders[coordKey] = (chunkIndices) => callGet(coordKey, chunkIndices)
86+
loader = createLoader({
87+
callGet,
88+
variable,
9789
})
9890
} catch (e) {
9991
// Fallback to openGroup
@@ -130,9 +122,15 @@ const initializeStore = async (
130122
})
131123
)
132124

133-
loaders = {}
134-
Object.keys(rawLoaders).forEach((key) => {
135-
loaders[key] = wrapGet(rawLoaders[key])
125+
loader = createLoader({
126+
callGet: (key, chunkIndices) =>
127+
new Promise((resolve, reject) => {
128+
rawLoaders[key](chunkIndices, (err, out) => {
129+
if (err) return reject(err)
130+
resolve(out)
131+
})
132+
}),
133+
variable,
136134
})
137135
}
138136

@@ -191,16 +189,16 @@ const initializeStore = async (
191189
})
192190
)
193191

194-
loaders = {}
195-
levels.forEach((level) => {
196-
const key = `${level}/${variable}`
197-
const meta = level === 0 ? arrayMetadata : null
198-
loaders[key] = (chunkIndices) => callGet(key, chunkIndices, meta)
199-
})
192+
const getChunk = (key, chunkIndices) => {
193+
const meta = key.startsWith(`${levels[0]}/${variable}`)
194+
? arrayMetadata
195+
: null
196+
return callGet(key, chunkIndices, meta)
197+
}
200198

201-
coordinateKeys.forEach((key) => {
202-
const coordKey = `${levels[0]}/${key}`
203-
loaders[coordKey] = (chunkIndices) => callGet(coordKey, chunkIndices)
199+
loader = createLoader({
200+
callGet: getChunk,
201+
variable,
204202
})
205203
break
206204
default:
@@ -211,7 +209,7 @@ const initializeStore = async (
211209

212210
return {
213211
metadata,
214-
loaders,
212+
loader,
215213
dimensions,
216214
shape,
217215
chunks,

src/raster.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const Raster = (props) => {
6464
return () => {
6565
if (tiles.current) {
6666
tiles.current.active = {}
67-
tiles.current.loaders = {}
67+
tiles.current.loader = null
6868
}
6969
}
7070
}, [

src/tiles.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const createTiles = (regl, opts) => {
5151
metadataCache = {},
5252
}) {
5353
this.tiles = {}
54-
this.loaders = {}
5554
this.active = {}
5655
this.display = display
5756
this.clim = clim
@@ -112,6 +111,8 @@ export const createTiles = (regl, opts) => {
112111
}
113112
}
114113
})
114+
this.loader = null
115+
this.availableLevels = new Set()
115116
this.initialized = new Promise((resolve) => {
116117
const loadingID = this.setLoading('metadata')
117118
initializeStore(
@@ -123,7 +124,7 @@ export const createTiles = (regl, opts) => {
123124
).then(
124125
({
125126
metadata,
126-
loaders,
127+
loader,
127128
dimensions,
128129
shape,
129130
chunks,
@@ -181,11 +182,8 @@ export const createTiles = (regl, opts) => {
181182
this.ndim = this.dimensions.length
182183

183184
this.coordinates = coordinates
184-
185-
levels.forEach((z) => {
186-
const loader = loaders[z + '/' + variable]
187-
this.loaders[z] = loader
188-
})
185+
this.loader = loader
186+
this.availableLevels = new Set(levels)
189187

190188
resolve(true)
191189
this.clearLoading(loadingID)
@@ -329,12 +327,13 @@ export const createTiles = (regl, opts) => {
329327

330328
this._initializeTile = (key, level) => {
331329
if (!this.tiles[key]) {
332-
const loader = this.loaders[level]
333-
if (!loader) return
330+
if (!this.loader || !this.availableLevels.has(level)) return
334331
this._removeOldestTile()
332+
const loadChunk = (chunk) => this.loader.load({ level, chunk })
333+
335334
this.tiles[key] = new Tile({
336335
key,
337-
loader,
336+
loader: loadChunk,
338337
shape: this.shape,
339338
chunks: this.chunks,
340339
dimensions: this.dimensions,
@@ -388,7 +387,7 @@ export const createTiles = (regl, opts) => {
388387
Object.keys(this.active).map(
389388
(key) =>
390389
new Promise((resolve) => {
391-
if (this.loaders[level]) {
390+
if (this.loader && this.availableLevels.has(level)) {
392391
const tileIndex = keyToTile(key)
393392
const tile = this._initializeTile(key, level)
394393

0 commit comments

Comments
 (0)