Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 2x 2x 2x | import { BackgroundJobRunner } from './background-job-runner.js'
/**
* JobsManager handles job execution with caching and deduplication.
* Shared jobs are cached so multiple callers get the same result.
*/
export class JobsManager {
private backgroundJobRunner: BackgroundJobRunner
// Using `any` here because this map caches promises with different result types.
// The generic type T varies per job. Type safety is enforced at the API boundary
// via the generic runSharedJob<T>() and runSharedBackgroundJob<T>() methods.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private runningActions: Map<string, Promise<any>>
constructor() {
this.backgroundJobRunner = new BackgroundJobRunner()
this.runningActions = new Map()
}
/**
* Run a shared job that can be called by multiple consumers.
* If the job is already running, returns the existing promise.
*/
runSharedJob<T>(key: string, job: () => Promise<T>): Promise<T> {
let promise = this.runningActions.get(key)
if (promise == null) {
promise = job()
promise.finally(() => this.runningActions.delete(key))
this.runningActions.set(key, promise)
}
return promise
}
/**
* Run a single background job in a worker thread.
* Each call creates a new worker.
*/
runBackgroundJob<T>(key: string, resolvedPath: string, payload?: unknown): Promise<T> {
return this.backgroundJobRunner.run<T>(resolvedPath, payload)
}
/**
* Run a shared background job in a worker thread.
* If the job is already running, returns the existing promise.
*/
runSharedBackgroundJob<T>(key: string, resolvedPath: string, payload?: unknown): Promise<T> {
let promise = this.runningActions.get(key)
if (promise == null) {
promise = this.backgroundJobRunner.run<T>(resolvedPath, payload)
promise.finally(() => this.runningActions.delete(key))
this.runningActions.set(key, promise)
}
return promise
}
/**
* Run a shared debounced background job.
* Maximum number of times it can be called over time.
*/
runSharedDebouncedBackgroundJob(): Promise<unknown> {
throw new Error('runSharedDebouncedJob is not implemented.')
}
/**
* Run a shared throttled background job.
* Cannot be called again until a certain amount of time has passed.
*/
runSharedThrottledBackgroundJob(): Promise<unknown> {
throw new Error('runSharedThrottledJob is not implemented.')
}
}
// Export singleton instance
export const jobsManager = new JobsManager()
|