@xyo-network/diviner-indexing-memory 2.111.3 → 3.0.1

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.
Files changed (48) hide show
  1. package/dist/neutral/index.d.ts +158 -2
  2. package/package.json +21 -40
  3. package/xy.config.ts +12 -0
  4. package/dist/browser/Diviner.d.cts +0 -102
  5. package/dist/browser/Diviner.d.cts.map +0 -1
  6. package/dist/browser/Diviner.d.mts +0 -102
  7. package/dist/browser/Diviner.d.mts.map +0 -1
  8. package/dist/browser/Diviner.d.ts +0 -102
  9. package/dist/browser/Diviner.d.ts.map +0 -1
  10. package/dist/browser/index.cjs +0 -238
  11. package/dist/browser/index.cjs.map +0 -1
  12. package/dist/browser/index.d.cts +0 -2
  13. package/dist/browser/index.d.cts.map +0 -1
  14. package/dist/browser/index.d.mts +0 -2
  15. package/dist/browser/index.d.mts.map +0 -1
  16. package/dist/browser/index.d.ts +0 -2
  17. package/dist/browser/index.d.ts.map +0 -1
  18. package/dist/browser/index.mjs +0 -217
  19. package/dist/browser/index.mjs.map +0 -1
  20. package/dist/neutral/Diviner.d.cts +0 -102
  21. package/dist/neutral/Diviner.d.cts.map +0 -1
  22. package/dist/neutral/Diviner.d.mts +0 -102
  23. package/dist/neutral/Diviner.d.mts.map +0 -1
  24. package/dist/neutral/Diviner.d.ts +0 -102
  25. package/dist/neutral/Diviner.d.ts.map +0 -1
  26. package/dist/neutral/index.cjs +0 -238
  27. package/dist/neutral/index.cjs.map +0 -1
  28. package/dist/neutral/index.d.cts +0 -2
  29. package/dist/neutral/index.d.cts.map +0 -1
  30. package/dist/neutral/index.d.mts +0 -2
  31. package/dist/neutral/index.d.mts.map +0 -1
  32. package/dist/neutral/index.d.ts.map +0 -1
  33. package/dist/node/Diviner.d.cts +0 -102
  34. package/dist/node/Diviner.d.cts.map +0 -1
  35. package/dist/node/Diviner.d.mts +0 -102
  36. package/dist/node/Diviner.d.mts.map +0 -1
  37. package/dist/node/Diviner.d.ts +0 -102
  38. package/dist/node/Diviner.d.ts.map +0 -1
  39. package/dist/node/index.cjs +0 -254
  40. package/dist/node/index.cjs.map +0 -1
  41. package/dist/node/index.d.cts +0 -2
  42. package/dist/node/index.d.cts.map +0 -1
  43. package/dist/node/index.d.mts +0 -2
  44. package/dist/node/index.d.mts.map +0 -1
  45. package/dist/node/index.d.ts +0 -2
  46. package/dist/node/index.d.ts.map +0 -1
  47. package/dist/node/index.mjs +0 -229
  48. package/dist/node/index.mjs.map +0 -1
@@ -1,2 +0,0 @@
1
- export * from './Diviner.ts';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}
@@ -1,2 +0,0 @@
1
- export * from './Diviner.ts';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}
@@ -1,229 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getProtoOf = Object.getPrototypeOf;
3
- var __reflectGet = Reflect.get;
4
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
- var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
8
-
9
- // src/Diviner.ts
10
- import { assertEx } from "@xylabs/assert";
11
- import { clearTimeoutEx, setTimeoutEx } from "@xylabs/timer";
12
- import { ArchivistWrapper } from "@xyo-network/archivist-wrapper";
13
- import { BoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
14
- import { isBoundWitness } from "@xyo-network/boundwitness-model";
15
- import { AbstractDiviner } from "@xyo-network/diviner-abstract";
16
- import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
17
- import { IndexingDivinerConfigSchema } from "@xyo-network/diviner-indexing-model";
18
- import { asDivinerInstance } from "@xyo-network/diviner-model";
19
- import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
20
- import { creatableModule, isModuleState, isModuleStateWithMeta, ModuleStateSchema } from "@xyo-network/module-model";
21
- import { PayloadBuilder } from "@xyo-network/payload-builder";
22
- function _ts_decorate(decorators, target, key, desc) {
23
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
24
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
25
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
26
- return c > 3 && r && Object.defineProperty(target, key, r), r;
27
- }
28
- __name(_ts_decorate, "_ts_decorate");
29
- var moduleName = "IndexingDiviner";
30
- var _IndexingDiviner = class _IndexingDiviner extends AbstractDiviner {
31
- _lastState;
32
- _pollId;
33
- get payloadDivinerLimit() {
34
- return this.config.payloadDivinerLimit ?? 1e3;
35
- }
36
- get pollFrequency() {
37
- return this.config.pollFrequency ?? 1e4;
38
- }
39
- /**
40
- * Works via batched iteration of the source archivist to populate the index.
41
- * @returns A promise that resolves when the background process is complete
42
- */
43
- backgroundDivine = /* @__PURE__ */ __name(async () => {
44
- const lastState = await this.retrieveState();
45
- const indexCandidateDiviner = await this.getIndexingDivinerStage("stateToIndexCandidateDiviner");
46
- const results = lastState ? await indexCandidateDiviner.divine([
47
- lastState
48
- ]) : await indexCandidateDiviner.divine();
49
- const nextState = results.find(isModuleStateWithMeta);
50
- const indexCandidates = results.filter((x) => !isModuleStateWithMeta(x));
51
- const toIndexTransformDiviner = await this.getIndexingDivinerStage("indexCandidateToIndexDiviner");
52
- const indexes = await toIndexTransformDiviner.divine(indexCandidates);
53
- const indexArchivist = await this.getArchivistForStore("indexStore");
54
- await indexArchivist.insert(indexes);
55
- if (nextState) {
56
- await this.commitState(nextState);
57
- }
58
- }, "backgroundDivine");
59
- /**
60
- * Commit the internal state of the Diviner process. This is similar
61
- * to a transaction completion in a database and should only be called
62
- * when results have been successfully persisted to the appropriate
63
- * external stores.
64
- * @param nextState The state to commit
65
- */
66
- async commitState(nextState) {
67
- var _a;
68
- if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset)) return;
69
- this._lastState = nextState;
70
- const archivist = await this.getArchivistForStore("stateStore");
71
- const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build();
72
- await archivist.insert([
73
- bw,
74
- nextState
75
- ]);
76
- }
77
- async divineHandler(payloads = []) {
78
- const indexPayloadDiviner = await this.getPayloadDivinerForStore("indexStore");
79
- const divinerQueryToIndexQueryDiviner = await this.getIndexingDivinerStage("divinerQueryToIndexQueryDiviner");
80
- const indexQueryResponseToDivinerQueryResponseDiviner = await this.getIndexingDivinerStage("indexQueryResponseToDivinerQueryResponseDiviner");
81
- const results = (await Promise.all(payloads.map(async (payload) => {
82
- const indexQuery = await divinerQueryToIndexQueryDiviner.divine([
83
- payload
84
- ]);
85
- const indexedResults = await indexPayloadDiviner.divine(indexQuery);
86
- const response = await Promise.all(indexedResults.flat().map((indexedResult) => indexQueryResponseToDivinerQueryResponseDiviner.divine([
87
- payload,
88
- indexedResult
89
- ])));
90
- return response.flat();
91
- }))).flat();
92
- return await Promise.all(results.map((result) => PayloadBuilder.build(result)));
93
- }
94
- /**
95
- * Retrieves the archivist for the specified store
96
- * @param store The store to retrieve the archivist for
97
- * @returns The archivist for the specified store
98
- */
99
- async getArchivistForStore(store) {
100
- var _a, _b;
101
- const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`);
102
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist [${name}]`);
103
- return ArchivistWrapper.wrap(mod, this.account);
104
- }
105
- /**
106
- * Retrieves the BoundWitness Diviner for the specified store
107
- * @param store The store to retrieve the BoundWitness Diviner for
108
- * @returns The BoundWitness Diviner for the specified store
109
- */
110
- async getBoundWitnessDivinerForStore(store) {
111
- var _a, _b;
112
- const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`);
113
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner [${name}]`);
114
- return DivinerWrapper.wrap(mod, this.account);
115
- }
116
- /**
117
- * Gets the Diviner for the supplied Indexing Diviner stage
118
- * @param transform The Indexing Diviner stage
119
- * @returns The diviner corresponding to the Indexing Diviner stage
120
- */
121
- async getIndexingDivinerStage(transform) {
122
- var _a, _b;
123
- const nameOrAddress = assertEx((_b = (_a = this.config) == null ? void 0 : _a.indexingDivinerStages) == null ? void 0 : _b[transform], () => `${moduleName}: Config for indexingDivinerStages.${transform} not specified`);
124
- const mod = await this.resolve(nameOrAddress);
125
- return assertEx(asDivinerInstance(mod), () => `${moduleName}: Failed to resolve indexing diviner stage for ${transform}`);
126
- }
127
- /**
128
- * Retrieves the Payload Diviner for the specified store
129
- * @param store The store to retrieve the Payload Diviner for
130
- * @returns The Payload Diviner for the specified store
131
- */
132
- async getPayloadDivinerForStore(store) {
133
- var _a, _b;
134
- const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`);
135
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner [${name}]`);
136
- return DivinerWrapper.wrap(mod, this.account);
137
- }
138
- /**
139
- * Retrieves the last state of the Diviner process. Used to recover state after
140
- * preemptions, reboots, etc.
141
- */
142
- async retrieveState() {
143
- const accountAddress = this.account.address;
144
- if (this._lastState) return this._lastState;
145
- let hash = "";
146
- const diviner = await this.getBoundWitnessDivinerForStore("stateStore");
147
- const query = await new PayloadBuilder({
148
- schema: BoundWitnessDivinerQuerySchema
149
- }).fields({
150
- address: accountAddress,
151
- limit: 1,
152
- offset: 0,
153
- order: "desc",
154
- payload_schemas: [
155
- ModuleStateSchema
156
- ]
157
- }).build();
158
- const boundWitnesses = await diviner.divine([
159
- query
160
- ]);
161
- if (boundWitnesses.length > 0) {
162
- const boundWitness = boundWitnesses[0];
163
- if (isBoundWitness(boundWitness)) {
164
- hash = boundWitness.addresses.map((address, index) => ({
165
- address,
166
- index
167
- })).filter(({ address }) => address === accountAddress).reduce((prev, curr) => {
168
- var _a;
169
- return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
170
- }, "");
171
- }
172
- }
173
- if (hash !== "") {
174
- const archivist = await this.getArchivistForStore("stateStore");
175
- const payload = (await archivist.get([
176
- hash
177
- ])).find(isModuleState);
178
- if (payload) {
179
- return payload;
180
- }
181
- }
182
- return void 0;
183
- }
184
- async startHandler() {
185
- await super.startHandler();
186
- this.poll();
187
- return true;
188
- }
189
- async stopHandler(_timeout) {
190
- if (this._pollId) {
191
- clearTimeout(this._pollId);
192
- this._pollId = void 0;
193
- }
194
- return await super.stopHandler();
195
- }
196
- /**
197
- * Runs the background divine process on a loop with a delay
198
- * specified by the `config.pollFrequency`
199
- */
200
- poll() {
201
- this._pollId = setTimeoutEx(async () => {
202
- try {
203
- await Promise.resolve();
204
- await this.backgroundDivine();
205
- } catch (e) {
206
- console.log(e);
207
- } finally {
208
- if (this._pollId) clearTimeoutEx(this._pollId);
209
- this._pollId = void 0;
210
- this.poll();
211
- }
212
- }, this.pollFrequency);
213
- }
214
- };
215
- __name(_IndexingDiviner, "IndexingDiviner");
216
- __publicField(_IndexingDiviner, "allowRandomAccount", false);
217
- __publicField(_IndexingDiviner, "configSchemas", [
218
- ...__superGet(_IndexingDiviner, _IndexingDiviner, "configSchemas"),
219
- IndexingDivinerConfigSchema
220
- ]);
221
- __publicField(_IndexingDiviner, "defaultConfigSchema", IndexingDivinerConfigSchema);
222
- var IndexingDiviner = _IndexingDiviner;
223
- IndexingDiviner = _ts_decorate([
224
- creatableModule()
225
- ], IndexingDiviner);
226
- export {
227
- IndexingDiviner
228
- };
229
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/Diviner.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { clearTimeoutEx, setTimeoutEx } from '@xylabs/timer'\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 { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport {\n IndexingDivinerConfig,\n IndexingDivinerConfigSchema,\n IndexingDivinerParams,\n IndexingDivinerStage,\n IndexingDivinerState,\n} from '@xyo-network/diviner-indexing-model'\nimport { asDivinerInstance, DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport { creatableModule, isModuleState, isModuleStateWithMeta, ModuleState, ModuleStateSchema } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\n\nexport type ConfigStoreKey = 'indexStore' | 'stateStore'\n\nexport type ConfigStore = Extract<keyof IndexingDivinerConfig, ConfigStoreKey>\n\nconst moduleName = 'IndexingDiviner'\n\n@creatableModule<IndexingDiviner>()\nexport class IndexingDiviner<\n TParams extends IndexingDivinerParams = IndexingDivinerParams,\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> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly allowRandomAccount = false\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexingDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexingDivinerConfigSchema\n\n private _lastState?: ModuleState<IndexingDivinerState>\n private _pollId?: string\n\n get payloadDivinerLimit() {\n return this.config.payloadDivinerLimit ?? 1000\n }\n\n get pollFrequency() {\n return this.config.pollFrequency ?? 10_000\n }\n\n /**\n * Works via batched iteration of the source archivist to populate the index.\n * @returns A promise that resolves when the background process is complete\n */\n protected backgroundDivine = async (): Promise<void> => {\n // Load last state\n const lastState = await this.retrieveState()\n // Get next batch of results\n const indexCandidateDiviner = await this.getIndexingDivinerStage('stateToIndexCandidateDiviner')\n const results = lastState ? await indexCandidateDiviner.divine([lastState]) : await indexCandidateDiviner.divine()\n // Filter next state out from results\n const nextState = results.find(isModuleStateWithMeta<IndexingDivinerState>)\n const indexCandidates = results.filter(x => !isModuleStateWithMeta(x))\n // Transform candidates to indexes\n const toIndexTransformDiviner = await this.getIndexingDivinerStage('indexCandidateToIndexDiviner')\n const indexes = await toIndexTransformDiviner.divine(indexCandidates)\n // Insert index results\n const indexArchivist = await this.getArchivistForStore('indexStore')\n await indexArchivist.insert(indexes)\n // Update state\n if (nextState) {\n await this.commitState(nextState)\n }\n }\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<IndexingDivinerState>) {\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('stateStore')\n const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n protected override async divineHandler(payloads: TIn[] = []): Promise<WithMeta<TOut>[]> {\n const indexPayloadDiviner = await this.getPayloadDivinerForStore('indexStore')\n const divinerQueryToIndexQueryDiviner = await this.getIndexingDivinerStage('divinerQueryToIndexQueryDiviner')\n const indexQueryResponseToDivinerQueryResponseDiviner = await this.getIndexingDivinerStage('indexQueryResponseToDivinerQueryResponseDiviner')\n const results = (\n await Promise.all(\n payloads.map(async (payload) => {\n const indexQuery = await divinerQueryToIndexQueryDiviner.divine([payload])\n // Divine the results\n const indexedResults = await indexPayloadDiviner.divine(indexQuery)\n // Transform the results to the response shape\n const response = await Promise.all(\n indexedResults.flat().map(indexedResult => indexQueryResponseToDivinerQueryResponseDiviner.divine([payload, indexedResult])),\n )\n return response.flat()\n }),\n )\n ).flat()\n // TODO: Infer this type over casting to this type\n return (await Promise.all(results.map(result => PayloadBuilder.build(result)))) as WithMeta<TOut>[]\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 getArchivistForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist [${name}]`)\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 getBoundWitnessDivinerForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner [${name}]`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Gets the Diviner for the supplied Indexing Diviner stage\n * @param transform The Indexing Diviner stage\n * @returns The diviner corresponding to the Indexing Diviner stage\n */\n protected async getIndexingDivinerStage(transform: IndexingDivinerStage) {\n const nameOrAddress = assertEx(\n this.config?.indexingDivinerStages?.[transform],\n () => `${moduleName}: Config for indexingDivinerStages.${transform} not specified`,\n )\n const mod = await this.resolve(nameOrAddress)\n return assertEx(asDivinerInstance(mod), () => `${moduleName}: Failed to resolve indexing diviner stage for ${transform}`) as DivinerInstance\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 getPayloadDivinerForStore(store: ConfigStore) {\n const name = assertEx(this.config?.[store]?.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner [${name}]`)\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<IndexingDivinerState> | undefined> {\n const accountAddress = this.account.address\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStore('stateStore')\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n address: accountAddress,\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 === accountAddress)\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('stateStore')\n const payload = (await archivist.get([hash])).find(isModuleState<IndexingDivinerState>)\n if (payload) {\n return payload as WithMeta<ModuleState<IndexingDivinerState>>\n }\n }\n return undefined\n }\n\n protected override async startHandler(): Promise<boolean> {\n await super.startHandler()\n this.poll()\n return true\n }\n\n protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {\n if (this._pollId) {\n clearTimeout(this._pollId)\n this._pollId = undefined\n }\n return await super.stopHandler()\n }\n\n /**\n * Runs the background divine process on a loop with a delay\n * specified by the `config.pollFrequency`\n */\n private poll() {\n this._pollId = setTimeoutEx(async () => {\n try {\n await Promise.resolve()\n await this.backgroundDivine()\n } catch (e) {\n console.log(e)\n } finally {\n if (this._pollId) clearTimeoutEx(this._pollId)\n this._pollId = undefined\n this.poll()\n }\n }, this.pollFrequency)\n }\n}\n"],"mappings":";;;;;;;;;AAAA,SAASA,gBAAgB;AAEzB,SAASC,gBAAgBC,oBAAoB;AAC7C,SAASC,wBAAwB;AACjC,SAASC,2BAA2B;AACpC,SAASC,sBAAsB;AAC/B,SAASC,uBAAuB;AAChC,SAA0CC,sCAAsC;AAChF,SAEEC,mCAIK;AACP,SAASC,yBAAkE;AAC3E,SAASC,sBAAsB;AAC/B,SAASC,iBAAiBC,eAAeC,uBAAoCC,yBAAyB;AACtG,SAASC,sBAAsB;;;;;;;;AAO/B,IAAMC,aAAa;AAGZ,IAAMC,mBAAN,MAAMA,yBASHC,gBAAAA;EAKAC;EACAC;EAER,IAAIC,sBAAsB;AACxB,WAAO,KAAKC,OAAOD,uBAAuB;EAC5C;EAEA,IAAIE,gBAAgB;AAClB,WAAO,KAAKD,OAAOC,iBAAiB;EACtC;;;;;EAMUC,mBAAmB,mCAAA;AAE3B,UAAMC,YAAY,MAAM,KAAKC,cAAa;AAE1C,UAAMC,wBAAwB,MAAM,KAAKC,wBAAwB,8BAAA;A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AAI;AAEf,YAAMxC,YAAY,MAAM,KAAKL,qBAAqB,YAAA;AAClD,YAAMQ,WAAW,MAAMH,UAAU4D,IAAI;QAACpB;OAAK,GAAGrD,KAAK0E,aAAAA;AACnD,UAAI1D,SAAS;AACX,eAAOA;MACT;IACF;AACA,WAAO2D;EACT;EAEA,MAAyBC,eAAiC;AACxD,UAAM,MAAMA,aAAAA;AACZ,SAAKC,KAAI;AACT,WAAO;EACT;EAEA,MAAyBC,YAAYC,UAAiD;AACpF,QAAI,KAAK3F,SAAS;AAChB4F,mBAAa,KAAK5F,OAAO;AACzB,WAAKA,UAAUuF;IACjB;AACA,WAAO,MAAM,MAAMG,YAAAA;EACrB;;;;;EAMQD,OAAO;AACb,SAAKzF,UAAU6F,aAAa,YAAA;AAC1B,UAAI;AACF,cAAMvD,QAAQc,QAAO;AACrB,cAAM,KAAKhD,iBAAgB;MAC7B,SAAS0F,GAAG;AACVC,gBAAQC,IAAIF,CAAAA;MACd,UAAA;AACE,YAAI,KAAK9F,QAASiG,gBAAe,KAAKjG,OAAO;AAC7C,aAAKA,UAAUuF;AACf,aAAKE,KAAI;MACX;IACF,GAAG,KAAKtF,aAAa;EACvB;AACF;AA5MUL;AACR,cAVWD,kBAUcqG,sBAAqB;AAC9C,cAXWrG,kBAWcsG,iBAA0B;KAAI,+CAAMA;EAAeC;;AAC5E,cAZWvG,kBAYcwG,uBAA8BD;AAZlD,IAAMvG,kBAAN;;;;","names":["assertEx","clearTimeoutEx","setTimeoutEx","ArchivistWrapper","BoundWitnessBuilder","isBoundWitness","AbstractDiviner","BoundWitnessDivinerQuerySchema","IndexingDivinerConfigSchema","asDivinerInstance","DivinerWrapper","creatableModule","isModuleState","isModuleStateWithMeta","ModuleStateSchema","PayloadBuilder","moduleName","IndexingDiviner","AbstractDiviner","_lastState","_pollId","payloadDivinerLimit","config","pollFrequency","backgroundDivine","lastState","retrieveState","indexCandidateDiviner","getIndexingDivinerStage","results","divine","nextState","find","isModuleStateWithMeta","indexCandidates","filter","x","toIndexTransformDiviner","indexes","indexArchivist","getArchivistForStore","insert","commitState","state","offset","archivist","bw","BoundWitnessBuilder","payload","signer","account","build","divineHandler","payloads","indexPayloadDiviner","getPayloadDivinerForStore","divinerQueryToIndexQueryDiviner","indexQueryResponseToDivinerQueryResponseDiviner","Promise","all","map","indexQuery","indexedResults","response","flat","indexedResult","result","PayloadBuilder","store","name","assertEx","mod","resolve","ArchivistWrapper","wrap","getBoundWitnessDivinerForStore","boundWitnessDiviner","DivinerWrapper","transform","nameOrAddress","indexingDivinerStages","asDivinerInstance","payloadDiviner","accountAddress","address","hash","diviner","query","schema","BoundWitnessDivinerQuerySchema","fields","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","length","boundWitness","isBoundWitness","addresses","index","reduce","prev","curr","payload_hashes","get","isModuleState","undefined","startHandler","poll","stopHandler","_timeout","clearTimeout","setTimeoutEx","e","console","log","clearTimeoutEx","allowRandomAccount","configSchemas","IndexingDivinerConfigSchema","defaultConfigSchema"]}