@actyx/sdk
Version:
Actyx SDK
85 lines • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mkTimeline = exports.emitter = exports.eventFactory = void 0;
/*
* Actyx SDK: Functions for writing distributed apps
* deployed on peer-to-peer networks, without any servers.
*
* Copyright (C) 2021 Actyx AG
*/
const __1 = require("..");
const eventFactory = () => {
const lastPublishedForSources = {};
const mkEvent = (raw) => {
const lastPublished = lastPublishedForSources[raw.source];
// Choosing random starting psns for sources should never change test outcomes.
const offset = lastPublished ? lastPublished.psn : Math.round(Math.random() * 1000);
if (lastPublished && raw.timestamp < lastPublished.timestamp) {
throw new Error('A single source will never timetravel, please review your test scenario.');
}
const fullEvent = {
timestamp: __1.Timestamp.of(raw.timestamp),
stream: __1.NodeId.of(raw.source),
lamport: __1.Lamport.of(raw.timestamp),
offset: __1.Offset.of(offset),
appId: __1.AppId.of('test'),
payload: raw.payload,
tags: (raw.tags || []).concat(['default']),
};
lastPublishedForSources[raw.source] = {
timestamp: raw.timestamp,
psn: offset + 1,
sequence: offset + 1,
};
return fullEvent;
};
const mkEvents = (raw) => raw.map(mkEvent);
return {
mkEvent,
mkEvents,
};
};
exports.eventFactory = eventFactory;
// eslint-disable-next-line no-prototype-builtins
const isPadding = (e) => e.hasOwnProperty('numEvents');
const incrementBy = (delta) => (t) => __1.Timestamp.of(t + delta);
const emitter = (source) => {
const r = (val) => ({
val,
source,
tAdd: incrementBy(100),
});
return r;
};
exports.emitter = emitter;
const mkTimeline = (...events) => {
const { mkEvent } = (0, exports.eventFactory)();
let t = __1.Timestamp.of(100);
const timeline = [];
for (const e of events) {
t = e.tAdd(t);
if (isPadding(e)) {
for (let i = 0; i < e.numEvents; i++) {
timeline.push(mkEvent({
payload: 'padding',
timestamp: t,
source: e.source,
}));
t = e.tAdd(t);
}
}
else {
timeline.push(mkEvent({
payload: e.val,
timestamp: t,
source: e.source,
}));
}
}
return {
all: timeline,
of: (...sources) => timeline.filter((ev) => sources.includes(ev.stream)),
};
};
exports.mkTimeline = mkTimeline;
//# sourceMappingURL=testHelper.js.map