| /** @license MIT License (c) copyright 2010-2014 original author or authors */ |
| /** @author Brian Cavalier */ |
| /** @author John Hann */ |
| |
| (function(define) { 'use strict'; |
| define(function() { |
| |
| return function generate(Promise) { |
| |
| var resolve = Promise.resolve; |
| |
| Promise.iterate = iterate; |
| Promise.unfold = unfold; |
| |
| return Promise; |
| |
| /** |
| * @deprecated Use github.com/cujojs/most streams and most.iterate |
| * Generate a (potentially infinite) stream of promised values: |
| * x, f(x), f(f(x)), etc. until condition(x) returns true |
| * @param {function} f function to generate a new x from the previous x |
| * @param {function} condition function that, given the current x, returns |
| * truthy when the iterate should stop |
| * @param {function} handler function to handle the value produced by f |
| * @param {*|Promise} x starting value, may be a promise |
| * @return {Promise} the result of the last call to f before |
| * condition returns true |
| */ |
| function iterate(f, condition, handler, x) { |
| return unfold(function(x) { |
| return [x, f(x)]; |
| }, condition, handler, x); |
| } |
| |
| /** |
| * @deprecated Use github.com/cujojs/most streams and most.unfold |
| * Generate a (potentially infinite) stream of promised values |
| * by applying handler(generator(seed)) iteratively until |
| * condition(seed) returns true. |
| * @param {function} unspool function that generates a [value, newSeed] |
| * given a seed. |
| * @param {function} condition function that, given the current seed, returns |
| * truthy when the unfold should stop |
| * @param {function} handler function to handle the value produced by unspool |
| * @param x {*|Promise} starting value, may be a promise |
| * @return {Promise} the result of the last value produced by unspool before |
| * condition returns true |
| */ |
| function unfold(unspool, condition, handler, x) { |
| return resolve(x).then(function(seed) { |
| return resolve(condition(seed)).then(function(done) { |
| return done ? seed : resolve(unspool(seed)).spread(next); |
| }); |
| }); |
| |
| function next(item, newSeed) { |
| return resolve(handler(item)).then(function() { |
| return unfold(unspool, condition, handler, newSeed); |
| }); |
| } |
| } |
| }; |
| |
| }); |
| }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); })); |