@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/node/index.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __publicField = (obj, key, value) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
8
|
+
|
|
1
9
|
// src/Schema.ts
|
|
2
10
|
var StatefulDivinerSchema = "network.xyo.diviner.stateful";
|
|
3
11
|
|
|
@@ -16,19 +24,18 @@ import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
|
|
|
16
24
|
import { isModuleState, ModuleStateSchema } from "@xyo-network/module-model";
|
|
17
25
|
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
18
26
|
var moduleName = "StatefulDiviner";
|
|
19
|
-
var
|
|
20
|
-
static configSchemas = [DivinerConfigSchema, StatefulDivinerConfigSchema];
|
|
27
|
+
var _StatefulDiviner = class _StatefulDiviner extends AbstractDiviner {
|
|
21
28
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
* The last state
|
|
30
|
+
*/
|
|
24
31
|
_lastState;
|
|
25
32
|
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
* Commit the internal state of the Diviner process. This is similar
|
|
34
|
+
* to a transaction completion in a database and should only be called
|
|
35
|
+
* when results have been successfully persisted to the appropriate
|
|
36
|
+
* external stores.
|
|
37
|
+
* @param nextState The state to commit
|
|
38
|
+
*/
|
|
32
39
|
async commitState(nextState) {
|
|
33
40
|
var _a;
|
|
34
41
|
if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset))
|
|
@@ -36,13 +43,16 @@ var StatefulDiviner = class extends AbstractDiviner {
|
|
|
36
43
|
this._lastState = nextState;
|
|
37
44
|
const archivist = await this.getArchivistForStateStore();
|
|
38
45
|
const [bw] = await new BoundWitnessBuilder().payload(nextState).witness(this.account).build();
|
|
39
|
-
await archivist.insert([
|
|
46
|
+
await archivist.insert([
|
|
47
|
+
bw,
|
|
48
|
+
nextState
|
|
49
|
+
]);
|
|
40
50
|
}
|
|
41
51
|
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
* Retrieves the archivist for the specified store
|
|
53
|
+
* @param store The store to retrieve the archivist for
|
|
54
|
+
* @returns The archivist for the specified store
|
|
55
|
+
*/
|
|
46
56
|
async getArchivistForStateStore() {
|
|
47
57
|
var _a;
|
|
48
58
|
const name = assertEx((_a = this.config) == null ? void 0 : _a.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
|
|
@@ -50,10 +60,10 @@ var StatefulDiviner = class extends AbstractDiviner {
|
|
|
50
60
|
return ArchivistWrapper.wrap(mod, this.account);
|
|
51
61
|
}
|
|
52
62
|
/**
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
63
|
+
* Retrieves the BoundWitness Diviner for the specified store
|
|
64
|
+
* @param store The store to retrieve the BoundWitness Diviner for
|
|
65
|
+
* @returns The BoundWitness Diviner for the specified store
|
|
66
|
+
*/
|
|
57
67
|
async getBoundWitnessDivinerForStateStore() {
|
|
58
68
|
var _a;
|
|
59
69
|
const name = assertEx((_a = this.config) == null ? void 0 : _a.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
|
|
@@ -61,10 +71,10 @@ var StatefulDiviner = class extends AbstractDiviner {
|
|
|
61
71
|
return DivinerWrapper.wrap(mod, this.account);
|
|
62
72
|
}
|
|
63
73
|
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
* Retrieves the Payload Diviner for the specified store
|
|
75
|
+
* @param store The store to retrieve the Payload Diviner for
|
|
76
|
+
* @returns The Payload Diviner for the specified store
|
|
77
|
+
*/
|
|
68
78
|
async getPayloadDivinerForStateStore() {
|
|
69
79
|
var _a, _b;
|
|
70
80
|
const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`);
|
|
@@ -72,37 +82,45 @@ var StatefulDiviner = class extends AbstractDiviner {
|
|
|
72
82
|
return DivinerWrapper.wrap(mod, this.account);
|
|
73
83
|
}
|
|
74
84
|
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
85
|
+
* Retrieves the last state of the Diviner process. Used to recover state after
|
|
86
|
+
* preemptions, reboots, etc.
|
|
87
|
+
*/
|
|
78
88
|
async retrieveState() {
|
|
79
89
|
if (this._lastState)
|
|
80
90
|
return this._lastState;
|
|
81
91
|
let hash = "";
|
|
82
92
|
const diviner = await this.getBoundWitnessDivinerForStateStore();
|
|
83
|
-
const query = await new PayloadBuilder({
|
|
93
|
+
const query = await new PayloadBuilder({
|
|
94
|
+
schema: BoundWitnessDivinerQuerySchema
|
|
95
|
+
}).fields({
|
|
84
96
|
address: this.account.address,
|
|
85
97
|
limit: 1,
|
|
86
98
|
offset: 0,
|
|
87
99
|
order: "desc",
|
|
88
|
-
payload_schemas: [
|
|
100
|
+
payload_schemas: [
|
|
101
|
+
ModuleStateSchema
|
|
102
|
+
]
|
|
89
103
|
}).build();
|
|
90
|
-
const boundWitnesses = await diviner.divine([
|
|
104
|
+
const boundWitnesses = await diviner.divine([
|
|
105
|
+
query
|
|
106
|
+
]);
|
|
91
107
|
if (boundWitnesses.length > 0) {
|
|
92
108
|
const boundWitness = boundWitnesses[0];
|
|
93
109
|
if (isBoundWitness(boundWitness)) {
|
|
94
|
-
hash = boundWitness.addresses.map((address, index) => ({
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
);
|
|
110
|
+
hash = boundWitness.addresses.map((address, index) => ({
|
|
111
|
+
address,
|
|
112
|
+
index
|
|
113
|
+
})).filter(({ address }) => address === this.account.address).reduce((prev, curr) => {
|
|
114
|
+
var _a;
|
|
115
|
+
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;
|
|
116
|
+
}, "");
|
|
101
117
|
}
|
|
102
118
|
}
|
|
103
119
|
if (hash) {
|
|
104
120
|
const archivist = await this.getArchivistForStateStore();
|
|
105
|
-
const payload = (await archivist.get([
|
|
121
|
+
const payload = (await archivist.get([
|
|
122
|
+
hash
|
|
123
|
+
])).find(isModuleState);
|
|
106
124
|
if (payload) {
|
|
107
125
|
return payload;
|
|
108
126
|
}
|
|
@@ -110,6 +128,12 @@ var StatefulDiviner = class extends AbstractDiviner {
|
|
|
110
128
|
return void 0;
|
|
111
129
|
}
|
|
112
130
|
};
|
|
131
|
+
__name(_StatefulDiviner, "StatefulDiviner");
|
|
132
|
+
__publicField(_StatefulDiviner, "configSchemas", [
|
|
133
|
+
DivinerConfigSchema,
|
|
134
|
+
StatefulDivinerConfigSchema
|
|
135
|
+
]);
|
|
136
|
+
var StatefulDiviner = _StatefulDiviner;
|
|
113
137
|
|
|
114
138
|
// src/DivinerMixin.ts
|
|
115
139
|
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
@@ -118,111 +142,117 @@ import { BoundWitnessBuilder as BoundWitnessBuilder2 } from "@xyo-network/boundw
|
|
|
118
142
|
import { isBoundWitness as isBoundWitness2 } from "@xyo-network/boundwitness-model";
|
|
119
143
|
import { BoundWitnessDivinerQuerySchema as BoundWitnessDivinerQuerySchema2 } from "@xyo-network/diviner-boundwitness-model";
|
|
120
144
|
import { asDivinerInstance } from "@xyo-network/diviner-model";
|
|
121
|
-
import {
|
|
122
|
-
isModuleState as isModuleState2,
|
|
123
|
-
ModuleStateSchema as ModuleStateSchema2
|
|
124
|
-
} from "@xyo-network/module-model";
|
|
145
|
+
import { isModuleState as isModuleState2, ModuleStateSchema as ModuleStateSchema2 } from "@xyo-network/module-model";
|
|
125
146
|
import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
|
|
126
147
|
var moduleName2 = "StatefulModuleMixin";
|
|
127
|
-
var StatefulModuleMixin = (ModuleBase) => {
|
|
128
|
-
|
|
148
|
+
var StatefulModuleMixin = /* @__PURE__ */ __name((ModuleBase) => {
|
|
149
|
+
var _a;
|
|
150
|
+
let StatefulModuleBase = (_a = class extends ModuleBase {
|
|
129
151
|
_lastState;
|
|
130
152
|
/**
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
153
|
+
* Commit the internal state of the Diviner process. This is similar
|
|
154
|
+
* to a transaction completion in a database and should only be called
|
|
155
|
+
* when results have been successfully persisted to the appropriate
|
|
156
|
+
* external stores.
|
|
157
|
+
* @param nextState The state to commit
|
|
158
|
+
*/
|
|
137
159
|
async commitState(nextState) {
|
|
138
|
-
var
|
|
139
|
-
if (nextState.state.offset === ((
|
|
160
|
+
var _a2;
|
|
161
|
+
if (nextState.state.offset === ((_a2 = this._lastState) == null ? void 0 : _a2.state.offset))
|
|
140
162
|
return;
|
|
141
163
|
this._lastState = nextState;
|
|
142
164
|
const archivist = await this.getArchivistForStore();
|
|
143
165
|
const [bw] = await new BoundWitnessBuilder2().payload(nextState).build();
|
|
144
|
-
await archivist.insert([
|
|
166
|
+
await archivist.insert([
|
|
167
|
+
bw,
|
|
168
|
+
nextState
|
|
169
|
+
]);
|
|
145
170
|
}
|
|
146
171
|
/**
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
172
|
+
* Retrieves the archivist for the specified store
|
|
173
|
+
* @param store The store to retrieve the archivist for
|
|
174
|
+
* @returns The archivist for the specified store
|
|
175
|
+
*/
|
|
151
176
|
async getArchivistForStore() {
|
|
152
|
-
var
|
|
153
|
-
const name = assertEx2((_b = (
|
|
177
|
+
var _a2, _b;
|
|
178
|
+
const name = assertEx2((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
|
|
154
179
|
const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
|
|
155
180
|
const instance = asArchivistInstance(mod);
|
|
156
181
|
return assertEx2(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
|
|
157
182
|
}
|
|
158
183
|
/**
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
184
|
+
* Retrieves the BoundWitness Diviner for the specified store
|
|
185
|
+
* @param store The store to retrieve the BoundWitness Diviner for
|
|
186
|
+
* @returns The BoundWitness Diviner for the specified store
|
|
187
|
+
*/
|
|
163
188
|
async getBoundWitnessDivinerForStore() {
|
|
164
|
-
var
|
|
165
|
-
const name = assertEx2(
|
|
166
|
-
(_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.boundWitnessDiviner,
|
|
167
|
-
() => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`
|
|
168
|
-
);
|
|
189
|
+
var _a2, _b;
|
|
190
|
+
const name = assertEx2((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.boundWitnessDiviner, () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`);
|
|
169
191
|
const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
|
|
170
192
|
const instance = asDivinerInstance(mod);
|
|
171
193
|
return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
|
|
172
194
|
}
|
|
173
195
|
/**
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
196
|
+
* Retrieves the Payload Diviner for the specified store
|
|
197
|
+
* @param store The store to retrieve the Payload Diviner for
|
|
198
|
+
* @returns The Payload Diviner for the specified store
|
|
199
|
+
*/
|
|
178
200
|
async getPayloadDivinerForStateStore() {
|
|
179
|
-
var
|
|
180
|
-
const name = assertEx2((_b = (
|
|
201
|
+
var _a2, _b;
|
|
202
|
+
const name = assertEx2((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
|
|
181
203
|
const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
|
|
182
204
|
const instance = asDivinerInstance(mod);
|
|
183
205
|
return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
|
|
184
206
|
}
|
|
185
207
|
/**
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
208
|
+
* Retrieves the last state of the Diviner process. Used to recover state after
|
|
209
|
+
* preemptions, reboots, etc.
|
|
210
|
+
*/
|
|
189
211
|
async retrieveState() {
|
|
190
212
|
if (this._lastState)
|
|
191
213
|
return this._lastState;
|
|
192
214
|
let hash = "";
|
|
193
215
|
const diviner = await this.getBoundWitnessDivinerForStore();
|
|
194
|
-
const query = await new PayloadBuilder2({
|
|
216
|
+
const query = await new PayloadBuilder2({
|
|
217
|
+
schema: BoundWitnessDivinerQuerySchema2
|
|
218
|
+
}).fields({
|
|
195
219
|
// address: this.account.address,
|
|
196
220
|
limit: 1,
|
|
197
221
|
offset: 0,
|
|
198
222
|
order: "desc",
|
|
199
|
-
payload_schemas: [
|
|
223
|
+
payload_schemas: [
|
|
224
|
+
ModuleStateSchema2
|
|
225
|
+
]
|
|
200
226
|
}).build();
|
|
201
|
-
const boundWitnesses = await diviner.divine([
|
|
227
|
+
const boundWitnesses = await diviner.divine([
|
|
228
|
+
query
|
|
229
|
+
]);
|
|
202
230
|
if (boundWitnesses.length > 0) {
|
|
203
231
|
const boundWitness = boundWitnesses[0];
|
|
204
232
|
if (isBoundWitness2(boundWitness)) {
|
|
205
|
-
hash = boundWitness.addresses.map((address, index) => ({
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
);
|
|
233
|
+
hash = boundWitness.addresses.map((address, index) => ({
|
|
234
|
+
address,
|
|
235
|
+
index
|
|
236
|
+
})).reduce((prev, curr) => {
|
|
237
|
+
var _a2;
|
|
238
|
+
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;
|
|
239
|
+
}, "");
|
|
212
240
|
}
|
|
213
241
|
}
|
|
214
242
|
if (hash) {
|
|
215
243
|
const archivist = await this.getArchivistForStore();
|
|
216
|
-
const payload = (await archivist.get([
|
|
244
|
+
const payload = (await archivist.get([
|
|
245
|
+
hash
|
|
246
|
+
])).find(isModuleState2);
|
|
217
247
|
if (payload) {
|
|
218
248
|
return payload;
|
|
219
249
|
}
|
|
220
250
|
}
|
|
221
251
|
return void 0;
|
|
222
252
|
}
|
|
223
|
-
}
|
|
253
|
+
}, __name(_a, "StatefulModuleBase"), _a);
|
|
224
254
|
return StatefulModuleBase;
|
|
225
|
-
};
|
|
255
|
+
}, "StatefulModuleMixin");
|
|
226
256
|
export {
|
|
227
257
|
StatefulDiviner,
|
|
228
258
|
StatefulDivinerConfigSchema,
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import { 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":";AAAO,IAAM,wBAAwB;;;ACO9B,IAAM,8BAA8B,GAAG,qBAAqB;;;ACPnE,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,wBAAwB;AACjC,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAC/B,SAA0C,sCAAsC;AAChF,SAAS,2BAAkE;AAC3E,SAAS,sBAAsB;AAC/B,SAAS,eAA4B,yBAA0C;AAC/E,SAAS,sBAAsB;AAM/B,IAAM,aAAa;AAKZ,IAAe,kBAAf,cAMG,gBAAgD;AAAA,EACxD,OAAyB,gBAA0B,CAAC,qBAAqB,2BAA2B;AAAA;AAAA;AAAA;AAAA,EAK1F;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASV,MAAgB,YAAY,WAAgC;AAzC9D;AA2CI,QAAI,UAAU,MAAM,aAAW,UAAK,eAAL,mBAAiB,MAAM;AAAQ;AAC9D,SAAK,aAAa;AAClB,UAAM,YAAY,MAAM,KAAK,0BAA0B;AACvD,UAAM,CAAC,EAAE,IAAI,MAAM,IAAI,oBAAoB,EAAE,QAAQ,SAAS,EAAE,QAAQ,KA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U,MAAM,GAAGO,WAAU,mCAAmC;AAAA,IAClF;AAAA;AAAA;AAAA;AAAA;AAAA,IAKA,MAAM,gBAA0D;AAC9D,UAAI,KAAK;AAAY,eAAO,KAAK;AACjC,UAAI,OAAe;AACnB,YAAM,UAAU,MAAM,KAAK,+BAA+B;AAC1D,YAAM,QAAQ,MAAM,IAAID,gBAAgD,EAAE,QAAQH,gCAA+B,CAAC,EAC/G,OAAO;AAAA;AAAA,QAEN,OAAO;AAAA,QACP,QAAQ;AAAA,QACR,OAAO;AAAA,QACP,iBAAiB,CAACE,kBAAiB;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,YAAIH,gBAAe,YAAY,GAAG;AAEhC,iBAAO,aAAa,UACjB,IAAI,CAAC,SAAS,WAAW,EAAE,SAAS,MAAM,EAAE,EAG5C;AAAA,YACC,CAAC,MAAM,SAAM;AA7H3B;AA6H+B,yCAAa,oBAAb,mBAA+B,6BAAM,YAAWG,qBAAoB,aAAa,eAAe,6BAAM,KAAK,IAAI;AAAA;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,KAAKD,cAAqB;AACxE,YAAI,SAAS;AACX,iBAAO;AAAA,QACT;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["assertEx","BoundWitnessBuilder","isBoundWitness","BoundWitnessDivinerQuerySchema","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName"]}
|
|
1
|
+
{"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import { 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 St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AAe;AAEhCd,iBAAOc,aAAaE,UACjBC,IAAI,CAACC,SAASC,WAAW;YAAED;YAASC;UAAM,EAAA,EAG1CC,OACC,CAACC,MAAMC,SAAAA;AA7HrB,gBAAA9C;AA6H+BsC,qBAAAA,MAAAA,aAAaL,oBAAbK,gBAAAA,IAA+BQ,6BAAMH,YAAWT,qBAAoBI,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,cAAAA;AACnD,YAAI3C,SAAS;AACX,iBAAOA;QACT;MACF;AACA,aAAO4C;IACT;EACF,GAxG0CxD,kCAA1C;AAyGA,SAAOC;AACT,GAjHmC;","names":["StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","assertEx","AbstractDiviner","ArchivistWrapper","BoundWitnessBuilder","isBoundWitness","BoundWitnessDivinerQuerySchema","DivinerConfigSchema","DivinerWrapper","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName","StatefulDiviner","AbstractDiviner","_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","configSchemas","DivinerConfigSchema","StatefulDivinerConfigSchema","assertEx","asArchivistInstance","BoundWitnessBuilder","isBoundWitness","BoundWitnessDivinerQuerySchema","asDivinerInstance","isModuleState","ModuleStateSchema","PayloadBuilder","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"]}
|
package/package.json
CHANGED
|
@@ -11,27 +11,27 @@
|
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@xylabs/assert": "^2.13.20",
|
|
14
|
-
"@xyo-network/abstract-diviner": "~2.
|
|
15
|
-
"@xyo-network/archivist": "~2.
|
|
16
|
-
"@xyo-network/archivist-wrapper": "~2.
|
|
17
|
-
"@xyo-network/boundwitness-builder": "~2.
|
|
18
|
-
"@xyo-network/boundwitness-model": "~2.
|
|
19
|
-
"@xyo-network/diviner-boundwitness-model": "~2.
|
|
20
|
-
"@xyo-network/diviner-model": "~2.
|
|
21
|
-
"@xyo-network/diviner-wrapper": "~2.
|
|
22
|
-
"@xyo-network/module-model": "~2.
|
|
23
|
-
"@xyo-network/payload-builder": "~2.
|
|
24
|
-
"@xyo-network/payload-model": "~2.
|
|
14
|
+
"@xyo-network/abstract-diviner": "~2.85.1",
|
|
15
|
+
"@xyo-network/archivist": "~2.85.1",
|
|
16
|
+
"@xyo-network/archivist-wrapper": "~2.85.1",
|
|
17
|
+
"@xyo-network/boundwitness-builder": "~2.85.1",
|
|
18
|
+
"@xyo-network/boundwitness-model": "~2.85.1",
|
|
19
|
+
"@xyo-network/diviner-boundwitness-model": "~2.85.1",
|
|
20
|
+
"@xyo-network/diviner-model": "~2.85.1",
|
|
21
|
+
"@xyo-network/diviner-wrapper": "~2.85.1",
|
|
22
|
+
"@xyo-network/module-model": "~2.85.1",
|
|
23
|
+
"@xyo-network/payload-builder": "~2.85.1",
|
|
24
|
+
"@xyo-network/payload-model": "~2.85.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@xylabs/ts-scripts-yarn3": "^3.2.25",
|
|
28
28
|
"@xylabs/tsconfig": "^3.2.25",
|
|
29
|
-
"@xyo-network/account": "~2.
|
|
30
|
-
"@xyo-network/diviner-boundwitness-memory": "~2.
|
|
31
|
-
"@xyo-network/diviner-payload-memory": "~2.
|
|
32
|
-
"@xyo-network/manifest": "~2.
|
|
33
|
-
"@xyo-network/memory-archivist": "~2.
|
|
34
|
-
"@xyo-network/node-memory": "~2.
|
|
29
|
+
"@xyo-network/account": "~2.85.1",
|
|
30
|
+
"@xyo-network/diviner-boundwitness-memory": "~2.85.1",
|
|
31
|
+
"@xyo-network/diviner-payload-memory": "~2.85.1",
|
|
32
|
+
"@xyo-network/manifest": "~2.85.1",
|
|
33
|
+
"@xyo-network/memory-archivist": "~2.85.1",
|
|
34
|
+
"@xyo-network/node-memory": "~2.85.1",
|
|
35
35
|
"typescript": "^5.3.3"
|
|
36
36
|
},
|
|
37
37
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
@@ -73,6 +73,6 @@
|
|
|
73
73
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
74
74
|
},
|
|
75
75
|
"sideEffects": false,
|
|
76
|
-
"version": "2.
|
|
76
|
+
"version": "2.85.1",
|
|
77
77
|
"type": "module"
|
|
78
78
|
}
|