Skip to content

Instantly share code, notes, and snippets.

@Raynos Raynos/x.js Secret
Created Mar 24, 2013

Embed
What would you like to do?
var observable = require("observable")
var foldp = require("signal/foldp")
var merge = require("signal/merge")
var transform = require("signal/transform")
var MAX = 100
// take an arbitrary source
var input = SomeSource()
// rewinder is a clock :3
var rewinder = observable(0)
var applyRewind = transform(rewinder, function (back) {
return function (state) {
return {
history: history
, rewind: back
, current: history[back] || null
}
}
})
var catchHistory = transform(input, function (value) {
return function (state) {
var history = state.history
history.unshift(value)
if (history.length > MAX) {
history.pop()
}
return {
history: history
, rewind: state.rewind
, current: history[state.rewind]
}
}
})
var fns = merge([applyRewind, catchHistory])
var currentInputState = input()
var inputWithHistory = foldp(fns, function (state, fn) {
return fn(state)
}, { history: [currentInputState], rewind: 0, current: currentInputState })
// rewind lol
rewinder(5)
// now when input updates it always shows the 5th most current value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.