@xyo-network/diviner-stateful 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 (120) hide show
  1. package/dist/neutral/index.d.ts +514 -6
  2. package/package.json +27 -46
  3. package/xy.config.ts +12 -0
  4. package/dist/browser/Config.d.cts +0 -13
  5. package/dist/browser/Config.d.cts.map +0 -1
  6. package/dist/browser/Config.d.mts +0 -13
  7. package/dist/browser/Config.d.mts.map +0 -1
  8. package/dist/browser/Config.d.ts +0 -13
  9. package/dist/browser/Config.d.ts.map +0 -1
  10. package/dist/browser/Diviner.d.cts +0 -72
  11. package/dist/browser/Diviner.d.cts.map +0 -1
  12. package/dist/browser/Diviner.d.mts +0 -72
  13. package/dist/browser/Diviner.d.mts.map +0 -1
  14. package/dist/browser/Diviner.d.ts +0 -72
  15. package/dist/browser/Diviner.d.ts.map +0 -1
  16. package/dist/browser/DivinerMixin.d.cts +0 -385
  17. package/dist/browser/DivinerMixin.d.cts.map +0 -1
  18. package/dist/browser/DivinerMixin.d.mts +0 -385
  19. package/dist/browser/DivinerMixin.d.mts.map +0 -1
  20. package/dist/browser/DivinerMixin.d.ts +0 -385
  21. package/dist/browser/DivinerMixin.d.ts.map +0 -1
  22. package/dist/browser/Params.d.cts +0 -5
  23. package/dist/browser/Params.d.cts.map +0 -1
  24. package/dist/browser/Params.d.mts +0 -5
  25. package/dist/browser/Params.d.mts.map +0 -1
  26. package/dist/browser/Params.d.ts +0 -5
  27. package/dist/browser/Params.d.ts.map +0 -1
  28. package/dist/browser/Schema.d.cts +0 -3
  29. package/dist/browser/Schema.d.cts.map +0 -1
  30. package/dist/browser/Schema.d.mts +0 -3
  31. package/dist/browser/Schema.d.mts.map +0 -1
  32. package/dist/browser/Schema.d.ts +0 -3
  33. package/dist/browser/Schema.d.ts.map +0 -1
  34. package/dist/browser/index.cjs +0 -263
  35. package/dist/browser/index.cjs.map +0 -1
  36. package/dist/browser/index.d.cts +0 -6
  37. package/dist/browser/index.d.cts.map +0 -1
  38. package/dist/browser/index.d.mts +0 -6
  39. package/dist/browser/index.d.mts.map +0 -1
  40. package/dist/browser/index.d.ts +0 -6
  41. package/dist/browser/index.d.ts.map +0 -1
  42. package/dist/browser/index.mjs +0 -242
  43. package/dist/browser/index.mjs.map +0 -1
  44. package/dist/neutral/Config.d.cts +0 -13
  45. package/dist/neutral/Config.d.cts.map +0 -1
  46. package/dist/neutral/Config.d.mts +0 -13
  47. package/dist/neutral/Config.d.mts.map +0 -1
  48. package/dist/neutral/Config.d.ts +0 -13
  49. package/dist/neutral/Config.d.ts.map +0 -1
  50. package/dist/neutral/Diviner.d.cts +0 -72
  51. package/dist/neutral/Diviner.d.cts.map +0 -1
  52. package/dist/neutral/Diviner.d.mts +0 -72
  53. package/dist/neutral/Diviner.d.mts.map +0 -1
  54. package/dist/neutral/Diviner.d.ts +0 -72
  55. package/dist/neutral/Diviner.d.ts.map +0 -1
  56. package/dist/neutral/DivinerMixin.d.cts +0 -385
  57. package/dist/neutral/DivinerMixin.d.cts.map +0 -1
  58. package/dist/neutral/DivinerMixin.d.mts +0 -385
  59. package/dist/neutral/DivinerMixin.d.mts.map +0 -1
  60. package/dist/neutral/DivinerMixin.d.ts +0 -385
  61. package/dist/neutral/DivinerMixin.d.ts.map +0 -1
  62. package/dist/neutral/Params.d.cts +0 -5
  63. package/dist/neutral/Params.d.cts.map +0 -1
  64. package/dist/neutral/Params.d.mts +0 -5
  65. package/dist/neutral/Params.d.mts.map +0 -1
  66. package/dist/neutral/Params.d.ts +0 -5
  67. package/dist/neutral/Params.d.ts.map +0 -1
  68. package/dist/neutral/Schema.d.cts +0 -3
  69. package/dist/neutral/Schema.d.cts.map +0 -1
  70. package/dist/neutral/Schema.d.mts +0 -3
  71. package/dist/neutral/Schema.d.mts.map +0 -1
  72. package/dist/neutral/Schema.d.ts +0 -3
  73. package/dist/neutral/Schema.d.ts.map +0 -1
  74. package/dist/neutral/index.cjs +0 -263
  75. package/dist/neutral/index.cjs.map +0 -1
  76. package/dist/neutral/index.d.cts +0 -6
  77. package/dist/neutral/index.d.cts.map +0 -1
  78. package/dist/neutral/index.d.mts +0 -6
  79. package/dist/neutral/index.d.mts.map +0 -1
  80. package/dist/neutral/index.d.ts.map +0 -1
  81. package/dist/node/Config.d.cts +0 -13
  82. package/dist/node/Config.d.cts.map +0 -1
  83. package/dist/node/Config.d.mts +0 -13
  84. package/dist/node/Config.d.mts.map +0 -1
  85. package/dist/node/Config.d.ts +0 -13
  86. package/dist/node/Config.d.ts.map +0 -1
  87. package/dist/node/Diviner.d.cts +0 -72
  88. package/dist/node/Diviner.d.cts.map +0 -1
  89. package/dist/node/Diviner.d.mts +0 -72
  90. package/dist/node/Diviner.d.mts.map +0 -1
  91. package/dist/node/Diviner.d.ts +0 -72
  92. package/dist/node/Diviner.d.ts.map +0 -1
  93. package/dist/node/DivinerMixin.d.cts +0 -385
  94. package/dist/node/DivinerMixin.d.cts.map +0 -1
  95. package/dist/node/DivinerMixin.d.mts +0 -385
  96. package/dist/node/DivinerMixin.d.mts.map +0 -1
  97. package/dist/node/DivinerMixin.d.ts +0 -385
  98. package/dist/node/DivinerMixin.d.ts.map +0 -1
  99. package/dist/node/Params.d.cts +0 -5
  100. package/dist/node/Params.d.cts.map +0 -1
  101. package/dist/node/Params.d.mts +0 -5
  102. package/dist/node/Params.d.mts.map +0 -1
  103. package/dist/node/Params.d.ts +0 -5
  104. package/dist/node/Params.d.ts.map +0 -1
  105. package/dist/node/Schema.d.cts +0 -3
  106. package/dist/node/Schema.d.cts.map +0 -1
  107. package/dist/node/Schema.d.mts +0 -3
  108. package/dist/node/Schema.d.mts.map +0 -1
  109. package/dist/node/Schema.d.ts +0 -3
  110. package/dist/node/Schema.d.ts.map +0 -1
  111. package/dist/node/index.cjs +0 -286
  112. package/dist/node/index.cjs.map +0 -1
  113. package/dist/node/index.d.cts +0 -6
  114. package/dist/node/index.d.cts.map +0 -1
  115. package/dist/node/index.d.mts +0 -6
  116. package/dist/node/index.d.mts.map +0 -1
  117. package/dist/node/index.d.ts +0 -6
  118. package/dist/node/index.d.ts.map +0 -1
  119. package/dist/node/index.mjs +0 -258
  120. package/dist/node/index.mjs.map +0 -1
@@ -1,286 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __reflectGet = Reflect.get;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
- var __export = (target, all) => {
11
- for (var name in all)
12
- __defProp(target, name, { get: all[name], enumerable: true });
13
- };
14
- var __copyProps = (to, from, except, desc) => {
15
- if (from && typeof from === "object" || typeof from === "function") {
16
- for (let key of __getOwnPropNames(from))
17
- if (!__hasOwnProp.call(to, key) && key !== except)
18
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
- }
20
- return to;
21
- };
22
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
- var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
25
-
26
- // src/index.ts
27
- var src_exports = {};
28
- __export(src_exports, {
29
- StatefulDiviner: () => StatefulDiviner,
30
- StatefulDivinerConfigSchema: () => StatefulDivinerConfigSchema,
31
- StatefulDivinerSchema: () => StatefulDivinerSchema,
32
- StatefulModuleMixin: () => StatefulModuleMixin
33
- });
34
- module.exports = __toCommonJS(src_exports);
35
-
36
- // src/Schema.ts
37
- var StatefulDivinerSchema = "network.xyo.diviner.stateful";
38
-
39
- // src/Config.ts
40
- var StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config`;
41
-
42
- // src/Diviner.ts
43
- var import_assert = require("@xylabs/assert");
44
- var import_archivist_wrapper = require("@xyo-network/archivist-wrapper");
45
- var import_boundwitness_builder = require("@xyo-network/boundwitness-builder");
46
- var import_boundwitness_model = require("@xyo-network/boundwitness-model");
47
- var import_diviner_abstract = require("@xyo-network/diviner-abstract");
48
- var import_diviner_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
49
- var import_diviner_wrapper = require("@xyo-network/diviner-wrapper");
50
- var import_module_model = require("@xyo-network/module-model");
51
- var import_payload_builder = require("@xyo-network/payload-builder");
52
- var moduleName = "StatefulDiviner";
53
- var _StatefulDiviner = class _StatefulDiviner extends import_diviner_abstract.AbstractDiviner {
54
- /**
55
- * The last state
56
- */
57
- _lastState;
58
- /**
59
- * Commit the internal state of the Diviner process. This is similar
60
- * to a transaction completion in a database and should only be called
61
- * when results have been successfully persisted to the appropriate
62
- * external stores.
63
- * @param nextState The state to commit
64
- */
65
- async commitState(nextState) {
66
- var _a;
67
- if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset)) return;
68
- this._lastState = nextState;
69
- const archivist = await this.getArchivistForStateStore();
70
- const [bw] = await new import_boundwitness_builder.BoundWitnessBuilder().payload(nextState).signer(this.account).build();
71
- await archivist.insert([
72
- bw,
73
- nextState
74
- ]);
75
- }
76
- /**
77
- * Retrieves the archivist for the specified store
78
- * @param store The store to retrieve the archivist for
79
- * @returns The archivist for the specified store
80
- */
81
- async getArchivistForStateStore() {
82
- var _a;
83
- const name = (0, import_assert.assertEx)((_a = this.config) == null ? void 0 : _a.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
84
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
85
- return import_archivist_wrapper.ArchivistWrapper.wrap(mod, this.account);
86
- }
87
- /**
88
- * Retrieves the BoundWitness Diviner for the specified store
89
- * @param store The store to retrieve the BoundWitness Diviner for
90
- * @returns The BoundWitness Diviner for the specified store
91
- */
92
- async getBoundWitnessDivinerForStateStore() {
93
- var _a;
94
- const name = (0, import_assert.assertEx)((_a = this.config) == null ? void 0 : _a.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
95
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
96
- return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
97
- }
98
- /**
99
- * Retrieves the Payload Diviner for the specified store
100
- * @param store The store to retrieve the Payload Diviner for
101
- * @returns The Payload Diviner for the specified store
102
- */
103
- async getPayloadDivinerForStateStore() {
104
- var _a, _b;
105
- const name = (0, import_assert.assertEx)((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`);
106
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
107
- return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
108
- }
109
- /**
110
- * Retrieves the last state of the Diviner process. Used to recover state after
111
- * preemptions, reboots, etc.
112
- */
113
- async retrieveState() {
114
- if (this._lastState) return this._lastState;
115
- let hash = "";
116
- const diviner = await this.getBoundWitnessDivinerForStateStore();
117
- const query = await new import_payload_builder.PayloadBuilder({
118
- schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema
119
- }).fields({
120
- address: this.account.address,
121
- limit: 1,
122
- offset: 0,
123
- order: "desc",
124
- payload_schemas: [
125
- import_module_model.ModuleStateSchema
126
- ]
127
- }).build();
128
- const boundWitnesses = await diviner.divine([
129
- query
130
- ]);
131
- if (boundWitnesses.length > 0) {
132
- const boundWitness = boundWitnesses[0];
133
- if ((0, import_boundwitness_model.isBoundWitness)(boundWitness)) {
134
- hash = boundWitness.addresses.map((address, index) => ({
135
- address,
136
- index
137
- })).filter(({ address }) => address === this.account.address).reduce((prev, curr) => {
138
- var _a;
139
- return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === import_module_model.ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
140
- }, "");
141
- }
142
- }
143
- if (hash) {
144
- const archivist = await this.getArchivistForStateStore();
145
- const payload = (await archivist.get([
146
- hash
147
- ])).find(import_module_model.isModuleState);
148
- if (payload) {
149
- return payload;
150
- }
151
- }
152
- return void 0;
153
- }
154
- };
155
- __name(_StatefulDiviner, "StatefulDiviner");
156
- __publicField(_StatefulDiviner, "configSchemas", [
157
- ...__superGet(_StatefulDiviner, _StatefulDiviner, "configSchemas"),
158
- StatefulDivinerConfigSchema
159
- ]);
160
- __publicField(_StatefulDiviner, "defaultConfigSchema", StatefulDivinerConfigSchema);
161
- var StatefulDiviner = _StatefulDiviner;
162
-
163
- // src/DivinerMixin.ts
164
- var import_assert2 = require("@xylabs/assert");
165
- var import_archivist_model = require("@xyo-network/archivist-model");
166
- var import_boundwitness_builder2 = require("@xyo-network/boundwitness-builder");
167
- var import_boundwitness_model2 = require("@xyo-network/boundwitness-model");
168
- var import_diviner_boundwitness_model2 = require("@xyo-network/diviner-boundwitness-model");
169
- var import_diviner_model = require("@xyo-network/diviner-model");
170
- var import_module_model2 = require("@xyo-network/module-model");
171
- var import_payload_builder2 = require("@xyo-network/payload-builder");
172
- var moduleName2 = "StatefulModuleMixin";
173
- var StatefulModuleMixin = /* @__PURE__ */ __name((ModuleBase) => {
174
- var _a;
175
- let StatefulModuleBase = (_a = class extends ModuleBase {
176
- _lastState;
177
- /**
178
- * Commit the internal state of the Diviner process. This is similar
179
- * to a transaction completion in a database and should only be called
180
- * when results have been successfully persisted to the appropriate
181
- * external stores.
182
- * @param nextState The state to commit
183
- */
184
- async commitState(nextState) {
185
- var _a2;
186
- if (nextState.state.offset === ((_a2 = this._lastState) == null ? void 0 : _a2.state.offset)) return;
187
- this._lastState = nextState;
188
- const archivist = await this.getArchivistForStore();
189
- const [bw] = await (await new import_boundwitness_builder2.BoundWitnessBuilder().payload(nextState)).build();
190
- await archivist.insert([
191
- bw,
192
- nextState
193
- ]);
194
- }
195
- /**
196
- * Retrieves the archivist for the specified store
197
- * @param store The store to retrieve the archivist for
198
- * @returns The archivist for the specified store
199
- */
200
- async getArchivistForStore() {
201
- var _a2, _b;
202
- const name = (0, import_assert2.assertEx)((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
203
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
204
- const instance = (0, import_archivist_model.asArchivistInstance)(mod);
205
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
206
- }
207
- /**
208
- * Retrieves the BoundWitness Diviner for the specified store
209
- * @param store The store to retrieve the BoundWitness Diviner for
210
- * @returns The BoundWitness Diviner for the specified store
211
- */
212
- async getBoundWitnessDivinerForStore() {
213
- var _a2, _b;
214
- const name = (0, import_assert2.assertEx)((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.boundWitnessDiviner, () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`);
215
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
216
- const instance = (0, import_diviner_model.asDivinerInstance)(mod);
217
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
218
- }
219
- /**
220
- * Retrieves the Payload Diviner for the specified store
221
- * @param store The store to retrieve the Payload Diviner for
222
- * @returns The Payload Diviner for the specified store
223
- */
224
- async getPayloadDivinerForStateStore() {
225
- var _a2, _b;
226
- const name = (0, import_assert2.assertEx)((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
227
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
228
- const instance = (0, import_diviner_model.asDivinerInstance)(mod);
229
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
230
- }
231
- /**
232
- * Retrieves the last state of the Diviner process. Used to recover state after
233
- * preemptions, reboots, etc.
234
- */
235
- async retrieveState() {
236
- if (this._lastState) return this._lastState;
237
- let hash = "";
238
- const diviner = await this.getBoundWitnessDivinerForStore();
239
- const query = await new import_payload_builder2.PayloadBuilder({
240
- schema: import_diviner_boundwitness_model2.BoundWitnessDivinerQuerySchema
241
- }).fields({
242
- // address: this.account.address,
243
- limit: 1,
244
- offset: 0,
245
- order: "desc",
246
- payload_schemas: [
247
- import_module_model2.ModuleStateSchema
248
- ]
249
- }).build();
250
- const boundWitnesses = await diviner.divine([
251
- query
252
- ]);
253
- if (boundWitnesses.length > 0) {
254
- const boundWitness = boundWitnesses[0];
255
- if ((0, import_boundwitness_model2.isBoundWitness)(boundWitness)) {
256
- hash = boundWitness.addresses.map((address, index) => ({
257
- address,
258
- index
259
- })).reduce((prev, curr) => {
260
- var _a2;
261
- return ((_a2 = boundWitness.payload_schemas) == null ? void 0 : _a2[curr == null ? void 0 : curr.index]) === import_module_model2.ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
262
- }, "");
263
- }
264
- }
265
- if (hash) {
266
- const archivist = await this.getArchivistForStore();
267
- const payload = (await archivist.get([
268
- hash
269
- ])).find(import_module_model2.isModuleState);
270
- if (payload) {
271
- return payload;
272
- }
273
- }
274
- return void 0;
275
- }
276
- }, __name(_a, "StatefulModuleBase"), _a);
277
- return StatefulModuleBase;
278
- }, "StatefulModuleMixin");
279
- // Annotate the CommonJS export names for ESM import in node:
280
- 0 && (module.exports = {
281
- StatefulDiviner,
282
- StatefulDivinerConfigSchema,
283
- StatefulDivinerSchema,
284
- StatefulModuleMixin
285
- });
286
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export * from './Config.ts'\nexport * from './Diviner.ts'\nexport * from './DivinerMixin.ts'\nexport * from './Params.ts'\nexport * from './Schema.ts'\n","export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import { DivinerConfig } from '@xyo-network/diviner-model'\nimport { 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 { 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 { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport { isModuleState, ModuleState, ModuleStateSchema, StateDictionary } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfigSchema } from './Config.ts'\nimport { 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 { 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 { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport {\n AnyConfigSchema,\n isModuleState,\n ModuleInstance,\n ModuleParams,\n ModuleState,\n ModuleStateSchema,\n StateDictionary,\n} from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { WithMeta } from '@xyo-network/payload-model'\n\nimport { 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":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACAO,IAAMA,wBAAwB;;;ACQ9B,IAAMC,8BAA8B,GAAGC,qBAAAA;;;ACR9C,oBAAyB;AAEzB,+BAAiC;AACjC,kCAAoC;AACpC,gCAA+B;AAC/B,8BAAgC;AAChC,wCAAgF;AAEhF,6BAA+B;AAC/B,0BAA+E;AAC/E,6BAA+B;AAM/B,IAAMC,aAAa;AAKZ,IAAeC,mBAAf,MAAeA,yBAUZC,wCAAAA;;;;EAOEC;;;;;;;;EASV,MAAgBC,YAAYC,WAA0C;AA/CxE;AAiDI,QAAIA,UAAUC,MAAMC,aAAW,UAAKJ,eAAL,mBAAiBG,MAAMC,QAAQ;AAC9D,SAAKJ,aAAaE;AAClB,UAAMG,YAAY,MAAM,KAAKC,0BAAyB;AACtD,UAAM,CAACC,EAAAA,IAAM,MAAM,IAAIC,gDAAAA,EAAsBC,QAAQ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AO,MAAM,GAAGjB,WAAAA,+CAAyD;AAEjH,YAAMuB,eAAWI,wCAAkBN,GAAAA;AACnC,iBAAOH,yBAASK,UAAU,MAAM,GAAGvB,WAAAA,mCAA6C;IAClF;;;;;IAMA,MAAM8B,gBAAoE;AACxE,UAAI,KAAK1B,WAAY,QAAO,KAAKA;AACjC,UAAI2B,OAAa;AACjB,YAAMC,UAAU,MAAM,KAAKP,+BAA8B;AACzD,YAAMQ,QAAQ,MAAM,IAAIC,uCAAgD;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,gBAAII,2CAAeD,YAAAA,GAAe;AAEhCd,iBAAOc,aAAaE,UACjBC,IAAI,CAACC,SAASC,WAAW;YAAED;YAASC;UAAM,EAAA,EAG1CC,OACC,CAACC,MAAMC,SAAAA;AAjIrB,gBAAA9C;AAiI+BsC,qBAAAA,MAAAA,aAAaL,oBAAbK,gBAAAA,IAA+BQ,6BAAMH,YAAWT,yCAAoBI,aAAaS,eAAeD,6BAAMH,KAAAA,IAASE;aAChI,EAAA;QAEN;MACF;AAGA,UAAIrB,MAAM;AAER,cAAMrB,YAAY,MAAM,KAAKC,qBAAoB;AACjD,cAAMG,WAAW,MAAMJ,UAAU6C,IAAI;UAACxB;SAAK,GAAGyB,KAAKC,kCAAAA;AACnD,YAAI3C,SAAS;AACX,iBAAOA;QACT;MACF;AACA,aAAO4C;IACT;EACF,GA1G0CxD,kCAA1C;AA2GA,SAAOC;AACT,GAnHmC;","names":["StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","moduleName","StatefulDiviner","AbstractDiviner","_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","configSchemas","StatefulDivinerConfigSchema","defaultConfigSchema","import_assert","import_boundwitness_builder","import_boundwitness_model","import_diviner_boundwitness_model","import_module_model","import_payload_builder","moduleName","StatefulModuleMixin","ModuleBase","StatefulModuleBase","_lastState","commitState","nextState","_a","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,6 +0,0 @@
1
- export * from './Config.ts';
2
- export * from './Diviner.ts';
3
- export * from './DivinerMixin.ts';
4
- export * from './Params.ts';
5
- export * from './Schema.ts';
6
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
@@ -1,6 +0,0 @@
1
- export * from './Config.ts';
2
- export * from './Diviner.ts';
3
- export * from './DivinerMixin.ts';
4
- export * from './Params.ts';
5
- export * from './Schema.ts';
6
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
@@ -1,6 +0,0 @@
1
- export * from './Config.ts';
2
- export * from './Diviner.ts';
3
- export * from './DivinerMixin.ts';
4
- export * from './Params.ts';
5
- export * from './Schema.ts';
6
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
@@ -1,258 +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/Schema.ts
10
- var StatefulDivinerSchema = "network.xyo.diviner.stateful";
11
-
12
- // src/Config.ts
13
- var StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config`;
14
-
15
- // src/Diviner.ts
16
- import { assertEx } from "@xylabs/assert";
17
- import { ArchivistWrapper } from "@xyo-network/archivist-wrapper";
18
- import { BoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
19
- import { isBoundWitness } from "@xyo-network/boundwitness-model";
20
- import { AbstractDiviner } from "@xyo-network/diviner-abstract";
21
- import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
22
- import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
23
- import { isModuleState, ModuleStateSchema } from "@xyo-network/module-model";
24
- import { PayloadBuilder } from "@xyo-network/payload-builder";
25
- var moduleName = "StatefulDiviner";
26
- var _StatefulDiviner = class _StatefulDiviner extends AbstractDiviner {
27
- /**
28
- * The last state
29
- */
30
- _lastState;
31
- /**
32
- * Commit the internal state of the Diviner process. This is similar
33
- * to a transaction completion in a database and should only be called
34
- * when results have been successfully persisted to the appropriate
35
- * external stores.
36
- * @param nextState The state to commit
37
- */
38
- async commitState(nextState) {
39
- var _a;
40
- if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset)) return;
41
- this._lastState = nextState;
42
- const archivist = await this.getArchivistForStateStore();
43
- const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build();
44
- await archivist.insert([
45
- bw,
46
- nextState
47
- ]);
48
- }
49
- /**
50
- * Retrieves the archivist for the specified store
51
- * @param store The store to retrieve the archivist for
52
- * @returns The archivist for the specified store
53
- */
54
- async getArchivistForStateStore() {
55
- var _a;
56
- const name = assertEx((_a = this.config) == null ? void 0 : _a.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
57
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
58
- return ArchivistWrapper.wrap(mod, this.account);
59
- }
60
- /**
61
- * Retrieves the BoundWitness Diviner for the specified store
62
- * @param store The store to retrieve the BoundWitness Diviner for
63
- * @returns The BoundWitness Diviner for the specified store
64
- */
65
- async getBoundWitnessDivinerForStateStore() {
66
- var _a;
67
- const name = assertEx((_a = this.config) == null ? void 0 : _a.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
68
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
69
- return DivinerWrapper.wrap(mod, this.account);
70
- }
71
- /**
72
- * Retrieves the Payload Diviner for the specified store
73
- * @param store The store to retrieve the Payload Diviner for
74
- * @returns The Payload Diviner for the specified store
75
- */
76
- async getPayloadDivinerForStateStore() {
77
- var _a, _b;
78
- const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`);
79
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
80
- return DivinerWrapper.wrap(mod, this.account);
81
- }
82
- /**
83
- * Retrieves the last state of the Diviner process. Used to recover state after
84
- * preemptions, reboots, etc.
85
- */
86
- async retrieveState() {
87
- if (this._lastState) return this._lastState;
88
- let hash = "";
89
- const diviner = await this.getBoundWitnessDivinerForStateStore();
90
- const query = await new PayloadBuilder({
91
- schema: BoundWitnessDivinerQuerySchema
92
- }).fields({
93
- address: this.account.address,
94
- limit: 1,
95
- offset: 0,
96
- order: "desc",
97
- payload_schemas: [
98
- ModuleStateSchema
99
- ]
100
- }).build();
101
- const boundWitnesses = await diviner.divine([
102
- query
103
- ]);
104
- if (boundWitnesses.length > 0) {
105
- const boundWitness = boundWitnesses[0];
106
- if (isBoundWitness(boundWitness)) {
107
- hash = boundWitness.addresses.map((address, index) => ({
108
- address,
109
- index
110
- })).filter(({ address }) => address === this.account.address).reduce((prev, curr) => {
111
- var _a;
112
- 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;
113
- }, "");
114
- }
115
- }
116
- if (hash) {
117
- const archivist = await this.getArchivistForStateStore();
118
- const payload = (await archivist.get([
119
- hash
120
- ])).find(isModuleState);
121
- if (payload) {
122
- return payload;
123
- }
124
- }
125
- return void 0;
126
- }
127
- };
128
- __name(_StatefulDiviner, "StatefulDiviner");
129
- __publicField(_StatefulDiviner, "configSchemas", [
130
- ...__superGet(_StatefulDiviner, _StatefulDiviner, "configSchemas"),
131
- StatefulDivinerConfigSchema
132
- ]);
133
- __publicField(_StatefulDiviner, "defaultConfigSchema", StatefulDivinerConfigSchema);
134
- var StatefulDiviner = _StatefulDiviner;
135
-
136
- // src/DivinerMixin.ts
137
- import { assertEx as assertEx2 } from "@xylabs/assert";
138
- import { asArchivistInstance } from "@xyo-network/archivist-model";
139
- import { BoundWitnessBuilder as BoundWitnessBuilder2 } from "@xyo-network/boundwitness-builder";
140
- import { isBoundWitness as isBoundWitness2 } from "@xyo-network/boundwitness-model";
141
- import { BoundWitnessDivinerQuerySchema as BoundWitnessDivinerQuerySchema2 } from "@xyo-network/diviner-boundwitness-model";
142
- import { asDivinerInstance } from "@xyo-network/diviner-model";
143
- import { isModuleState as isModuleState2, ModuleStateSchema as ModuleStateSchema2 } from "@xyo-network/module-model";
144
- import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
145
- var moduleName2 = "StatefulModuleMixin";
146
- var StatefulModuleMixin = /* @__PURE__ */ __name((ModuleBase) => {
147
- var _a;
148
- let StatefulModuleBase = (_a = class extends ModuleBase {
149
- _lastState;
150
- /**
151
- * Commit the internal state of the Diviner process. This is similar
152
- * to a transaction completion in a database and should only be called
153
- * when results have been successfully persisted to the appropriate
154
- * external stores.
155
- * @param nextState The state to commit
156
- */
157
- async commitState(nextState) {
158
- var _a2;
159
- if (nextState.state.offset === ((_a2 = this._lastState) == null ? void 0 : _a2.state.offset)) return;
160
- this._lastState = nextState;
161
- const archivist = await this.getArchivistForStore();
162
- const [bw] = await (await new BoundWitnessBuilder2().payload(nextState)).build();
163
- await archivist.insert([
164
- bw,
165
- nextState
166
- ]);
167
- }
168
- /**
169
- * Retrieves the archivist for the specified store
170
- * @param store The store to retrieve the archivist for
171
- * @returns The archivist for the specified store
172
- */
173
- async getArchivistForStore() {
174
- var _a2, _b;
175
- const name = assertEx2((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
176
- const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
177
- const instance = asArchivistInstance(mod);
178
- return assertEx2(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
179
- }
180
- /**
181
- * Retrieves the BoundWitness Diviner for the specified store
182
- * @param store The store to retrieve the BoundWitness Diviner for
183
- * @returns The BoundWitness Diviner for the specified store
184
- */
185
- async getBoundWitnessDivinerForStore() {
186
- var _a2, _b;
187
- const name = assertEx2((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.boundWitnessDiviner, () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`);
188
- const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
189
- const instance = asDivinerInstance(mod);
190
- return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
191
- }
192
- /**
193
- * Retrieves the Payload Diviner for the specified store
194
- * @param store The store to retrieve the Payload Diviner for
195
- * @returns The Payload Diviner for the specified store
196
- */
197
- async getPayloadDivinerForStateStore() {
198
- var _a2, _b;
199
- const name = assertEx2((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
200
- const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
201
- const instance = asDivinerInstance(mod);
202
- return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
203
- }
204
- /**
205
- * Retrieves the last state of the Diviner process. Used to recover state after
206
- * preemptions, reboots, etc.
207
- */
208
- async retrieveState() {
209
- if (this._lastState) return this._lastState;
210
- let hash = "";
211
- const diviner = await this.getBoundWitnessDivinerForStore();
212
- const query = await new PayloadBuilder2({
213
- schema: BoundWitnessDivinerQuerySchema2
214
- }).fields({
215
- // address: this.account.address,
216
- limit: 1,
217
- offset: 0,
218
- order: "desc",
219
- payload_schemas: [
220
- ModuleStateSchema2
221
- ]
222
- }).build();
223
- const boundWitnesses = await diviner.divine([
224
- query
225
- ]);
226
- if (boundWitnesses.length > 0) {
227
- const boundWitness = boundWitnesses[0];
228
- if (isBoundWitness2(boundWitness)) {
229
- hash = boundWitness.addresses.map((address, index) => ({
230
- address,
231
- index
232
- })).reduce((prev, curr) => {
233
- var _a2;
234
- return ((_a2 = boundWitness.payload_schemas) == null ? void 0 : _a2[curr == null ? void 0 : curr.index]) === ModuleStateSchema2 ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
235
- }, "");
236
- }
237
- }
238
- if (hash) {
239
- const archivist = await this.getArchivistForStore();
240
- const payload = (await archivist.get([
241
- hash
242
- ])).find(isModuleState2);
243
- if (payload) {
244
- return payload;
245
- }
246
- }
247
- return void 0;
248
- }
249
- }, __name(_a, "StatefulModuleBase"), _a);
250
- return StatefulModuleBase;
251
- }, "StatefulModuleMixin");
252
- export {
253
- StatefulDiviner,
254
- StatefulDivinerConfigSchema,
255
- StatefulDivinerSchema,
256
- StatefulModuleMixin
257
- };
258
- //# sourceMappingURL=index.mjs.map