@actyx/sdk
Version:
Actyx SDK
94 lines • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebsocketSnapshotStore = exports.RetrieveSnapshotResponseOrNull = exports.RetrieveSnapshotResponse = exports.InvalidateSnapshotsRequest = exports.RetrieveSnapshotRequest = exports.StoreSnapshotRequest = void 0;
/*
* Actyx SDK: Functions for writing distributed apps
* deployed on peer-to-peer networks, without any servers.
*
* Copyright (C) 2021 Actyx AG
*/
const t = require("io-ts");
const wire_1 = require("../types/wire");
const util_1 = require("../util");
const rxjs_1 = require("../../node_modules/rxjs");
const operators_1 = require("../../node_modules/rxjs/operators");
// TODO: generic io-ts mapping from undefined <-> null
var RequestTypes;
(function (RequestTypes) {
RequestTypes["StoreSnapshot"] = "/ax/snapshots/storeSnapshot";
RequestTypes["RetrieveSnapshot"] = "/ax/snapshots/retrieveSnapshot";
RequestTypes["InvalidateSnapshots"] = "/ax/snapshots/invalidateSnapshots";
})(RequestTypes || (RequestTypes = {}));
const nullable = (base) => t.union([base, t.null]);
exports.StoreSnapshotRequest = t.readonly(t.type({
semantics: t.string,
name: t.string,
key: wire_1.EventKeyIO,
offsets: wire_1.OffsetMapIO,
horizon: nullable(wire_1.EventKeyIO),
cycle: t.number,
version: t.number,
tag: t.string,
blob: t.string,
}));
exports.RetrieveSnapshotRequest = t.readonly(t.type({
semantics: t.string,
name: t.string,
version: t.number,
}));
exports.InvalidateSnapshotsRequest = t.readonly(t.type({
semantics: t.string,
name: t.string,
key: wire_1.EventKeyIO,
}));
exports.RetrieveSnapshotResponse = t.readonly(t.type({
state: t.string,
offsets: wire_1.OffsetMapIO,
eventKey: wire_1.EventKeyIO,
horizon: nullable(wire_1.EventKeyIO),
cycle: t.number,
}));
exports.RetrieveSnapshotResponseOrNull = t.union([t.null, exports.RetrieveSnapshotResponse]);
class WebsocketSnapshotStore {
constructor(multiplexer) {
this.multiplexer = multiplexer;
this.storeSnapshot = (semantics, name, key, offsets, horizon, cycle, version, tag, blob) => (0, rxjs_1.lastValueFrom)(this.multiplexer
.request("/ax/snapshots/storeSnapshot" /* StoreSnapshot */, exports.StoreSnapshotRequest.encode({
semantics,
name,
key,
offsets,
horizon: horizon || null,
cycle,
version,
tag,
blob,
}))
.pipe((0, operators_1.map)((0, util_1.validateOrThrow)(t.boolean))));
this.retrieveSnapshot = (semantics, name, version) => (0, rxjs_1.lastValueFrom)(this.multiplexer
.request("/ax/snapshots/retrieveSnapshot" /* RetrieveSnapshot */, exports.RetrieveSnapshotRequest.encode({
semantics,
name,
version,
}))
.pipe((0, operators_1.map)((0, util_1.validateOrThrow)(exports.RetrieveSnapshotResponseOrNull)), (0, operators_1.map)((x) => x === null
? undefined
: {
state: x.state,
eventKey: x.eventKey,
offsets: x.offsets,
horizon: x.horizon || undefined,
cycle: x.cycle,
})));
this.invalidateSnapshots = (semantics, name, key) => (0, rxjs_1.lastValueFrom)(this.multiplexer
.request("/ax/snapshots/invalidateSnapshots" /* InvalidateSnapshots */, exports.InvalidateSnapshotsRequest.encode({
semantics,
name,
key,
}))
.pipe((0, operators_1.map)((0, util_1.validateOrThrow)(t.null)), (0, operators_1.map)((_) => undefined)));
this.invalidateAllSnapshots = () => Promise.resolve(undefined);
}
}
exports.WebsocketSnapshotStore = WebsocketSnapshotStore;
//# sourceMappingURL=websocketSnapshotStore.js.map