UNPKG

@actyx/sdk

Version:
72 lines 3.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mockEventStore = void 0; /* * Actyx SDK: Functions for writing distributed apps * deployed on peer-to-peer networks, without any servers. * * Copyright (C) 2021 Actyx AG */ const rxjs_1 = require("../../node_modules/rxjs"); const operators_1 = require("../../node_modules/rxjs/operators"); const types_1 = require("../types"); const log_1 = require("./log"); const mockEventStore = () => { const nodeId = types_1.NodeId.of('MOCK'); const present = new rxjs_1.ReplaySubject(1); const events = new rxjs_1.ReplaySubject(1e3); events.next([]); let psn = types_1.Offset.of(0); present.next({}); const query = (_from, _to, query, __sortOrder) => { if (typeof query === 'string') { throw new Error('direct AQL not yet supported by mockEventStore'); } return events.pipe( // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore this needs to complete (0, operators_1.take)(events._events.length), (0, operators_1.concatMap)((x) => x.filter((0, types_1.toEventPredicate)(query))), (0, operators_1.tap)((x) => log_1.default.ws.debug('persistedEvents', x))); }; const subscribe = (_from, query) => { if (typeof query === 'string') { throw new Error('direct AQL not yet supported by mockEventStore'); } return events.asObservable().pipe((0, operators_1.concatMap)((x) => x.filter((0, types_1.toEventPredicate)(query))), (0, operators_1.tap)((x) => log_1.default.ws.debug('allEvents', x))); }; const streamId = types_1.NodeId.streamNo(nodeId, 0); const persistEvents = (x) => { log_1.default.ws.debug('putEvents', x); const newEvents = x.map((payload) => ({ payload: payload.payload, tags: [], appId: types_1.AppId.of('test'), stream: streamId, timestamp: types_1.Timestamp.now(), lamport: types_1.Lamport.of(types_1.Timestamp.now()), offset: types_1.Offset.of(psn++), })); events.next(newEvents); present.next({ [streamId]: psn }); return (0, rxjs_1.of)(newEvents); }; const getPresent = () => (0, rxjs_1.lastValueFrom)(present.asObservable().pipe((0, operators_1.tap)(() => log_1.default.ws.debug('present')), (0, operators_1.take)(1), (0, operators_1.map)((present) => ({ present, toReplicate: {} })))); return { nodeId, offsets: getPresent, query, queryUnchecked: () => { throw new Error('not implemented for mock event store'); }, subscribe, subscribeUnchecked: () => { throw new Error('not implemented for mock event store'); }, subscribeMonotonic: () => { throw new Error('not implemented for mock event store'); }, persistEvents, connectivityStatus: () => rxjs_1.EMPTY, }; }; exports.mockEventStore = mockEventStore; //# sourceMappingURL=mockEventStore.js.map