-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jolevesq
committed
Jan 13, 2025
1 parent
016f7e7
commit 4382915
Showing
9 changed files
with
246 additions
and
9 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
packages/geoview-core/src/core/workers/abstract-worker-pool.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { AbstractWorker } from './abstract-worker'; | ||
|
||
export abstract class AbstractWorkerPool<T> { | ||
protected workers: AbstractWorker<T>[] = []; | ||
|
||
protected busyWorkers = new Set<AbstractWorker<T>>(); | ||
|
||
protected WorkerClass: new () => AbstractWorker<T>; | ||
|
||
protected name: string; | ||
|
||
constructor(name: string, workerClass: new () => AbstractWorker<T>, numWorkers = navigator.hardwareConcurrency || 4) { | ||
this.name = name; | ||
this.WorkerClass = workerClass; | ||
this.initializeWorkers(numWorkers); | ||
} | ||
|
||
protected initializeWorkers(numWorkers: number): void { | ||
for (let i = 0; i < numWorkers; i++) { | ||
const worker = new this.WorkerClass(); | ||
this.workers.push(worker); | ||
} | ||
} | ||
|
||
public abstract init(): Promise<void>; | ||
|
||
public abstract process(params: unknown): Promise<unknown>; | ||
|
||
public terminate(): void { | ||
this.workers.forEach((worker) => worker.terminate()); | ||
this.workers = []; | ||
this.busyWorkers.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/geoview-core/src/core/workers/fetch-esri-worker-pool.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { AbstractWorkerPool } from './abstract-worker-pool'; | ||
import { FetchEsriWorker, FetchEsriWorkerType } from './fetch-esri-worker'; | ||
import { QueryParams } from './fetch-esri-worker-script'; | ||
import { createWorkerLogger } from './helper/logger-worker'; | ||
|
||
import { TypeJsonObject } from '@/api/config/types/config-types'; | ||
|
||
export class FetchEsriWorkerPool extends AbstractWorkerPool<FetchEsriWorkerType> { | ||
#logger = createWorkerLogger('FetchEsriWorkerPool'); | ||
|
||
constructor(numWorkers = navigator.hardwareConcurrency || 4) { | ||
super('FetchEsriWorkerPool', FetchEsriWorker, numWorkers); | ||
this.#logger.logInfo('Worker pool created', `Number of workers: ${numWorkers}`); | ||
} | ||
|
||
public async init(): Promise<void> { | ||
try { | ||
this.#logger.logTrace('Initializing worker pool'); | ||
await Promise.all(this.workers.map((worker) => worker.init())); | ||
this.#logger.logTrace('Worker pool initialized'); | ||
} catch (error) { | ||
this.#logger.logError('Worker pool initialization failed', error); | ||
throw error; | ||
} | ||
} | ||
|
||
public async process(params: QueryParams): Promise<TypeJsonObject> { | ||
const availableWorker = this.workers.find((w) => !this.busyWorkers.has(w)); | ||
if (!availableWorker) { | ||
throw new Error('No available workers'); | ||
} | ||
|
||
const result = await availableWorker.process(params); | ||
return result as TypeJsonObject; | ||
} | ||
|
||
// /** | ||
// * Process an ESRI query and transform features using a worker from the pool | ||
// */ | ||
// public async processQuery(params: QueryParams): Promise<TypeJsonObject> { | ||
// try { | ||
// this.#logger.logTrace('Starting query process', params.url); | ||
// const result = await this.process(params); | ||
// this.#logger.logTrace('Query process completed'); | ||
// return result as TypeJsonObject; | ||
// } catch (error) { | ||
// this.#logger.logError('Query process failed', error); | ||
// throw error; | ||
// } | ||
// } | ||
} |
69 changes: 69 additions & 0 deletions
69
packages/geoview-core/src/core/workers/fetch-esri-worker-script.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { expose } from 'comlink'; | ||
|
||
import { createWorkerLogger } from './helper/logger-worker'; | ||
|
||
import { TypeJsonObject } from '@/api/config/types/config-types'; | ||
import { TypeStyleGeometry } from '@/api/config/types/map-schema-types'; | ||
|
||
export interface QueryParams { | ||
url: string; | ||
geometryType: TypeStyleGeometry; | ||
objectIds: number[]; | ||
queryGeometry: boolean; | ||
projection: number; | ||
maxAllowableOffset: number; | ||
} | ||
|
||
const logger = createWorkerLogger('FetchEsriWorker'); | ||
|
||
// Move the ESRI query function directly into the worker to avoid circular dependencies | ||
async function queryEsriFeatures(params: QueryParams): Promise<TypeJsonObject> { | ||
const response = await fetch(`${params.url}/query`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
body: new URLSearchParams({ | ||
f: 'json', | ||
geometryType: params.geometryType, | ||
objectIds: params.objectIds.join(','), | ||
outFields: '*', | ||
returnGeometry: params.queryGeometry.toString(), | ||
outSR: params.projection.toString(), | ||
maxAllowableOffset: params.maxAllowableOffset.toString(), | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} | ||
|
||
const worker = { | ||
// eslint-disable-next-line require-await | ||
async init(): Promise<void> { | ||
try { | ||
logger.logTrace('init worker', 'FetchEsriWorker initialized'); | ||
} catch { | ||
logger.logError('init worker', 'FetchEsriWorker failed to initialize'); | ||
} | ||
}, | ||
|
||
async process(params: QueryParams): Promise<TypeJsonObject> { | ||
try { | ||
logger.logTrace('process worker - Starting query processing', params.url); | ||
const response = await queryEsriFeatures(params); | ||
logger.logDebug('process worker - Query completed'); | ||
return response; | ||
} catch (error) { | ||
logger.logError('process worker - Query processing failed', error); | ||
throw error; | ||
} | ||
}, | ||
}; | ||
|
||
// Expose the worker methods to be accessible from the main thread | ||
expose(worker); | ||
export default {} as typeof Worker & { new (): Worker }; |
42 changes: 42 additions & 0 deletions
42
packages/geoview-core/src/core/workers/fetch-esri-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { AbstractWorker } from './abstract-worker'; | ||
import { QueryParams } from './fetch-esri-worker-script'; | ||
import { TypeJsonObject } from '@/api/config/types/config-types'; | ||
|
||
export interface FetchEsriWorkerType { | ||
/** | ||
* Initializes the worker - empty for now. | ||
*/ | ||
init: () => Promise<void>; | ||
|
||
/** | ||
* Processes an ESRI query JSON export. | ||
* @param {QueryParams} queryParams - The query parameters for the fetch. | ||
* @returns {TypeJsonObject} A promise that resolves to the response fetch as JSON string. | ||
*/ | ||
process: (queryParams: QueryParams) => Promise<TypeJsonObject>; | ||
} | ||
|
||
export class FetchEsriWorker extends AbstractWorker<FetchEsriWorkerType> { | ||
constructor() { | ||
super('FetchEsriWorker', new Worker(new URL('./fetch-esri-worker-script.ts', import.meta.url))); | ||
} | ||
|
||
/** | ||
* Initializes the worker - empty for now. | ||
* @returns A promise that resolves when initialization is complete. | ||
*/ | ||
public async init(): Promise<void> { | ||
const result = await this.proxy.init(); | ||
return result; | ||
} | ||
|
||
/** | ||
* Processes a JSON fetch for an esri query. | ||
* @param {QueryParams} queryParams - The query parameters for the fetch. | ||
* @returns A promise that resolves to the processed JSON string. | ||
*/ | ||
public async process(queryParams: QueryParams): Promise<TypeJsonObject> { | ||
const result = await this.proxy.process(queryParams); | ||
return result; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/geoview-core/src/core/workers/fetch-worker-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// import { TypeStyleGeometry } from '@/api/config/types/map-schema-types'; | ||
|
||
// export interface QueryParams { | ||
// url: string; | ||
// geometryType: TypeStyleGeometry; | ||
// objectIds: number[]; | ||
// queryGeometry: boolean; | ||
// projection: number; | ||
// maxAllowableOffset: number; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters