UNPKG

e-lado

Version:

[![CircleCI](https://circleci.com/gh/sharetribe/sharetribe/tree/master.svg?style=svg)](https://circleci.com/gh/sharetribe/sharetribe/tree/master) [![Dependency Status](https://gemnasium.com/sharetribe/sharetribe.png)](https://gemnasium.com/sharetribe/shar

1,636 lines (1,466 loc) 88.5 kB
(function() { var Bacon, BufferingSource, Bus, CompositeUnsubscribe, ConsumingSource, Desc, Dispatcher, End, Error, Event, EventStream, Exception, Initial, Next, None, Observable, Property, PropertyDispatcher, Some, Source, UpdateBarrier, addPropertyInitValueToStream, assert, assertArray, assertEventStream, assertFunction, assertNoArguments, assertString, cloneArray, compositeUnsubscribe, containsDuplicateDeps, convertArgsToFunction, describe, end, eventIdCounter, findDeps, flatMap_, former, idCounter, initial, isArray, isFieldKey, isFunction, isObservable, latter, liftCallback, makeFunction, makeFunctionArgs, makeFunction_, makeObservable, makeSpawner, next, nop, partiallyApplied, recursionDepth, registerObs, spys, toCombinator, toEvent, toFieldExtractor, toFieldKey, toOption, toSimpleExtractor, withDescription, withMethodCallSupport, _, _ref, __slice = [].slice, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; Bacon = { toString: function() { return "Bacon"; } }; Bacon.version = '0.7.35'; Exception = (typeof global !== "undefined" && global !== null ? global : this).Error; Bacon.fromBinder = function(binder, eventTransformer) { if (eventTransformer == null) { eventTransformer = _.id; } return new EventStream(describe(Bacon, "fromBinder", binder, eventTransformer), function(sink) { var unbind, unbinder, unbound; unbound = false; unbind = function() { if (typeof unbinder !== "undefined" && unbinder !== null) { if (!unbound) { unbinder(); } return unbound = true; } }; unbinder = binder(function() { var args, event, reply, value, _i, _len; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; value = eventTransformer.apply(this, args); if (!(isArray(value) && _.last(value) instanceof Event)) { value = [value]; } reply = Bacon.more; for (_i = 0, _len = value.length; _i < _len; _i++) { event = value[_i]; reply = sink(event = toEvent(event)); if (reply === Bacon.noMore || event.isEnd()) { if (unbinder != null) { unbind(); } else { Bacon.scheduler.setTimeout(unbind, 0); } return reply; } } return reply; }); return unbind; }); }; Bacon.$ = {}; Bacon.$.asEventStream = function(eventName, selector, eventTransformer) { var _ref; if (isFunction(selector)) { _ref = [selector, void 0], eventTransformer = _ref[0], selector = _ref[1]; } return withDescription(this.selector || this, "asEventStream", eventName, Bacon.fromBinder((function(_this) { return function(handler) { _this.on(eventName, selector, handler); return function() { return _this.off(eventName, selector, handler); }; }; })(this), eventTransformer)); }; if ((_ref = typeof jQuery !== "undefined" && jQuery !== null ? jQuery : typeof Zepto !== "undefined" && Zepto !== null ? Zepto : void 0) != null) { _ref.fn.asEventStream = Bacon.$.asEventStream; } Bacon.fromEventTarget = function(target, eventName, eventTransformer) { var sub, unsub, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6; sub = (_ref1 = (_ref2 = (_ref3 = target.addEventListener) != null ? _ref3 : target.addListener) != null ? _ref2 : target.bind) != null ? _ref1 : target.on; unsub = (_ref4 = (_ref5 = (_ref6 = target.removeEventListener) != null ? _ref6 : target.removeListener) != null ? _ref5 : target.unbind) != null ? _ref4 : target.off; return withDescription(Bacon, "fromEventTarget", target, eventName, Bacon.fromBinder(function(handler) { sub.call(target, eventName, handler); return function() { return unsub.call(target, eventName, handler); }; }, eventTransformer)); }; Bacon.fromPromise = function(promise, abort) { return withDescription(Bacon, "fromPromise", promise, Bacon.fromBinder(function(handler) { promise.then(handler, function(e) { return handler(new Error(e)); }); return function() { if (abort) { return typeof promise.abort === "function" ? promise.abort() : void 0; } }; }, (function(value) { return [value, end()]; }))); }; Bacon.noMore = ["<no-more>"]; Bacon.more = ["<more>"]; Bacon.later = function(delay, value) { return withDescription(Bacon, "later", delay, value, Bacon.fromPoll(delay, function() { return [value, end()]; })); }; Bacon.sequentially = function(delay, values) { var index; index = 0; return withDescription(Bacon, "sequentially", delay, values, Bacon.fromPoll(delay, function() { var value; value = values[index++]; if (index < values.length) { return value; } else if (index === values.length) { return [value, end()]; } else { return end(); } })); }; Bacon.repeatedly = function(delay, values) { var index; index = 0; return withDescription(Bacon, "repeatedly", delay, values, Bacon.fromPoll(delay, function() { return values[index++ % values.length]; })); }; Bacon.spy = function(spy) { return spys.push(spy); }; spys = []; registerObs = function(obs) { var spy, _i, _len; if (spys.length) { if (!registerObs.running) { try { registerObs.running = true; for (_i = 0, _len = spys.length; _i < _len; _i++) { spy = spys[_i]; spy(obs); } } finally { delete registerObs.running; } } } return void 0; }; withMethodCallSupport = function(wrapped) { return function() { var args, context, f, methodName; f = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; if (typeof f === "object" && args.length) { context = f; methodName = args[0]; f = function() { return context[methodName].apply(context, arguments); }; args = args.slice(1); } return wrapped.apply(null, [f].concat(__slice.call(args))); }; }; liftCallback = function(desc, wrapped) { return withMethodCallSupport(function() { var args, f, stream; f = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; stream = partiallyApplied(wrapped, [ function(values, callback) { return f.apply(null, __slice.call(values).concat([callback])); } ]); return withDescription.apply(null, [Bacon, desc, f].concat(__slice.call(args), [Bacon.combineAsArray(args).flatMap(stream)])); }); }; Bacon.fromCallback = liftCallback("fromCallback", function() { var args, f; f = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return Bacon.fromBinder(function(handler) { makeFunction(f, args)(handler); return nop; }, (function(value) { return [value, end()]; })); }); Bacon.fromNodeCallback = liftCallback("fromNodeCallback", function() { var args, f; f = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return Bacon.fromBinder(function(handler) { makeFunction(f, args)(handler); return nop; }, function(error, value) { if (error) { return [new Error(error), end()]; } return [value, end()]; }); }); Bacon.fromPoll = function(delay, poll) { return withDescription(Bacon, "fromPoll", delay, poll, Bacon.fromBinder((function(handler) { var id; id = Bacon.scheduler.setInterval(handler, delay); return function() { return Bacon.scheduler.clearInterval(id); }; }), poll)); }; Bacon.interval = function(delay, value) { if (value == null) { value = {}; } return withDescription(Bacon, "interval", delay, value, Bacon.fromPoll(delay, function() { return next(value); })); }; Bacon.constant = function(value) { return new Property(describe(Bacon, "constant", value), function(sink) { sink(initial(value)); sink(end()); return nop; }); }; Bacon.never = function() { return new EventStream(describe(Bacon, "never"), function(sink) { sink(end()); return nop; }); }; Bacon.once = function(value) { return new EventStream(describe(Bacon, "once", value), function(sink) { sink(toEvent(value)); sink(end()); return nop; }); }; Bacon.fromArray = function(values) { var i; assertArray(values); i = 0; return new EventStream(describe(Bacon, "fromArray", values), function(sink) { var reply, unsubd, value; unsubd = false; reply = Bacon.more; while ((reply !== Bacon.noMore) && !unsubd) { if (i >= values.length) { sink(end()); reply = Bacon.noMore; } else { value = values[i++]; reply = sink(toEvent(value)); } } return function() { return unsubd = true; }; }); }; Bacon.mergeAll = function() { var streams; streams = 1 <= arguments.length ? __slice.call(arguments, 0) : []; if (isArray(streams[0])) { streams = streams[0]; } if (streams.length) { return new EventStream(describe.apply(null, [Bacon, "mergeAll"].concat(__slice.call(streams))), function(sink) { var ends, sinks, smartSink; ends = 0; smartSink = function(obs) { return function(unsubBoth) { return obs.dispatcher.subscribe(function(event) { var reply; if (event.isEnd()) { ends++; if (ends === streams.length) { return sink(end()); } else { return Bacon.more; } } else { reply = sink(event); if (reply === Bacon.noMore) { unsubBoth(); } return reply; } }); }; }; sinks = _.map(smartSink, streams); return compositeUnsubscribe.apply(null, sinks); }); } else { return Bacon.never(); } }; Bacon.zipAsArray = function() { var streams; streams = 1 <= arguments.length ? __slice.call(arguments, 0) : []; if (isArray(streams[0])) { streams = streams[0]; } return withDescription.apply(null, [Bacon, "zipAsArray"].concat(__slice.call(streams), [Bacon.zipWith(streams, function() { var xs; xs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return xs; })])); }; Bacon.zipWith = function() { var f, streams, _ref1; f = arguments[0], streams = 2 <= arguments.length ? __slice.call(arguments, 1) : []; if (!isFunction(f)) { _ref1 = [f, streams[0]], streams = _ref1[0], f = _ref1[1]; } streams = _.map((function(s) { return s.toEventStream(); }), streams); return withDescription.apply(null, [Bacon, "zipWith", f].concat(__slice.call(streams), [Bacon.when(streams, f)])); }; Bacon.groupSimultaneous = function() { var s, sources, streams; streams = 1 <= arguments.length ? __slice.call(arguments, 0) : []; if (streams.length === 1 && isArray(streams[0])) { streams = streams[0]; } sources = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = streams.length; _i < _len; _i++) { s = streams[_i]; _results.push(new BufferingSource(s)); } return _results; })(); return withDescription.apply(null, [Bacon, "groupSimultaneous"].concat(__slice.call(streams), [Bacon.when(sources, (function() { var xs; xs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return xs; }))])); }; Bacon.combineAsArray = function() { var index, s, sources, stream, streams, _i, _len; streams = 1 <= arguments.length ? __slice.call(arguments, 0) : []; if (streams.length === 1 && isArray(streams[0])) { streams = streams[0]; } for (index = _i = 0, _len = streams.length; _i < _len; index = ++_i) { stream = streams[index]; if (!(isObservable(stream))) { streams[index] = Bacon.constant(stream); } } if (streams.length) { sources = (function() { var _j, _len1, _results; _results = []; for (_j = 0, _len1 = streams.length; _j < _len1; _j++) { s = streams[_j]; _results.push(new Source(s, true)); } return _results; })(); return withDescription.apply(null, [Bacon, "combineAsArray"].concat(__slice.call(streams), [Bacon.when(sources, (function() { var xs; xs = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return xs; })).toProperty()])); } else { return Bacon.constant([]); } }; Bacon.onValues = function() { var f, streams, _i; streams = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), f = arguments[_i++]; return Bacon.combineAsArray(streams).onValues(f); }; Bacon.combineWith = function() { var f, streams; f = arguments[0], streams = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return withDescription.apply(null, [Bacon, "combineWith", f].concat(__slice.call(streams), [Bacon.combineAsArray(streams).map(function(values) { return f.apply(null, values); })])); }; Bacon.combineTemplate = function(template) { var applyStreamValue, combinator, compile, compileTemplate, constantValue, current, funcs, mkContext, setValue, streams; funcs = []; streams = []; current = function(ctxStack) { return ctxStack[ctxStack.length - 1]; }; setValue = function(ctxStack, key, value) { return current(ctxStack)[key] = value; }; applyStreamValue = function(key, index) { return function(ctxStack, values) { return setValue(ctxStack, key, values[index]); }; }; constantValue = function(key, value) { return function(ctxStack) { return setValue(ctxStack, key, value); }; }; mkContext = function(template) { if (isArray(template)) { return []; } else { return {}; } }; compile = function(key, value) { var popContext, pushContext; if (isObservable(value)) { streams.push(value); return funcs.push(applyStreamValue(key, streams.length - 1)); } else if (value === Object(value) && typeof value !== "function" && !(value instanceof RegExp) && !(value instanceof Date)) { pushContext = function(key) { return function(ctxStack) { var newContext; newContext = mkContext(value); setValue(ctxStack, key, newContext); return ctxStack.push(newContext); }; }; popContext = function(ctxStack) { return ctxStack.pop(); }; funcs.push(pushContext(key)); compileTemplate(value); return funcs.push(popContext); } else { return funcs.push(constantValue(key, value)); } }; compileTemplate = function(template) { return _.each(template, compile); }; compileTemplate(template); combinator = function(values) { var ctxStack, f, rootContext, _i, _len; rootContext = mkContext(template); ctxStack = [rootContext]; for (_i = 0, _len = funcs.length; _i < _len; _i++) { f = funcs[_i]; f(ctxStack, values); } return rootContext; }; return withDescription(Bacon, "combineTemplate", template, Bacon.combineAsArray(streams).map(combinator)); }; Bacon.retry = function(options) { var delay, isRetryable, maxRetries, retries, retry, source; if (!isFunction(options.source)) { throw new Exception("'source' option has to be a function"); } source = options.source; retries = options.retries || 0; maxRetries = options.maxRetries || retries; delay = options.delay || function() { return 0; }; isRetryable = options.isRetryable || function() { return true; }; retry = function(context) { var delayedRetry, nextAttemptOptions; nextAttemptOptions = { source: source, retries: retries - 1, maxRetries: maxRetries, delay: delay, isRetryable: isRetryable }; delayedRetry = function() { return Bacon.retry(nextAttemptOptions); }; return Bacon.later(delay(context)).filter(false).concat(Bacon.once().flatMap(delayedRetry)); }; return withDescription(Bacon, "retry", options, source().flatMapError(function(e) { if (isRetryable(e) && retries > 0) { return retry({ error: e, retriesDone: maxRetries - retries }); } else { return Bacon.once(new Error(e)); } })); }; eventIdCounter = 0; Event = (function() { function Event() { this.id = ++eventIdCounter; } Event.prototype.isEvent = function() { return true; }; Event.prototype.isEnd = function() { return false; }; Event.prototype.isInitial = function() { return false; }; Event.prototype.isNext = function() { return false; }; Event.prototype.isError = function() { return false; }; Event.prototype.hasValue = function() { return false; }; Event.prototype.filter = function() { return true; }; Event.prototype.inspect = function() { return this.toString(); }; Event.prototype.log = function() { return this.toString(); }; return Event; })(); Next = (function(_super) { __extends(Next, _super); function Next(valueF, eager) { Next.__super__.constructor.call(this); if (!eager && isFunction(valueF) || valueF instanceof Next) { this.valueF = valueF; this.valueInternal = void 0; } else { this.valueF = void 0; this.valueInternal = valueF; } } Next.prototype.isNext = function() { return true; }; Next.prototype.hasValue = function() { return true; }; Next.prototype.value = function() { if (this.valueF instanceof Next) { this.valueInternal = this.valueF.value(); this.valueF = void 0; } else if (this.valueF) { this.valueInternal = this.valueF(); this.valueF = void 0; } return this.valueInternal; }; Next.prototype.fmap = function(f) { var event, value; if (this.valueInternal) { value = this.valueInternal; return this.apply(function() { return f(value); }); } else { event = this; return this.apply(function() { return f(event.value()); }); } }; Next.prototype.apply = function(value) { return new Next(value); }; Next.prototype.filter = function(f) { return f(this.value()); }; Next.prototype.toString = function() { return _.toString(this.value()); }; Next.prototype.log = function() { return this.value(); }; return Next; })(Event); Initial = (function(_super) { __extends(Initial, _super); function Initial() { return Initial.__super__.constructor.apply(this, arguments); } Initial.prototype.isInitial = function() { return true; }; Initial.prototype.isNext = function() { return false; }; Initial.prototype.apply = function(value) { return new Initial(value); }; Initial.prototype.toNext = function() { return new Next(this); }; return Initial; })(Next); End = (function(_super) { __extends(End, _super); function End() { return End.__super__.constructor.apply(this, arguments); } End.prototype.isEnd = function() { return true; }; End.prototype.fmap = function() { return this; }; End.prototype.apply = function() { return this; }; End.prototype.toString = function() { return "<end>"; }; return End; })(Event); Error = (function(_super) { __extends(Error, _super); function Error(error) { this.error = error; } Error.prototype.isError = function() { return true; }; Error.prototype.fmap = function() { return this; }; Error.prototype.apply = function() { return this; }; Error.prototype.toString = function() { return "<error> " + _.toString(this.error); }; return Error; })(Event); idCounter = 0; Observable = (function() { function Observable(desc) { this.id = ++idCounter; withDescription(desc, this); this.initialDesc = this.desc; } Observable.prototype.subscribe = function(sink) { return UpdateBarrier.wrappedSubscribe(this, sink); }; Observable.prototype.subscribeInternal = function(sink) { return this.dispatcher.subscribe(sink); }; Observable.prototype.onValue = function() { var f; f = makeFunctionArgs(arguments); return this.subscribe(function(event) { if (event.hasValue()) { return f(event.value()); } }); }; Observable.prototype.onValues = function(f) { return this.onValue(function(args) { return f.apply(null, args); }); }; Observable.prototype.onError = function() { var f; f = makeFunctionArgs(arguments); return this.subscribe(function(event) { if (event.isError()) { return f(event.error); } }); }; Observable.prototype.onEnd = function() { var f; f = makeFunctionArgs(arguments); return this.subscribe(function(event) { if (event.isEnd()) { return f(); } }); }; Observable.prototype.errors = function() { return withDescription(this, "errors", this.filter(function() { return false; })); }; Observable.prototype.filter = function() { var args, f; f = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return convertArgsToFunction(this, f, args, function(f) { return withDescription(this, "filter", f, this.withHandler(function(event) { if (event.filter(f)) { return this.push(event); } else { return Bacon.more; } })); }); }; Observable.prototype.takeWhile = function() { var args, f; f = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return convertArgsToFunction(this, f, args, function(f) { return withDescription(this, "takeWhile", f, this.withHandler(function(event) { if (event.filter(f)) { return this.push(event); } else { this.push(end()); return Bacon.noMore; } })); }); }; Observable.prototype.endOnError = function() { var args, f; f = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; if (f == null) { f = true; } return convertArgsToFunction(this, f, args, function(f) { return withDescription(this, "endOnError", this.withHandler(function(event) { if (event.isError() && f(event.error)) { this.push(event); return this.push(end()); } else { return this.push(event); } })); }); }; Observable.prototype.take = function(count) { if (count <= 0) { return Bacon.never(); } return withDescription(this, "take", count, this.withHandler(function(event) { if (!event.hasValue()) { return this.push(event); } else { count--; if (count > 0) { return this.push(event); } else { if (count === 0) { this.push(event); } this.push(end()); return Bacon.noMore; } } })); }; Observable.prototype.map = function() { var args, p; p = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; if (p instanceof Property) { return p.sampledBy(this, former); } else { return convertArgsToFunction(this, p, args, function(f) { return withDescription(this, "map", f, this.withHandler(function(event) { return this.push(event.fmap(f)); })); }); } }; Observable.prototype.mapError = function() { var f; f = makeFunctionArgs(arguments); return withDescription(this, "mapError", f, this.withHandler(function(event) { if (event.isError()) { return this.push(next(f(event.error))); } else { return this.push(event); } })); }; Observable.prototype.mapEnd = function() { var f; f = makeFunctionArgs(arguments); return withDescription(this, "mapEnd", f, this.withHandler(function(event) { if (event.isEnd()) { this.push(next(f(event))); this.push(end()); return Bacon.noMore; } else { return this.push(event); } })); }; Observable.prototype.doAction = function() { var f; f = makeFunctionArgs(arguments); return withDescription(this, "doAction", f, this.withHandler(function(event) { if (event.hasValue()) { f(event.value()); } return this.push(event); })); }; Observable.prototype.skip = function(count) { return withDescription(this, "skip", count, this.withHandler(function(event) { if (!event.hasValue()) { return this.push(event); } else if (count > 0) { count--; return Bacon.more; } else { return this.push(event); } })); }; Observable.prototype.skipDuplicates = function(isEqual) { if (isEqual == null) { isEqual = function(a, b) { return a === b; }; } return withDescription(this, "skipDuplicates", this.withStateMachine(None, function(prev, event) { if (!event.hasValue()) { return [prev, [event]]; } else if (event.isInitial() || prev === None || !isEqual(prev.get(), event.value())) { return [new Some(event.value()), [event]]; } else { return [prev, []]; } })); }; Observable.prototype.skipErrors = function() { return withDescription(this, "skipErrors", this.withHandler(function(event) { if (event.isError()) { return Bacon.more; } else { return this.push(event); } })); }; Observable.prototype.withStateMachine = function(initState, f) { var state; state = initState; return withDescription(this, "withStateMachine", initState, f, this.withHandler(function(event) { var fromF, newState, output, outputs, reply, _i, _len; fromF = f(state, event); newState = fromF[0], outputs = fromF[1]; state = newState; reply = Bacon.more; for (_i = 0, _len = outputs.length; _i < _len; _i++) { output = outputs[_i]; reply = this.push(output); if (reply === Bacon.noMore) { return reply; } } return reply; })); }; Observable.prototype.scan = function(seed, f) { var acc, resultProperty, subscribe; f = toCombinator(f); acc = toOption(seed); subscribe = (function(_this) { return function(sink) { var initSent, reply, sendInit, unsub; initSent = false; unsub = nop; reply = Bacon.more; sendInit = function() { if (!initSent) { return acc.forEach(function(value) { initSent = true; reply = sink(new Initial(function() { return value; })); if (reply === Bacon.noMore) { unsub(); return unsub = nop; } }); } }; unsub = _this.dispatcher.subscribe(function(event) { var next, prev; if (event.hasValue()) { if (initSent && event.isInitial()) { return Bacon.more; } else { if (!event.isInitial()) { sendInit(); } initSent = true; prev = acc.getOrElse(void 0); next = f(prev, event.value()); acc = new Some(next); return sink(event.apply(function() { return next; })); } } else { if (event.isEnd()) { reply = sendInit(); } if (reply !== Bacon.noMore) { return sink(event); } } }); UpdateBarrier.whenDoneWith(resultProperty, sendInit); return unsub; }; })(this); return resultProperty = new Property(describe(this, "scan", seed, f), subscribe); }; Observable.prototype.fold = function(seed, f) { return withDescription(this, "fold", seed, f, this.scan(seed, f).sampledBy(this.filter(false).mapEnd().toProperty())); }; Observable.prototype.zip = function(other, f) { if (f == null) { f = Array; } return withDescription(this, "zip", other, Bacon.zipWith([this, other], f)); }; Observable.prototype.diff = function(start, f) { f = toCombinator(f); return withDescription(this, "diff", start, f, this.scan([start], function(prevTuple, next) { return [next, f(prevTuple[0], next)]; }).filter(function(tuple) { return tuple.length === 2; }).map(function(tuple) { return tuple[1]; })); }; Observable.prototype.flatMap = function() { return flatMap_(this, makeSpawner(arguments)); }; Observable.prototype.flatMapFirst = function() { return flatMap_(this, makeSpawner(arguments), true); }; Observable.prototype.flatMapWithConcurrencyLimit = function() { var args, limit; limit = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return withDescription.apply(null, [this, "flatMapWithConcurrencyLimit", limit].concat(__slice.call(args), [flatMap_(this, makeSpawner(args), false, limit)])); }; Observable.prototype.flatMapLatest = function() { var f, stream; f = makeSpawner(arguments); stream = this.toEventStream(); return withDescription(this, "flatMapLatest", f, stream.flatMap(function(value) { return makeObservable(f(value)).takeUntil(stream); })); }; Observable.prototype.flatMapError = function(fn) { return withDescription(this, "flatMapError", fn, this.mapError(function(err) { return new Error(err); }).flatMap(function(x) { if (x instanceof Error) { return fn(x.error); } else { return Bacon.once(x); } })); }; Observable.prototype.flatMapConcat = function() { return withDescription.apply(null, [this, "flatMapConcat"].concat(__slice.call(arguments), [this.flatMapWithConcurrencyLimit.apply(this, [1].concat(__slice.call(arguments)))])); }; Observable.prototype.bufferingThrottle = function(minimumInterval) { return withDescription(this, "bufferingThrottle", minimumInterval, this.flatMapConcat(function(x) { return Bacon.once(x).concat(Bacon.later(minimumInterval).filter(false)); })); }; Observable.prototype.not = function() { return withDescription(this, "not", this.map(function(x) { return !x; })); }; Observable.prototype.log = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; this.subscribe(function(event) { return typeof console !== "undefined" && console !== null ? typeof console.log === "function" ? console.log.apply(console, __slice.call(args).concat([event.log()])) : void 0 : void 0; }); return this; }; Observable.prototype.slidingWindow = function(n, minValues) { if (minValues == null) { minValues = 0; } return withDescription(this, "slidingWindow", n, minValues, this.scan([], (function(window, value) { return window.concat([value]).slice(-n); })).filter((function(values) { return values.length >= minValues; }))); }; Observable.prototype.combine = function(other, f) { var combinator; combinator = toCombinator(f); return withDescription(this, "combine", other, f, Bacon.combineAsArray(this, other).map(function(values) { return combinator(values[0], values[1]); })); }; Observable.prototype.decode = function(cases) { return withDescription(this, "decode", cases, this.combine(Bacon.combineTemplate(cases), function(key, values) { return values[key]; })); }; Observable.prototype.awaiting = function(other) { return withDescription(this, "awaiting", other, Bacon.groupSimultaneous(this, other).map(function(_arg) { var myValues, otherValues; myValues = _arg[0], otherValues = _arg[1]; return otherValues.length === 0; }).toProperty(false).skipDuplicates()); }; Observable.prototype.name = function(name) { this._name = name; return this; }; Observable.prototype.withDescription = function() { return describe.apply(null, arguments).apply(this); }; Observable.prototype.toString = function() { if (this._name) { return this._name; } else { return this.desc.toString(); } }; Observable.prototype.internalDeps = function() { return this.initialDesc.deps(); }; return Observable; })(); Observable.prototype.reduce = Observable.prototype.fold; Observable.prototype.assign = Observable.prototype.onValue; Observable.prototype.inspect = Observable.prototype.toString; flatMap_ = function(root, f, firstOnly, limit) { var childDeps, result, rootDep; rootDep = [root]; childDeps = []; result = new EventStream(describe(root, "flatMap" + (firstOnly ? "First" : ""), f), function(sink) { var checkEnd, checkQueue, composite, queue, spawn; composite = new CompositeUnsubscribe(); queue = []; spawn = function(event) { var child; child = makeObservable(f(event.value())); childDeps.push(child); return composite.add(function(unsubAll, unsubMe) { return child.dispatcher.subscribe(function(event) { var reply; if (event.isEnd()) { _.remove(child, childDeps); checkQueue(); checkEnd(unsubMe); return Bacon.noMore; } else { if (event instanceof Initial) { event = event.toNext(); } reply = sink(event); if (reply === Bacon.noMore) { unsubAll(); } return reply; } }); }); }; checkQueue = function() { var event; event = queue.shift(); if (event) { return spawn(event); } }; checkEnd = function(unsub) { unsub(); if (composite.empty()) { return sink(end()); } }; composite.add(function(__, unsubRoot) { return root.dispatcher.subscribe(function(event) { if (event.isEnd()) { return checkEnd(unsubRoot); } else if (event.isError()) { return sink(event); } else if (firstOnly && composite.count() > 1) { return Bacon.more; } else { if (composite.unsubscribed) { return Bacon.noMore; } if (limit && composite.count() > limit) { return queue.push(event); } else { return spawn(event); } } }); }); return composite.unsubscribe; }); result.internalDeps = function() { if (childDeps.length) { return rootDep.concat(childDeps); } else { return rootDep; } }; return result; }; EventStream = (function(_super) { __extends(EventStream, _super); function EventStream(desc, subscribe, handler) { if (isFunction(desc)) { handler = subscribe; subscribe = desc; desc = []; } EventStream.__super__.constructor.call(this, desc); assertFunction(subscribe); this.dispatcher = new Dispatcher(subscribe, handler); registerObs(this); } EventStream.prototype.delay = function(delay) { return withDescription(this, "delay", delay, this.flatMap(function(value) { return Bacon.later(delay, value); })); }; EventStream.prototype.debounce = function(delay) { return withDescription(this, "debounce", delay, this.flatMapLatest(function(value) { return Bacon.later(delay, value); })); }; EventStream.prototype.debounceImmediate = function(delay) { return withDescription(this, "debounceImmediate", delay, this.flatMapFirst(function(value) { return Bacon.once(value).concat(Bacon.later(delay).filter(false)); })); }; EventStream.prototype.throttle = function(delay) { return withDescription(this, "throttle", delay, this.bufferWithTime(delay).map(function(values) { return values[values.length - 1]; })); }; EventStream.prototype.bufferWithTime = function(delay) { return withDescription(this, "bufferWithTime", delay, this.bufferWithTimeOrCount(delay, Number.MAX_VALUE)); }; EventStream.prototype.bufferWithCount = function(count) { return withDescription(this, "bufferWithCount", count, this.bufferWithTimeOrCount(void 0, count)); }; EventStream.prototype.bufferWithTimeOrCount = function(delay, count) { var flushOrSchedule; flushOrSchedule = function(buffer) { if (buffer.values.length === count) { return buffer.flush(); } else if (delay !== void 0) { return buffer.schedule(); } }; return withDescription(this, "bufferWithTimeOrCount", delay, count, this.buffer(delay, flushOrSchedule, flushOrSchedule)); }; EventStream.prototype.buffer = function(delay, onInput, onFlush) { var buffer, delayMs, reply; if (onInput == null) { onInput = nop; } if (onFlush == null) { onFlush = nop; } buffer = { scheduled: false, end: void 0, values: [], flush: function() { var reply; this.scheduled = false; if (this.values.length > 0) { reply = this.push(next(this.values)); this.values = []; if (this.end != null) { return this.push(this.end); } else if (reply !== Bacon.noMore) { return onFlush(this); } } else { if (this.end != null) { return this.push(this.end); } } }, schedule: function() { if (!this.scheduled) { this.scheduled = true; return delay((function(_this) { return function() { return _this.flush(); }; })(this)); } } }; reply = Bacon.more; if (!isFunction(delay)) { delayMs = delay; delay = function(f) { return Bacon.scheduler.setTimeout(f, delayMs); }; } return withDescription(this, "buffer", this.withHandler(function(event) { buffer.push = (function(_this) { return function(event) { return _this.push(event); }; })(this); if (event.isError()) { reply = this.push(event); } else if (event.isEnd()) { buffer.end = event; if (!buffer.scheduled) { buffer.flush(); } } else { buffer.values.push(event.value()); onInput(buffer); } return reply; })); }; EventStream.prototype.merge = function(right) { var left; assertEventStream(right); left = this; return withDescription(left, "merge", right, Bacon.mergeAll(this, right)); }; EventStream.prototype.toProperty = function(initValue_) { var disp, initValue; initValue = arguments.length === 0 ? None : toOption(function() { return initValue_; }); disp = this.dispatcher; return new Property(describe(this, "toProperty", initValue_), function(sink) { var initSent, reply, sendInit, unsub; initSent = false; unsub = nop; reply = Bacon.more; sendInit = function() { if (!initSent) { return initValue.forEach(function(value) { initSent = true; reply = sink(new Initial(value)); if (reply === Bacon.noMore) { unsub(); return unsub = nop; } }); } }; unsub = disp.subscribe(function(event) { if (event.hasValue()) { if (initSent && event.isInitial()) { return Bacon.more; } else { if (!event.isInitial()) { sendInit(); } initSent = true; initValue = new Some(event); return sink(event); } } else { if (event.isEnd()) { reply = sendInit(); } if (reply !== Bacon.noMore) { return sink(event); } } }); sendInit(); return unsub; }); }; EventStream.prototype.toEventStream = function() { return this; }; EventStream.prototype.sampledBy = function(sampler, combinator) { return withDescription(this, "sampledBy", sampler, combinator, this.toProperty().sampledBy(sampler, combinator)); }; EventStream.prototype.concat = function(right) { var left; left = this; return new EventStream(describe(left, "concat", right), function(sink) { var unsubLeft, unsubRight; unsubRight = nop; unsubLeft = left.dispatcher.subscribe(function(e) { if (e.isEnd()) { return unsubRight = right.dispatcher.subscribe(sink); } else { return sink(e); } }); return function() { unsubLeft(); return unsubRight(); }; }); }; EventStream.prototype.takeUntil = function(stopper) { var endMarker; endMarker = {}; return withDescription(this, "takeUntil", stopper, Bacon.groupSimultaneous(this.mapEnd(endMarker), stopper.skipErrors()).withHandler(function(event) { var data, reply, value, _i, _len, _ref1; if (!event.hasValue()) { return this.push(event); } else { _ref1 = event.value(), data = _ref1[0], stopper = _ref1[1]; if (stopper.length) { return this.push(end()); } else { reply = Bacon.more; for (_i = 0, _len = data.length; _i < _len; _i++) { value = data[_i]; if (value === endMarker) { reply = this.push(end()); } else { reply = this.push(next(value)); } } return reply; } } })); }; EventStream.prototype.skipUntil = function(starter) { var started; started = starter.take(1).map(true).toProperty(false); return withDescription(this, "skipUntil", starter, this.filter(started)); }; EventStream.prototype.skipWhile = function() { var args, f, ok; f = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; ok = false; return convertArgsToFunction(this, f, args, function(f) { return withDescription(this, "skipWhile", f, this.withHandler(function(event) { if (ok || !event.hasValue() || !f(event.value())) { if (event.hasValue()) { ok = true; } return this.push(event); } else { return Bacon.more; } })); }); }; EventStream.prototype.holdWhen = function(valve) { var putToHold, releaseHold, valve_; valve_ = valve.startWith(false); releaseHold = valve_.filter(function(x) { return !x; }); putToHold = valve_.filter(_.id); return withDescription(this, "holdWhen", valve, this.filter(false).merge(valve_.flatMapConcat((function(_this) { return function(shouldHold) { if (!shouldHold) { return _this.takeUntil(putToHold); } else { return _this.scan([], (function(xs, x) { return xs.concat(x); })).sampledBy(releaseHold).take(1).flatMap(Bacon.fromArray); } }; })(this)))); }; EventStream.prototype.startWith = function(seed) { return withDescription(this, "startWith", seed, Bacon.once(seed).concat(this)); }; EventStream.prototype.withHandler = function(handler) { return new EventStream(describe(this, "withHandler", handler), this.dispatcher.subscribe, handler); }; return EventStream; })(Observable); Property = (function(_super) { __extends(Property, _super); function Property(desc, subscribe, handler) { if (isFunction(desc)) { handler = subscribe; subscribe = desc; desc = []; } Property.__super__.constructor.call(this, desc); assertFunction(subscribe); this.dispatcher = new PropertyDispatcher(this, subscribe, handler); registerObs(this); } Property.prototype.sampledBy = function(sampler, combinator) { var lazy, result, samplerSource, stream, thisSource; if (combinator != null) { combinator = toCombinator(combinator); } else { lazy = true; combinator = function(f) { return f.value(); }; } thisSource = new Source(this, false, lazy); samplerSource = new Source(sampler, true, lazy); stream = Bacon.when([thisSource, samplerSource], combinator); result = sampler instanceof Property ? stream.toProperty() : stream; return withDescription(this, "sampledBy", sampler, combinator, result); }; Property.prototype.sample = function(interval) { return withDescription(this, "sample", interval, this.sampledBy(Bacon.interval(interval, {}))); }; Property.prototype.changes = function() { return new EventStream(describe(this, "changes"), (function(_this) { return function(sink) { return _this.dispatcher.subscribe(function(event) { if (!event.isInitial()) { return sink(event); } }); }; })(this)); }; Property.prototype.withHandler = function(handler) { return new Property(describe(this, "withHandler", handler), this.dispatcher.subscribe, handler); }; Property.prototype.toProperty = function() { assertNoArguments(arguments); return this; }; Property.prototype.toEventStream = function() { return new EventStream(describe(this, "toEventStream"), (function(_this) { return function(sink) { return _this.dispatcher.subscribe(function(event) { if (event.isInitial()) { event = event.toNext(); } return sink(event); }); }; })(this)); }; Property.prototype.and = function(other) { return withDescription(this, "and", other, this.combine(other, function(x, y) { return x && y; })); }; Property.prototype.or = function(other) { return withDescription(this, "or", other, this.combine(other, function(x, y) { return x || y; })); }; Property.prototype.delay = function(delay) { return this.delayChanges("delay", delay, function(changes) { return changes.delay(delay); }); }; Property.prototype.debounce = function(delay) { return this.delayChanges("debounce", delay, function(changes) { return changes.debounce(delay); }); }; Property.prototype.throttle = function(delay) { return this.delayChanges("throttle", delay, function(changes) { return changes.throttle(delay); }); }; Property.prototype.delayChanges = function() { var desc, f, _i; desc = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_