UNPKG

rlayers

Version:

React Components for OpenLayers

79 lines 2.84 kB
import { Tile as LayerTile } from 'ol/layer'; import { default as RLayerRaster } from './RLayerRaster'; import debug from '../debug'; /** * A layer from a remote MBTiles raster source * * WARNING: Using this module requires manually installing `ol-mbtiles` * * `npm install ol-mbtiles` * * Be sure to read its own documentation for tips on how to improve * its performance (COOP/COEP, WASM compression, preloading...) * * Requires an `RMap` context */ export default class RLayerRasterMBTiles extends RLayerRaster { constructor(props, context) { super(props, context); this.addon = import('ol-mbtiles'); this.ol = new LayerTile(); this.source = null; this.abort = null; this.createSource(); } createSource() { debug('createSource start', this); this.metadata = this.addon.then((mod) => { var _a, _b, _c, _d; return mod.importMBTiles({ url: this.props.url, sqlWorkers: (_a = this.props.workers) !== null && _a !== void 0 ? _a : 4, sqlCacheSize: (_b = this.props.sqlCacheSize) !== null && _b !== void 0 ? _b : 4096, maxSqlPageSize: (_c = this.props.maxSqlPageSize) !== null && _c !== void 0 ? _c : 4096, backendType: (_d = this.props.backend) !== null && _d !== void 0 ? _d : 'sync' }); }); const abort = new AbortController(); this.abort = abort; Promise.all([this.addon, this.metadata]).then(([addon, md]) => { if (abort.signal.aborted) { debug('createSource aborted', this); md.pool.then((p) => p.close()); return; } this.source = new addon.MBTilesRasterSource(md); this.eventSources = [this.ol, this.source]; this.ol.setSource(this.source); this.attachOldEventHandlers(this.source); if (this.props.onMetadataReady) this.props.onMetadataReady.call(this, md); return this.source; }); } destroySource() { debug('destroySource', this, this.abort); if (this.source) { this.source.dispose(); this.source = null; if (this.ol) this.ol.setSource(null); } if (this.abort) { this.abort.abort(); this.abort = null; } } componentWillUnmount() { super.componentWillUnmount(); this.destroySource(); } refresh(prevProps) { super.refresh(prevProps); if ((prevProps === null || prevProps === void 0 ? void 0 : prevProps.url) !== this.props.url) { this.destroySource(); this.createSource(); } } } //# sourceMappingURL=RLayerRasterMBTiles.js.map