Skip to content

Commit

Permalink
Timeout and error management on getDownloadUrl fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ger-benjamin committed Feb 9, 2024
1 parent a5f9baa commit 367835d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
- Add utility functions.
- In `BaseCustomizer`, the printExtent can be now set and get/set are dedicated methods.
- `pdfA` (allow transparency) is now a spec.map optional param.
- spec.attributes are now partial and `datasources` attribute is removed.
- spec.attributes are now partial and `datasource` attribute is removed.
- CreateSpecOptions accepts now every format.
- Add a timeout and manage errors on the `getDownloadUrl` utils function.

## v0.2.2
- Add optional MVTEncoder
Expand Down
10 changes: 5 additions & 5 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ document.querySelector('#print').addEventListener('click', async () => {
dpi: 254,
layout: layout,
format: 'pdf',
customAttributes: {},
customAttributes: {datasource: []},
customizer: customizer,
});

Expand All @@ -54,10 +54,10 @@ document.querySelector('#print').addEventListener('click', async () => {
document.location = url;
return url;
},
(err) => {
console.log('result', 'error', err);
resultEl.innerHTML = 'Error';
return err;
(error) => {
console.log('result', 'error', error);
resultEl.innerHTML = error;
return error;
},
);
});
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export const Constants = {
/** "Standardized rendering pixel size" is defined as 0.28 mm, see http://www.opengeospatial.org/standards/wmts */
WMTS_PIXEL_SIZE: 0.28e-3,
/** Standard PPI */
POINTS_PER_INCH: 72,
/** Standard DPI */
DOTS_PER_INCH: 72,
/** According to the "international yard" definition 1 inch is defined as exactly 2.54 cm. */
METERS_PER_INCH: 0.0254,
};

export const CalculatedConstants = {
/** Default to PPI / METERS per Inch */
POINTS_PER_DISTANCE_UNIT: () => Constants.POINTS_PER_INCH / Constants.METERS_PER_INCH,
/** Default to DPI / METERS per Inch */
DPI_PER_DISTANCE_UNIT: () => Constants.DOTS_PER_INCH / Constants.METERS_PER_INCH,
};
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export interface MFPStatusResponse {
done: boolean;
downloadURL: string;
elapsedTime: number;
error?: string;
status: string;
waitingTime: number;
}
32 changes: 26 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import type {Extent} from 'ol/extent';
import {Constants, CalculatedConstants} from './constants';

/**
* @param mapPageSize The page size (width, height)
* @param mapPageSize The page size in pixels (width, height)
* @param center The coordinate of the extent's center.
* @param scale The scale to calculate the extent width.
* @returns an extent that fit the page size. Calculated with POINTS_PER_DISTANCE_UNIT (by default using meters)
* @returns an extent that fit the page size. Calculated with DPI_PER_DISTANCE_UNIT (by default using meters)
*/
export function getPrintExtent(mapPageSize: number[], center: number[], scale: number): Extent {
const [mapPageWidthMeters, mapPageHeightMeters] = mapPageSize.map(
(side) => ((side / CalculatedConstants.POINTS_PER_DISTANCE_UNIT()) * scale) / 2,
(side) => ((side / CalculatedConstants.DPI_PER_DISTANCE_UNIT()) * scale) / 2,
);
return [
center[0] - mapPageWidthMeters,
Expand Down Expand Up @@ -118,20 +118,40 @@ export async function requestReport(mfpBaseUrl: string, spec: MFPSpec): Promise<
return await report.json();
}

// FIXME: add timeout
// FIXME: handle errors
/**
* @param requestReport the name of the requested report
* @param response The initial print response.
* @param interval (s) the internal to poll the download url.
* @param timeout (s) A timeout for this operation.
* @returns a Promise with the download url once the document is printed or an error.
*/
export async function getDownloadUrl(
requestReport: string,
response: MFPReportResponse,
interval = 1000,
timeout = 30000,
): Promise<string> {
let totalDuration = 0 - interval;
return new Promise((resolve, reject) => {
const intervalId = setInterval(async () => {
const status = await getStatus(requestReport, response.ref);
let status: MFPStatusResponse | undefined;
try {
status = await getStatus(requestReport, response.ref);
if (status.error) {
throw new Error(status.error);
}
} catch (error) {
reject(error);
}
if (status.done) {
clearInterval(intervalId);
resolve(`${requestReport}/report/${response.ref}`);
}
totalDuration += interval;
if (totalDuration >= timeout) {
clearInterval(intervalId);
reject(new Error('Print duration exceeded'));
}
}, interval);
});
}

0 comments on commit 367835d

Please sign in to comment.