/* flyd.js - The extraction of flyd's central function which was sufficient for the example to work. It lacks many important functions of flyd, so use the original version on your project: https://github.com/paldepind/flyd However it is changed slightly to ensure that subsequent updates run in the same order as the original order, which simplified the initial implementation. It may also be simpler to study operations on this 100-line version. Original license of flyd is included below. The MIT License (MIT) Copyright (c) 2015 Simon Friis Vindum Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var flyd = (function () { var inStream var order = [] var orderNextIdx = -1 // pure functions start here function allDependentsHaveValues(stream) { return stream.deps.every(function(s) { return s.val !== void(0) }) } // pure functions end here function updateStream(s) { if (allDependentsHaveValues(s)) { inStream = s var returnVal = s.fn(s) if (returnVal !== undefined) { s(returnVal) } inStream = undefined s.shouldUpdate = false } } function findDeps(s) { var i, listeners = s.listeners if (s.queued === false) { s.queued = true for (i = 0; i < listeners.length; ++i) { findDeps(listeners[i]) } order[++orderNextIdx] = s } } function updateDeps(s) { var i, o, list, listeners = s.listeners for (i = 0; i < listeners.length; ++i) { list = listeners[i] list.shouldUpdate = true findDeps(list) } for (; orderNextIdx >= 0; --orderNextIdx) { o = order[orderNextIdx] if (o.shouldUpdate === true) updateStream(o) o.queued = false } } function createStream() { function stream(n) { var i, list if (arguments.length === 0) { return stream.val } else { stream.val = n if (inStream === undefined) { updateDeps(stream) } else if (inStream === stream) { for (i = 0; i < stream.listeners.length; ++i) { list = stream.listeners[i] list.shouldUpdate = true } } else { debugger; throw new Error('Restore toUpdate') // was: toUpdate.push(s) } return stream } } stream.val = void(0) stream.listeners = [] stream.queued = false return stream } function createDependentStream(deps, fn) { var i, s = createStream() s.fn = fn s.deps = deps s.shouldUpdate = false for (i = 0; i < deps.length; ++i) { deps[i].listeners.unshift(s) } return s } function stream(arg, fn) { var s = createDependentStream(arg, fn) updateStream(s) return s } return stream })()