Reputation: 1995
I have an object that looks like this: [{ timestamp: object }]
The timestamp
is created timestamp = Date.now();
Now, I am looking for a way to create an Observable
that emits the objects in the real sequence, i.e. when the time between entries is 200ms then it needs to wait 200ms, if it's 2.5 seconds then it needs to wait 2500ms.
I get the difference by subtracting the two adjacent index values from each other.
My code is here:
startReplay() {
this.recordingRunning = true;
const replayValues = this.recordedListeningValues;
this.formObservable = Observable.create(function(observer) {
let lastTs;
Object.keys(replayValues).forEach(function(key) {
observer.timer(Math.floor((Number(key) - lastTs) / 1000)).next(key, lastTs);
lastTs = Number(key);
});
});
var source = this.formObservable.subscribe(x => console.log(x));
}
It throws the error: observer.timer is not a function
At the moment, I am only trying to log
the differences between the timestamps in seconds. Now, I want to emit it in the differences between the two timestamps.
Upvotes: 0
Views: 640
Reputation: 39192
You can use delayWhen operator. But you need to know the timestamp of the start of the recording so that you can calculate the time of each event relative to the start of the recording
const startOfRecording = ??? // Date.now() when recording started
const recordedValues = ???
// each time someone subscribes to this observable
// the recorded events will play back in "real time"
this.formObservable = Observable
.from(Object.keys(replayValues))
.delayWhen(key => Observable.timer(key - startOfRecording))
.map(key => replayValues[key]);
Upvotes: 1