@xyo-network/diviner-stateful 3.5.2 → 3.6.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/neutral/Diviner.d.ts +4 -4
- package/dist/neutral/Diviner.d.ts.map +1 -1
- package/dist/neutral/DivinerMixin.d.ts +3 -4
- package/dist/neutral/DivinerMixin.d.ts.map +1 -1
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +25 -24
- package/src/Diviner.ts +5 -7
- package/src/DivinerMixin.ts +4 -5
|
@@ -3,7 +3,7 @@ import { AbstractDiviner } from '@xyo-network/diviner-abstract';
|
|
|
3
3
|
import type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model';
|
|
4
4
|
import { DivinerWrapper } from '@xyo-network/diviner-wrapper';
|
|
5
5
|
import type { ModuleState, StateDictionary } from '@xyo-network/module-model';
|
|
6
|
-
import type { Payload, Schema
|
|
6
|
+
import type { Payload, Schema } from '@xyo-network/payload-model';
|
|
7
7
|
import type { StatefulDivinerParams } from './Params.ts';
|
|
8
8
|
/**
|
|
9
9
|
* A Diviner that maintains state
|
|
@@ -14,7 +14,7 @@ export declare abstract class StatefulDiviner<TParams extends StatefulDivinerPar
|
|
|
14
14
|
/**
|
|
15
15
|
* The last state
|
|
16
16
|
*/
|
|
17
|
-
protected _lastState?:
|
|
17
|
+
protected _lastState?: ModuleState<TState>;
|
|
18
18
|
/**
|
|
19
19
|
* Commit the internal state of the Diviner process. This is similar
|
|
20
20
|
* to a transaction completion in a database and should only be called
|
|
@@ -22,7 +22,7 @@ export declare abstract class StatefulDiviner<TParams extends StatefulDivinerPar
|
|
|
22
22
|
* external stores.
|
|
23
23
|
* @param nextState The state to commit
|
|
24
24
|
*/
|
|
25
|
-
protected commitState(nextState:
|
|
25
|
+
protected commitState(nextState: ModuleState<TState>): Promise<void>;
|
|
26
26
|
/**
|
|
27
27
|
* Retrieves the archivist for the specified store
|
|
28
28
|
* @param store The store to retrieve the archivist for
|
|
@@ -99,6 +99,6 @@ export declare abstract class StatefulDiviner<TParams extends StatefulDivinerPar
|
|
|
99
99
|
* Retrieves the last state of the Diviner process. Used to recover state after
|
|
100
100
|
* preemptions, reboots, etc.
|
|
101
101
|
*/
|
|
102
|
-
protected retrieveState(): Promise<
|
|
102
|
+
protected retrieveState(): Promise<ModuleState<TState> | undefined>;
|
|
103
103
|
}
|
|
104
104
|
//# sourceMappingURL=Diviner.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAA;AAGjE,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAG/D,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAA;AACzF,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAG7E,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAA;AAGjE,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAG/D,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAA;AACzF,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAG7E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAGjE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AAIxD;;GAEG;AACH,8BAAsB,eAAe,CACnC,OAAO,SAAS,qBAAqB,GAAG,qBAAqB,EAC7D,GAAG,SAAS,OAAO,GAAG,OAAO,EAC7B,IAAI,SAAS,OAAO,GAAG,OAAO,EAC9B,UAAU,SAAS,sBAAsB,CAAC,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,sBAAsB,CAChH,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,EACnC,GAAG,EACH,IAAI,CACL,EACD,MAAM,SAAS,eAAe,GAAG,eAAe,CAChD,SAAQ,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,UAAU,CAAC;IACvD,gBAAyB,aAAa,EAAE,MAAM,EAAE,CAAwD;IACxG,gBAAyB,mBAAmB,EAAE,MAAM,CAA8B;IAElF;;OAEG;IACH,SAAS,CAAC,UAAU,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;IAE1C;;;;;;OAMG;cACa,WAAW,CAAC,SAAS,EAAE,WAAW,CAAC,MAAM,CAAC;IAS1D;;;;OAIG;cACa,yBAAyB;;;;;;;;;;;;;;;;;;;IAMzC;;;;OAIG;cACa,mCAAmC;;;;;;;;;;;;;;;;;;;IAMnD;;;;OAIG;cACa,8BAA8B;;;;;;;;;;;;;;;;;;;IAM9C;;;OAGG;cACa,aAAa,IAAI,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;CAwC1E"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { AnyConfigSchema, ModuleInstance, ModuleParams, ModuleState, StateDictionary } from '@xyo-network/module-model';
|
|
2
|
-
import type { WithMeta } from '@xyo-network/payload-model';
|
|
3
2
|
import type { StatefulDivinerConfig } from './Config.ts';
|
|
4
3
|
export type StatefulModuleParams = ModuleParams<AnyConfigSchema<StatefulDivinerConfig>>;
|
|
5
4
|
export type AnyModule<TParams extends StatefulModuleParams = StatefulModuleParams> = new (...args: any[]) => ModuleInstance<TParams>;
|
|
@@ -30,7 +29,7 @@ export declare const StatefulModuleMixin: <TParams extends StatefulModuleParams
|
|
|
30
29
|
ephemeralQueryAccountEnabled?: boolean;
|
|
31
30
|
moduleIdentifierTransformers?: import("@xyo-network/module-model").ModuleIdentifierTransformer[];
|
|
32
31
|
}, TModule extends AnyModule<TParams> = AnyModule<TParams>, TState extends StateDictionary = StateDictionary>(ModuleBase: TModule) => (abstract new (...args: any[]) => {
|
|
33
|
-
_lastState?:
|
|
32
|
+
_lastState?: ModuleState<TState>;
|
|
34
33
|
/**
|
|
35
34
|
* Commit the internal state of the Diviner process. This is similar
|
|
36
35
|
* to a transaction completion in a database and should only be called
|
|
@@ -38,7 +37,7 @@ export declare const StatefulModuleMixin: <TParams extends StatefulModuleParams
|
|
|
38
37
|
* external stores.
|
|
39
38
|
* @param nextState The state to commit
|
|
40
39
|
*/
|
|
41
|
-
commitState(nextState:
|
|
40
|
+
commitState(nextState: ModuleState<TState>): Promise<void>;
|
|
42
41
|
/**
|
|
43
42
|
* Retrieves the archivist for the specified store
|
|
44
43
|
* @param store The store to retrieve the archivist for
|
|
@@ -149,7 +148,7 @@ export declare const StatefulModuleMixin: <TParams extends StatefulModuleParams
|
|
|
149
148
|
* Retrieves the last state of the Diviner process. Used to recover state after
|
|
150
149
|
* preemptions, reboots, etc.
|
|
151
150
|
*/
|
|
152
|
-
retrieveState(): Promise<
|
|
151
|
+
retrieveState(): Promise<ModuleState<TState> | undefined>;
|
|
153
152
|
readonly pipeline?: import("@xyo-network/module-model").ModulePipeLine;
|
|
154
153
|
readonly status: import("@xyo-network/module-model").ModuleStatus;
|
|
155
154
|
address: import("@xylabs/hex").Address;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DivinerMixin.d.ts","sourceRoot":"","sources":["../../src/DivinerMixin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,YAAY,E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,8BAA8B,4BAA4B,wBAAwB,4BAA4B,8BAA8B,2BAA2B,iCAAiC,2BAA2B;;;;;;;;8CAAoT,2BAA2B;;;;;;;;;;;;;;;;;;;;;;IA5D1wC;;;;OAIG;;yBAwDkS,4BAA4B;;mCAAmG,4BAA4B;;uBAAmF,4BAA4B,wBAAwB,4BAA4B,8BAA8B,2BAA2B,iCAAiC,2BAA2B,8BAA8B,4BAA4B,wBAAwB,4BAA4B,8BAA8B,2BAA2B,iCAAiC,2BAA2B;;;;;;;;8CAAoT,2BAA2B;;;;;;;;;;;;;;;;;;;;;;IA/C1wC;;;OAGG;qBACoB,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;;;;;;;;;;sRArFrB,CAAC,6DAAwB,CAAC;4RAGiB,CAAC,6DAAwB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBAnB1E,CAAC,yBACtC,CAAC;2FAMI,CAAC,yBACV,CAAC;;;;;;;;;;;;YAuIA,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import type { DivinerConfig } from '@xyo-network/diviner-model'\nimport type { ModuleIdentifier } from '@xyo-network/module-model'\n\nimport { StatefulDivinerSchema } from './Schema.ts'\n\n/**\n * The schema for a Stateful Diviner config\n */\nexport const StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config` as const\n/**\n * The schema for a Stateful Diviner config\n */\nexport type StatefulDivinerConfigSchema = typeof StatefulDivinerConfigSchema\n\n/**\n * The config for a Stateful Diviner\n */\nexport type StatefulDivinerConfig = DivinerConfig<{\n schema: StatefulDivinerConfigSchema\n stateStore: {\n archivist: ModuleIdentifier\n boundWitnessDiviner: ModuleIdentifier\n payloadDiviner: ModuleIdentifier\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport type { Hash } from '@xylabs/hex'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport type { ModuleState, StateDictionary } from '@xyo-network/module-model'\nimport { isModuleState, ModuleStateSchema } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithMeta,\n} from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfigSchema } from './Config.ts'\nimport type { StatefulDivinerParams } from './Params.ts'\n\nconst moduleName = 'StatefulDiviner'\n\n/**\n * A Diviner that maintains state\n */\nexport abstract class StatefulDiviner<\n TParams extends StatefulDivinerParams = StatefulDivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, StatefulDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = StatefulDivinerConfigSchema\n\n /**\n * The last state\n */\n protected _lastState?: WithMeta<ModuleState<TState>>\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n protected async commitState(nextState: WithMeta<ModuleState<TState>>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStateStore()\n const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n protected async getArchivistForStateStore() {\n const name = assertEx(this.config?.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)\n return ArchivistWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n protected async getBoundWitnessDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n protected async getPayloadDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n protected async retrieveState(): Promise<WithMeta<ModuleState<TState>> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStateStore()\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const archivist = await this.getArchivistForStateStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as WithMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport type { Hash } from '@xylabs/hex'\nimport { asArchivistInstance } from '@xyo-network/archivist-model'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport type {\n AnyConfigSchema,\n ModuleInstance,\n ModuleParams,\n ModuleState,\n StateDictionary,\n} from '@xyo-network/module-model'\nimport {\n isModuleState,\n ModuleStateSchema,\n} from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type { WithMeta } from '@xyo-network/payload-model'\n\nimport type { StatefulDivinerConfig } from './Config.ts'\n\nexport type StatefulModuleParams = ModuleParams<AnyConfigSchema<StatefulDivinerConfig>>\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyModule<TParams extends StatefulModuleParams = StatefulModuleParams> = new (...args: any[]) => ModuleInstance<TParams>\n\nconst moduleName = 'StatefulModuleMixin'\n\n/**\n * @ignore Inherit from StatefulDiviner instead\n * @param ModuleBase\n * @returns\n */\nexport const StatefulModuleMixin = <\n TParams extends StatefulModuleParams = StatefulModuleParams,\n TModule extends AnyModule<TParams> = AnyModule<TParams>,\n TState extends StateDictionary = StateDictionary,\n>(\n ModuleBase: TModule,\n) => {\n abstract class StatefulModuleBase extends ModuleBase {\n _lastState?: WithMeta<ModuleState<TState>>\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n async commitState(nextState: WithMeta<ModuleState<TState>>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStore()\n // const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n const [bw] = await (await new BoundWitnessBuilder().payload(nextState)).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n async getArchivistForStore() {\n const name = assertEx(this.config?.stateStore?.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)\n // return ArchivistWrapper.wrap(mod, this.account)\n const instance = asArchivistInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap archivist instance`)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n async getBoundWitnessDivinerForStore() {\n const name = assertEx(\n this.config?.stateStore?.boundWitnessDiviner,\n () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`,\n )\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)\n // return DivinerWrapper.wrap(mod, this.account)\n const instance = asDivinerInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)\n }\n\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n async getPayloadDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)\n // return DivinerWrapper.wrap(mod, this.account)\n const instance = asDivinerInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n async retrieveState(): Promise<WithMeta<ModuleState<TState>> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStore()\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n // address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n // .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const archivist = await this.getArchivistForStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as WithMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";;;;AAAO,IAAMA,wBAAwB;;;ACQ9B,IAAMC,8BAA8B,GAAGC,qBAAAA;;;ACR9C,SAASC,gBAAgB;AAEzB,SAASC,wBAAwB;AACjC,SAASC,2BAA2B;AACpC,SAASC,sBAAsB;AAC/B,SAASC,uBAAuB;AAEhC,SAASC,sCAAsC;AAE/C,SAASC,sBAAsB;AAE/B,SAASC,eAAeC,yBAAyB;AACjD,SAASC,sBAAsB;AAQ/B,IAAMC,aAAa;AAKZ,IAAeC,kBAAf,cAUGC,gBAAAA;EAnCV,OAmCUA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeC;;EAC5E,OAAyBC,sBAA8BD;;;;EAK7CE;;;;;;;;EASV,MAAgBC,YAAYC,WAA0C;AAEpE,QAAIA,UAAUC,MAAMC,WAAW,KAAKJ,YAAYG,MAAMC,OAAQ;AAC9D,SAAKJ,aAAaE;AAClB,UAAMG,YAAY,MAAM,KAAKC,0BAAyB;AACtD,UAAM,CAACC,EAAAA,IAAM,MAAM,IAAIC,oBAAAA,EAAsBC,QAAQP,SAAAA,EAAWQ,OAAO,KAAKC,OAAO,EAAEC,MAAK;AAC1F,UAAMP,UAAUQ,OAAO;MAACN;MAAIL;KAAU;EACxC;;;;;;EAOA,MAAgBI,4BAA4B;AAC1C,UAAMQ,OAAOC,SAAS,KAAKC,QAAQC,WAAWZ,WAAW,MAAM,GAAGX,UAAAA,iDAA2D;AAC7H,UAAMwB,MAAMH,SAAS,MAAM,KAAKI,QAAQL,IAAAA,GAAO,MAAM,GAA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YAAED;YAASC;UAAM,EAAA,EAG1CC,OACC,CAACC,MAAMC,SAAUR,aAAaL,kBAAkBa,MAAMH,KAAAA,MAAWT,qBAAoBI,aAAaS,eAAeD,MAAMH,KAAAA,IAASE,MAChI,EAAA;QAEN;MACF;AAGA,UAAIrB,MAAM;AAER,cAAMrB,YAAY,MAAM,KAAKC,qBAAoB;AACjD,cAAMG,WAAW,MAAMJ,UAAU6C,IAAI;UAACxB;SAAK,GAAGyB,KAAKC,cAAAA;AACnD,YAAI3C,SAAS;AACX,iBAAOA;QACT;MACF;AACA,aAAO4C;IACT;EACF;AACA,SAAOtD;AACT,GAnHmC;","names":["StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","assertEx","ArchivistWrapper","BoundWitnessBuilder","isBoundWitness","AbstractDiviner","BoundWitnessDivinerQuerySchema","DivinerWrapper","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName","StatefulDiviner","AbstractDiviner","configSchemas","StatefulDivinerConfigSchema","defaultConfigSchema","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStateStore","bw","BoundWitnessBuilder","payload","signer","account","build","insert","name","assertEx","config","stateStore","mod","resolve","ArchivistWrapper","wrap","getBoundWitnessDivinerForStateStore","boundWitnessDiviner","DivinerWrapper","getPayloadDivinerForStateStore","payloadDiviner","retrieveState","hash","diviner","query","PayloadBuilder","schema","BoundWitnessDivinerQuerySchema","fields","address","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","divine","length","boundWitness","isBoundWitness","addresses","map","index","filter","reduce","prev","curr","payload_hashes","get","find","isModuleState","undefined","assertEx","asArchivistInstance","BoundWitnessBuilder","isBoundWitness","BoundWitnessDivinerQuerySchema","asDivinerInstance","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName","StatefulModuleMixin","ModuleBase","StatefulModuleBase","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStore","bw","BoundWitnessBuilder","payload","build","insert","name","assertEx","config","stateStore","mod","resolve","instance","asArchivistInstance","getBoundWitnessDivinerForStore","boundWitnessDiviner","asDivinerInstance","getPayloadDivinerForStateStore","payloadDiviner","retrieveState","hash","diviner","query","PayloadBuilder","schema","BoundWitnessDivinerQuerySchema","fields","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","divine","length","boundWitness","isBoundWitness","addresses","map","address","index","reduce","prev","curr","payload_hashes","get","find","isModuleState","undefined"]}
|
|
1
|
+
{"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import type { DivinerConfig } from '@xyo-network/diviner-model'\nimport type { ModuleIdentifier } from '@xyo-network/module-model'\n\nimport { StatefulDivinerSchema } from './Schema.ts'\n\n/**\n * The schema for a Stateful Diviner config\n */\nexport const StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config` as const\n/**\n * The schema for a Stateful Diviner config\n */\nexport type StatefulDivinerConfigSchema = typeof StatefulDivinerConfigSchema\n\n/**\n * The config for a Stateful Diviner\n */\nexport type StatefulDivinerConfig = DivinerConfig<{\n schema: StatefulDivinerConfigSchema\n stateStore: {\n archivist: ModuleIdentifier\n boundWitnessDiviner: ModuleIdentifier\n payloadDiviner: ModuleIdentifier\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport type { Hash } from '@xylabs/hex'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport type { ModuleState, StateDictionary } from '@xyo-network/module-model'\nimport { isModuleState, ModuleStateSchema } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type { Payload, Schema } from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfigSchema } from './Config.ts'\nimport type { StatefulDivinerParams } from './Params.ts'\n\nconst moduleName = 'StatefulDiviner'\n\n/**\n * A Diviner that maintains state\n */\nexport abstract class StatefulDiviner<\n TParams extends StatefulDivinerParams = StatefulDivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, StatefulDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = StatefulDivinerConfigSchema\n\n /**\n * The last state\n */\n protected _lastState?: ModuleState<TState>\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n protected async commitState(nextState: ModuleState<TState>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStateStore()\n const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n protected async getArchivistForStateStore() {\n const name = assertEx(this.config?.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)\n return ArchivistWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n protected async getBoundWitnessDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n protected async getPayloadDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n protected async retrieveState(): Promise<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStateStore()\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const archivist = await this.getArchivistForStateStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as ModuleState<TState>\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport type { Hash } from '@xylabs/hex'\nimport { asArchivistInstance } from '@xyo-network/archivist-model'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport type {\n AnyConfigSchema,\n ModuleInstance,\n ModuleParams,\n ModuleState,\n StateDictionary,\n} from '@xyo-network/module-model'\nimport {\n isModuleState,\n ModuleStateSchema,\n} from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\n\nimport type { StatefulDivinerConfig } from './Config.ts'\n\nexport type StatefulModuleParams = ModuleParams<AnyConfigSchema<StatefulDivinerConfig>>\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyModule<TParams extends StatefulModuleParams = StatefulModuleParams> = new (...args: any[]) => ModuleInstance<TParams>\n\nconst moduleName = 'StatefulModuleMixin'\n\n/**\n * @ignore Inherit from StatefulDiviner instead\n * @param ModuleBase\n * @returns\n */\nexport const StatefulModuleMixin = <\n TParams extends StatefulModuleParams = StatefulModuleParams,\n TModule extends AnyModule<TParams> = AnyModule<TParams>,\n TState extends StateDictionary = StateDictionary,\n>(\n ModuleBase: TModule,\n) => {\n abstract class StatefulModuleBase extends ModuleBase {\n _lastState?: ModuleState<TState>\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n async commitState(nextState: ModuleState<TState>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStore()\n // const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n const [bw] = await (await new BoundWitnessBuilder().payload(nextState)).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n async getArchivistForStore() {\n const name = assertEx(this.config?.stateStore?.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)\n // return ArchivistWrapper.wrap(mod, this.account)\n const instance = asArchivistInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap archivist instance`)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n async getBoundWitnessDivinerForStore() {\n const name = assertEx(\n this.config?.stateStore?.boundWitnessDiviner,\n () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`,\n )\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)\n // return DivinerWrapper.wrap(mod, this.account)\n const instance = asDivinerInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)\n }\n\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n async getPayloadDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)\n // return DivinerWrapper.wrap(mod, this.account)\n const instance = asDivinerInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n async retrieveState(): Promise<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStore()\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n // address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n // .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const archivist = await this.getArchivistForStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as ModuleState<TState>\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";;;;AAAO,IAAMA,wBAAwB;;;ACQ9B,IAAMC,8BAA8B,GAAGC,qBAAAA;;;ACR9C,SAASC,gBAAgB;AAEzB,SAASC,wBAAwB;AACjC,SAASC,2BAA2B;AACpC,SAASC,sBAAsB;AAC/B,SAASC,uBAAuB;AAEhC,SAASC,sCAAsC;AAE/C,SAASC,sBAAsB;AAE/B,SAASC,eAAeC,yBAAyB;AACjD,SAASC,sBAAsB;AAM/B,IAAMC,aAAa;AAKZ,IAAeC,kBAAf,cAUGC,gBAAAA;EAjCV,OAiCUA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeC;;EAC5E,OAAyBC,sBAA8BD;;;;EAK7CE;;;;;;;;EASV,MAAgBC,YAAYC,WAAgC;AAE1D,QAAIA,UAAUC,MAAM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AAY,QAAO,KAAKA;AACjC,UAAI0B,OAAa;AACjB,YAAMC,UAAU,MAAM,KAAKP,+BAA8B;AACzD,YAAMQ,QAAQ,MAAM,IAAIC,gBAAgD;QAAEC,QAAQC;MAA+B,CAAA,EAC9GC,OAAO;;QAENC,OAAO;QACP7B,QAAQ;QACR8B,OAAO;QACPC,iBAAiB;UAACC;;MACpB,CAAA,EACC1B,MAAK;AACR,YAAM2B,iBAAiB,MAAMV,QAAQW,OAAO;QAACV;OAAM;AACnD,UAAIS,eAAeE,SAAS,GAAG;AAC7B,cAAMC,eAAeH,eAAe,CAAA;AACpC,YAAII,gBAAeD,YAAAA,GAAe;AAEhCd,iBAAOc,aAAaE,UACjBC,IAAI,CAACC,SAASC,WAAW;YAAED;YAASC;UAAM,EAAA,EAG1CC,OACC,CAACC,MAAMC,SAAUR,aAAaL,kBAAkBa,MAAMH,KAAAA,MAAWT,qBAAoBI,aAAaS,eAAeD,MAAMH,KAAAA,IAASE,MAChI,EAAA;QAEN;MACF;AAGA,UAAIrB,MAAM;AAER,cAAMrB,YAAY,MAAM,KAAKC,qBAAoB;AACjD,cAAMG,WAAW,MAAMJ,UAAU6C,IAAI;UAACxB;SAAK,GAAGyB,KAAKC,cAAAA;AACnD,YAAI3C,SAAS;AACX,iBAAOA;QACT;MACF;AACA,aAAO4C;IACT;EACF;AACA,SAAOtD;AACT,GAnHmC;","names":["StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","assertEx","ArchivistWrapper","BoundWitnessBuilder","isBoundWitness","AbstractDiviner","BoundWitnessDivinerQuerySchema","DivinerWrapper","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName","StatefulDiviner","AbstractDiviner","configSchemas","StatefulDivinerConfigSchema","defaultConfigSchema","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStateStore","bw","BoundWitnessBuilder","payload","signer","account","build","insert","name","assertEx","config","stateStore","mod","resolve","ArchivistWrapper","wrap","getBoundWitnessDivinerForStateStore","boundWitnessDiviner","DivinerWrapper","getPayloadDivinerForStateStore","payloadDiviner","retrieveState","hash","diviner","query","PayloadBuilder","schema","BoundWitnessDivinerQuerySchema","fields","address","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","divine","length","boundWitness","isBoundWitness","addresses","map","index","filter","reduce","prev","curr","payload_hashes","get","find","isModuleState","undefined","assertEx","asArchivistInstance","BoundWitnessBuilder","isBoundWitness","BoundWitnessDivinerQuerySchema","asDivinerInstance","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName","StatefulModuleMixin","ModuleBase","StatefulModuleBase","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStore","bw","BoundWitnessBuilder","payload","build","insert","name","assertEx","config","stateStore","mod","resolve","instance","asArchivistInstance","getBoundWitnessDivinerForStore","boundWitnessDiviner","asDivinerInstance","getPayloadDivinerForStateStore","payloadDiviner","retrieveState","hash","diviner","query","PayloadBuilder","schema","BoundWitnessDivinerQuerySchema","fields","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","divine","length","boundWitness","isBoundWitness","addresses","map","address","index","reduce","prev","curr","payload_hashes","get","find","isModuleState","undefined"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/diviner-stateful",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0-rc.2",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -29,35 +29,36 @@
|
|
|
29
29
|
"module": "dist/neutral/index.mjs",
|
|
30
30
|
"types": "dist/neutral/index.d.ts",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xylabs/assert": "^4.4.
|
|
33
|
-
"@xylabs/hex": "^4.4.
|
|
34
|
-
"@xyo-network/archivist-model": "^3.
|
|
35
|
-
"@xyo-network/archivist-wrapper": "^3.
|
|
36
|
-
"@xyo-network/boundwitness-builder": "^3.
|
|
37
|
-
"@xyo-network/boundwitness-model": "^3.
|
|
38
|
-
"@xyo-network/diviner-abstract": "^3.
|
|
39
|
-
"@xyo-network/diviner-boundwitness-model": "^3.
|
|
40
|
-
"@xyo-network/diviner-model": "^3.
|
|
41
|
-
"@xyo-network/diviner-wrapper": "^3.
|
|
42
|
-
"@xyo-network/module-model": "^3.
|
|
43
|
-
"@xyo-network/payload-builder": "^3.
|
|
44
|
-
"@xyo-network/payload-model": "^3.
|
|
32
|
+
"@xylabs/assert": "^4.4.12",
|
|
33
|
+
"@xylabs/hex": "^4.4.12",
|
|
34
|
+
"@xyo-network/archivist-model": "^3.6.0-rc.2",
|
|
35
|
+
"@xyo-network/archivist-wrapper": "^3.6.0-rc.2",
|
|
36
|
+
"@xyo-network/boundwitness-builder": "^3.6.0-rc.2",
|
|
37
|
+
"@xyo-network/boundwitness-model": "^3.6.0-rc.2",
|
|
38
|
+
"@xyo-network/diviner-abstract": "^3.6.0-rc.2",
|
|
39
|
+
"@xyo-network/diviner-boundwitness-model": "^3.6.0-rc.2",
|
|
40
|
+
"@xyo-network/diviner-model": "^3.6.0-rc.2",
|
|
41
|
+
"@xyo-network/diviner-wrapper": "^3.6.0-rc.2",
|
|
42
|
+
"@xyo-network/module-model": "^3.6.0-rc.2",
|
|
43
|
+
"@xyo-network/payload-builder": "^3.6.0-rc.2",
|
|
44
|
+
"@xyo-network/payload-model": "^3.6.0-rc.2"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@xylabs/ts-scripts-yarn3": "^4.2.4",
|
|
48
48
|
"@xylabs/tsconfig": "^4.2.4",
|
|
49
|
-
"@xylabs/vitest-extended": "^4.4.
|
|
50
|
-
"@xyo-network/account": "^3.
|
|
51
|
-
"@xyo-network/archivist-memory": "^3.
|
|
52
|
-
"@xyo-network/diviner-boundwitness-memory": "^3.
|
|
53
|
-
"@xyo-network/diviner-payload-memory": "^3.
|
|
54
|
-
"@xyo-network/manifest": "^3.
|
|
55
|
-
"@xyo-network/module-factory-locator": "^3.
|
|
56
|
-
"@xyo-network/node-memory": "^3.
|
|
49
|
+
"@xylabs/vitest-extended": "^4.4.12",
|
|
50
|
+
"@xyo-network/account": "^3.6.0-rc.2",
|
|
51
|
+
"@xyo-network/archivist-memory": "^3.6.0-rc.2",
|
|
52
|
+
"@xyo-network/diviner-boundwitness-memory": "^3.6.0-rc.2",
|
|
53
|
+
"@xyo-network/diviner-payload-memory": "^3.6.0-rc.2",
|
|
54
|
+
"@xyo-network/manifest": "^3.6.0-rc.2",
|
|
55
|
+
"@xyo-network/module-factory-locator": "^3.6.0-rc.2",
|
|
56
|
+
"@xyo-network/node-memory": "^3.6.0-rc.2",
|
|
57
57
|
"typescript": "^5.7.2",
|
|
58
|
-
"vitest": "^2.1.
|
|
58
|
+
"vitest": "^2.1.8"
|
|
59
59
|
},
|
|
60
60
|
"publishConfig": {
|
|
61
61
|
"access": "public"
|
|
62
|
-
}
|
|
62
|
+
},
|
|
63
|
+
"stableVersion": "3.5.2"
|
|
63
64
|
}
|
package/src/Diviner.ts
CHANGED
|
@@ -11,9 +11,7 @@ import { DivinerWrapper } from '@xyo-network/diviner-wrapper'
|
|
|
11
11
|
import type { ModuleState, StateDictionary } from '@xyo-network/module-model'
|
|
12
12
|
import { isModuleState, ModuleStateSchema } from '@xyo-network/module-model'
|
|
13
13
|
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
14
|
-
import type {
|
|
15
|
-
Payload, Schema, WithMeta,
|
|
16
|
-
} from '@xyo-network/payload-model'
|
|
14
|
+
import type { Payload, Schema } from '@xyo-network/payload-model'
|
|
17
15
|
|
|
18
16
|
import { StatefulDivinerConfigSchema } from './Config.ts'
|
|
19
17
|
import type { StatefulDivinerParams } from './Params.ts'
|
|
@@ -40,7 +38,7 @@ export abstract class StatefulDiviner<
|
|
|
40
38
|
/**
|
|
41
39
|
* The last state
|
|
42
40
|
*/
|
|
43
|
-
protected _lastState?:
|
|
41
|
+
protected _lastState?: ModuleState<TState>
|
|
44
42
|
|
|
45
43
|
/**
|
|
46
44
|
* Commit the internal state of the Diviner process. This is similar
|
|
@@ -49,7 +47,7 @@ export abstract class StatefulDiviner<
|
|
|
49
47
|
* external stores.
|
|
50
48
|
* @param nextState The state to commit
|
|
51
49
|
*/
|
|
52
|
-
protected async commitState(nextState:
|
|
50
|
+
protected async commitState(nextState: ModuleState<TState>) {
|
|
53
51
|
// Don't commit state if no state has changed
|
|
54
52
|
if (nextState.state.offset === this._lastState?.state.offset) return
|
|
55
53
|
this._lastState = nextState
|
|
@@ -95,7 +93,7 @@ export abstract class StatefulDiviner<
|
|
|
95
93
|
* Retrieves the last state of the Diviner process. Used to recover state after
|
|
96
94
|
* preemptions, reboots, etc.
|
|
97
95
|
*/
|
|
98
|
-
protected async retrieveState(): Promise<
|
|
96
|
+
protected async retrieveState(): Promise<ModuleState<TState> | undefined> {
|
|
99
97
|
if (this._lastState) return this._lastState
|
|
100
98
|
let hash: Hash = ''
|
|
101
99
|
const diviner = await this.getBoundWitnessDivinerForStateStore()
|
|
@@ -130,7 +128,7 @@ export abstract class StatefulDiviner<
|
|
|
130
128
|
const archivist = await this.getArchivistForStateStore()
|
|
131
129
|
const payload = (await archivist.get([hash])).find(isModuleState<TState>)
|
|
132
130
|
if (payload) {
|
|
133
|
-
return payload as
|
|
131
|
+
return payload as ModuleState<TState>
|
|
134
132
|
}
|
|
135
133
|
}
|
|
136
134
|
return undefined
|
package/src/DivinerMixin.ts
CHANGED
|
@@ -18,7 +18,6 @@ import {
|
|
|
18
18
|
ModuleStateSchema,
|
|
19
19
|
} from '@xyo-network/module-model'
|
|
20
20
|
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
21
|
-
import type { WithMeta } from '@xyo-network/payload-model'
|
|
22
21
|
|
|
23
22
|
import type { StatefulDivinerConfig } from './Config.ts'
|
|
24
23
|
|
|
@@ -42,7 +41,7 @@ export const StatefulModuleMixin = <
|
|
|
42
41
|
ModuleBase: TModule,
|
|
43
42
|
) => {
|
|
44
43
|
abstract class StatefulModuleBase extends ModuleBase {
|
|
45
|
-
_lastState?:
|
|
44
|
+
_lastState?: ModuleState<TState>
|
|
46
45
|
|
|
47
46
|
/**
|
|
48
47
|
* Commit the internal state of the Diviner process. This is similar
|
|
@@ -51,7 +50,7 @@ export const StatefulModuleMixin = <
|
|
|
51
50
|
* external stores.
|
|
52
51
|
* @param nextState The state to commit
|
|
53
52
|
*/
|
|
54
|
-
async commitState(nextState:
|
|
53
|
+
async commitState(nextState: ModuleState<TState>) {
|
|
55
54
|
// Don't commit state if no state has changed
|
|
56
55
|
if (nextState.state.offset === this._lastState?.state.offset) return
|
|
57
56
|
this._lastState = nextState
|
|
@@ -107,7 +106,7 @@ export const StatefulModuleMixin = <
|
|
|
107
106
|
* Retrieves the last state of the Diviner process. Used to recover state after
|
|
108
107
|
* preemptions, reboots, etc.
|
|
109
108
|
*/
|
|
110
|
-
async retrieveState(): Promise<
|
|
109
|
+
async retrieveState(): Promise<ModuleState<TState> | undefined> {
|
|
111
110
|
if (this._lastState) return this._lastState
|
|
112
111
|
let hash: Hash = ''
|
|
113
112
|
const diviner = await this.getBoundWitnessDivinerForStore()
|
|
@@ -142,7 +141,7 @@ export const StatefulModuleMixin = <
|
|
|
142
141
|
const archivist = await this.getArchivistForStore()
|
|
143
142
|
const payload = (await archivist.get([hash])).find(isModuleState<TState>)
|
|
144
143
|
if (payload) {
|
|
145
|
-
return payload as
|
|
144
|
+
return payload as ModuleState<TState>
|
|
146
145
|
}
|
|
147
146
|
}
|
|
148
147
|
return undefined
|