15 lines
543 B
JavaScript
15 lines
543 B
JavaScript
export class RequestCoordinator {
|
|
#versions = new Map()
|
|
async run(channel, ownerId, operation) {
|
|
const version = (this.#versions.get(channel) || 0) + 1
|
|
this.#versions.set(channel, version)
|
|
try {
|
|
const value = await operation()
|
|
return { accepted: this.#versions.get(channel) === version, ownerId, value }
|
|
} catch (error) {
|
|
return { accepted: this.#versions.get(channel) === version, ownerId, error }
|
|
}
|
|
}
|
|
invalidate(channel) { this.#versions.set(channel, (this.#versions.get(channel) || 0) + 1) }
|
|
}
|