Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow replacing fetch tile and decode image for local Dem manager #359

Merged
merged 11 commits into from
Dec 20, 2024
11 changes: 5 additions & 6 deletions src/dem-source.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { DemManager } from "./dem-manager";
import { LocalDemManager } from "./dem-manager";
import { LocalDemManager } from "./local-dem-manager";
import { decodeOptions, encodeOptions, getOptionsForZoom } from "./utils";
import RemoteDemManager from "./remote-dem-manager";
import type { DemTile, GlobalContourTileOptions, Timing } from "./types";
import type { DemManager, DemTile, GlobalContourTileOptions, Timing } from "./types";
import type WorkerDispatch from "./worker-dispatch";
import Actor from "./actor";
import { Timer } from "./performance";
Expand Down Expand Up @@ -129,14 +128,14 @@ export class DemSource {
this.sharedDemProtocolUrl = `${this.sharedDemProtocolId}://{z}/{x}/{y}`;
this.contourProtocolUrlBase = `${this.contourProtocolId}://{z}/{x}/{y}`;
const ManagerClass = worker ? RemoteDemManager : LocalDemManager;
this.manager = new ManagerClass(
url,
this.manager = new ManagerClass({
demUrlPattern: url,
cacheSize,
encoding,
maxzoom,
timeoutMs,
actor,
);
});
}

/** Registers a callback to be invoked with a performance report after each tile is requested. */
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import generateIsolines from "./isolines";
import { DemSource } from "./dem-source";
import { decodeParsedImage } from "./decode-image";
import { LocalDemManager } from "./dem-manager";
import { LocalDemManager } from "./local-dem-manager";
import CONFIG from "./config";
import { HeightTile } from "./height-tile";

Expand Down
97 changes: 33 additions & 64 deletions src/dem-manager.ts → src/local-dem-manager.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
import AsyncCache from "./cache";
import decodeImage from "./decode-image";
import defaultDecodeImage from "./decode-image";
import { HeightTile } from "./height-tile";
import generateIsolines from "./isolines";
import { encodeIndividualOptions, isAborted, withTimeout } from "./utils";
import type {
ContourTile,
DecodeImageFunction,
DemManager,
DemManagerInitizlizationParameters,
DemTile,
Encoding,
FetchResponse,
GetTileFunction,
IndividualContourTileOptions,
} from "./types";
import encodeVectorTile, { GeomType } from "./vtpbf";
import { Timer } from "./performance";

/**
* Holds cached tile state, and exposes `fetchContourTile` which fetches the necessary
* tiles and returns an encoded contour vector tiles.
*/
export interface DemManager {
loaded: Promise<any>;
fetchTile(
z: number,
x: number,
y: number,
abortController: AbortController,
timer?: Timer,
): Promise<FetchResponse>;
fetchAndParseTile(
z: number,
x: number,
y: number,
abortController: AbortController,
timer?: Timer,
): Promise<DemTile>;
fetchContourTile(
z: number,
x: number,
y: number,
options: IndividualContourTileOptions,
abortController: AbortController,
timer?: Timer,
): Promise<ContourTile>;
const defaultGetTile: GetTileFunction = async (url: string, abortController: AbortController) => {
const options: RequestInit = {
signal: abortController.signal,
};
return fetch(url, options).then(async (response) => {
HarelM marked this conversation as resolved.
Show resolved Hide resolved
if (!response.ok) {
throw new Error(`Bad response: ${response.status} for ${url}`);
}
return {
data: await response.blob(),
expires: response.headers.get("expires") || undefined,
cacheControl: response.headers.get("cache-control") || undefined,
};
});
}

/**
Expand All @@ -55,26 +45,19 @@ export class LocalDemManager implements DemManager {
maxzoom: number;
timeoutMs: number;
loaded = Promise.resolve();
decodeImage: (
blob: Blob,
encoding: Encoding,
abortController: AbortController,
) => Promise<DemTile> = decodeImage;

constructor(
demUrlPattern: string,
cacheSize: number,
encoding: Encoding,
maxzoom: number,
timeoutMs: number,
) {
this.tileCache = new AsyncCache(cacheSize);
this.parsedCache = new AsyncCache(cacheSize);
this.contourCache = new AsyncCache(cacheSize);
this.timeoutMs = timeoutMs;
this.demUrlPattern = demUrlPattern;
this.encoding = encoding;
this.maxzoom = maxzoom;
decodeImage: DecodeImageFunction;
getTile: GetTileFunction;

constructor(options: DemManagerInitizlizationParameters) {
HarelM marked this conversation as resolved.
Show resolved Hide resolved
this.tileCache = new AsyncCache(options.cacheSize);
this.parsedCache = new AsyncCache(options.cacheSize);
this.contourCache = new AsyncCache(options.cacheSize);
this.timeoutMs = options.timeoutMs;
this.demUrlPattern = options.demUrlPattern;
this.encoding = options.encoding;
this.maxzoom = options.maxzoom;
this.decodeImage = options.decodeImage || defaultDecodeImage;
this.getTile = options.getTile || defaultGetTile;
}

fetchTile(
Expand All @@ -92,24 +75,10 @@ export class LocalDemManager implements DemManager {
return this.tileCache.get(
url,
(_, childAbortController) => {
const options: RequestInit = {
signal: childAbortController.signal,
};
timer?.fetchTile(url);
const mark = timer?.marker("fetch");
msbarry marked this conversation as resolved.
Show resolved Hide resolved
return withTimeout(
this.timeoutMs,
fetch(url, options).then(async (response) => {
mark?.();
if (!response.ok) {
throw new Error(`Bad response: ${response.status} for ${url}`);
}
return {
data: await response.blob(),
expires: response.headers.get("expires") || undefined,
cacheControl: response.headers.get("cache-control") || undefined,
};
}),
this.getTile(url, childAbortController),
childAbortController,
);
},
Expand Down
20 changes: 5 additions & 15 deletions src/remote-dem-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Actor from "./actor";
import CONFIG from "./config";
import type WorkerDispatch from "./worker-dispatch";
import decodeImage from "./decode-image";
import type { DemManager } from "./dem-manager";
import { Timer } from "./performance";
import type {
ContourTile,
DemManager,
DemManagerInitizlizationParameters,
DemTile,
Encoding,
FetchResponse,
Expand Down Expand Up @@ -41,28 +42,17 @@ export default class RemoteDemManager implements DemManager {
actor: Actor<WorkerDispatch>;
loaded: Promise<any>;

constructor(
demUrlPattern: string,
cacheSize: number,
encoding: Encoding,
maxzoom: number,
timeoutMs: number,
actor?: Actor<WorkerDispatch>,
) {
constructor(options: DemManagerInitizlizationParameters) {
const managerId = (this.managerId = ++id);
this.actor = actor || defaultActor();
this.actor = options.actor || defaultActor();
this.loaded = this.actor.send(
"init",
[],
new AbortController(),
undefined,
{
cacheSize,
demUrlPattern,
encoding,
maxzoom,
...options,
managerId,
timeoutMs,
},
);
}
Expand Down
64 changes: 56 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type Actor from "./actor";
import type { Timer } from "./performance";
import type WorkerDispatch from "./worker-dispatch";

/** Scheme used to map pixel rgb values elevations. */
export type Encoding = "terrarium" | "mapbox";
export interface IsTransferrable {
Expand Down Expand Up @@ -77,14 +81,6 @@ export interface Image {
data: Uint8Array;
}

export interface InitMessage {
managerId: number;
demUrlPattern: string;
cacheSize: number;
encoding: Encoding;
maxzoom: number;
timeoutMs: number;
}

export type TimingCategory = "main" | "worker" | "fetch" | "decode" | "isoline";

Expand Down Expand Up @@ -114,3 +110,55 @@ export interface Timing {
/** If the tile failed with an error */
error?: boolean;
}

/**
* Holds cached tile state, and exposes `fetchContourTile` which fetches the necessary
* tiles and returns an encoded contour vector tiles.
*/
export interface DemManager {
loaded: Promise<any>;
fetchTile(
z: number,
x: number,
y: number,
abortController: AbortController,
timer?: Timer,
): Promise<FetchResponse>;
fetchAndParseTile(
z: number,
x: number,
y: number,
abortController: AbortController,
timer?: Timer,
): Promise<DemTile>;
fetchContourTile(
z: number,
x: number,
y: number,
options: IndividualContourTileOptions,
abortController: AbortController,
timer?: Timer,
): Promise<ContourTile>;
}

export type GetTileFunction = (url: string, abortController: AbortController) => Promise<FetchResponse>;

export type DecodeImageFunction = (blob: Blob, encoding: Encoding, abortController: AbortController) => Promise<DemTile>;

export type DemManagerRequiredInitizlizationParameters = {
demUrlPattern: string;
cacheSize: number;
encoding: Encoding;
maxzoom: number;
timeoutMs: number;
}

export type DemManagerInitizlizationParameters = DemManagerRequiredInitizlizationParameters & {
decodeImage?: DecodeImageFunction;
getTile?: GetTileFunction;
actor?: Actor<WorkerDispatch>;
}

export type InitMessage = DemManagerRequiredInitizlizationParameters & {
managerId: number;
}
HarelM marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 2 additions & 8 deletions src/worker-dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LocalDemManager } from "./dem-manager";
import { LocalDemManager } from "./local-dem-manager";
import { Timer } from "./performance";
import type {
ContourTile,
Expand All @@ -20,13 +20,7 @@ export default class WorkerDispatch {
managers: { [id: number]: LocalDemManager } = {};

init = (message: InitMessage, _: AbortController): Promise<void> => {
this.managers[message.managerId] = new LocalDemManager(
message.demUrlPattern,
message.cacheSize,
message.encoding,
message.maxzoom,
message.timeoutMs,
);
this.managers[message.managerId] = new LocalDemManager(message);
return Promise.resolve();
};

Expand Down
Loading