@xyo-network/diviner-stateful 2.84.19 → 2.85.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.
- package/dist/browser/DivinerMixin.d.cts +0 -28
- package/dist/browser/DivinerMixin.d.cts.map +1 -1
- package/dist/browser/DivinerMixin.d.mts +0 -28
- package/dist/browser/DivinerMixin.d.mts.map +1 -1
- package/dist/browser/DivinerMixin.d.ts +0 -28
- package/dist/browser/DivinerMixin.d.ts.map +1 -1
- package/dist/browser/index.cjs +100 -71
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +103 -75
- package/dist/browser/index.js.map +1 -1
- package/dist/node/DivinerMixin.d.cts +0 -28
- package/dist/node/DivinerMixin.d.cts.map +1 -1
- package/dist/node/DivinerMixin.d.mts +0 -28
- package/dist/node/DivinerMixin.d.mts.map +1 -1
- package/dist/node/DivinerMixin.d.ts +0 -28
- package/dist/node/DivinerMixin.d.ts.map +1 -1
- package/dist/node/index.cjs +116 -85
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +119 -89
- package/dist/node/index.js.map +1 -1
- package/package.json +18 -18
package/dist/browser/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
7
|
var __export = (target, all) => {
|
|
7
8
|
for (var name in all)
|
|
8
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -46,85 +47,102 @@ var import_module_model = require("@xyo-network/module-model");
|
|
|
46
47
|
var import_payload_builder = require("@xyo-network/payload-builder");
|
|
47
48
|
var moduleName = "StatefulDiviner";
|
|
48
49
|
var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
|
|
49
|
-
static
|
|
50
|
+
static {
|
|
51
|
+
__name(this, "StatefulDiviner");
|
|
52
|
+
}
|
|
53
|
+
static configSchemas = [
|
|
54
|
+
import_diviner_model.DivinerConfigSchema,
|
|
55
|
+
StatefulDivinerConfigSchema
|
|
56
|
+
];
|
|
50
57
|
/**
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
* The last state
|
|
59
|
+
*/
|
|
53
60
|
_lastState;
|
|
54
61
|
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
* Commit the internal state of the Diviner process. This is similar
|
|
63
|
+
* to a transaction completion in a database and should only be called
|
|
64
|
+
* when results have been successfully persisted to the appropriate
|
|
65
|
+
* external stores.
|
|
66
|
+
* @param nextState The state to commit
|
|
67
|
+
*/
|
|
61
68
|
async commitState(nextState) {
|
|
62
69
|
if (nextState.state.offset === this._lastState?.state.offset)
|
|
63
70
|
return;
|
|
64
71
|
this._lastState = nextState;
|
|
65
72
|
const archivist = await this.getArchivistForStateStore();
|
|
66
73
|
const [bw] = await new import_boundwitness_builder.BoundWitnessBuilder().payload(nextState).witness(this.account).build();
|
|
67
|
-
await archivist.insert([
|
|
74
|
+
await archivist.insert([
|
|
75
|
+
bw,
|
|
76
|
+
nextState
|
|
77
|
+
]);
|
|
68
78
|
}
|
|
69
79
|
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
80
|
+
* Retrieves the archivist for the specified store
|
|
81
|
+
* @param store The store to retrieve the archivist for
|
|
82
|
+
* @returns The archivist for the specified store
|
|
83
|
+
*/
|
|
74
84
|
async getArchivistForStateStore() {
|
|
75
85
|
const name = (0, import_assert.assertEx)(this.config?.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
|
|
76
86
|
const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
|
|
77
87
|
return import_archivist_wrapper.ArchivistWrapper.wrap(mod, this.account);
|
|
78
88
|
}
|
|
79
89
|
/**
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
90
|
+
* Retrieves the BoundWitness Diviner for the specified store
|
|
91
|
+
* @param store The store to retrieve the BoundWitness Diviner for
|
|
92
|
+
* @returns The BoundWitness Diviner for the specified store
|
|
93
|
+
*/
|
|
84
94
|
async getBoundWitnessDivinerForStateStore() {
|
|
85
95
|
const name = (0, import_assert.assertEx)(this.config?.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
|
|
86
96
|
const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
|
|
87
97
|
return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
|
|
88
98
|
}
|
|
89
99
|
/**
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
100
|
+
* Retrieves the Payload Diviner for the specified store
|
|
101
|
+
* @param store The store to retrieve the Payload Diviner for
|
|
102
|
+
* @returns The Payload Diviner for the specified store
|
|
103
|
+
*/
|
|
94
104
|
async getPayloadDivinerForStateStore() {
|
|
95
105
|
const name = (0, import_assert.assertEx)(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`);
|
|
96
106
|
const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
|
|
97
107
|
return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
|
|
98
108
|
}
|
|
99
109
|
/**
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
110
|
+
* Retrieves the last state of the Diviner process. Used to recover state after
|
|
111
|
+
* preemptions, reboots, etc.
|
|
112
|
+
*/
|
|
103
113
|
async retrieveState() {
|
|
104
114
|
if (this._lastState)
|
|
105
115
|
return this._lastState;
|
|
106
116
|
let hash = "";
|
|
107
117
|
const diviner = await this.getBoundWitnessDivinerForStateStore();
|
|
108
|
-
const query = await new import_payload_builder.PayloadBuilder({
|
|
118
|
+
const query = await new import_payload_builder.PayloadBuilder({
|
|
119
|
+
schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema
|
|
120
|
+
}).fields({
|
|
109
121
|
address: this.account.address,
|
|
110
122
|
limit: 1,
|
|
111
123
|
offset: 0,
|
|
112
124
|
order: "desc",
|
|
113
|
-
payload_schemas: [
|
|
125
|
+
payload_schemas: [
|
|
126
|
+
import_module_model.ModuleStateSchema
|
|
127
|
+
]
|
|
114
128
|
}).build();
|
|
115
|
-
const boundWitnesses = await diviner.divine([
|
|
129
|
+
const boundWitnesses = await diviner.divine([
|
|
130
|
+
query
|
|
131
|
+
]);
|
|
116
132
|
if (boundWitnesses.length > 0) {
|
|
117
133
|
const boundWitness = boundWitnesses[0];
|
|
118
134
|
if ((0, import_boundwitness_model.isBoundWitness)(boundWitness)) {
|
|
119
|
-
hash = boundWitness.addresses.map((address, index) => ({
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
);
|
|
135
|
+
hash = boundWitness.addresses.map((address, index) => ({
|
|
136
|
+
address,
|
|
137
|
+
index
|
|
138
|
+
})).filter(({ address }) => address === this.account.address).reduce((prev, curr) => boundWitness.payload_schemas?.[curr?.index] === import_module_model.ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev, "");
|
|
123
139
|
}
|
|
124
140
|
}
|
|
125
141
|
if (hash) {
|
|
126
142
|
const archivist = await this.getArchivistForStateStore();
|
|
127
|
-
const payload = (await archivist.get([
|
|
143
|
+
const payload = (await archivist.get([
|
|
144
|
+
hash
|
|
145
|
+
])).find(import_module_model.isModuleState);
|
|
128
146
|
if (payload) {
|
|
129
147
|
return payload;
|
|
130
148
|
}
|
|
@@ -143,29 +161,35 @@ var import_diviner_model2 = require("@xyo-network/diviner-model");
|
|
|
143
161
|
var import_module_model2 = require("@xyo-network/module-model");
|
|
144
162
|
var import_payload_builder2 = require("@xyo-network/payload-builder");
|
|
145
163
|
var moduleName2 = "StatefulModuleMixin";
|
|
146
|
-
var StatefulModuleMixin = (ModuleBase) => {
|
|
147
|
-
class StatefulModuleBase extends ModuleBase {
|
|
164
|
+
var StatefulModuleMixin = /* @__PURE__ */ __name((ModuleBase) => {
|
|
165
|
+
let StatefulModuleBase = class StatefulModuleBase extends ModuleBase {
|
|
166
|
+
static {
|
|
167
|
+
__name(this, "StatefulModuleBase");
|
|
168
|
+
}
|
|
148
169
|
_lastState;
|
|
149
170
|
/**
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
171
|
+
* Commit the internal state of the Diviner process. This is similar
|
|
172
|
+
* to a transaction completion in a database and should only be called
|
|
173
|
+
* when results have been successfully persisted to the appropriate
|
|
174
|
+
* external stores.
|
|
175
|
+
* @param nextState The state to commit
|
|
176
|
+
*/
|
|
156
177
|
async commitState(nextState) {
|
|
157
178
|
if (nextState.state.offset === this._lastState?.state.offset)
|
|
158
179
|
return;
|
|
159
180
|
this._lastState = nextState;
|
|
160
181
|
const archivist = await this.getArchivistForStore();
|
|
161
182
|
const [bw] = await new import_boundwitness_builder2.BoundWitnessBuilder().payload(nextState).build();
|
|
162
|
-
await archivist.insert([
|
|
183
|
+
await archivist.insert([
|
|
184
|
+
bw,
|
|
185
|
+
nextState
|
|
186
|
+
]);
|
|
163
187
|
}
|
|
164
188
|
/**
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
189
|
+
* Retrieves the archivist for the specified store
|
|
190
|
+
* @param store The store to retrieve the archivist for
|
|
191
|
+
* @returns The archivist for the specified store
|
|
192
|
+
*/
|
|
169
193
|
async getArchivistForStore() {
|
|
170
194
|
const name = (0, import_assert2.assertEx)(this.config?.stateStore?.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
|
|
171
195
|
const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
|
|
@@ -173,24 +197,21 @@ var StatefulModuleMixin = (ModuleBase) => {
|
|
|
173
197
|
return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
|
|
174
198
|
}
|
|
175
199
|
/**
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
200
|
+
* Retrieves the BoundWitness Diviner for the specified store
|
|
201
|
+
* @param store The store to retrieve the BoundWitness Diviner for
|
|
202
|
+
* @returns The BoundWitness Diviner for the specified store
|
|
203
|
+
*/
|
|
180
204
|
async getBoundWitnessDivinerForStore() {
|
|
181
|
-
const name = (0, import_assert2.assertEx)(
|
|
182
|
-
this.config?.stateStore?.boundWitnessDiviner,
|
|
183
|
-
() => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`
|
|
184
|
-
);
|
|
205
|
+
const name = (0, import_assert2.assertEx)(this.config?.stateStore?.boundWitnessDiviner, () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`);
|
|
185
206
|
const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
|
|
186
207
|
const instance = (0, import_diviner_model2.asDivinerInstance)(mod);
|
|
187
208
|
return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
|
|
188
209
|
}
|
|
189
210
|
/**
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
211
|
+
* Retrieves the Payload Diviner for the specified store
|
|
212
|
+
* @param store The store to retrieve the Payload Diviner for
|
|
213
|
+
* @returns The Payload Diviner for the specified store
|
|
214
|
+
*/
|
|
194
215
|
async getPayloadDivinerForStateStore() {
|
|
195
216
|
const name = (0, import_assert2.assertEx)(this.config?.stateStore?.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
|
|
196
217
|
const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
|
|
@@ -198,41 +219,49 @@ var StatefulModuleMixin = (ModuleBase) => {
|
|
|
198
219
|
return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
|
|
199
220
|
}
|
|
200
221
|
/**
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
222
|
+
* Retrieves the last state of the Diviner process. Used to recover state after
|
|
223
|
+
* preemptions, reboots, etc.
|
|
224
|
+
*/
|
|
204
225
|
async retrieveState() {
|
|
205
226
|
if (this._lastState)
|
|
206
227
|
return this._lastState;
|
|
207
228
|
let hash = "";
|
|
208
229
|
const diviner = await this.getBoundWitnessDivinerForStore();
|
|
209
|
-
const query = await new import_payload_builder2.PayloadBuilder({
|
|
230
|
+
const query = await new import_payload_builder2.PayloadBuilder({
|
|
231
|
+
schema: import_diviner_boundwitness_model2.BoundWitnessDivinerQuerySchema
|
|
232
|
+
}).fields({
|
|
210
233
|
// address: this.account.address,
|
|
211
234
|
limit: 1,
|
|
212
235
|
offset: 0,
|
|
213
236
|
order: "desc",
|
|
214
|
-
payload_schemas: [
|
|
237
|
+
payload_schemas: [
|
|
238
|
+
import_module_model2.ModuleStateSchema
|
|
239
|
+
]
|
|
215
240
|
}).build();
|
|
216
|
-
const boundWitnesses = await diviner.divine([
|
|
241
|
+
const boundWitnesses = await diviner.divine([
|
|
242
|
+
query
|
|
243
|
+
]);
|
|
217
244
|
if (boundWitnesses.length > 0) {
|
|
218
245
|
const boundWitness = boundWitnesses[0];
|
|
219
246
|
if ((0, import_boundwitness_model2.isBoundWitness)(boundWitness)) {
|
|
220
|
-
hash = boundWitness.addresses.map((address, index) => ({
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
);
|
|
247
|
+
hash = boundWitness.addresses.map((address, index) => ({
|
|
248
|
+
address,
|
|
249
|
+
index
|
|
250
|
+
})).reduce((prev, curr) => boundWitness.payload_schemas?.[curr?.index] === import_module_model2.ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev, "");
|
|
224
251
|
}
|
|
225
252
|
}
|
|
226
253
|
if (hash) {
|
|
227
254
|
const archivist = await this.getArchivistForStore();
|
|
228
|
-
const payload = (await archivist.get([
|
|
255
|
+
const payload = (await archivist.get([
|
|
256
|
+
hash
|
|
257
|
+
])).find(import_module_model2.isModuleState);
|
|
229
258
|
if (payload) {
|
|
230
259
|
return payload;
|
|
231
260
|
}
|
|
232
261
|
}
|
|
233
262
|
return void 0;
|
|
234
263
|
}
|
|
235
|
-
}
|
|
264
|
+
};
|
|
236
265
|
return StatefulModuleBase;
|
|
237
|
-
};
|
|
266
|
+
}, "StatefulModuleMixin");
|
|
238
267
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './DivinerMixin'\nexport * from './Params'\nexport * from './Schema'\n","export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { StatefulDivinerSchema } from './Schema'\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: string\n boundWitnessDiviner: string\n payloadDiviner: string\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport { AbstractDiviner } from '@xyo-network/abstract-diviner'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerConfigSchema, DivinerModule, 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 } from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfigSchema } from './Config'\nimport { StatefulDivinerParams } from './Params'\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<DivinerModule<TParams>, TIn, TOut> = DivinerModuleEventData<DivinerModule<TParams>, TIn, TOut>,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: string[] = [DivinerConfigSchema, 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).witness(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: string = ''\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 '',\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\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { asArchivistInstance } from '@xyo-network/archivist'\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'\n\nimport { StatefulDivinerConfig } from './Config'\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).witness(this.account).build()\n const [bw] = 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 * 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 * 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: string = ''\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 '',\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\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,wBAAwB;;;ACO9B,IAAM,8BAA8B,GAAG,qBAAqB;;;ACPnE,oBAAyB;AACzB,8BAAgC;AAChC,+BAAiC;AACjC,kCAAoC;AACpC,gCAA+B;AAC/B,wCAAgF;AAChF,2BAA2E;AAC3E,6BAA+B;AAC/B,0BAA+E;AAC/E,6BAA+B;AAM/B,IAAM,aAAa;AAKZ,IAAe,kBAAf,cAMG,wCAAgD;AAAA,EACxD,OAAyB,gBAA0B,CAAC,0CAAqB,2BAA2B;AAAA;AAAA;AAAA;AAAA,EAK1F;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASV,MAAgB,YAAY,WAAgC;AAE1D,QAAI,UAAU,MAAM,WAAW,KAAK,YAAY,MAAM;AAAQ;AAC9D,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AAI,KAAK;AAAY,eAAO,KAAK;AACjC,UAAI,OAAe;AACnB,YAAM,UAAU,MAAM,KAAK,+BAA+B;AAC1D,YAAM,QAAQ,MAAM,IAAI,uCAAgD,EAAE,QAAQ,kEAA+B,CAAC,EAC/G,OAAO;AAAA;AAAA,QAEN,OAAO;AAAA,QACP,QAAQ;AAAA,QACR,OAAO;AAAA,QACP,iBAAiB,CAAC,sCAAiB;AAAA,MACrC,CAAC,EACA,MAAM;AACT,YAAM,iBAAiB,MAAM,QAAQ,OAAO,CAAC,KAAK,CAAC;AACnD,UAAI,eAAe,SAAS,GAAG;AAC7B,cAAM,eAAe,eAAe,CAAC;AACrC,gBAAI,2CAAe,YAAY,GAAG;AAEhC,iBAAO,aAAa,UACjB,IAAI,CAAC,SAAS,WAAW,EAAE,SAAS,MAAM,EAAE,EAG5C;AAAA,YACC,CAAC,MAAM,SAAU,aAAa,kBAAkB,MAAM,KAAK,MAAM,yCAAoB,aAAa,eAAe,MAAM,KAAK,IAAI;AAAA,YAChI;AAAA,UACF;AAAA,QACJ;AAAA,MACF;AAGA,UAAI,MAAM;AAER,cAAM,YAAY,MAAM,KAAK,qBAAqB;AAClD,cAAM,WAAW,MAAM,UAAU,IAAI,CAAC,IAAI,CAAC,GAAG,KAAK,kCAAqB;AACxE,YAAI,SAAS;AACX,iBAAO;AAAA,QACT;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["import_assert","import_boundwitness_builder","import_boundwitness_model","import_diviner_boundwitness_model","import_diviner_model","import_module_model","import_payload_builder","moduleName"]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './DivinerMixin'\nexport * from './Params'\nexport * from './Schema'\n","export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { StatefulDivinerSchema } from './Schema'\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: string\n boundWitnessDiviner: string\n payloadDiviner: string\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport { AbstractDiviner } from '@xyo-network/abstract-diviner'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerConfigSchema, DivinerModule, 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 } from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfigSchema } from './Config'\nimport { StatefulDivinerParams } from './Params'\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<DivinerModule<TParams>, TIn, TOut> = DivinerModuleEventData<DivinerModule<TParams>, TIn, TOut>,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: string[] = [DivinerConfigSchema, 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).witness(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: string = ''\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 '',\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\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { asArchivistInstance } from '@xyo-network/archivist'\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'\n\nimport { StatefulDivinerConfig } from './Config'\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).witness(this.account).build()\n const [bw] = 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 * 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 * 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: string = ''\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 '',\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\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;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":["StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","moduleName","StatefulDiviner","AbstractDiviner","configSchemas","DivinerConfigSchema","StatefulDivinerConfigSchema","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStateStore","bw","BoundWitnessBuilder","payload","witness","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","import_assert","import_boundwitness_builder","import_boundwitness_model","import_diviner_boundwitness_model","import_diviner_model","import_module_model","import_payload_builder","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"]}
|