Skip to content

Events with timestamp

Tyler Long edited this page Oct 11, 2018 · 1 revision

Events with timestamp

Want events with timestamp? RxJS provides us with such an operator.

import { timestamp } from 'rxjs/operators'

const rectangle = SubX.create({ position: { } })
rectangle.$.pipe(timestamp()).subscribe(console.log)
rectangle.position.x = 0

Console output

Timestamp {
    value:
    { type: 'SET',
        val: 0,
        oldVal: undefined,
        path: [ 'position', 'x' ] },
    timestamp: 1537247605018 }

flat event with timestamp

Want a flat event with timestamp instead? While, you can map it.

import { timestamp, map } from 'rxjs/operators'

const rectangle = SubX.create({ position: { } })
rectangle.$.pipe(
    timestamp(),
    map(event => ({ ...event.value, timestamp: event.timestamp }))
).subscribe(console.log)
rectangle.position.x = 0

Console output

{ type: 'SET',
    val: 0,
    oldVal: undefined,
    path: [ 'position', 'x' ],
    timestamp: 1537248003912 }