Skip to content

Commit

Permalink
Use correct access to layer arrays in monkey patches
Browse files Browse the repository at this point in the history
Also, expect ts errors as the typing of ol is in this case wrong.
  • Loading branch information
dopenguin committed Jul 30, 2024
1 parent 1a34edf commit 27e33b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/monkeyCrossOrigin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ Map.prototype.addLayer = function (...parameters) {
// Change all layers to be crossOrigin safe
Map.prototype.getLayers
.call(this)
.array_ // Change layers with wrong crossOrigin
.getArray() // Change layers with wrong crossOrigin
.forEach((layer) => {
// @ts-expect-error | All layers here are instantiated layers including a source.
const source = layer.getSource()
source.crossOrigin = 'anonymous'
// @ts-expect-error | All layers here are instantiated layers including a source.
layer.setSource(source)
})
}
21 changes: 13 additions & 8 deletions packages/core/src/monkeyHeaderLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ const originalAddLayer = Map.prototype.addLayer
Map.prototype.addLayer = function (...parameters) {
// Add layer to map
originalAddLayer.call(this, ...parameters)
Map.prototype.getLayers.call(this).array_.forEach((layer) => {
const source = layer.getSource()
const headerRequired = source?.urls?.some((url) => headerRegex.test(url))
if (headerRequired && typeof source.setTileLoadFunction === 'function') {
source.setTileLoadFunction(customLoader)
layer.setSource(source)
}
})
Map.prototype.getLayers
.call(this)
.getArray()
.forEach((layer) => {
// @ts-expect-error | All layers here are instantiated layers including a source.
const source = layer.getSource()
const headerRequired = source?.urls?.some((url) => headerRegex.test(url))
if (headerRequired && typeof source.setTileLoadFunction === 'function') {
source.setTileLoadFunction(customLoader)
// @ts-expect-error | All layers here are instantiated layers including a source.
layer.setSource(source)
}
})
}

0 comments on commit 27e33b0

Please sign in to comment.