Skip to content

Instantly share code, notes, and snippets.

@RWalkling
Created February 21, 2020 07:01
Show Gist options
  • Select an option

  • Save RWalkling/010eaa32cdf29ee856e9eae8858095b3 to your computer and use it in GitHub Desktop.

Select an option

Save RWalkling/010eaa32cdf29ee856e9eae8858095b3 to your computer and use it in GitHub Desktop.
import { MapDeltaStream } from '../delta-streams/streams/map'
import { Point } from './Points'
import { mapValues } from 'lodash'
import { deltaNone, deltaUnknown } from '../delta-streams/deltas/collection'
import mapDeltaStream from '../delta-streams/mapDeltaStream'
export default (points: MapDeltaStream<Point>): MapDeltaStream<number> => {
const outSubject = mapDeltaStream<number>({})
points.stream.subscribe(({ data, delta }) => {
const { data: previousData } = outSubject.stream.value
switch (delta.type) {
case 'none': {
outSubject.stream.next({ data: previousData, delta: deltaNone() })
break
}
case 'unknown': {
const xValues = mapValues(data, point => point.x)
outSubject.stream.next({
data: xValues,
delta: deltaUnknown(xValues),
})
break
}
case 'swapped': {
const { sourceKey, destinationKey } = delta
outSubject.swap.next({ sourceKey, destinationKey })
break
}
case 'removed': {
outSubject.remove.next({ key: delta.key })
break
}
case 'inserted': {
outSubject.insert.next({ key: delta.key, value: data[delta.key].x })
break
}
case 'mutated': {
outSubject.mutate.next({ key: delta.key, value: data[delta.key].x })
break
}
case 'renamed': {
outSubject.rename.next({ key: delta.key, newKey: delta.newKey })
break
}
}
})
return outSubject
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment