From bb3219681cbc089326a776973f5b70200b7071d1 Mon Sep 17 00:00:00 2001 From: gchoqueux Date: Thu, 12 Sep 2019 11:05:53 +0200 Subject: [PATCH] refactor(core): Clean up DataSourceProvider. --- src/Provider/DataSourceProvider.js | 66 ++++++++++-------------------- src/Source/FileSource.js | 8 ++++ src/Source/Source.js | 4 ++ src/Source/WFSSource.js | 4 +- test/unit/source.js | 2 +- 5 files changed, 37 insertions(+), 47 deletions(-) diff --git a/src/Provider/DataSourceProvider.js b/src/Provider/DataSourceProvider.js index ab96dbf790..f3223d0dfb 100644 --- a/src/Provider/DataSourceProvider.js +++ b/src/Provider/DataSourceProvider.js @@ -2,7 +2,6 @@ import GeoJsonParser from 'Parser/GeoJsonParser'; import VectorTileParser from 'Parser/VectorTileParser'; import Fetcher from 'Provider/Fetcher'; import Cache from 'Core/Scheduler/Cache'; -import CancelledCommandException from 'Core/Scheduler/CancelledCommandException'; export const supportedFetchers = new Map([ ['image/x-bil;bits=32', Fetcher.textureFloat], @@ -23,16 +22,12 @@ function isValidData(data, extentDestination, validFn) { } } -const error = (err, url, source) => { - source.handlingError(err, url); +const error = (err, source) => { + source.handlingError(err); throw err; }; -function convertSourceData(data, extDest, layer) { - return layer.convert(data, extDest, layer); -} - -function parseSourceData(data, extSrc, extDest, layer) { +function parseSourceData(data, extDest, layer) { const source = layer.source; const parser = source.parser || supportedParsers.get(source.format) || (d => Promise.resolve(d)); @@ -51,26 +46,21 @@ function parseSourceData(data, extSrc, extDest, layer) { withAltitude: layer.isGeometryLayer, }; - data.coords = extSrc; - - return parser(data, options).then((parsedFile) => { - if (source.isFileSource) { - source.parsedData = parsedFile; - if (parsedFile.extent && parsedFile.extent.crs != 'EPSG:4978') { - source.extent = parsedFile.extent; - } - } - - return parsedFile; - }); + return parser(data, options).then(parsedFile => source.onParsedFile(parsedFile)); } -function fetchSourceData(url, layer) { +function fetchSourceData(extSrc, layer) { const source = layer.source; const fetcher = source.fetcher || supportedFetchers.get(source.format) || Fetcher.texture; + // If source, we must fetch and convert data + // URL of the resource you want to fetch + const url = source.urlFromExtent(extSrc); // Fetch data - return fetcher(url, source.networkOptions); + return fetcher(url, source.networkOptions).then((f) => { + f.coords = extSrc; + return f; + }); } export default { @@ -78,28 +68,12 @@ export default { const promises = []; const layer = command.layer; const source = layer.source; - const requester = command.requester; const extentsSource = command.extentsSource; const extentsDestination = command.extentsDestination || extentsSource; const parsedData = command.parsedData || []; - // TODO: Find best place to cancel Command - if (requester && - !requester.material) { - // request has been deleted - return Promise.reject(new CancelledCommandException(command)); - } - for (let i = 0, max = extentsSource.length; i < max; i++) { const extSource = extentsSource[i]; - const extDest = extentsDestination[i]; - - // If source, we must fetch and convert data - // URL of the resource you want to fetch - const url = source.urlFromExtent(extSource); - - // Already fetched and parsed data that can be used - const validedParsedData = isValidData(parsedData[i], extDest, layer.isValidData) || source.parsedData; // Tag to Cache data const tag = `${source.uid}-${extSource.toString('-')}`; @@ -109,18 +83,22 @@ export default { // If data isn't in cache if (!convertedSourceData) { + const extDest = extentsDestination[i]; + + // Already fetched and parsed data that can be used + const validedParsedData = isValidData(parsedData[i], extDest, layer.isValidData) || source.parsedData; if (validedParsedData) { // Convert - convertedSourceData = convertSourceData(validedParsedData, extDest, layer); + convertedSourceData = layer.convert(validedParsedData, extDest, layer); } else if (source.fetchedData) { // Parse and convert - convertedSourceData = parseSourceData(source.fetchedData, extSource, extDest, layer) - .then(parsedData => convertSourceData(parsedData, extDest, layer), err => error(err, url, source)); + convertedSourceData = parseSourceData(source.fetchedData, extDest, layer) + .then(parsedData => layer.convert(parsedData, extDest, layer), err => error(err, source)); } else { // Fetch, parse and convert - convertedSourceData = fetchSourceData(url, layer) - .then(fetchedData => parseSourceData(fetchedData, extSource, extDest, layer), err => error(err, url, source)) - .then(parsedData => convertSourceData(parsedData, extDest, layer), err => error(err, url, source)); + convertedSourceData = fetchSourceData(extSource, layer) + .then(fetchedData => parseSourceData(fetchedData, extDest, layer), err => error(err, source)) + .then(parsedData => layer.convert(parsedData, extDest, layer), err => error(err, source)); } // Put converted data in cache Cache.set(tag, convertedSourceData, Cache.POLICIES.TEXTURE); diff --git a/src/Source/FileSource.js b/src/Source/FileSource.js index 2f26f34752..396191d44f 100644 --- a/src/Source/FileSource.js +++ b/src/Source/FileSource.js @@ -136,6 +136,14 @@ class FileSource extends Source { return this.url; } + onParsedFile(parsedFile) { + this.parsedData = parsedFile; + if (parsedFile.extent && parsedFile.extent.crs != 'EPSG:4978') { + this.extent = parsedFile.extent; + } + return parsedFile; + } + extentInsideLimit(extent) { // Fix me => may be not const localExtent = this.extent.crs == extent.crs ? extent : extent.as(this.extent.crs, ext); diff --git a/src/Source/Source.js b/src/Source/Source.js index 0f53b31678..3cbbae9c58 100644 --- a/src/Source/Source.js +++ b/src/Source/Source.js @@ -138,6 +138,10 @@ class Source { } return true; } + + onParsedFile(parsedFile) { + return parsedFile; + } } export default Source; diff --git a/src/Source/WFSSource.js b/src/Source/WFSSource.js index 991e39d21a..f6f1ae4027 100644 --- a/src/Source/WFSSource.js +++ b/src/Source/WFSSource.js @@ -128,7 +128,7 @@ class WFSSource extends Source { } } - handlingError(err, url) { + handlingError(err) { if (err.response && err.response.status == 400) { return err.response.text().then((text) => { const getCapUrl = `${this.url}SERVICE=WFS&REQUEST=GetCapabilities&VERSION=${this.version}`; @@ -140,7 +140,7 @@ class WFSSource extends Source { throw err; }); } else { - console.error(`Source ${this.typeName}: error while trying to fetch/parse/convert WFS data. Url was ${url}.`, err); + console.error(`Source ${this.typeName}: error while trying to fetch/parse/convert WFS data.`, err); throw err; } } diff --git a/test/unit/source.js b/test/unit/source.js index de4cf4d5af..ee1615d8ef 100644 --- a/test/unit/source.js +++ b/test/unit/source.js @@ -85,7 +85,7 @@ describe('Sources', function () { // Mute console error console.error = () => {}; const source = new WFSSource(paramsWFS); - assert.throws(() => source.handlingError(new Error('error'), 'http://test')); + assert.throws(() => source.handlingError(new Error('error'))); console.error = bce; }); });