Skip to content

Commit

Permalink
Simplify state store interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Oct 12, 2023
1 parent 4a98294 commit fe85472
Showing 1 changed file with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEx } from '@xylabs/assert'
import { exists } from '@xylabs/exists'
import { AbstractDiviner } from '@xyo-network/abstract-diviner'
import { asArchivistInstance, withArchivistModule } from '@xyo-network/archivist-model'
import { asArchivistInstance, withArchivistInstance } from '@xyo-network/archivist-model'
import { ArchivistWrapper } from '@xyo-network/archivist-wrapper'
import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
import { isBoundWitness } from '@xyo-network/boundwitness-model'
Expand Down Expand Up @@ -157,13 +157,12 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
* external stores.
*/
protected async commitState(state: ImageThumbnailDivinerState) {
const stateStore = assertEx(this.config.stateStore?.archivist, `${moduleName}: No stateStore configured`)
const module = assertEx(await this.resolve(stateStore), `${moduleName}: Failed to resolve stateStore`)
await withArchivistModule(module, async (archivist) => {
const mod = ArchivistWrapper.wrap(archivist, this.account)
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 mod.insert([bw, payload])
await archivist.insert([bw, payload])
})
}

Expand Down Expand Up @@ -247,20 +246,17 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
// If we able to located the last state
if (hash) {
// Get last state
const stateStoreArchivist = assertEx(this.config.stateStore?.archivist, `${moduleName}: No stateStore archivist configured`)
return await withArchivistModule(
assertEx(await this.resolve(stateStoreArchivist), `${moduleName}: Failed to resolve stateStore archivist`),
async (mod) => {
const archivist = ArchivistWrapper.wrap(mod, this.account)
const payloads = await archivist.get([hash])
if (payloads.length > 0) {
const payload = payloads[0]
if (isModuleState(payload)) {
return payload.state as 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`)
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
}
},
)
}
})
}
return undefined
}
Expand Down

0 comments on commit fe85472

Please sign in to comment.