Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 554 Bytes

history.md

File metadata and controls

30 lines (25 loc) · 554 Bytes

Track history for N events

RxJS

let {append, drop, repeat} = require("ramda")

let appendSliding = curry((n, x, xs) => {
  let ys = append(x, xs)
  if (ys.length > n) {
    return drop(ys.length - n, ys)
  } else {
    return ys
  }
})

let history = function (n) {
  if (n <= 0) {
    throw Error("n must be an unsigned integer, got "+ String(n))
  }
  let put = appendSliding(n)
  return this.scan((stateHistory, newState) => {
    return put(newState, stateHistory)
  }, repeat(null, n))
}

// Usage
statefulStream
  ::history(3)