Skip to content

Commit

Permalink
Simplify state store using helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Oct 12, 2023
1 parent fe85472 commit f10a9eb
Showing 1 changed file with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,10 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
* external stores.
*/
protected async commitState(state: ImageThumbnailDivinerState) {
const stateStore = assertEx(this.config.stateStore?.archivist, `${moduleName}: No stateStore.archivist configured`)
const mod = assertEx(await this.resolve(stateStore), `${moduleName}: Failed to resolve stateStore.archivist`)
await withArchivistInstance(mod, async (archivist) => {
const payload = new PayloadBuilder<ModuleState<ImageThumbnailDivinerState>>({ schema: ModuleStateSchema }).fields({ state }).build()
const [bw] = await new BoundWitnessBuilder().payloads([payload]).witness(this.account).build()
await archivist.insert([bw, payload])
})
const archivist = await this.getArchivistForStore('stateStore')
const payload = new PayloadBuilder<ModuleState<ImageThumbnailDivinerState>>({ schema: ModuleStateSchema }).fields({ state }).build()
const [bw] = await new BoundWitnessBuilder().payloads([payload]).witness(this.account).build()
await archivist.insert([bw, payload])
}

protected override async divineHandler(payloads: Payload[] = []): Promise<ImageThumbnailResult[]> {
Expand Down Expand Up @@ -246,17 +243,11 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
// If we able to located the last state
if (hash) {
// Get last state
const stateStore = assertEx(this.config.stateStore?.archivist, `${moduleName}: No stateStore.archivist configured`)
const mod = assertEx(await this.resolve(stateStore), `${moduleName}: Failed to resolve stateStore.archivist`)
return await withArchivistInstance(mod, async (archivist) => {
const payloads = await archivist.get([hash])
if (payloads.length > 0) {
const payload = payloads[0]
if (isModuleState(payload)) {
return payload.state as ImageThumbnailDivinerState
}
}
})
const archivist = await this.getArchivistForStore('stateStore')
const payload = (await archivist.get([hash])).find(isModuleState)
if (payload) {
return payload.state as ImageThumbnailDivinerState
}
}
return undefined
}
Expand Down

0 comments on commit f10a9eb

Please sign in to comment.