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