index.js 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@most/prelude'), require('@most/scheduler'), require('@most/disposable')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@most/prelude', '@most/scheduler', '@most/disposable'], factory) :
  4. (factory((global.mostCore = {}),global.mostPrelude,global.mostScheduler,global.mostDisposable));
  5. }(this, (function (exports,prelude,scheduler,disposable) { 'use strict';
  6. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  7. /** @author Brian Cavalier */
  8. /** @author John Hann */
  9. function fatalError(e) {
  10. setTimeout(rethrow, 0, e);
  11. }
  12. function rethrow(e) {
  13. throw e;
  14. }
  15. var asyncGenerator = function () {
  16. function AwaitValue(value) {
  17. this.value = value;
  18. }
  19. function AsyncGenerator(gen) {
  20. var front, back;
  21. function send(key, arg) {
  22. return new Promise(function (resolve, reject) {
  23. var request = {
  24. key: key,
  25. arg: arg,
  26. resolve: resolve,
  27. reject: reject,
  28. next: null
  29. };
  30. if (back) {
  31. back = back.next = request;
  32. } else {
  33. front = back = request;
  34. resume(key, arg);
  35. }
  36. });
  37. }
  38. function resume(key, arg) {
  39. try {
  40. var result = gen[key](arg);
  41. var value = result.value;
  42. if (value instanceof AwaitValue) {
  43. Promise.resolve(value.value).then(function (arg) {
  44. resume("next", arg);
  45. }, function (arg) {
  46. resume("throw", arg);
  47. });
  48. } else {
  49. settle(result.done ? "return" : "normal", result.value);
  50. }
  51. } catch (err) {
  52. settle("throw", err);
  53. }
  54. }
  55. function settle(type, value) {
  56. switch (type) {
  57. case "return":
  58. front.resolve({
  59. value: value,
  60. done: true
  61. });
  62. break;
  63. case "throw":
  64. front.reject(value);
  65. break;
  66. default:
  67. front.resolve({
  68. value: value,
  69. done: false
  70. });
  71. break;
  72. }
  73. front = front.next;
  74. if (front) {
  75. resume(front.key, front.arg);
  76. } else {
  77. back = null;
  78. }
  79. }
  80. this._invoke = send;
  81. if (typeof gen.return !== "function") {
  82. this.return = undefined;
  83. }
  84. }
  85. if (typeof Symbol === "function" && Symbol.asyncIterator) {
  86. AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
  87. return this;
  88. };
  89. }
  90. AsyncGenerator.prototype.next = function (arg) {
  91. return this._invoke("next", arg);
  92. };
  93. AsyncGenerator.prototype.throw = function (arg) {
  94. return this._invoke("throw", arg);
  95. };
  96. AsyncGenerator.prototype.return = function (arg) {
  97. return this._invoke("return", arg);
  98. };
  99. return {
  100. wrap: function (fn) {
  101. return function () {
  102. return new AsyncGenerator(fn.apply(this, arguments));
  103. };
  104. },
  105. await: function (value) {
  106. return new AwaitValue(value);
  107. }
  108. };
  109. }();
  110. var classCallCheck = function (instance, Constructor) {
  111. if (!(instance instanceof Constructor)) {
  112. throw new TypeError("Cannot call a class as a function");
  113. }
  114. };
  115. var inherits = function (subClass, superClass) {
  116. if (typeof superClass !== "function" && superClass !== null) {
  117. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  118. }
  119. subClass.prototype = Object.create(superClass && superClass.prototype, {
  120. constructor: {
  121. value: subClass,
  122. enumerable: false,
  123. writable: true,
  124. configurable: true
  125. }
  126. });
  127. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  128. };
  129. var possibleConstructorReturn = function (self, call) {
  130. if (!self) {
  131. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  132. }
  133. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  134. };
  135. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  136. /** @author Brian Cavalier */
  137. /** @author John Hann */
  138. var propagateTask$1 = function propagateTask(run, value, sink) {
  139. return new PropagateTask(run, value, sink);
  140. };
  141. var propagateEventTask$1 = function propagateEventTask(value, sink) {
  142. return propagateTask$1(runEvent, value, sink);
  143. };
  144. var propagateEndTask = function propagateEndTask(sink) {
  145. return propagateTask$1(runEnd, undefined, sink);
  146. };
  147. var propagateErrorTask$1 = function propagateErrorTask(value, sink) {
  148. return propagateTask$1(runError, value, sink);
  149. };
  150. var PropagateTask = /*#__PURE__*/function () {
  151. function PropagateTask(run, value, sink) {
  152. classCallCheck(this, PropagateTask);
  153. this._run = run;
  154. this.value = value;
  155. this.sink = sink;
  156. this.active = true;
  157. }
  158. PropagateTask.prototype.dispose = function dispose() {
  159. this.active = false;
  160. };
  161. PropagateTask.prototype.run = function run(t) {
  162. if (!this.active) {
  163. return;
  164. }
  165. var run = this._run;
  166. run(t, this.value, this.sink);
  167. };
  168. PropagateTask.prototype.error = function error(t, e) {
  169. // TODO: Remove this check and just do this.sink.error(t, e)?
  170. if (!this.active) {
  171. return fatalError(e);
  172. }
  173. this.sink.error(t, e);
  174. };
  175. return PropagateTask;
  176. }();
  177. var runEvent = function runEvent(t, x, sink) {
  178. return sink.event(t, x);
  179. };
  180. var runEnd = function runEnd(t, _, sink) {
  181. return sink.end(t);
  182. };
  183. var runError = function runError(t, e, sink) {
  184. return sink.error(t, e);
  185. };
  186. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  187. var empty = function empty() {
  188. return EMPTY;
  189. };
  190. var isCanonicalEmpty = function isCanonicalEmpty(stream) {
  191. return stream === EMPTY;
  192. };
  193. var containsCanonicalEmpty = function containsCanonicalEmpty(streams) {
  194. return streams.some(isCanonicalEmpty);
  195. };
  196. var Empty = /*#__PURE__*/function () {
  197. function Empty() {
  198. classCallCheck(this, Empty);
  199. }
  200. Empty.prototype.run = function run(sink, scheduler$$1) {
  201. return scheduler.asap(propagateEndTask(sink), scheduler$$1);
  202. };
  203. return Empty;
  204. }();
  205. var EMPTY = /*#__PURE__*/new Empty();
  206. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  207. var never = function never() {
  208. return NEVER;
  209. };
  210. var Never = /*#__PURE__*/function () {
  211. function Never() {
  212. classCallCheck(this, Never);
  213. }
  214. Never.prototype.run = function run() {
  215. return disposable.disposeNone();
  216. };
  217. return Never;
  218. }();
  219. var NEVER = /*#__PURE__*/new Never();
  220. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  221. var at = function at(t, x) {
  222. return new At(t, x);
  223. };
  224. var At = /*#__PURE__*/function () {
  225. function At(t, x) {
  226. classCallCheck(this, At);
  227. this.time = t;
  228. this.value = x;
  229. }
  230. At.prototype.run = function run(sink, scheduler$$1) {
  231. return scheduler.delay(this.time, propagateTask$1(runAt, this.value, sink), scheduler$$1);
  232. };
  233. return At;
  234. }();
  235. function runAt(t, x, sink) {
  236. sink.event(t, x);
  237. sink.end(t);
  238. }
  239. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  240. var now = function now(x) {
  241. return at(0, x);
  242. };
  243. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  244. /** @author Brian Cavalier */
  245. /** @author John Hann */
  246. /**
  247. * Create a stream of events that occur at a regular period
  248. * @param {Number} period periodicity of events
  249. * @returns {Stream} new stream of periodic events, the event value is undefined
  250. */
  251. var periodic$1 = function periodic$$1(period) {
  252. return new Periodic(period);
  253. };
  254. var Periodic = /*#__PURE__*/function () {
  255. function Periodic(period) {
  256. classCallCheck(this, Periodic);
  257. this.period = period;
  258. }
  259. Periodic.prototype.run = function run(sink, scheduler$$1) {
  260. return scheduler.periodic(this.period, propagateEventTask$1(undefined, sink), scheduler$$1);
  261. };
  262. return Periodic;
  263. }();
  264. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  265. var newStream = function newStream(run) {
  266. return new Stream(run);
  267. };
  268. var Stream = function Stream(run) {
  269. classCallCheck(this, Stream);
  270. this.run = run;
  271. };
  272. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  273. /** @author Brian Cavalier */
  274. var Pipe = /*#__PURE__*/function () {
  275. function Pipe(sink) {
  276. classCallCheck(this, Pipe);
  277. this.sink = sink;
  278. }
  279. Pipe.prototype.event = function event(t, x) {
  280. return this.sink.event(t, x);
  281. };
  282. Pipe.prototype.end = function end(t) {
  283. return this.sink.end(t);
  284. };
  285. Pipe.prototype.error = function error(t, e) {
  286. return this.sink.error(t, e);
  287. };
  288. return Pipe;
  289. }();
  290. /** @license MIT License (c) copyright 2010 original author or authors */
  291. // A slice Bounds type that narrows min values via accumulation
  292. // and max values via Math.min.
  293. // type Bounds = { min: number, max: number }
  294. // Notes:
  295. // 0 <= min <= max
  296. // slice(min2, max2, slice(min1, max1, s)) ~ slice(min1 + min2, Math.min(max1, min1 + max2), s)
  297. // A bounds has a 1d coord system with origin 0, extending to Infinity. Both min and max
  298. // are relative to the origin (0). However, when merging bounds b1 and b2, we
  299. // *interpret* b2 as being relative to b1, hence adding min1 to *both* min2 and max2.
  300. // This essentially translates b2's coordinates back into origin coordinates
  301. // as bounds are merged.
  302. // Construct a constrained bounds
  303. var boundsFrom = function boundsFrom(unsafeMin, unsafeMax) {
  304. var min = Math.max(0, unsafeMin);
  305. var max = Math.max(min, unsafeMax);
  306. return { min: min, max: max };
  307. };
  308. // Combine 2 bounds by narrowing min and max
  309. var mergeBounds = function mergeBounds(b1, b2) {
  310. return boundsFrom(b1.min + b2.min, Math.min(b1.max, b1.min + b2.max));
  311. };
  312. // Nil bounds excludes all slice indices
  313. var isNilBounds = function isNilBounds(b) {
  314. return b.min >= b.max;
  315. };
  316. // Infinite bounds includes all slice indices
  317. var isInfiniteBounds = function isInfiniteBounds(b) {
  318. return b.min <= 0 && b.max === Infinity;
  319. };
  320. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  321. /** @author Brian Cavalier */
  322. /** @author John Hann */
  323. var Filter = /*#__PURE__*/function () {
  324. function Filter(p, source) {
  325. classCallCheck(this, Filter);
  326. this.p = p;
  327. this.source = source;
  328. }
  329. Filter.prototype.run = function run(sink, scheduler$$1) {
  330. return this.source.run(new FilterSink(this.p, sink), scheduler$$1);
  331. };
  332. /**
  333. * Create a filtered source, fusing adjacent filter.filter if possible
  334. * @param {function(x:*):boolean} p filtering predicate
  335. * @param {{run:function}} source source to filter
  336. * @returns {Filter} filtered source
  337. */
  338. Filter.create = function create(p, source) {
  339. if (isCanonicalEmpty(source)) {
  340. return source;
  341. }
  342. if (source instanceof Filter) {
  343. return new Filter(and(source.p, p), source.source);
  344. }
  345. return new Filter(p, source);
  346. };
  347. return Filter;
  348. }();
  349. var FilterSink = /*#__PURE__*/function (_Pipe) {
  350. inherits(FilterSink, _Pipe);
  351. function FilterSink(p, sink) {
  352. classCallCheck(this, FilterSink);
  353. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  354. _this.p = p;
  355. return _this;
  356. }
  357. FilterSink.prototype.event = function event(t, x) {
  358. var p = this.p;
  359. p(x) && this.sink.event(t, x);
  360. };
  361. return FilterSink;
  362. }(Pipe);
  363. var and = function and(p, q) {
  364. return function (x) {
  365. return p(x) && q(x);
  366. };
  367. };
  368. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  369. /** @author Brian Cavalier */
  370. /** @author John Hann */
  371. var FilterMap = /*#__PURE__*/function () {
  372. function FilterMap(p, f, source) {
  373. classCallCheck(this, FilterMap);
  374. this.p = p;
  375. this.f = f;
  376. this.source = source;
  377. }
  378. FilterMap.prototype.run = function run(sink, scheduler$$1) {
  379. return this.source.run(new FilterMapSink(this.p, this.f, sink), scheduler$$1);
  380. };
  381. return FilterMap;
  382. }();
  383. var FilterMapSink = /*#__PURE__*/function (_Pipe) {
  384. inherits(FilterMapSink, _Pipe);
  385. function FilterMapSink(p, f, sink) {
  386. classCallCheck(this, FilterMapSink);
  387. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  388. _this.p = p;
  389. _this.f = f;
  390. return _this;
  391. }
  392. FilterMapSink.prototype.event = function event(t, x) {
  393. var f = this.f;
  394. var p = this.p;
  395. p(x) && this.sink.event(t, f(x));
  396. };
  397. return FilterMapSink;
  398. }(Pipe);
  399. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  400. /** @author Brian Cavalier */
  401. /** @author John Hann */
  402. var Map = /*#__PURE__*/function () {
  403. function Map(f, source) {
  404. classCallCheck(this, Map);
  405. this.f = f;
  406. this.source = source;
  407. }
  408. Map.prototype.run = function run(sink, scheduler$$1) {
  409. // eslint-disable-line no-extend-native
  410. return this.source.run(new MapSink(this.f, sink), scheduler$$1);
  411. };
  412. /**
  413. * Create a mapped source, fusing adjacent map.map, filter.map,
  414. * and filter.map.map if possible
  415. * @param {function(*):*} f mapping function
  416. * @param {{run:function}} source source to map
  417. * @returns {Map|FilterMap} mapped source, possibly fused
  418. */
  419. Map.create = function create(f, source) {
  420. if (isCanonicalEmpty(source)) {
  421. return empty();
  422. }
  423. if (source instanceof Map) {
  424. return new Map(prelude.compose(f, source.f), source.source);
  425. }
  426. if (source instanceof Filter) {
  427. return new FilterMap(source.p, f, source.source);
  428. }
  429. return new Map(f, source);
  430. };
  431. return Map;
  432. }();
  433. var MapSink = /*#__PURE__*/function (_Pipe) {
  434. inherits(MapSink, _Pipe);
  435. function MapSink(f, sink) {
  436. classCallCheck(this, MapSink);
  437. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  438. _this.f = f;
  439. return _this;
  440. }
  441. MapSink.prototype.event = function event(t, x) {
  442. var f = this.f;
  443. this.sink.event(t, f(x));
  444. };
  445. return MapSink;
  446. }(Pipe);
  447. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  448. var SettableDisposable = /*#__PURE__*/function () {
  449. function SettableDisposable() {
  450. classCallCheck(this, SettableDisposable);
  451. this.disposable = undefined;
  452. this.disposed = false;
  453. }
  454. SettableDisposable.prototype.setDisposable = function setDisposable(disposable$$1) {
  455. if (this.disposable !== void 0) {
  456. throw new Error('setDisposable called more than once');
  457. }
  458. this.disposable = disposable$$1;
  459. if (this.disposed) {
  460. disposable$$1.dispose();
  461. }
  462. };
  463. SettableDisposable.prototype.dispose = function dispose() {
  464. if (this.disposed) {
  465. return;
  466. }
  467. this.disposed = true;
  468. if (this.disposable !== void 0) {
  469. this.disposable.dispose();
  470. }
  471. };
  472. return SettableDisposable;
  473. }();
  474. /**
  475. * @param {number} n
  476. * @param {Stream} stream
  477. * @returns {Stream} new stream containing only up to the first n items from stream
  478. */
  479. var take$1 = function take(n, stream) {
  480. return slice$1(0, n, stream);
  481. };
  482. /**
  483. * @param {number} n
  484. * @param {Stream} stream
  485. * @returns {Stream} new stream with the first n items removed
  486. */
  487. var skip$1 = function skip(n, stream) {
  488. return slice$1(n, Infinity, stream);
  489. };
  490. /**
  491. * Slice a stream by index. Negative start/end indexes are not supported
  492. * @param {number} start
  493. * @param {number} end
  494. * @param {Stream} stream
  495. * @returns {Stream} stream containing items where start <= index < end
  496. */
  497. var slice$1 = function slice(start, end, stream) {
  498. return sliceBounds(boundsFrom(start, end), stream);
  499. };
  500. var sliceBounds = function sliceBounds(bounds, stream) {
  501. return isSliceEmpty(bounds, stream) ? empty() : stream instanceof Map ? commuteMapSlice(bounds, stream) : stream instanceof Slice ? fuseSlice(bounds, stream) : createSlice(bounds, stream);
  502. };
  503. var isSliceEmpty = function isSliceEmpty(bounds, stream) {
  504. return isCanonicalEmpty(stream) || isNilBounds(bounds);
  505. };
  506. var createSlice = function createSlice(bounds, stream) {
  507. return isInfiniteBounds(bounds) ? stream : new Slice(bounds, stream);
  508. };
  509. var commuteMapSlice = function commuteMapSlice(bounds, mapStream) {
  510. return Map.create(mapStream.f, sliceBounds(bounds, mapStream.source));
  511. };
  512. var fuseSlice = function fuseSlice(bounds, sliceStream) {
  513. return sliceBounds(mergeBounds(sliceStream.bounds, bounds), sliceStream.source);
  514. };
  515. var Slice = /*#__PURE__*/function () {
  516. function Slice(bounds, source) {
  517. classCallCheck(this, Slice);
  518. this.source = source;
  519. this.bounds = bounds;
  520. }
  521. Slice.prototype.run = function run(sink, scheduler$$1) {
  522. var disposable$$1 = new SettableDisposable();
  523. var sliceSink = new SliceSink(this.bounds.min, this.bounds.max - this.bounds.min, sink, disposable$$1);
  524. disposable$$1.setDisposable(this.source.run(sliceSink, scheduler$$1));
  525. return disposable$$1;
  526. };
  527. return Slice;
  528. }();
  529. var SliceSink = /*#__PURE__*/function (_Pipe) {
  530. inherits(SliceSink, _Pipe);
  531. function SliceSink(skip, take, sink, disposable$$1) {
  532. classCallCheck(this, SliceSink);
  533. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  534. _this.skip = skip;
  535. _this.take = take;
  536. _this.disposable = disposable$$1;
  537. return _this;
  538. }
  539. SliceSink.prototype.event = function event(t, x) {
  540. /* eslint complexity: [1, 4] */
  541. if (this.skip > 0) {
  542. this.skip -= 1;
  543. return;
  544. }
  545. if (this.take === 0) {
  546. return;
  547. }
  548. this.take -= 1;
  549. this.sink.event(t, x);
  550. if (this.take === 0) {
  551. this.disposable.dispose();
  552. this.sink.end(t);
  553. }
  554. };
  555. return SliceSink;
  556. }(Pipe);
  557. var takeWhile$1 = function takeWhile(p, stream) {
  558. return isCanonicalEmpty(stream) ? empty() : new TakeWhile(p, stream);
  559. };
  560. var TakeWhile = /*#__PURE__*/function () {
  561. function TakeWhile(p, source) {
  562. classCallCheck(this, TakeWhile);
  563. this.p = p;
  564. this.source = source;
  565. }
  566. TakeWhile.prototype.run = function run(sink, scheduler$$1) {
  567. var disposable$$1 = new SettableDisposable();
  568. var takeWhileSink = new TakeWhileSink(this.p, sink, disposable$$1);
  569. disposable$$1.setDisposable(this.source.run(takeWhileSink, scheduler$$1));
  570. return disposable$$1;
  571. };
  572. return TakeWhile;
  573. }();
  574. var TakeWhileSink = /*#__PURE__*/function (_Pipe2) {
  575. inherits(TakeWhileSink, _Pipe2);
  576. function TakeWhileSink(p, sink, disposable$$1) {
  577. classCallCheck(this, TakeWhileSink);
  578. var _this2 = possibleConstructorReturn(this, _Pipe2.call(this, sink));
  579. _this2.p = p;
  580. _this2.active = true;
  581. _this2.disposable = disposable$$1;
  582. return _this2;
  583. }
  584. TakeWhileSink.prototype.event = function event(t, x) {
  585. if (!this.active) {
  586. return;
  587. }
  588. var p = this.p;
  589. this.active = p(x);
  590. if (this.active) {
  591. this.sink.event(t, x);
  592. } else {
  593. this.disposable.dispose();
  594. this.sink.end(t);
  595. }
  596. };
  597. return TakeWhileSink;
  598. }(Pipe);
  599. var skipWhile$1 = function skipWhile(p, stream) {
  600. return isCanonicalEmpty(stream) ? empty() : new SkipWhile(p, stream);
  601. };
  602. var SkipWhile = /*#__PURE__*/function () {
  603. function SkipWhile(p, source) {
  604. classCallCheck(this, SkipWhile);
  605. this.p = p;
  606. this.source = source;
  607. }
  608. SkipWhile.prototype.run = function run(sink, scheduler$$1) {
  609. return this.source.run(new SkipWhileSink(this.p, sink), scheduler$$1);
  610. };
  611. return SkipWhile;
  612. }();
  613. var SkipWhileSink = /*#__PURE__*/function (_Pipe3) {
  614. inherits(SkipWhileSink, _Pipe3);
  615. function SkipWhileSink(p, sink) {
  616. classCallCheck(this, SkipWhileSink);
  617. var _this3 = possibleConstructorReturn(this, _Pipe3.call(this, sink));
  618. _this3.p = p;
  619. _this3.skipping = true;
  620. return _this3;
  621. }
  622. SkipWhileSink.prototype.event = function event(t, x) {
  623. if (this.skipping) {
  624. var p = this.p;
  625. this.skipping = p(x);
  626. if (this.skipping) {
  627. return;
  628. }
  629. }
  630. this.sink.event(t, x);
  631. };
  632. return SkipWhileSink;
  633. }(Pipe);
  634. var skipAfter$1 = function skipAfter(p, stream) {
  635. return isCanonicalEmpty(stream) ? empty() : new SkipAfter(p, stream);
  636. };
  637. var SkipAfter = /*#__PURE__*/function () {
  638. function SkipAfter(p, source) {
  639. classCallCheck(this, SkipAfter);
  640. this.p = p;
  641. this.source = source;
  642. }
  643. SkipAfter.prototype.run = function run(sink, scheduler$$1) {
  644. return this.source.run(new SkipAfterSink(this.p, sink), scheduler$$1);
  645. };
  646. return SkipAfter;
  647. }();
  648. var SkipAfterSink = /*#__PURE__*/function (_Pipe4) {
  649. inherits(SkipAfterSink, _Pipe4);
  650. function SkipAfterSink(p, sink) {
  651. classCallCheck(this, SkipAfterSink);
  652. var _this4 = possibleConstructorReturn(this, _Pipe4.call(this, sink));
  653. _this4.p = p;
  654. _this4.skipping = false;
  655. return _this4;
  656. }
  657. SkipAfterSink.prototype.event = function event(t, x) {
  658. if (this.skipping) {
  659. return;
  660. }
  661. var p = this.p;
  662. this.skipping = p(x);
  663. this.sink.event(t, x);
  664. if (this.skipping) {
  665. this.sink.end(t);
  666. }
  667. };
  668. return SkipAfterSink;
  669. }(Pipe);
  670. /** @license MIT License (c) copyright 2017 original author or authors */
  671. var withItems$1 = function withItems(items, stream) {
  672. return zipItems$1(keepLeft, items, stream);
  673. };
  674. var zipItems$1 = function zipItems(f, items, stream) {
  675. return isCanonicalEmpty(stream) || items.length === 0 ? empty() : new ZipItems(f, items, take$1(items.length, stream));
  676. };
  677. var keepLeft = function keepLeft(a, _) {
  678. return a;
  679. };
  680. var ZipItems = /*#__PURE__*/function () {
  681. function ZipItems(f, items, source) {
  682. classCallCheck(this, ZipItems);
  683. this.f = f;
  684. this.items = items;
  685. this.source = source;
  686. }
  687. ZipItems.prototype.run = function run(sink, scheduler$$1) {
  688. return this.source.run(new ZipItemsSink(this.f, this.items, sink), scheduler$$1);
  689. };
  690. return ZipItems;
  691. }();
  692. var ZipItemsSink = /*#__PURE__*/function (_Pipe) {
  693. inherits(ZipItemsSink, _Pipe);
  694. function ZipItemsSink(f, items, sink) {
  695. classCallCheck(this, ZipItemsSink);
  696. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  697. _this.f = f;
  698. _this.items = items;
  699. _this.index = 0;
  700. return _this;
  701. }
  702. ZipItemsSink.prototype.event = function event(t, b) {
  703. var f = this.f;
  704. this.sink.event(t, f(this.items[this.index], b));
  705. this.index += 1;
  706. };
  707. return ZipItemsSink;
  708. }(Pipe);
  709. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  710. var runEffects$1 = /*#__PURE__*/prelude.curry2(function (stream, scheduler$$1) {
  711. return new Promise(function (resolve, reject) {
  712. return runStream(stream, scheduler$$1, resolve, reject);
  713. });
  714. });
  715. function runStream(stream, scheduler$$1, resolve, reject) {
  716. var disposable$$1 = new SettableDisposable();
  717. var observer = new RunEffectsSink(resolve, reject, disposable$$1);
  718. disposable$$1.setDisposable(stream.run(observer, scheduler$$1));
  719. }
  720. var RunEffectsSink = /*#__PURE__*/function () {
  721. function RunEffectsSink(end, error, disposable$$1) {
  722. classCallCheck(this, RunEffectsSink);
  723. this._end = end;
  724. this._error = error;
  725. this._disposable = disposable$$1;
  726. this.active = true;
  727. }
  728. RunEffectsSink.prototype.event = function event(t, x) {};
  729. RunEffectsSink.prototype.end = function end(t) {
  730. if (!this.active) {
  731. return;
  732. }
  733. this._dispose(this._error, this._end, undefined);
  734. };
  735. RunEffectsSink.prototype.error = function error(t, e) {
  736. this._dispose(this._error, this._error, e);
  737. };
  738. RunEffectsSink.prototype._dispose = function _dispose(error, end, x) {
  739. this.active = false;
  740. tryDispose$1(error, end, x, this._disposable);
  741. };
  742. return RunEffectsSink;
  743. }();
  744. function tryDispose$1(error, end, x, disposable$$1) {
  745. try {
  746. disposable$$1.dispose();
  747. } catch (e) {
  748. error(e);
  749. return;
  750. }
  751. end(x);
  752. }
  753. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  754. // Run a Stream, sending all its events to the
  755. // provided Sink.
  756. var run$1 = function run(sink, scheduler$$1, stream) {
  757. return stream.run(sink, scheduler$$1);
  758. };
  759. var RelativeSink = /*#__PURE__*/function () {
  760. function RelativeSink(offset, sink) {
  761. classCallCheck(this, RelativeSink);
  762. this.sink = sink;
  763. this.offset = offset;
  764. }
  765. RelativeSink.prototype.event = function event(t, x) {
  766. this.sink.event(t + this.offset, x);
  767. };
  768. RelativeSink.prototype.error = function error(t, e) {
  769. this.sink.error(t + this.offset, e);
  770. };
  771. RelativeSink.prototype.end = function end(t) {
  772. this.sink.end(t + this.offset);
  773. };
  774. return RelativeSink;
  775. }();
  776. // Create a stream with its own local clock
  777. // This transforms time from the provided scheduler's clock to a stream-local
  778. // clock (which starts at 0), and then *back* to the scheduler's clock before
  779. // propagating events to sink. In other words, upstream sources will see local times,
  780. // and downstream sinks will see non-local (original) times.
  781. var withLocalTime$1 = function withLocalTime(origin, stream) {
  782. return new WithLocalTime(origin, stream);
  783. };
  784. var WithLocalTime = /*#__PURE__*/function () {
  785. function WithLocalTime(origin, source) {
  786. classCallCheck(this, WithLocalTime);
  787. this.origin = origin;
  788. this.source = source;
  789. }
  790. WithLocalTime.prototype.run = function run(sink, scheduler$$1) {
  791. return this.source.run(relativeSink(this.origin, sink), scheduler.schedulerRelativeTo(this.origin, scheduler$$1));
  792. };
  793. return WithLocalTime;
  794. }();
  795. // Accumulate offsets instead of nesting RelativeSinks, which can happen
  796. // with higher-order stream and combinators like continueWith when they're
  797. // applied recursively.
  798. var relativeSink = function relativeSink(origin, sink) {
  799. return sink instanceof RelativeSink ? new RelativeSink(origin + sink.offset, sink.sink) : new RelativeSink(origin, sink);
  800. };
  801. /** @license MIT License (c) copyright 2010 original author or authors */
  802. /**
  803. * Generalized feedback loop. Call a stepper function for each event. The stepper
  804. * will be called with 2 params: the current seed and the an event value. It must
  805. * return a new { seed, value } pair. The `seed` will be fed back into the next
  806. * invocation of stepper, and the `value` will be propagated as the event value.
  807. * @param {function(seed:*, value:*):{seed:*, value:*}} stepper loop step function
  808. * @param {*} seed initial seed value passed to first stepper call
  809. * @param {Stream} stream event stream
  810. * @returns {Stream} new stream whose values are the `value` field of the objects
  811. * returned by the stepper
  812. */
  813. var loop$1 = function loop(stepper, seed, stream) {
  814. return isCanonicalEmpty(stream) ? empty() : new Loop(stepper, seed, stream);
  815. };
  816. var Loop = /*#__PURE__*/function () {
  817. function Loop(stepper, seed, source) {
  818. classCallCheck(this, Loop);
  819. this.step = stepper;
  820. this.seed = seed;
  821. this.source = source;
  822. }
  823. Loop.prototype.run = function run(sink, scheduler$$1) {
  824. return this.source.run(new LoopSink(this.step, this.seed, sink), scheduler$$1);
  825. };
  826. return Loop;
  827. }();
  828. var LoopSink = /*#__PURE__*/function (_Pipe) {
  829. inherits(LoopSink, _Pipe);
  830. function LoopSink(stepper, seed, sink) {
  831. classCallCheck(this, LoopSink);
  832. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  833. _this.step = stepper;
  834. _this.seed = seed;
  835. return _this;
  836. }
  837. LoopSink.prototype.event = function event(t, x) {
  838. var result = this.step(this.seed, x);
  839. this.seed = result.seed;
  840. this.sink.event(t, result.value);
  841. };
  842. return LoopSink;
  843. }(Pipe);
  844. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  845. /** @author Brian Cavalier */
  846. /** @author John Hann */
  847. /**
  848. * Create a stream containing successive reduce results of applying f to
  849. * the previous reduce result and the current stream item.
  850. * @param {function(result:*, x:*):*} f reducer function
  851. * @param {*} initial initial value
  852. * @param {Stream} stream stream to scan
  853. * @returns {Stream} new stream containing successive reduce results
  854. */
  855. var scan$1 = function scan(f, initial, stream) {
  856. return new Scan(f, initial, stream);
  857. };
  858. var Scan = /*#__PURE__*/function () {
  859. function Scan(f, z, source) {
  860. classCallCheck(this, Scan);
  861. this.source = source;
  862. this.f = f;
  863. this.value = z;
  864. }
  865. Scan.prototype.run = function run(sink, scheduler$$1) {
  866. var d1 = scheduler.asap(propagateEventTask$1(this.value, sink), scheduler$$1);
  867. var d2 = this.source.run(new ScanSink(this.f, this.value, sink), scheduler$$1);
  868. return disposable.disposeBoth(d1, d2);
  869. };
  870. return Scan;
  871. }();
  872. var ScanSink = /*#__PURE__*/function (_Pipe) {
  873. inherits(ScanSink, _Pipe);
  874. function ScanSink(f, z, sink) {
  875. classCallCheck(this, ScanSink);
  876. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  877. _this.f = f;
  878. _this.value = z;
  879. return _this;
  880. }
  881. ScanSink.prototype.event = function event(t, x) {
  882. var f = this.f;
  883. this.value = f(this.value, x);
  884. this.sink.event(t, this.value);
  885. };
  886. return ScanSink;
  887. }(Pipe);
  888. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  889. /** @author Brian Cavalier */
  890. /** @author John Hann */
  891. var continueWith$1 = function continueWith(f, stream) {
  892. return new ContinueWith(f, stream);
  893. };
  894. var ContinueWith = /*#__PURE__*/function () {
  895. function ContinueWith(f, source) {
  896. classCallCheck(this, ContinueWith);
  897. this.f = f;
  898. this.source = source;
  899. }
  900. ContinueWith.prototype.run = function run(sink, scheduler$$1) {
  901. return new ContinueWithSink(this.f, this.source, sink, scheduler$$1);
  902. };
  903. return ContinueWith;
  904. }();
  905. var ContinueWithSink = /*#__PURE__*/function (_Pipe) {
  906. inherits(ContinueWithSink, _Pipe);
  907. function ContinueWithSink(f, source, sink, scheduler$$1) {
  908. classCallCheck(this, ContinueWithSink);
  909. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  910. _this.f = f;
  911. _this.scheduler = scheduler$$1;
  912. _this.active = true;
  913. _this.disposable = disposable.disposeOnce(source.run(_this, scheduler$$1));
  914. return _this;
  915. }
  916. ContinueWithSink.prototype.event = function event(t, x) {
  917. if (!this.active) {
  918. return;
  919. }
  920. this.sink.event(t, x);
  921. };
  922. ContinueWithSink.prototype.end = function end(t) {
  923. if (!this.active) {
  924. return;
  925. }
  926. disposable.tryDispose(t, this.disposable, this.sink);
  927. this._startNext(t, this.sink);
  928. };
  929. ContinueWithSink.prototype._startNext = function _startNext(t, sink) {
  930. try {
  931. this.disposable = this._continue(this.f, t, sink);
  932. } catch (e) {
  933. sink.error(t, e);
  934. }
  935. };
  936. ContinueWithSink.prototype._continue = function _continue(f, t, sink) {
  937. return run$1(sink, this.scheduler, withLocalTime$1(t, f()));
  938. };
  939. ContinueWithSink.prototype.dispose = function dispose() {
  940. this.active = false;
  941. return this.disposable.dispose();
  942. };
  943. return ContinueWithSink;
  944. }(Pipe);
  945. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  946. var startWith$1 = function startWith(x, stream) {
  947. return continueWith$1(function () {
  948. return stream;
  949. }, now(x));
  950. };
  951. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  952. /** @author Brian Cavalier */
  953. /** @author John Hann */
  954. /**
  955. * Transform each value in the stream by applying f to each
  956. * @param {function(*):*} f mapping function
  957. * @param {Stream} stream stream to map
  958. * @returns {Stream} stream containing items transformed by f
  959. */
  960. var map$2 = function map$$1(f, stream) {
  961. return Map.create(f, stream);
  962. };
  963. /**
  964. * Replace each value in the stream with x
  965. * @param {*} x
  966. * @param {Stream} stream
  967. * @returns {Stream} stream containing items replaced with x
  968. */
  969. var constant$1 = function constant(x, stream) {
  970. return map$2(function () {
  971. return x;
  972. }, stream);
  973. };
  974. /**
  975. * Perform a side effect for each item in the stream
  976. * @param {function(x:*):*} f side effect to execute for each item. The
  977. * return value will be discarded.
  978. * @param {Stream} stream stream to tap
  979. * @returns {Stream} new stream containing the same items as this stream
  980. */
  981. var tap$1 = function tap(f, stream) {
  982. return new Tap(f, stream);
  983. };
  984. var Tap = /*#__PURE__*/function () {
  985. function Tap(f, source) {
  986. classCallCheck(this, Tap);
  987. this.source = source;
  988. this.f = f;
  989. }
  990. Tap.prototype.run = function run(sink, scheduler$$1) {
  991. return this.source.run(new TapSink(this.f, sink), scheduler$$1);
  992. };
  993. return Tap;
  994. }();
  995. var TapSink = /*#__PURE__*/function (_Pipe) {
  996. inherits(TapSink, _Pipe);
  997. function TapSink(f, sink) {
  998. classCallCheck(this, TapSink);
  999. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1000. _this.f = f;
  1001. return _this;
  1002. }
  1003. TapSink.prototype.event = function event(t, x) {
  1004. var f = this.f;
  1005. f(x);
  1006. this.sink.event(t, x);
  1007. };
  1008. return TapSink;
  1009. }(Pipe);
  1010. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1011. /** @author Brian Cavalier */
  1012. /** @author John Hann */
  1013. var IndexSink = /*#__PURE__*/function (_Sink) {
  1014. inherits(IndexSink, _Sink);
  1015. function IndexSink(i, sink) {
  1016. classCallCheck(this, IndexSink);
  1017. var _this = possibleConstructorReturn(this, _Sink.call(this, sink));
  1018. _this.index = i;
  1019. _this.active = true;
  1020. _this.value = undefined;
  1021. return _this;
  1022. }
  1023. IndexSink.prototype.event = function event(t, x) {
  1024. if (!this.active) {
  1025. return;
  1026. }
  1027. this.value = x;
  1028. this.sink.event(t, this);
  1029. };
  1030. IndexSink.prototype.end = function end(t) {
  1031. if (!this.active) {
  1032. return;
  1033. }
  1034. this.active = false;
  1035. this.sink.event(t, this);
  1036. };
  1037. return IndexSink;
  1038. }(Pipe);
  1039. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1040. /** @author Brian Cavalier */
  1041. /** @author John Hann */
  1042. function invoke(f, args) {
  1043. /* eslint complexity: [2,7] */
  1044. switch (args.length) {
  1045. case 0:
  1046. return f();
  1047. case 1:
  1048. return f(args[0]);
  1049. case 2:
  1050. return f(args[0], args[1]);
  1051. case 3:
  1052. return f(args[0], args[1], args[2]);
  1053. case 4:
  1054. return f(args[0], args[1], args[2], args[3]);
  1055. case 5:
  1056. return f(args[0], args[1], args[2], args[3], args[4]);
  1057. default:
  1058. return f.apply(void 0, args);
  1059. }
  1060. }
  1061. /** @license MIT License (c) copyright 2010 original author or authors */
  1062. /**
  1063. * Combine latest events from two streams
  1064. * @param {function(...events):*} f function to combine most recent events
  1065. * @returns {Stream} stream containing the result of applying f to the most recent
  1066. * event of each input stream, whenever a new event arrives on any stream.
  1067. */
  1068. var combine$1 = function combine(f, stream1, stream2) {
  1069. return combineArray$1(f, [stream1, stream2]);
  1070. };
  1071. /**
  1072. * Combine latest events from all input streams
  1073. * @param {function(...events):*} f function to combine most recent events
  1074. * @param {[Stream]} streams most recent events
  1075. * @returns {Stream} stream containing the result of applying f to the most recent
  1076. * event of each input stream, whenever a new event arrives on any stream.
  1077. */
  1078. var combineArray$1 = function combineArray(f, streams) {
  1079. return streams.length === 0 || containsCanonicalEmpty(streams) ? empty() : streams.length === 1 ? map$2(f, streams[0]) : new Combine(f, streams);
  1080. };
  1081. var Combine = /*#__PURE__*/function () {
  1082. function Combine(f, sources) {
  1083. classCallCheck(this, Combine);
  1084. this.f = f;
  1085. this.sources = sources;
  1086. }
  1087. Combine.prototype.run = function run(sink, scheduler$$1) {
  1088. var l = this.sources.length;
  1089. var disposables = new Array(l);
  1090. var sinks = new Array(l);
  1091. var mergeSink = new CombineSink(disposables, sinks, sink, this.f);
  1092. for (var indexSink, i = 0; i < l; ++i) {
  1093. indexSink = sinks[i] = new IndexSink(i, mergeSink);
  1094. disposables[i] = this.sources[i].run(indexSink, scheduler$$1);
  1095. }
  1096. return disposable.disposeAll(disposables);
  1097. };
  1098. return Combine;
  1099. }();
  1100. var CombineSink = /*#__PURE__*/function (_Pipe) {
  1101. inherits(CombineSink, _Pipe);
  1102. function CombineSink(disposables, sinks, sink, f) {
  1103. classCallCheck(this, CombineSink);
  1104. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1105. _this.disposables = disposables;
  1106. _this.sinks = sinks;
  1107. _this.f = f;
  1108. var l = sinks.length;
  1109. _this.awaiting = l;
  1110. _this.values = new Array(l);
  1111. _this.hasValue = new Array(l).fill(false);
  1112. _this.activeCount = sinks.length;
  1113. return _this;
  1114. }
  1115. CombineSink.prototype.event = function event(t, indexedValue) {
  1116. if (!indexedValue.active) {
  1117. this._dispose(t, indexedValue.index);
  1118. return;
  1119. }
  1120. var i = indexedValue.index;
  1121. var awaiting = this._updateReady(i);
  1122. this.values[i] = indexedValue.value;
  1123. if (awaiting === 0) {
  1124. this.sink.event(t, invoke(this.f, this.values));
  1125. }
  1126. };
  1127. CombineSink.prototype._updateReady = function _updateReady(index) {
  1128. if (this.awaiting > 0) {
  1129. if (!this.hasValue[index]) {
  1130. this.hasValue[index] = true;
  1131. this.awaiting -= 1;
  1132. }
  1133. }
  1134. return this.awaiting;
  1135. };
  1136. CombineSink.prototype._dispose = function _dispose(t, index) {
  1137. disposable.tryDispose(t, this.disposables[index], this.sink);
  1138. if (--this.activeCount === 0) {
  1139. this.sink.end(t);
  1140. }
  1141. };
  1142. return CombineSink;
  1143. }(Pipe);
  1144. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1145. /** @author Brian Cavalier */
  1146. /** @author John Hann */
  1147. /**
  1148. * Assume fs is a stream containing functions, and apply the latest function
  1149. * in fs to the latest value in xs.
  1150. * fs: --f---------g--------h------>
  1151. * xs: -a-------b-------c-------d-->
  1152. * ap(fs, xs): --fa-----fb-gb---gc--hc--hd->
  1153. * @param {Stream} fs stream of functions to apply to the latest x
  1154. * @param {Stream} xs stream of values to which to apply all the latest f
  1155. * @returns {Stream} stream containing all the applications of fs to xs
  1156. */
  1157. function ap$1(fs, xs) {
  1158. return combine$1(prelude.apply, fs, xs);
  1159. }
  1160. /** @license MIT License (c) copyright 2010 original author or authors */
  1161. var mergeConcurrently$1 = function mergeConcurrently(concurrency, stream) {
  1162. return mergeMapConcurrently$1(prelude.id, concurrency, stream);
  1163. };
  1164. var mergeMapConcurrently$1 = function mergeMapConcurrently(f, concurrency, stream) {
  1165. return isCanonicalEmpty(stream) ? empty() : new MergeConcurrently(f, concurrency, stream);
  1166. };
  1167. var MergeConcurrently = /*#__PURE__*/function () {
  1168. function MergeConcurrently(f, concurrency, source) {
  1169. classCallCheck(this, MergeConcurrently);
  1170. this.f = f;
  1171. this.concurrency = concurrency;
  1172. this.source = source;
  1173. }
  1174. MergeConcurrently.prototype.run = function run(sink, scheduler$$1) {
  1175. return new Outer(this.f, this.concurrency, this.source, sink, scheduler$$1);
  1176. };
  1177. return MergeConcurrently;
  1178. }();
  1179. var Outer = /*#__PURE__*/function () {
  1180. function Outer(f, concurrency, source, sink, scheduler$$1) {
  1181. classCallCheck(this, Outer);
  1182. this.f = f;
  1183. this.concurrency = concurrency;
  1184. this.sink = sink;
  1185. this.scheduler = scheduler$$1;
  1186. this.pending = [];
  1187. this.current = [];
  1188. this.disposable = disposable.disposeOnce(source.run(this, scheduler$$1));
  1189. this.active = true;
  1190. }
  1191. Outer.prototype.event = function event(t, x) {
  1192. this._addInner(t, x);
  1193. };
  1194. Outer.prototype._addInner = function _addInner(t, x) {
  1195. if (this.current.length < this.concurrency) {
  1196. this._startInner(t, x);
  1197. } else {
  1198. this.pending.push(x);
  1199. }
  1200. };
  1201. Outer.prototype._startInner = function _startInner(t, x) {
  1202. try {
  1203. this._initInner(t, x);
  1204. } catch (e) {
  1205. this.error(t, e);
  1206. }
  1207. };
  1208. Outer.prototype._initInner = function _initInner(t, x) {
  1209. var innerSink = new Inner(t, this, this.sink);
  1210. innerSink.disposable = mapAndRun(this.f, t, x, innerSink, this.scheduler);
  1211. this.current.push(innerSink);
  1212. };
  1213. Outer.prototype.end = function end(t) {
  1214. this.active = false;
  1215. disposable.tryDispose(t, this.disposable, this.sink);
  1216. this._checkEnd(t);
  1217. };
  1218. Outer.prototype.error = function error(t, e) {
  1219. this.active = false;
  1220. this.sink.error(t, e);
  1221. };
  1222. Outer.prototype.dispose = function dispose() {
  1223. this.active = false;
  1224. this.pending.length = 0;
  1225. this.disposable.dispose();
  1226. disposable.disposeAll(this.current).dispose();
  1227. };
  1228. Outer.prototype._endInner = function _endInner(t, inner) {
  1229. var i = this.current.indexOf(inner);
  1230. if (i >= 0) {
  1231. this.current.splice(i, 1);
  1232. }
  1233. disposable.tryDispose(t, inner, this);
  1234. if (this.pending.length === 0) {
  1235. this._checkEnd(t);
  1236. } else {
  1237. this._startInner(t, this.pending.shift());
  1238. }
  1239. };
  1240. Outer.prototype._checkEnd = function _checkEnd(t) {
  1241. if (!this.active && this.current.length === 0) {
  1242. this.sink.end(t);
  1243. }
  1244. };
  1245. return Outer;
  1246. }();
  1247. var mapAndRun = function mapAndRun(f, t, x, sink, scheduler$$1) {
  1248. return f(x).run(sink, scheduler.schedulerRelativeTo(t, scheduler$$1));
  1249. };
  1250. var Inner = /*#__PURE__*/function () {
  1251. function Inner(time, outer, sink) {
  1252. classCallCheck(this, Inner);
  1253. this.time = time;
  1254. this.outer = outer;
  1255. this.sink = sink;
  1256. this.disposable = disposable.disposeNone();
  1257. }
  1258. Inner.prototype.event = function event(t, x) {
  1259. this.sink.event(t + this.time, x);
  1260. };
  1261. Inner.prototype.end = function end(t) {
  1262. this.outer._endInner(t + this.time, this);
  1263. };
  1264. Inner.prototype.error = function error(t, e) {
  1265. this.outer.error(t + this.time, e);
  1266. };
  1267. Inner.prototype.dispose = function dispose() {
  1268. return this.disposable.dispose();
  1269. };
  1270. return Inner;
  1271. }();
  1272. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1273. /** @author Brian Cavalier */
  1274. /** @author John Hann */
  1275. /**
  1276. * Map each value in the stream to a new stream, and merge it into the
  1277. * returned outer stream. Event arrival times are preserved.
  1278. * @param {function(x:*):Stream} f chaining function, must return a Stream
  1279. * @param {Stream} stream
  1280. * @returns {Stream} new stream containing all events from each stream returned by f
  1281. */
  1282. var chain$1 = function chain(f, stream) {
  1283. return mergeMapConcurrently$1(f, Infinity, stream);
  1284. };
  1285. /**
  1286. * Monadic join. Flatten a Stream<Stream<X>> to Stream<X> by merging inner
  1287. * streams to the outer. Event arrival times are preserved.
  1288. * @param {Stream<Stream<X>>} stream stream of streams
  1289. * @returns {Stream<X>} new stream containing all events of all inner streams
  1290. */
  1291. var join = function join(stream) {
  1292. return mergeConcurrently$1(Infinity, stream);
  1293. };
  1294. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1295. /** @author Brian Cavalier */
  1296. /** @author John Hann */
  1297. /**
  1298. * Map each value in stream to a new stream, and concatenate them all
  1299. * stream: -a---b---cX
  1300. * f(a): 1-1-1-1X
  1301. * f(b): -2-2-2-2X
  1302. * f(c): -3-3-3-3X
  1303. * stream.concatMap(f): -1-1-1-1-2-2-2-2-3-3-3-3X
  1304. * @param {function(x:*):Stream} f function to map each value to a stream
  1305. * @param {Stream} stream
  1306. * @returns {Stream} new stream containing all events from each stream returned by f
  1307. */
  1308. var concatMap$1 = function concatMap(f, stream) {
  1309. return mergeMapConcurrently$1(f, 1, stream);
  1310. };
  1311. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1312. /** @author Brian Cavalier */
  1313. /** @author John Hann */
  1314. /**
  1315. * @returns {Stream} stream containing events from two streams in time order.
  1316. * If two events are simultaneous they will be merged in arbitrary order.
  1317. */
  1318. function merge$1(stream1, stream2) {
  1319. return mergeArray([stream1, stream2]);
  1320. }
  1321. /**
  1322. * @param {Array} streams array of stream to merge
  1323. * @returns {Stream} stream containing events from all input observables
  1324. * in time order. If two events are simultaneous they will be merged in
  1325. * arbitrary order.
  1326. */
  1327. var mergeArray = function mergeArray(streams) {
  1328. return mergeStreams(withoutCanonicalEmpty(streams));
  1329. };
  1330. /**
  1331. * This implements fusion/flattening for merge. It will
  1332. * fuse adjacent merge operations. For example:
  1333. * - a.merge(b).merge(c) effectively becomes merge(a, b, c)
  1334. * - merge(a, merge(b, c)) effectively becomes merge(a, b, c)
  1335. * It does this by concatenating the sources arrays of
  1336. * any nested Merge sources, in effect "flattening" nested
  1337. * merge operations into a single merge.
  1338. */
  1339. var mergeStreams = function mergeStreams(streams) {
  1340. return streams.length === 0 ? empty() : streams.length === 1 ? streams[0] : new Merge(prelude.reduce(appendSources, [], streams));
  1341. };
  1342. var withoutCanonicalEmpty = function withoutCanonicalEmpty(streams) {
  1343. return streams.filter(isNotCanonicalEmpty);
  1344. };
  1345. var isNotCanonicalEmpty = function isNotCanonicalEmpty(stream) {
  1346. return !isCanonicalEmpty(stream);
  1347. };
  1348. var appendSources = function appendSources(sources, stream) {
  1349. return sources.concat(stream instanceof Merge ? stream.sources : stream);
  1350. };
  1351. var Merge = /*#__PURE__*/function () {
  1352. function Merge(sources) {
  1353. classCallCheck(this, Merge);
  1354. this.sources = sources;
  1355. }
  1356. Merge.prototype.run = function run(sink, scheduler$$1) {
  1357. var l = this.sources.length;
  1358. var disposables = new Array(l);
  1359. var sinks = new Array(l);
  1360. var mergeSink = new MergeSink(disposables, sinks, sink);
  1361. for (var indexSink, i = 0; i < l; ++i) {
  1362. indexSink = sinks[i] = new IndexSink(i, mergeSink);
  1363. disposables[i] = this.sources[i].run(indexSink, scheduler$$1);
  1364. }
  1365. return disposable.disposeAll(disposables);
  1366. };
  1367. return Merge;
  1368. }();
  1369. var MergeSink = /*#__PURE__*/function (_Pipe) {
  1370. inherits(MergeSink, _Pipe);
  1371. function MergeSink(disposables, sinks, sink) {
  1372. classCallCheck(this, MergeSink);
  1373. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1374. _this.disposables = disposables;
  1375. _this.activeCount = sinks.length;
  1376. return _this;
  1377. }
  1378. MergeSink.prototype.event = function event(t, indexValue) {
  1379. if (!indexValue.active) {
  1380. this._dispose(t, indexValue.index);
  1381. return;
  1382. }
  1383. this.sink.event(t, indexValue.value);
  1384. };
  1385. MergeSink.prototype._dispose = function _dispose(t, index) {
  1386. disposable.tryDispose(t, this.disposables[index], this.sink);
  1387. if (--this.activeCount === 0) {
  1388. this.sink.end(t);
  1389. }
  1390. };
  1391. return MergeSink;
  1392. }(Pipe);
  1393. /** @license MIT License (c) copyright 2010 original author or authors */
  1394. var sample$1 = function sample(values, sampler) {
  1395. return snapshot$1(function (x, _) {
  1396. return x;
  1397. }, values, sampler);
  1398. };
  1399. var snapshot$1 = function snapshot(f, values, sampler) {
  1400. return isCanonicalEmpty(sampler) || isCanonicalEmpty(values) ? empty() : new Snapshot(f, values, sampler);
  1401. };
  1402. var Snapshot = /*#__PURE__*/function () {
  1403. function Snapshot(f, values, sampler) {
  1404. classCallCheck(this, Snapshot);
  1405. this.f = f;
  1406. this.values = values;
  1407. this.sampler = sampler;
  1408. }
  1409. Snapshot.prototype.run = function run(sink, scheduler$$1) {
  1410. var sampleSink = new SnapshotSink(this.f, sink);
  1411. var valuesDisposable = this.values.run(sampleSink.latest, scheduler$$1);
  1412. var samplerDisposable = this.sampler.run(sampleSink, scheduler$$1);
  1413. return disposable.disposeBoth(samplerDisposable, valuesDisposable);
  1414. };
  1415. return Snapshot;
  1416. }();
  1417. var SnapshotSink = /*#__PURE__*/function (_Pipe) {
  1418. inherits(SnapshotSink, _Pipe);
  1419. function SnapshotSink(f, sink) {
  1420. classCallCheck(this, SnapshotSink);
  1421. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1422. _this.f = f;
  1423. _this.latest = new LatestValueSink(_this);
  1424. return _this;
  1425. }
  1426. SnapshotSink.prototype.event = function event(t, x) {
  1427. if (this.latest.hasValue) {
  1428. var f = this.f;
  1429. this.sink.event(t, f(this.latest.value, x));
  1430. }
  1431. };
  1432. return SnapshotSink;
  1433. }(Pipe);
  1434. var LatestValueSink = /*#__PURE__*/function (_Pipe2) {
  1435. inherits(LatestValueSink, _Pipe2);
  1436. function LatestValueSink(sink) {
  1437. classCallCheck(this, LatestValueSink);
  1438. var _this2 = possibleConstructorReturn(this, _Pipe2.call(this, sink));
  1439. _this2.hasValue = false;
  1440. return _this2;
  1441. }
  1442. LatestValueSink.prototype.event = function event(t, x) {
  1443. this.value = x;
  1444. this.hasValue = true;
  1445. };
  1446. LatestValueSink.prototype.end = function end() {};
  1447. return LatestValueSink;
  1448. }(Pipe);
  1449. // Copied and modified from https://github.com/invertase/denque
  1450. // MIT License
  1451. // These constants were extracted directly from denque's shift()
  1452. // It's not clear exactly why the authors chose these particular
  1453. // values, but given denque's stated goals, it seems likely that
  1454. // they were chosen for speed/memory reasons.
  1455. // Max value of _head at which Queue is willing to shink
  1456. // its internal array
  1457. var HEAD_MAX_SHRINK = 2;
  1458. // Min value of _tail at which Queue is willing to shink
  1459. // its internal array
  1460. var TAIL_MIN_SHRINK = 10000;
  1461. var Queue = /*#__PURE__*/function () {
  1462. function Queue() {
  1463. classCallCheck(this, Queue);
  1464. this._head = 0;
  1465. this._tail = 0;
  1466. this._capacityMask = 0x3;
  1467. this._list = new Array(4);
  1468. }
  1469. Queue.prototype.push = function push(x) {
  1470. var tail = this._tail;
  1471. this._list[tail] = x;
  1472. this._tail = tail + 1 & this._capacityMask;
  1473. if (this._tail === this._head) {
  1474. this._growArray();
  1475. }
  1476. if (this._head < this._tail) {
  1477. return this._tail - this._head;
  1478. } else {
  1479. return this._capacityMask + 1 - (this._head - this._tail);
  1480. }
  1481. };
  1482. Queue.prototype.shift = function shift() {
  1483. var head = this._head;
  1484. if (head === this._tail) {
  1485. return undefined;
  1486. }
  1487. var x = this._list[head];
  1488. this._list[head] = undefined;
  1489. this._head = head + 1 & this._capacityMask;
  1490. if (head < HEAD_MAX_SHRINK && this._tail > TAIL_MIN_SHRINK && this._tail <= this._list.length >>> 2) {
  1491. this._shrinkArray();
  1492. }
  1493. return x;
  1494. };
  1495. Queue.prototype.isEmpty = function isEmpty() {
  1496. return this._head === this._tail;
  1497. };
  1498. Queue.prototype.length = function length() {
  1499. if (this._head === this._tail) {
  1500. return 0;
  1501. } else if (this._head < this._tail) {
  1502. return this._tail - this._head;
  1503. } else {
  1504. return this._capacityMask + 1 - (this._head - this._tail);
  1505. }
  1506. };
  1507. Queue.prototype._growArray = function _growArray() {
  1508. if (this._head) {
  1509. // copy existing data, head to end, then beginning to tail.
  1510. this._list = this._copyArray();
  1511. this._head = 0;
  1512. }
  1513. // head is at 0 and array is now full, safe to extend
  1514. this._tail = this._list.length;
  1515. this._list.length *= 2;
  1516. this._capacityMask = this._capacityMask << 1 | 1;
  1517. };
  1518. Queue.prototype._shrinkArray = function _shrinkArray() {
  1519. this._list.length >>>= 1;
  1520. this._capacityMask >>>= 1;
  1521. };
  1522. Queue.prototype._copyArray = function _copyArray() {
  1523. var newArray = [];
  1524. var list = this._list;
  1525. var len = list.length;
  1526. var i = void 0;
  1527. for (i = this._head; i < len; i++) {
  1528. newArray.push(list[i]);
  1529. }
  1530. for (i = 0; i < this._tail; i++) {
  1531. newArray.push(list[i]);
  1532. }
  1533. return newArray;
  1534. };
  1535. return Queue;
  1536. }();
  1537. /** @license MIT License (c) copyright 2010 original author or authors */
  1538. /**
  1539. * Combine two streams pairwise by index by applying f to values at corresponding
  1540. * indices. The returned stream ends when either of the input streams ends.
  1541. * @param {function} f function to combine values
  1542. * @returns {Stream} new stream with items at corresponding indices combined
  1543. * using f
  1544. */
  1545. function zip$1(f, stream1, stream2) {
  1546. return zipArray$1(f, [stream1, stream2]);
  1547. }
  1548. /**
  1549. * Combine streams pairwise (or tuple-wise) by index by applying f to values
  1550. * at corresponding indices. The returned stream ends when any of the input
  1551. * streams ends.
  1552. * @param {function} f function to combine values
  1553. * @param {[Stream]} streams streams to zip using f
  1554. * @returns {Stream} new stream with items at corresponding indices combined
  1555. * using f
  1556. */
  1557. var zipArray$1 = function zipArray(f, streams) {
  1558. return streams.length === 0 || containsCanonicalEmpty(streams) ? empty() : streams.length === 1 ? map$2(f, streams[0]) : new Zip(f, streams);
  1559. };
  1560. var Zip = /*#__PURE__*/function () {
  1561. function Zip(f, sources) {
  1562. classCallCheck(this, Zip);
  1563. this.f = f;
  1564. this.sources = sources;
  1565. }
  1566. Zip.prototype.run = function run(sink, scheduler$$1) {
  1567. var l = this.sources.length;
  1568. var disposables = new Array(l);
  1569. var sinks = new Array(l);
  1570. var buffers = new Array(l);
  1571. var zipSink = new ZipSink(this.f, buffers, sinks, sink);
  1572. for (var indexSink, i = 0; i < l; ++i) {
  1573. buffers[i] = new Queue();
  1574. indexSink = sinks[i] = new IndexSink(i, zipSink);
  1575. disposables[i] = this.sources[i].run(indexSink, scheduler$$1);
  1576. }
  1577. return disposable.disposeAll(disposables);
  1578. };
  1579. return Zip;
  1580. }();
  1581. var ZipSink = /*#__PURE__*/function (_Pipe) {
  1582. inherits(ZipSink, _Pipe);
  1583. function ZipSink(f, buffers, sinks, sink) {
  1584. classCallCheck(this, ZipSink);
  1585. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1586. _this.f = f;
  1587. _this.sinks = sinks;
  1588. _this.buffers = buffers;
  1589. return _this;
  1590. }
  1591. ZipSink.prototype.event = function event(t, indexedValue) {
  1592. /* eslint complexity: [1, 5] */
  1593. if (!indexedValue.active) {
  1594. this._dispose(t, indexedValue.index);
  1595. return;
  1596. }
  1597. var buffers = this.buffers;
  1598. var buffer = buffers[indexedValue.index];
  1599. buffer.push(indexedValue.value);
  1600. if (buffer.length() === 1) {
  1601. if (!ready(this.buffers)) {
  1602. return;
  1603. }
  1604. emitZipped(this.f, t, buffers, this.sink);
  1605. if (ended(this.buffers, this.sinks)) {
  1606. this.sink.end(t);
  1607. }
  1608. }
  1609. };
  1610. ZipSink.prototype._dispose = function _dispose(t, index) {
  1611. var buffer = this.buffers[index];
  1612. if (buffer.isEmpty()) {
  1613. this.sink.end(t);
  1614. }
  1615. };
  1616. return ZipSink;
  1617. }(Pipe);
  1618. var emitZipped = function emitZipped(f, t, buffers, sink) {
  1619. return sink.event(t, invoke(f, prelude.map(head, buffers)));
  1620. };
  1621. var head = function head(buffer) {
  1622. return buffer.shift();
  1623. };
  1624. function ended(buffers, sinks) {
  1625. for (var i = 0, l = buffers.length; i < l; ++i) {
  1626. if (buffers[i].isEmpty() && !sinks[i].active) {
  1627. return true;
  1628. }
  1629. }
  1630. return false;
  1631. }
  1632. function ready(buffers) {
  1633. for (var i = 0, l = buffers.length; i < l; ++i) {
  1634. if (buffers[i].isEmpty()) {
  1635. return false;
  1636. }
  1637. }
  1638. return true;
  1639. }
  1640. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1641. /** @author Brian Cavalier */
  1642. /** @author John Hann */
  1643. /**
  1644. * Given a stream of streams, return a new stream that adopts the behavior
  1645. * of the most recent inner stream.
  1646. * @param {Stream} stream of streams on which to switch
  1647. * @returns {Stream} switching stream
  1648. */
  1649. var switchLatest = function switchLatest(stream) {
  1650. return isCanonicalEmpty(stream) ? empty() : new Switch(stream);
  1651. };
  1652. var Switch = /*#__PURE__*/function () {
  1653. function Switch(source) {
  1654. classCallCheck(this, Switch);
  1655. this.source = source;
  1656. }
  1657. Switch.prototype.run = function run(sink, scheduler$$1) {
  1658. var switchSink = new SwitchSink(sink, scheduler$$1);
  1659. return disposable.disposeBoth(switchSink, this.source.run(switchSink, scheduler$$1));
  1660. };
  1661. return Switch;
  1662. }();
  1663. var SwitchSink = /*#__PURE__*/function () {
  1664. function SwitchSink(sink, scheduler$$1) {
  1665. classCallCheck(this, SwitchSink);
  1666. this.sink = sink;
  1667. this.scheduler = scheduler$$1;
  1668. this.current = null;
  1669. this.ended = false;
  1670. }
  1671. SwitchSink.prototype.event = function event(t, stream) {
  1672. this._disposeCurrent(t);
  1673. this.current = new Segment(stream, t, Infinity, this, this.sink, this.scheduler);
  1674. };
  1675. SwitchSink.prototype.end = function end(t) {
  1676. this.ended = true;
  1677. this._checkEnd(t);
  1678. };
  1679. SwitchSink.prototype.error = function error(t, e) {
  1680. this.ended = true;
  1681. this.sink.error(t, e);
  1682. };
  1683. SwitchSink.prototype.dispose = function dispose() {
  1684. return this._disposeCurrent(scheduler.currentTime(this.scheduler));
  1685. };
  1686. SwitchSink.prototype._disposeCurrent = function _disposeCurrent(t) {
  1687. if (this.current !== null) {
  1688. return this.current._dispose(t);
  1689. }
  1690. };
  1691. SwitchSink.prototype._disposeInner = function _disposeInner(t, inner) {
  1692. inner._dispose(t);
  1693. if (inner === this.current) {
  1694. this.current = null;
  1695. }
  1696. };
  1697. SwitchSink.prototype._checkEnd = function _checkEnd(t) {
  1698. if (this.ended && this.current === null) {
  1699. this.sink.end(t);
  1700. }
  1701. };
  1702. SwitchSink.prototype._endInner = function _endInner(t, inner) {
  1703. this._disposeInner(t, inner);
  1704. this._checkEnd(t);
  1705. };
  1706. SwitchSink.prototype._errorInner = function _errorInner(t, e, inner) {
  1707. this._disposeInner(t, inner);
  1708. this.sink.error(t, e);
  1709. };
  1710. return SwitchSink;
  1711. }();
  1712. var Segment = /*#__PURE__*/function () {
  1713. function Segment(source, min, max, outer, sink, scheduler$$1) {
  1714. classCallCheck(this, Segment);
  1715. this.min = min;
  1716. this.max = max;
  1717. this.outer = outer;
  1718. this.sink = sink;
  1719. this.disposable = source.run(this, scheduler.schedulerRelativeTo(min, scheduler$$1));
  1720. }
  1721. Segment.prototype.event = function event(t, x) {
  1722. var time = Math.max(0, t + this.min);
  1723. if (time < this.max) {
  1724. this.sink.event(time, x);
  1725. }
  1726. };
  1727. Segment.prototype.end = function end(t) {
  1728. this.outer._endInner(t + this.min, this);
  1729. };
  1730. Segment.prototype.error = function error(t, e) {
  1731. this.outer._errorInner(t + this.min, e, this);
  1732. };
  1733. Segment.prototype._dispose = function _dispose(t) {
  1734. disposable.tryDispose(t, this.disposable, this.sink);
  1735. };
  1736. return Segment;
  1737. }();
  1738. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1739. /** @author Brian Cavalier */
  1740. /** @author John Hann */
  1741. /**
  1742. * Retain only items matching a predicate
  1743. * @param {function(x:*):boolean} p filtering predicate called for each item
  1744. * @param {Stream} stream stream to filter
  1745. * @returns {Stream} stream containing only items for which predicate returns truthy
  1746. */
  1747. var filter$1 = function filter(p, stream) {
  1748. return Filter.create(p, stream);
  1749. };
  1750. /**
  1751. * Skip repeated events, using === to detect duplicates
  1752. * @param {Stream} stream stream from which to omit repeated events
  1753. * @returns {Stream} stream without repeated events
  1754. */
  1755. var skipRepeats = function skipRepeats(stream) {
  1756. return skipRepeatsWith$1(same, stream);
  1757. };
  1758. /**
  1759. * Skip repeated events using the provided equals function to detect duplicates
  1760. * @param {function(a:*, b:*):boolean} equals optional function to compare items
  1761. * @param {Stream} stream stream from which to omit repeated events
  1762. * @returns {Stream} stream without repeated events
  1763. */
  1764. var skipRepeatsWith$1 = function skipRepeatsWith(equals, stream) {
  1765. return isCanonicalEmpty(stream) ? empty() : new SkipRepeats(equals, stream);
  1766. };
  1767. var SkipRepeats = /*#__PURE__*/function () {
  1768. function SkipRepeats(equals, source) {
  1769. classCallCheck(this, SkipRepeats);
  1770. this.equals = equals;
  1771. this.source = source;
  1772. }
  1773. SkipRepeats.prototype.run = function run(sink, scheduler$$1) {
  1774. return this.source.run(new SkipRepeatsSink(this.equals, sink), scheduler$$1);
  1775. };
  1776. return SkipRepeats;
  1777. }();
  1778. var SkipRepeatsSink = /*#__PURE__*/function (_Pipe) {
  1779. inherits(SkipRepeatsSink, _Pipe);
  1780. function SkipRepeatsSink(equals, sink) {
  1781. classCallCheck(this, SkipRepeatsSink);
  1782. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1783. _this.equals = equals;
  1784. _this.value = void 0;
  1785. _this.init = true;
  1786. return _this;
  1787. }
  1788. SkipRepeatsSink.prototype.event = function event(t, x) {
  1789. if (this.init) {
  1790. this.init = false;
  1791. this.value = x;
  1792. this.sink.event(t, x);
  1793. } else if (!this.equals(this.value, x)) {
  1794. this.value = x;
  1795. this.sink.event(t, x);
  1796. }
  1797. };
  1798. return SkipRepeatsSink;
  1799. }(Pipe);
  1800. function same(a, b) {
  1801. return a === b;
  1802. }
  1803. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1804. /** @author Brian Cavalier */
  1805. /** @author John Hann */
  1806. var until$1 = function until(signal, stream) {
  1807. return new Until(signal, stream);
  1808. };
  1809. var since$1 = function since(signal, stream) {
  1810. return new Since(signal, stream);
  1811. };
  1812. var during$1 = function during(timeWindow, stream) {
  1813. return until$1(join(timeWindow), since$1(timeWindow, stream));
  1814. };
  1815. var Until = /*#__PURE__*/function () {
  1816. function Until(maxSignal, source) {
  1817. classCallCheck(this, Until);
  1818. this.maxSignal = maxSignal;
  1819. this.source = source;
  1820. }
  1821. Until.prototype.run = function run(sink, scheduler$$1) {
  1822. var disposable$$1 = new SettableDisposable();
  1823. var d1 = this.source.run(sink, scheduler$$1);
  1824. var d2 = this.maxSignal.run(new UntilSink(sink, disposable$$1), scheduler$$1);
  1825. disposable$$1.setDisposable(disposable.disposeBoth(d1, d2));
  1826. return disposable$$1;
  1827. };
  1828. return Until;
  1829. }();
  1830. var Since = /*#__PURE__*/function () {
  1831. function Since(minSignal, source) {
  1832. classCallCheck(this, Since);
  1833. this.minSignal = minSignal;
  1834. this.source = source;
  1835. }
  1836. Since.prototype.run = function run(sink, scheduler$$1) {
  1837. var min = new LowerBoundSink(this.minSignal, sink, scheduler$$1);
  1838. var d = this.source.run(new SinceSink(min, sink), scheduler$$1);
  1839. return disposable.disposeBoth(min, d);
  1840. };
  1841. return Since;
  1842. }();
  1843. var SinceSink = /*#__PURE__*/function (_Pipe) {
  1844. inherits(SinceSink, _Pipe);
  1845. function SinceSink(min, sink) {
  1846. classCallCheck(this, SinceSink);
  1847. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1848. _this.min = min;
  1849. return _this;
  1850. }
  1851. SinceSink.prototype.event = function event(t, x) {
  1852. if (this.min.allow) {
  1853. this.sink.event(t, x);
  1854. }
  1855. };
  1856. return SinceSink;
  1857. }(Pipe);
  1858. var LowerBoundSink = /*#__PURE__*/function (_Pipe2) {
  1859. inherits(LowerBoundSink, _Pipe2);
  1860. function LowerBoundSink(signal, sink, scheduler$$1) {
  1861. classCallCheck(this, LowerBoundSink);
  1862. var _this2 = possibleConstructorReturn(this, _Pipe2.call(this, sink));
  1863. _this2.allow = false;
  1864. _this2.disposable = signal.run(_this2, scheduler$$1);
  1865. return _this2;
  1866. }
  1867. LowerBoundSink.prototype.event = function event() /* t, x */{
  1868. this.allow = true;
  1869. this.dispose();
  1870. };
  1871. LowerBoundSink.prototype.end = function end() {};
  1872. LowerBoundSink.prototype.dispose = function dispose() {
  1873. this.disposable.dispose();
  1874. };
  1875. return LowerBoundSink;
  1876. }(Pipe);
  1877. var UntilSink = /*#__PURE__*/function (_Pipe3) {
  1878. inherits(UntilSink, _Pipe3);
  1879. function UntilSink(sink, disposable$$1) {
  1880. classCallCheck(this, UntilSink);
  1881. var _this3 = possibleConstructorReturn(this, _Pipe3.call(this, sink));
  1882. _this3.disposable = disposable$$1;
  1883. return _this3;
  1884. }
  1885. UntilSink.prototype.event = function event(t, x) {
  1886. this.disposable.dispose();
  1887. this.sink.end(t);
  1888. };
  1889. UntilSink.prototype.end = function end() {};
  1890. return UntilSink;
  1891. }(Pipe);
  1892. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  1893. /** @author Brian Cavalier */
  1894. /** @author John Hann */
  1895. /**
  1896. * @param {Number} delayTime milliseconds to delay each item
  1897. * @param {Stream} stream
  1898. * @returns {Stream} new stream containing the same items, but delayed by ms
  1899. */
  1900. var delay$2 = function delay$$1(delayTime, stream) {
  1901. return delayTime <= 0 ? stream : new Delay(delayTime, stream);
  1902. };
  1903. var Delay = /*#__PURE__*/function () {
  1904. function Delay(dt, source) {
  1905. classCallCheck(this, Delay);
  1906. this.dt = dt;
  1907. this.source = source;
  1908. }
  1909. Delay.prototype.run = function run(sink, scheduler$$1) {
  1910. var delaySink = new DelaySink(this.dt, sink, scheduler$$1);
  1911. return disposable.disposeBoth(delaySink, this.source.run(delaySink, scheduler$$1));
  1912. };
  1913. return Delay;
  1914. }();
  1915. var DelaySink = /*#__PURE__*/function (_Pipe) {
  1916. inherits(DelaySink, _Pipe);
  1917. function DelaySink(dt, sink, scheduler$$1) {
  1918. classCallCheck(this, DelaySink);
  1919. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1920. _this.dt = dt;
  1921. _this.scheduler = scheduler$$1;
  1922. _this.tasks = [];
  1923. return _this;
  1924. }
  1925. DelaySink.prototype.dispose = function dispose() {
  1926. this.tasks.forEach(scheduler.cancelTask);
  1927. };
  1928. DelaySink.prototype.event = function event(t, x) {
  1929. this.tasks.push(scheduler.delay(this.dt, propagateEventTask$1(x, this.sink), this.scheduler));
  1930. };
  1931. DelaySink.prototype.end = function end(t) {
  1932. this.tasks.push(scheduler.delay(this.dt, propagateEndTask(this.sink), this.scheduler));
  1933. };
  1934. return DelaySink;
  1935. }(Pipe);
  1936. /** @license MIT License (c) copyright 2010-2017 original author or authors */
  1937. /**
  1938. * Limit the rate of events by suppressing events that occur too often
  1939. * @param {Number} period time to suppress events
  1940. * @param {Stream} stream
  1941. * @returns {Stream}
  1942. */
  1943. var throttle$1 = function throttle(period, stream) {
  1944. return isCanonicalEmpty(stream) ? empty() : stream instanceof Map ? commuteMapThrottle(period, stream) : stream instanceof Throttle ? fuseThrottle(period, stream) : new Throttle(period, stream);
  1945. };
  1946. var commuteMapThrottle = function commuteMapThrottle(period, mapStream) {
  1947. return Map.create(mapStream.f, throttle$1(period, mapStream.source));
  1948. };
  1949. var fuseThrottle = function fuseThrottle(period, throttleStream) {
  1950. return new Throttle(Math.max(period, throttleStream.period), throttleStream.source);
  1951. };
  1952. var Throttle = /*#__PURE__*/function () {
  1953. function Throttle(period, source) {
  1954. classCallCheck(this, Throttle);
  1955. this.period = period;
  1956. this.source = source;
  1957. }
  1958. Throttle.prototype.run = function run(sink, scheduler$$1) {
  1959. return this.source.run(new ThrottleSink(this.period, sink), scheduler$$1);
  1960. };
  1961. return Throttle;
  1962. }();
  1963. var ThrottleSink = /*#__PURE__*/function (_Pipe) {
  1964. inherits(ThrottleSink, _Pipe);
  1965. function ThrottleSink(period, sink) {
  1966. classCallCheck(this, ThrottleSink);
  1967. var _this = possibleConstructorReturn(this, _Pipe.call(this, sink));
  1968. _this.time = 0;
  1969. _this.period = period;
  1970. return _this;
  1971. }
  1972. ThrottleSink.prototype.event = function event(t, x) {
  1973. if (t >= this.time) {
  1974. this.time = t + this.period;
  1975. this.sink.event(t, x);
  1976. }
  1977. };
  1978. return ThrottleSink;
  1979. }(Pipe);
  1980. /**
  1981. * Wait for a burst of events to subside and emit only the last event in the burst
  1982. * @param {Number} period events occuring more frequently than this
  1983. * will be suppressed
  1984. * @param {Stream} stream stream to debounce
  1985. * @returns {Stream} new debounced stream
  1986. */
  1987. var debounce$1 = function debounce(period, stream) {
  1988. return isCanonicalEmpty(stream) ? empty() : new Debounce(period, stream);
  1989. };
  1990. var Debounce = /*#__PURE__*/function () {
  1991. function Debounce(dt, source) {
  1992. classCallCheck(this, Debounce);
  1993. this.dt = dt;
  1994. this.source = source;
  1995. }
  1996. Debounce.prototype.run = function run(sink, scheduler$$1) {
  1997. return new DebounceSink(this.dt, this.source, sink, scheduler$$1);
  1998. };
  1999. return Debounce;
  2000. }();
  2001. var DebounceSink = /*#__PURE__*/function () {
  2002. function DebounceSink(dt, source, sink, scheduler$$1) {
  2003. classCallCheck(this, DebounceSink);
  2004. this.dt = dt;
  2005. this.sink = sink;
  2006. this.scheduler = scheduler$$1;
  2007. this.value = void 0;
  2008. this.timer = null;
  2009. this.disposable = source.run(this, scheduler$$1);
  2010. }
  2011. DebounceSink.prototype.event = function event(t, x) {
  2012. this._clearTimer();
  2013. this.value = x;
  2014. this.timer = scheduler.delay(this.dt, new DebounceTask(this, x), this.scheduler);
  2015. };
  2016. DebounceSink.prototype._event = function _event(t, x) {
  2017. this._clearTimer();
  2018. this.sink.event(t, x);
  2019. };
  2020. DebounceSink.prototype.end = function end(t) {
  2021. if (this._clearTimer()) {
  2022. this.sink.event(t, this.value);
  2023. this.value = undefined;
  2024. }
  2025. this.sink.end(t);
  2026. };
  2027. DebounceSink.prototype.error = function error(t, x) {
  2028. this._clearTimer();
  2029. this.sink.error(t, x);
  2030. };
  2031. DebounceSink.prototype.dispose = function dispose() {
  2032. this._clearTimer();
  2033. this.disposable.dispose();
  2034. };
  2035. DebounceSink.prototype._clearTimer = function _clearTimer() {
  2036. if (this.timer === null) {
  2037. return false;
  2038. }
  2039. this.timer.dispose();
  2040. this.timer = null;
  2041. return true;
  2042. };
  2043. return DebounceSink;
  2044. }();
  2045. var DebounceTask = /*#__PURE__*/function () {
  2046. function DebounceTask(debounce, value) {
  2047. classCallCheck(this, DebounceTask);
  2048. this.debounce = debounce;
  2049. this.value = value;
  2050. }
  2051. DebounceTask.prototype.run = function run(t) {
  2052. this.debounce._event(t, this.value);
  2053. };
  2054. DebounceTask.prototype.error = function error(t, e) {
  2055. this.debounce.error(t, e);
  2056. };
  2057. DebounceTask.prototype.dispose = function dispose() {};
  2058. return DebounceTask;
  2059. }();
  2060. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  2061. /** @author Brian Cavalier */
  2062. /** @author John Hann */
  2063. /**
  2064. * Turn a Stream<Promise<T>> into Stream<T> by awaiting each promise.
  2065. * Event order is preserved. The stream will fail if any promise rejects.
  2066. */
  2067. var awaitPromises = function awaitPromises(stream) {
  2068. return isCanonicalEmpty(stream) ? empty() : new Await(stream);
  2069. };
  2070. /**
  2071. * Create a stream containing only the promise's fulfillment
  2072. * value at the time it fulfills.
  2073. * @param {Promise<T>} p promise
  2074. * @return {Stream<T>} stream containing promise's fulfillment value.
  2075. * If the promise rejects, the stream will error
  2076. */
  2077. var fromPromise = /*#__PURE__*/prelude.compose(awaitPromises, now);
  2078. var Await = /*#__PURE__*/function () {
  2079. function Await(source) {
  2080. classCallCheck(this, Await);
  2081. this.source = source;
  2082. }
  2083. Await.prototype.run = function run(sink, scheduler$$1) {
  2084. return this.source.run(new AwaitSink(sink, scheduler$$1), scheduler$$1);
  2085. };
  2086. return Await;
  2087. }();
  2088. var AwaitSink = /*#__PURE__*/function () {
  2089. function AwaitSink(sink, scheduler$$1) {
  2090. var _this = this;
  2091. classCallCheck(this, AwaitSink);
  2092. this.sink = sink;
  2093. this.scheduler = scheduler$$1;
  2094. this.queue = Promise.resolve();
  2095. // Pre-create closures, to avoid creating them per event
  2096. this._eventBound = function (x) {
  2097. return _this.sink.event(scheduler.currentTime(_this.scheduler), x);
  2098. };
  2099. this._endBound = function () {
  2100. return _this.sink.end(scheduler.currentTime(_this.scheduler));
  2101. };
  2102. this._errorBound = function (e) {
  2103. return _this.sink.error(scheduler.currentTime(_this.scheduler), e);
  2104. };
  2105. }
  2106. AwaitSink.prototype.event = function event(t, promise) {
  2107. var _this2 = this;
  2108. this.queue = this.queue.then(function () {
  2109. return _this2._event(promise);
  2110. }).catch(this._errorBound);
  2111. };
  2112. AwaitSink.prototype.end = function end(t) {
  2113. this.queue = this.queue.then(this._endBound).catch(this._errorBound);
  2114. };
  2115. AwaitSink.prototype.error = function error(t, e) {
  2116. var _this3 = this;
  2117. // Don't resolve error values, propagate directly
  2118. this.queue = this.queue.then(function () {
  2119. return _this3._errorBound(e);
  2120. }).catch(fatalError);
  2121. };
  2122. AwaitSink.prototype._event = function _event(promise) {
  2123. return promise.then(this._eventBound);
  2124. };
  2125. return AwaitSink;
  2126. }();
  2127. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  2128. /** @author Brian Cavalier */
  2129. /** @author John Hann */
  2130. var SafeSink = /*#__PURE__*/function () {
  2131. function SafeSink(sink) {
  2132. classCallCheck(this, SafeSink);
  2133. this.sink = sink;
  2134. this.active = true;
  2135. }
  2136. SafeSink.prototype.event = function event(t, x) {
  2137. if (!this.active) {
  2138. return;
  2139. }
  2140. this.sink.event(t, x);
  2141. };
  2142. SafeSink.prototype.end = function end(t, x) {
  2143. if (!this.active) {
  2144. return;
  2145. }
  2146. this.disable();
  2147. this.sink.end(t, x);
  2148. };
  2149. SafeSink.prototype.error = function error(t, e) {
  2150. this.disable();
  2151. this.sink.error(t, e);
  2152. };
  2153. SafeSink.prototype.disable = function disable() {
  2154. this.active = false;
  2155. return this.sink;
  2156. };
  2157. return SafeSink;
  2158. }();
  2159. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  2160. /** @author Brian Cavalier */
  2161. /** @author John Hann */
  2162. function tryEvent(t, x, sink) {
  2163. try {
  2164. sink.event(t, x);
  2165. } catch (e) {
  2166. sink.error(t, e);
  2167. }
  2168. }
  2169. function tryEnd(t, sink) {
  2170. try {
  2171. sink.end(t);
  2172. } catch (e) {
  2173. sink.error(t, e);
  2174. }
  2175. }
  2176. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  2177. /** @author Brian Cavalier */
  2178. /** @author John Hann */
  2179. /**
  2180. * If stream encounters an error, recover and continue with items from stream
  2181. * returned by f.
  2182. * @param {function(error:*):Stream} f function which returns a new stream
  2183. * @param {Stream} stream
  2184. * @returns {Stream} new stream which will recover from an error by calling f
  2185. */
  2186. var recoverWith$1 = function recoverWith(f, stream) {
  2187. return isCanonicalEmpty(stream) ? empty() : new RecoverWith(f, stream);
  2188. };
  2189. /**
  2190. * Create a stream containing only an error
  2191. * @param {*} e error value, preferably an Error or Error subtype
  2192. * @returns {Stream} new stream containing only an error
  2193. */
  2194. var throwError = function throwError(e) {
  2195. return new ErrorStream(e);
  2196. };
  2197. var ErrorStream = /*#__PURE__*/function () {
  2198. function ErrorStream(e) {
  2199. classCallCheck(this, ErrorStream);
  2200. this.value = e;
  2201. }
  2202. ErrorStream.prototype.run = function run(sink, scheduler$$1) {
  2203. return scheduler.asap(propagateErrorTask$1(this.value, sink), scheduler$$1);
  2204. };
  2205. return ErrorStream;
  2206. }();
  2207. var RecoverWith = /*#__PURE__*/function () {
  2208. function RecoverWith(f, source) {
  2209. classCallCheck(this, RecoverWith);
  2210. this.f = f;
  2211. this.source = source;
  2212. }
  2213. RecoverWith.prototype.run = function run(sink, scheduler$$1) {
  2214. return new RecoverWithSink(this.f, this.source, sink, scheduler$$1);
  2215. };
  2216. return RecoverWith;
  2217. }();
  2218. var RecoverWithSink = /*#__PURE__*/function () {
  2219. function RecoverWithSink(f, source, sink, scheduler$$1) {
  2220. classCallCheck(this, RecoverWithSink);
  2221. this.f = f;
  2222. this.sink = new SafeSink(sink);
  2223. this.scheduler = scheduler$$1;
  2224. this.disposable = source.run(this, scheduler$$1);
  2225. }
  2226. RecoverWithSink.prototype.event = function event(t, x) {
  2227. tryEvent(t, x, this.sink);
  2228. };
  2229. RecoverWithSink.prototype.end = function end(t) {
  2230. tryEnd(t, this.sink);
  2231. };
  2232. RecoverWithSink.prototype.error = function error(t, e) {
  2233. var nextSink = this.sink.disable();
  2234. disposable.tryDispose(t, this.disposable, this.sink);
  2235. this._startNext(t, e, nextSink);
  2236. };
  2237. RecoverWithSink.prototype._startNext = function _startNext(t, x, sink) {
  2238. try {
  2239. this.disposable = this._continue(this.f, t, x, sink);
  2240. } catch (e) {
  2241. sink.error(t, e);
  2242. }
  2243. };
  2244. RecoverWithSink.prototype._continue = function _continue(f, t, x, sink) {
  2245. return run$1(sink, this.scheduler, withLocalTime$1(t, f(x)));
  2246. };
  2247. RecoverWithSink.prototype.dispose = function dispose() {
  2248. return this.disposable.dispose();
  2249. };
  2250. return RecoverWithSink;
  2251. }();
  2252. var multicast = function multicast(stream) {
  2253. return stream instanceof Multicast || isCanonicalEmpty(stream) ? stream : new Multicast(stream);
  2254. };
  2255. var Multicast = /*#__PURE__*/function () {
  2256. function Multicast(source) {
  2257. classCallCheck(this, Multicast);
  2258. this.source = new MulticastSource(source);
  2259. }
  2260. Multicast.prototype.run = function run(sink, scheduler$$1) {
  2261. return this.source.run(sink, scheduler$$1);
  2262. };
  2263. return Multicast;
  2264. }();
  2265. var MulticastSource = /*#__PURE__*/function () {
  2266. function MulticastSource(source) {
  2267. classCallCheck(this, MulticastSource);
  2268. this.source = source;
  2269. this.sinks = [];
  2270. this.disposable = disposable.disposeNone();
  2271. }
  2272. MulticastSource.prototype.run = function run(sink, scheduler$$1) {
  2273. var n = this.add(sink);
  2274. if (n === 1) {
  2275. this.disposable = this.source.run(this, scheduler$$1);
  2276. }
  2277. return disposable.disposeOnce(new MulticastDisposable(this, sink));
  2278. };
  2279. MulticastSource.prototype.dispose = function dispose() {
  2280. var disposable$$1 = this.disposable;
  2281. this.disposable = disposable.disposeNone();
  2282. return disposable$$1.dispose();
  2283. };
  2284. MulticastSource.prototype.add = function add(sink) {
  2285. this.sinks = prelude.append(sink, this.sinks);
  2286. return this.sinks.length;
  2287. };
  2288. MulticastSource.prototype.remove = function remove$$1(sink) {
  2289. var i = prelude.findIndex(sink, this.sinks);
  2290. // istanbul ignore next
  2291. if (i >= 0) {
  2292. this.sinks = prelude.remove(i, this.sinks);
  2293. }
  2294. return this.sinks.length;
  2295. };
  2296. MulticastSource.prototype.event = function event(time, value) {
  2297. var s = this.sinks;
  2298. if (s.length === 1) {
  2299. return s[0].event(time, value);
  2300. }
  2301. for (var i = 0; i < s.length; ++i) {
  2302. tryEvent(time, value, s[i]);
  2303. }
  2304. };
  2305. MulticastSource.prototype.end = function end(time) {
  2306. var s = this.sinks;
  2307. for (var i = 0; i < s.length; ++i) {
  2308. tryEnd(time, s[i]);
  2309. }
  2310. };
  2311. MulticastSource.prototype.error = function error(time, err) {
  2312. var s = this.sinks;
  2313. for (var i = 0; i < s.length; ++i) {
  2314. s[i].error(time, err);
  2315. }
  2316. };
  2317. return MulticastSource;
  2318. }();
  2319. var MulticastDisposable = /*#__PURE__*/function () {
  2320. function MulticastDisposable(source, sink) {
  2321. classCallCheck(this, MulticastDisposable);
  2322. this.source = source;
  2323. this.sink = sink;
  2324. }
  2325. MulticastDisposable.prototype.dispose = function dispose() {
  2326. if (this.source.remove(this.sink) === 0) {
  2327. this.source.dispose();
  2328. }
  2329. };
  2330. return MulticastDisposable;
  2331. }();
  2332. /** @license MIT License (c) copyright 2016 original author or authors */
  2333. /* eslint-disable import/first */
  2334. var zipItems$$1 = /*#__PURE__*/prelude.curry3(zipItems$1);
  2335. var withItems$$1 = /*#__PURE__*/prelude.curry2(withItems$1);
  2336. // -----------------------------------------------------------------------
  2337. // Observing
  2338. var runEffects$$1 = /*#__PURE__*/prelude.curry2(runEffects$1);
  2339. var run$$1 = /*#__PURE__*/prelude.curry3(run$1);
  2340. // -------------------------------------------------------
  2341. var withLocalTime$$1 = /*#__PURE__*/prelude.curry2(withLocalTime$1);
  2342. // -------------------------------------------------------
  2343. var loop$$1 = /*#__PURE__*/prelude.curry3(loop$1);
  2344. // -------------------------------------------------------
  2345. var scan$$1 = /*#__PURE__*/prelude.curry3(scan$1);
  2346. // -----------------------------------------------------------------------
  2347. // Extending
  2348. var startWith$$1 = /*#__PURE__*/prelude.curry2(startWith$1);
  2349. // -----------------------------------------------------------------------
  2350. // Transforming
  2351. var map$1 = /*#__PURE__*/prelude.curry2(map$2);
  2352. var constant$$1 = /*#__PURE__*/prelude.curry2(constant$1);
  2353. var tap$$1 = /*#__PURE__*/prelude.curry2(tap$1);
  2354. var ap$$1 = /*#__PURE__*/prelude.curry2(ap$1);
  2355. // -----------------------------------------------------------------------
  2356. // FlatMapping
  2357. var chain$$1 = /*#__PURE__*/prelude.curry2(chain$1);
  2358. var continueWith$$1 = /*#__PURE__*/prelude.curry2(continueWith$1);
  2359. var concatMap$$1 = /*#__PURE__*/prelude.curry2(concatMap$1);
  2360. // -----------------------------------------------------------------------
  2361. // Concurrent merging
  2362. var mergeConcurrently$$1 = /*#__PURE__*/prelude.curry2(mergeConcurrently$1);
  2363. var mergeMapConcurrently$$1 = /*#__PURE__*/prelude.curry3(mergeMapConcurrently$1);
  2364. // -----------------------------------------------------------------------
  2365. // Merging
  2366. var merge$$1 = /*#__PURE__*/prelude.curry2(merge$1);
  2367. // -----------------------------------------------------------------------
  2368. // Combining
  2369. var combine$$1 = /*#__PURE__*/prelude.curry3(combine$1);
  2370. var combineArray$$1 = /*#__PURE__*/prelude.curry2(combineArray$1);
  2371. // -----------------------------------------------------------------------
  2372. // Sampling
  2373. var sample$$1 = /*#__PURE__*/prelude.curry2(sample$1);
  2374. var snapshot$$1 = /*#__PURE__*/prelude.curry3(snapshot$1);
  2375. // -----------------------------------------------------------------------
  2376. // Zipping
  2377. var zip$$1 = /*#__PURE__*/prelude.curry3(zip$1);
  2378. var zipArray$$1 = /*#__PURE__*/prelude.curry2(zipArray$1);
  2379. // -----------------------------------------------------------------------
  2380. // Filtering
  2381. var filter$$1 = /*#__PURE__*/prelude.curry2(filter$1);
  2382. var skipRepeatsWith$$1 = /*#__PURE__*/prelude.curry2(skipRepeatsWith$1);
  2383. // -----------------------------------------------------------------------
  2384. // Slicing
  2385. var take$$1 = /*#__PURE__*/prelude.curry2(take$1);
  2386. var skip$$1 = /*#__PURE__*/prelude.curry2(skip$1);
  2387. var slice$$1 = /*#__PURE__*/prelude.curry3(slice$1);
  2388. var takeWhile$$1 = /*#__PURE__*/prelude.curry2(takeWhile$1);
  2389. var skipWhile$$1 = /*#__PURE__*/prelude.curry2(skipWhile$1);
  2390. var skipAfter$$1 = /*#__PURE__*/prelude.curry2(skipAfter$1);
  2391. // -----------------------------------------------------------------------
  2392. // Time slicing
  2393. var until$$1 = /*#__PURE__*/prelude.curry2(until$1);
  2394. var since$$1 = /*#__PURE__*/prelude.curry2(since$1);
  2395. var during$$1 = /*#__PURE__*/prelude.curry2(during$1);
  2396. // -----------------------------------------------------------------------
  2397. // Delaying
  2398. var delay$1 = /*#__PURE__*/prelude.curry2(delay$2);
  2399. // -----------------------------------------------------------------------
  2400. // Rate limiting
  2401. var throttle$$1 = /*#__PURE__*/prelude.curry2(throttle$1);
  2402. var debounce$$1 = /*#__PURE__*/prelude.curry2(debounce$1);
  2403. // -----------------------------------------------------------------------
  2404. // Error handling
  2405. var recoverWith$$1 = /*#__PURE__*/prelude.curry2(recoverWith$1);
  2406. // ----------------------------------------------------------------------
  2407. var propagateTask$$1 = /*#__PURE__*/prelude.curry3(propagateTask$1);
  2408. var propagateEventTask$$1 = /*#__PURE__*/prelude.curry2(propagateEventTask$1);
  2409. var propagateErrorTask$$1 = /*#__PURE__*/prelude.curry2(propagateErrorTask$1);
  2410. exports.zipItems = zipItems$$1;
  2411. exports.withItems = withItems$$1;
  2412. exports.runEffects = runEffects$$1;
  2413. exports.run = run$$1;
  2414. exports.withLocalTime = withLocalTime$$1;
  2415. exports.loop = loop$$1;
  2416. exports.scan = scan$$1;
  2417. exports.startWith = startWith$$1;
  2418. exports.map = map$1;
  2419. exports.constant = constant$$1;
  2420. exports.tap = tap$$1;
  2421. exports.ap = ap$$1;
  2422. exports.chain = chain$$1;
  2423. exports.join = join;
  2424. exports.continueWith = continueWith$$1;
  2425. exports.concatMap = concatMap$$1;
  2426. exports.mergeConcurrently = mergeConcurrently$$1;
  2427. exports.mergeMapConcurrently = mergeMapConcurrently$$1;
  2428. exports.merge = merge$$1;
  2429. exports.mergeArray = mergeArray;
  2430. exports.combine = combine$$1;
  2431. exports.combineArray = combineArray$$1;
  2432. exports.sample = sample$$1;
  2433. exports.snapshot = snapshot$$1;
  2434. exports.zip = zip$$1;
  2435. exports.zipArray = zipArray$$1;
  2436. exports.filter = filter$$1;
  2437. exports.skipRepeats = skipRepeats;
  2438. exports.skipRepeatsWith = skipRepeatsWith$$1;
  2439. exports.take = take$$1;
  2440. exports.skip = skip$$1;
  2441. exports.slice = slice$$1;
  2442. exports.takeWhile = takeWhile$$1;
  2443. exports.skipWhile = skipWhile$$1;
  2444. exports.skipAfter = skipAfter$$1;
  2445. exports.until = until$$1;
  2446. exports.since = since$$1;
  2447. exports.during = during$$1;
  2448. exports.delay = delay$1;
  2449. exports.throttle = throttle$$1;
  2450. exports.debounce = debounce$$1;
  2451. exports.recoverWith = recoverWith$$1;
  2452. exports.throwError = throwError;
  2453. exports.propagateTask = propagateTask$$1;
  2454. exports.propagateEventTask = propagateEventTask$$1;
  2455. exports.propagateErrorTask = propagateErrorTask$$1;
  2456. exports.propagateEndTask = propagateEndTask;
  2457. exports.empty = empty;
  2458. exports.never = never;
  2459. exports.now = now;
  2460. exports.at = at;
  2461. exports.periodic = periodic$1;
  2462. exports.newStream = newStream;
  2463. exports.switchLatest = switchLatest;
  2464. exports.fromPromise = fromPromise;
  2465. exports.awaitPromises = awaitPromises;
  2466. exports.multicast = multicast;
  2467. exports.MulticastSource = MulticastSource;
  2468. Object.defineProperty(exports, '__esModule', { value: true });
  2469. })));
  2470. //# sourceMappingURL=index.js.map