UNPKG

@basic-streams/emulation

Version:
187 lines (186 loc) 6.13 kB
"use strict"; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); var Value = /** @class */ (function () { function Value(value) { this.value = value; } return Value; }()); exports.Value = Value; var TimeSpan = /** @class */ (function () { function TimeSpan(ms) { this.ms = ms; } return TimeSpan; }()); exports.TimeSpan = TimeSpan; var Event = /** @class */ (function () { function Event(time, value, cb) { this.time = time; this.value = value; this.cb = cb; } Event.prototype.callCb = function () { if (this.cb) { this.cb(this.value); } }; return Event; }()); exports.Event = Event; var EventsList = /** @class */ (function () { function EventsList(items) { this.items = items; } EventsList.fromTimeline = function (items, currentTime) { if (currentTime === void 0) { currentTime = 0; } var e_1, _a; var events = []; try { for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) { var item = items_1_1.value; if (item instanceof TimeSpan) { currentTime = currentTime + item.ms; } else { events.push(new Event(currentTime, item.value)); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1); } finally { if (e_1) throw e_1.error; } } return new EventsList(events); }; EventsList.prototype.withCb = function (cb) { return new EventsList(this.items.map(function (item) { return new Event(item.time, item.value, cb); })); }; EventsList.prototype.merge = function (another) { var events = []; var indexA = 0; var indexB = 0; var itemsA = this.items; var itemsB = another.items; while (itemsA.length > indexA || itemsB.length > indexB) { if (itemsA.length !== indexA && (itemsB.length === indexB || itemsA[indexA].time <= itemsB[indexB].time)) { events.push(itemsA[indexA]); indexA++; } else { events.push(itemsB[indexB]); indexB++; } } return new EventsList(events); }; EventsList.prototype.takeOne = function () { var _a = __read(this.items), event = _a[0], rest = _a.slice(1); return { event: event, rest: new EventsList(rest) }; }; EventsList.prototype.toJSON = function () { return this.items.map(function (item) { return ({ time: item.time, value: item.value }); }); }; EventsList.jestSerializer = { test: function (x) { return x instanceof EventsList; }, // for outdated TS typings print: function (x) { return ""; }, serialize: function (val, config, indentation, depth, refs, printer) { var separator = "\n"; function printItem(item) { var indentation1 = indentation + config.indent; var value = printer(item.value, config, indentation1, depth + 1, refs); return indentation1 + item.time + ": " + value; } return "EventsList(" + (val.items.length === 0 ? "" : separator + val.items.map(printItem).join(separator) + separator) + ")"; }, }; return EventsList; }()); exports.EventsList = EventsList; function t(ms) { return new TimeSpan(ms); } exports.t = t; function v(x) { return new Value(x); } exports.v = v; function emulate(generator, maxTime) { if (maxTime === void 0) { maxTime = Infinity; } var state = { time: 0, toProduce: new EventsList([]), result: [], }; var resultStream = generator(function () { var timeline = []; for (var _i = 0; _i < arguments.length; _i++) { timeline[_i] = arguments[_i]; } return function (cb) { // wrapping cb to make it a unique value // that we will compare to in unsuscribe var _cb = function (x) { return cb(x); }; state.toProduce = state.toProduce.merge(EventsList.fromTimeline(timeline, state.time).withCb(_cb)); return function () { state.toProduce = new EventsList(state.toProduce.items.filter(function (item) { return item.cb !== _cb; })); }; }; }); resultStream(function (value) { state.result.push(new Event(state.time, value)); }); while (true) { var _a = state.toProduce.takeOne(), event_1 = _a.event, rest = _a.rest; if (state.time >= maxTime || !event_1) { return new EventsList(state.result); } state.time = event_1.time; state.toProduce = rest; event_1.callCb(); } } exports.emulate = emulate; function laterMock(createStream) { return function later(time, value) { return createStream(t(time), v(value)); }; } exports.laterMock = laterMock;