@basic-streams/emulation
Advanced tools
Comparing version 0.0.1 to 0.0.4
298
index.js
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __values = (this && this.__values) || function (o) { | ||
@@ -27,165 +28,160 @@ var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; | ||
}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Value = /** @class */ (function () { | ||
function Value(value) { | ||
this.value = value; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
return Value; | ||
}()); | ||
exports.Value = Value; | ||
var TimeSpan = /** @class */ (function () { | ||
function TimeSpan(ms) { | ||
this.ms = ms; | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Value = /** @class */ (function () { | ||
function Value(value) { | ||
this.value = value; | ||
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 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)); | ||
} | ||
} | ||
}; | ||
return Event; | ||
}()); | ||
exports.Event = Event; | ||
var EventsList = /** @class */ (function () { | ||
function EventsList(items) { | ||
this.items = items; | ||
} | ||
EventsList.fromTimeline = function (items, currentTime) { | ||
var e_1, _a; | ||
if (currentTime === void 0) { currentTime = 0; } | ||
var events = []; | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
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)); | ||
} | ||
} | ||
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1); | ||
} | ||
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; } | ||
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++; | ||
} | ||
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++; | ||
} | ||
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 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 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; })); | ||
}; | ||
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 = _a.event, rest = _a.rest; | ||
if (state.time >= maxTime || !event) { | ||
return new EventsList(state.result); | ||
} | ||
state.time = event.time; | ||
state.toProduce = rest; | ||
event.callCb(); | ||
}; | ||
}); | ||
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; | ||
}); | ||
} | ||
exports.emulate = emulate; | ||
function laterMock(createStream) { | ||
return function later(time, value) { | ||
return createStream(t(time), v(value)); | ||
}; | ||
} | ||
exports.laterMock = laterMock; |
10
index.ts
@@ -1,2 +0,2 @@ | ||
type Stream<T> = (cb: (payload: T) => void) => (() => void) | ||
import {Stream} from "@basic-streams/stream" | ||
@@ -166,1 +166,9 @@ export class Value<T> { | ||
} | ||
export function laterMock( | ||
createStream: <U>(...timeline: Timeline<U>) => Stream<U>, | ||
) { | ||
return function later<T>(time: number, value?: T): Stream<T> { | ||
return createStream(t(time), v(value as any)) | ||
} | ||
} |
{ | ||
"name": "@basic-streams/emulation", | ||
"version": "0.0.1", | ||
"version": "0.0.4", | ||
"description": "Emulation for basic-streams", | ||
@@ -20,3 +20,6 @@ "keywords": [ | ||
}, | ||
"homepage": "https://github.com/rpominov/basic-streams/packages/emulation" | ||
"homepage": "https://github.com/rpominov/basic-streams/packages/emulation", | ||
"dependencies": { | ||
"@basic-streams/stream": "0.0.1" | ||
} | ||
} |
@@ -1,3 +0,3 @@ | ||
# Emulation for basic-streams | ||
# [@basic-streams](https://github.com/rpominov/basic-streams)/emulation | ||
TODO: docs | ||
TODO: fill up README |
7
399
14153
1
+ Added@basic-streams/stream@0.0.1
+ Added@basic-streams/stream@0.0.1(transitive)