Skip to content

Instantly share code, notes, and snippets.

@Raynos

Raynos/x.js Secret

Created March 24, 2013 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/848ab8b7c79aafbcf6e7 to your computer and use it in GitHub Desktop.
Save Raynos/848ab8b7c79aafbcf6e7 to your computer and use it in GitHub Desktop.
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