-
-
Save Raynos/848ab8b7c79aafbcf6e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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