├── .eslintrc.js ├── .gitignore ├── .prettierrc.js ├── .travis.yml ├── LICENSE ├── README.md ├── examples ├── add-inputs │ ├── app.js │ ├── index.html │ └── index.ts ├── secret-combination │ ├── app.js │ ├── index.html │ └── index.ts └── simple-clock │ ├── app.js │ ├── index.html │ └── index.ts ├── package-lock.json ├── package.json ├── src ├── index.ts └── snapshotTime.ts └── tsconfig.json /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | extends: [ 4 | 'plugin:@typescript-eslint/recommended', 5 | 'prettier/@typescript-eslint', 6 | 'plugin:prettier/recommended' 7 | ], 8 | parserOptions: { 9 | ecmaVersion: 2018, 10 | sourceType: 'module' 11 | }, 12 | rules: { 13 | '@typescript-eslint/no-use-before-define': 'off' 14 | } 15 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .idea/ 3 | .nyc_output/ 4 | dist/ 5 | .DS_Store 6 | experiments/ 7 | .cache/ 8 | .rts2_cache_*/ 9 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | semi: false, 3 | singleQuote: true, 4 | printWidth: 120, 5 | tabWidth: 2 6 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - "lts/*" 5 | env: 6 | - CXX=g++-4.8 7 | branches: 8 | only: 9 | - master 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 mostjs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Behavior 2 | 3 | **EXPERIMENTAL** Don't use this for anything real ... yet. 4 | 5 | Continuous time-varying values for most.js. Behaviors are the continuous complement to most.js discrete Event Streams, or, if you prefer, the "pull" complement to most.js "push" Event Streams. 6 | 7 | ## Try it 8 | 9 | Feedback welcome via [gitter](https://gitter.im/cujojs/most), but seriously, don't use it for anything real yet. 10 | 11 | ``` 12 | npm i --save @briancavalier/most-behavior 13 | ``` 14 | 15 | ## Behavior 16 | 17 | A Behavior is a continuous value. In contrast to an Event Stream which has discrete occurrences at particular instants in time, a Behavior's value is defined at all *real number* (not integer) values of time and may vary continuously (or not) over time. 18 | 19 | Because they are defined for all real number values of time, a Behavior must be *sampled* to obtain its value at specific instants in time. To sample a Behavior, pass it an Event Stream whose occurrences define all the points in time at which the behavior's value should be sampled. 20 | 21 | Here's a simple example. Note that because `clock` is "pull", it does no work at the instants between clicks, where it is *not* being sampled. 22 | 23 | ```js 24 | import { time } from '@briancavalier/most-behavior' 25 | import { click } from '@most/dom-event' 26 | 27 | // A Behavior that always represents milliseconds since the application started 28 | const clock = time 29 | 30 | // Sample the clock each time the user clicks 31 | const timeAtEachClick = sample(clock, click(document)) 32 | ``` 33 | 34 | For now, [see the examples dir](examples) for more realistic code, how to run a `@most/core` app that integrates behaviors, etc. 35 | 36 | ## API 37 | 38 | ### Creating Behaviors 39 | 40 | #### time :: Behavior number 41 | 42 | A behavior that represents the current time in milliseconds since the application started. 43 | 44 | #### always :: a → Behavior a 45 | 46 | Create a Behavior whose value is always `a`. 47 | 48 | #### step :: a → Stream a → Behavior a 49 | 50 | Create a Behavior that starts with an initial value and updates to each new value in the Event Stream. 51 | 52 | ### Transforming Behaviors 53 | 54 | #### map :: (a → b) → Behavior a → Behavior b 55 | 56 | Apply a function to a Behavior at all points in time. 57 | 58 | #### apply :: Behavior (a → b) → Behavior a → Behavior b 59 | 60 | Apply a (time-varying) function to a Behavior at all points in time. 61 | 62 | #### liftA2 :: (a → b → c) → Behavior a → Behavior b → Behavior c 63 | 64 | Apply a function to 2 Behaviors at all points in time. 65 | 66 | ### Sampling Behaviors 67 | 68 | #### sample :: Behavior a → Stream b → Stream a 69 | 70 | Sample a Behavior's value at every occurrence of an Event Stream. 71 | 72 | #### snapshot :: Behavior a → Stream b → Stream [a, b] 73 | 74 | Sample a Behavior at every occurrence of an event, and compute a new event from the (event, sample) pair. 75 | 76 | ## Potential APIs 77 | 78 | Potentially useful APIs we could add: 79 | 80 | ### when :: Behavior bool → Stream a → Stream a 81 | 82 | Allow events only when a Behavior's value is `true`. 83 | 84 | ### accum :: a → Stream (a → a) → Behavior a 85 | 86 | Create a Behavior with an initial value and an Event Stream carrying update functions. 87 | 88 | ### scanB :: (a → b → a) → a → Stream b → Behavior a 89 | 90 | Like scan, but produces a Behavior. Needs a helpful name ... 91 | 92 | ### scanB :: (a → b → Behavior a) → Behavior a → Stream b → Behavior b 93 | 94 | Generalized scan for Behaviors. When event occurs, sample Behavior, and apply a function that creates a new Behavior. Somewhat like `switch`. Needs a helpful name ... 95 | 96 | ### count :: Stream a → Behavior number 97 | 98 | Create a Behavior representing the number of event occurrences. 99 | 100 | ### switch :: Behavior a → Stream (Behavior a) → Behavior a 101 | 102 | Create a Behavior that acts like an initial Behavior and switches to act like each new Behavior that occurs in the Event Stream. 103 | -------------------------------------------------------------------------------- /examples/add-inputs/app.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 'use strict'; 3 | 4 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 5 | 6 | // append :: a -> [a] -> [a] 7 | // a with x appended 8 | function append(x, a) { 9 | var l = a.length; 10 | var b = new Array(l + 1); 11 | for (var i = 0; i < l; ++i) { 12 | b[i] = a[i]; 13 | } 14 | 15 | b[l] = x; 16 | return b; 17 | } 18 | 19 | // map :: (a -> b) -> [a] -> [b] 20 | // transform each element with f 21 | function map(f, a) { 22 | var l = a.length; 23 | var b = new Array(l); 24 | for (var i = 0; i < l; ++i) { 25 | b[i] = f(a[i]); 26 | } 27 | return b; 28 | } 29 | 30 | // reduce :: (a -> b -> a) -> a -> [b] -> a 31 | // accumulate via left-fold 32 | function reduce(f, z, a) { 33 | var r = z; 34 | for (var i = 0, l = a.length; i < l; ++i) { 35 | r = f(r, a[i], i); 36 | } 37 | return r; 38 | } 39 | 40 | // remove :: Int -> [a] -> [a] 41 | // remove element at index 42 | function remove(i, a) { 43 | // eslint-disable-line complexity 44 | if (i < 0) { 45 | throw new TypeError('i must be >= 0'); 46 | } 47 | 48 | var l = a.length; 49 | if (l === 0 || i >= l) { 50 | // exit early if index beyond end of array 51 | return a; 52 | } 53 | 54 | if (l === 1) { 55 | // exit early if index in bounds and length === 1 56 | return []; 57 | } 58 | 59 | return unsafeRemove(i, a, l - 1); 60 | } 61 | 62 | // unsafeRemove :: Int -> [a] -> Int -> [a] 63 | // Internal helper to remove element at index 64 | function unsafeRemove(i, a, l) { 65 | var b = new Array(l); 66 | var j = void 0; 67 | for (j = 0; j < i; ++j) { 68 | b[j] = a[j]; 69 | } 70 | for (j = i; j < l; ++j) { 71 | b[j] = a[j + 1]; 72 | } 73 | 74 | return b; 75 | } 76 | 77 | // removeAll :: (a -> boolean) -> [a] -> [a] 78 | // remove all elements matching a predicate 79 | // @deprecated 80 | function removeAll(f, a) { 81 | var l = a.length; 82 | var b = new Array(l); 83 | var j = 0; 84 | for (var x, i = 0; i < l; ++i) { 85 | x = a[i]; 86 | if (!f(x)) { 87 | b[j] = x; 88 | ++j; 89 | } 90 | } 91 | 92 | b.length = j; 93 | return b; 94 | } 95 | 96 | // findIndex :: a -> [a] -> Int 97 | // find index of x in a, from the left 98 | function findIndex(x, a) { 99 | for (var i = 0, l = a.length; i < l; ++i) { 100 | if (x === a[i]) { 101 | return i; 102 | } 103 | } 104 | return -1; 105 | } 106 | 107 | // compose :: (b -> c) -> (a -> b) -> (a -> c) 108 | var compose = function compose(f, g) { 109 | return function (x) { 110 | return f(g(x)); 111 | }; 112 | }; 113 | 114 | // curry2 :: ((a, b) -> c) -> (a -> b -> c) 115 | function curry2(f) { 116 | function curried(a, b) { 117 | switch (arguments.length) { 118 | case 0: 119 | return curried; 120 | case 1: 121 | return function (b) { 122 | return f(a, b); 123 | }; 124 | default: 125 | return f(a, b); 126 | } 127 | } 128 | return curried; 129 | } 130 | 131 | // curry3 :: ((a, b, c) -> d) -> (a -> b -> c -> d) 132 | function curry3(f) { 133 | function curried(a, b, c) { 134 | // eslint-disable-line complexity 135 | switch (arguments.length) { 136 | case 0: 137 | return curried; 138 | case 1: 139 | return curry2(function (b, c) { 140 | return f(a, b, c); 141 | }); 142 | case 2: 143 | return function (c) { 144 | return f(a, b, c); 145 | }; 146 | default: 147 | return f(a, b, c); 148 | } 149 | } 150 | return curried; 151 | } 152 | 153 | var classCallCheck = function (instance, Constructor) { 154 | if (!(instance instanceof Constructor)) { 155 | throw new TypeError("Cannot call a class as a function"); 156 | } 157 | }; 158 | 159 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 160 | 161 | var ScheduledTask = /*#__PURE__*/function () { 162 | function ScheduledTask(time, localOffset, period, task, scheduler) { 163 | classCallCheck(this, ScheduledTask); 164 | 165 | this.time = time; 166 | this.localOffset = localOffset; 167 | this.period = period; 168 | this.task = task; 169 | this.scheduler = scheduler; 170 | this.active = true; 171 | } 172 | 173 | ScheduledTask.prototype.run = function run() { 174 | return this.task.run(this.time - this.localOffset); 175 | }; 176 | 177 | ScheduledTask.prototype.error = function error(e) { 178 | return this.task.error(this.time - this.localOffset, e); 179 | }; 180 | 181 | ScheduledTask.prototype.dispose = function dispose() { 182 | this.scheduler.cancel(this); 183 | return this.task.dispose(); 184 | }; 185 | 186 | return ScheduledTask; 187 | }(); 188 | 189 | var RelativeScheduler = /*#__PURE__*/function () { 190 | function RelativeScheduler(origin, scheduler) { 191 | classCallCheck(this, RelativeScheduler); 192 | 193 | this.origin = origin; 194 | this.scheduler = scheduler; 195 | } 196 | 197 | RelativeScheduler.prototype.currentTime = function currentTime() { 198 | return this.scheduler.currentTime() - this.origin; 199 | }; 200 | 201 | RelativeScheduler.prototype.scheduleTask = function scheduleTask(localOffset, delay, period, task) { 202 | return this.scheduler.scheduleTask(localOffset + this.origin, delay, period, task); 203 | }; 204 | 205 | RelativeScheduler.prototype.relative = function relative(origin) { 206 | return new RelativeScheduler(origin + this.origin, this.scheduler); 207 | }; 208 | 209 | RelativeScheduler.prototype.cancel = function cancel(task) { 210 | return this.scheduler.cancel(task); 211 | }; 212 | 213 | RelativeScheduler.prototype.cancelAll = function cancelAll(f) { 214 | return this.scheduler.cancelAll(f); 215 | }; 216 | 217 | return RelativeScheduler; 218 | }(); 219 | 220 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 221 | 222 | var defer = function defer(task) { 223 | return Promise.resolve(task).then(runTask); 224 | }; 225 | 226 | function runTask(task) { 227 | try { 228 | return task.run(); 229 | } catch (e) { 230 | return task.error(e); 231 | } 232 | } 233 | 234 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 235 | 236 | var Scheduler = /*#__PURE__*/function () { 237 | function Scheduler(timer, timeline) { 238 | var _this = this; 239 | 240 | classCallCheck(this, Scheduler); 241 | 242 | this.timer = timer; 243 | this.timeline = timeline; 244 | 245 | this._timer = null; 246 | this._nextArrival = Infinity; 247 | 248 | this._runReadyTasksBound = function () { 249 | return _this._runReadyTasks(_this.currentTime()); 250 | }; 251 | } 252 | 253 | Scheduler.prototype.currentTime = function currentTime() { 254 | return this.timer.now(); 255 | }; 256 | 257 | Scheduler.prototype.scheduleTask = function scheduleTask(localOffset, delay, period, task) { 258 | var time = this.currentTime() + Math.max(0, delay); 259 | var st = new ScheduledTask(time, localOffset, period, task, this); 260 | 261 | this.timeline.add(st); 262 | this._scheduleNextRun(); 263 | return st; 264 | }; 265 | 266 | Scheduler.prototype.relative = function relative(offset) { 267 | return new RelativeScheduler(offset, this); 268 | }; 269 | 270 | Scheduler.prototype.cancel = function cancel(task) { 271 | task.active = false; 272 | if (this.timeline.remove(task)) { 273 | this._reschedule(); 274 | } 275 | }; 276 | 277 | // @deprecated 278 | 279 | 280 | Scheduler.prototype.cancelAll = function cancelAll(f) { 281 | this.timeline.removeAll(f); 282 | this._reschedule(); 283 | }; 284 | 285 | Scheduler.prototype._reschedule = function _reschedule() { 286 | if (this.timeline.isEmpty()) { 287 | this._unschedule(); 288 | } else { 289 | this._scheduleNextRun(this.currentTime()); 290 | } 291 | }; 292 | 293 | Scheduler.prototype._unschedule = function _unschedule() { 294 | this.timer.clearTimer(this._timer); 295 | this._timer = null; 296 | }; 297 | 298 | Scheduler.prototype._scheduleNextRun = function _scheduleNextRun() { 299 | // eslint-disable-line complexity 300 | if (this.timeline.isEmpty()) { 301 | return; 302 | } 303 | 304 | var nextArrival = this.timeline.nextArrival(); 305 | 306 | if (this._timer === null) { 307 | this._scheduleNextArrival(nextArrival); 308 | } else if (nextArrival < this._nextArrival) { 309 | this._unschedule(); 310 | this._scheduleNextArrival(nextArrival); 311 | } 312 | }; 313 | 314 | Scheduler.prototype._scheduleNextArrival = function _scheduleNextArrival(nextArrival) { 315 | this._nextArrival = nextArrival; 316 | var delay = Math.max(0, nextArrival - this.currentTime()); 317 | this._timer = this.timer.setTimer(this._runReadyTasksBound, delay); 318 | }; 319 | 320 | Scheduler.prototype._runReadyTasks = function _runReadyTasks() { 321 | this._timer = null; 322 | this.timeline.runTasks(this.currentTime(), runTask); 323 | this._scheduleNextRun(); 324 | }; 325 | 326 | return Scheduler; 327 | }(); 328 | 329 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 330 | 331 | var Timeline = /*#__PURE__*/function () { 332 | function Timeline() { 333 | classCallCheck(this, Timeline); 334 | 335 | this.tasks = []; 336 | } 337 | 338 | Timeline.prototype.nextArrival = function nextArrival() { 339 | return this.isEmpty() ? Infinity : this.tasks[0].time; 340 | }; 341 | 342 | Timeline.prototype.isEmpty = function isEmpty() { 343 | return this.tasks.length === 0; 344 | }; 345 | 346 | Timeline.prototype.add = function add(st) { 347 | insertByTime(st, this.tasks); 348 | }; 349 | 350 | Timeline.prototype.remove = function remove$$1(st) { 351 | var i = binarySearch(getTime(st), this.tasks); 352 | 353 | if (i >= 0 && i < this.tasks.length) { 354 | var at = findIndex(st, this.tasks[i].events); 355 | if (at >= 0) { 356 | this.tasks[i].events.splice(at, 1); 357 | return true; 358 | } 359 | } 360 | 361 | return false; 362 | }; 363 | 364 | // @deprecated 365 | 366 | 367 | Timeline.prototype.removeAll = function removeAll$$1(f) { 368 | for (var i = 0; i < this.tasks.length; ++i) { 369 | removeAllFrom(f, this.tasks[i]); 370 | } 371 | }; 372 | 373 | Timeline.prototype.runTasks = function runTasks(t, runTask) { 374 | var tasks = this.tasks; 375 | var l = tasks.length; 376 | var i = 0; 377 | 378 | while (i < l && tasks[i].time <= t) { 379 | ++i; 380 | } 381 | 382 | this.tasks = tasks.slice(i); 383 | 384 | // Run all ready tasks 385 | for (var j = 0; j < i; ++j) { 386 | this.tasks = runReadyTasks(runTask, tasks[j].events, this.tasks); 387 | } 388 | }; 389 | 390 | return Timeline; 391 | }(); 392 | 393 | function runReadyTasks(runTask, events, tasks) { 394 | // eslint-disable-line complexity 395 | for (var i = 0; i < events.length; ++i) { 396 | var task = events[i]; 397 | 398 | if (task.active) { 399 | runTask(task); 400 | 401 | // Reschedule periodic repeating tasks 402 | // Check active again, since a task may have canceled itself 403 | if (task.period >= 0 && task.active) { 404 | task.time = task.time + task.period; 405 | insertByTime(task, tasks); 406 | } 407 | } 408 | } 409 | 410 | return tasks; 411 | } 412 | 413 | function insertByTime(task, timeslots) { 414 | var l = timeslots.length; 415 | var time = getTime(task); 416 | 417 | if (l === 0) { 418 | timeslots.push(newTimeslot(time, [task])); 419 | return; 420 | } 421 | 422 | var i = binarySearch(time, timeslots); 423 | 424 | if (i >= l) { 425 | timeslots.push(newTimeslot(time, [task])); 426 | } else { 427 | insertAtTimeslot(task, timeslots, time, i); 428 | } 429 | } 430 | 431 | function insertAtTimeslot(task, timeslots, time, i) { 432 | var timeslot = timeslots[i]; 433 | if (time === timeslot.time) { 434 | addEvent(task, timeslot.events, time); 435 | } else { 436 | timeslots.splice(i, 0, newTimeslot(time, [task])); 437 | } 438 | } 439 | 440 | function addEvent(task, events) { 441 | if (events.length === 0 || task.time >= events[events.length - 1].time) { 442 | events.push(task); 443 | } else { 444 | spliceEvent(task, events); 445 | } 446 | } 447 | 448 | function spliceEvent(task, events) { 449 | for (var j = 0; j < events.length; j++) { 450 | if (task.time < events[j].time) { 451 | events.splice(j, 0, task); 452 | break; 453 | } 454 | } 455 | } 456 | 457 | function getTime(scheduledTask) { 458 | return Math.floor(scheduledTask.time); 459 | } 460 | 461 | // @deprecated 462 | function removeAllFrom(f, timeslot) { 463 | timeslot.events = removeAll(f, timeslot.events); 464 | } 465 | 466 | function binarySearch(t, sortedArray) { 467 | // eslint-disable-line complexity 468 | var lo = 0; 469 | var hi = sortedArray.length; 470 | var mid = void 0, 471 | y = void 0; 472 | 473 | while (lo < hi) { 474 | mid = Math.floor((lo + hi) / 2); 475 | y = sortedArray[mid]; 476 | 477 | if (t === y.time) { 478 | return mid; 479 | } else if (t < y.time) { 480 | hi = mid; 481 | } else { 482 | lo = mid + 1; 483 | } 484 | } 485 | return hi; 486 | } 487 | 488 | var newTimeslot = function newTimeslot(t, events) { 489 | return { time: t, events: events }; 490 | }; 491 | 492 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 493 | 494 | /* global setTimeout, clearTimeout */ 495 | 496 | var ClockTimer = /*#__PURE__*/function () { 497 | function ClockTimer(clock) { 498 | classCallCheck(this, ClockTimer); 499 | 500 | this._clock = clock; 501 | } 502 | 503 | ClockTimer.prototype.now = function now() { 504 | return this._clock.now(); 505 | }; 506 | 507 | ClockTimer.prototype.setTimer = function setTimer(f, dt) { 508 | return dt <= 0 ? runAsap(f) : setTimeout(f, dt); 509 | }; 510 | 511 | ClockTimer.prototype.clearTimer = function clearTimer(t) { 512 | return t instanceof Asap ? t.cancel() : clearTimeout(t); 513 | }; 514 | 515 | return ClockTimer; 516 | }(); 517 | 518 | var Asap = /*#__PURE__*/function () { 519 | function Asap(f) { 520 | classCallCheck(this, Asap); 521 | 522 | this.f = f; 523 | this.active = true; 524 | } 525 | 526 | Asap.prototype.run = function run() { 527 | return this.active && this.f(); 528 | }; 529 | 530 | Asap.prototype.error = function error(e) { 531 | throw e; 532 | }; 533 | 534 | Asap.prototype.cancel = function cancel() { 535 | this.active = false; 536 | }; 537 | 538 | return Asap; 539 | }(); 540 | 541 | function runAsap(f) { 542 | var task = new Asap(f); 543 | defer(task); 544 | return task; 545 | } 546 | 547 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 548 | 549 | /* global performance, process */ 550 | 551 | var RelativeClock = /*#__PURE__*/function () { 552 | function RelativeClock(clock, origin) { 553 | classCallCheck(this, RelativeClock); 554 | 555 | this.origin = origin; 556 | this.clock = clock; 557 | } 558 | 559 | RelativeClock.prototype.now = function now() { 560 | return this.clock.now() - this.origin; 561 | }; 562 | 563 | return RelativeClock; 564 | }(); 565 | 566 | var HRTimeClock = /*#__PURE__*/function () { 567 | function HRTimeClock(hrtime, origin) { 568 | classCallCheck(this, HRTimeClock); 569 | 570 | this.origin = origin; 571 | this.hrtime = hrtime; 572 | } 573 | 574 | HRTimeClock.prototype.now = function now() { 575 | var hrt = this.hrtime(this.origin); 576 | return (hrt[0] * 1e9 + hrt[1]) / 1e6; 577 | }; 578 | 579 | return HRTimeClock; 580 | }(); 581 | 582 | // Read the current time from the provided Scheduler 583 | var currentTime = function currentTime(scheduler) { 584 | return scheduler.currentTime(); 585 | }; 586 | 587 | // Schedule a task to run as soon as possible, but 588 | // not in the current call stack 589 | var asap = /*#__PURE__*/curry2(function (task, scheduler) { 590 | return scheduler.scheduleTask(0, 0, -1, task); 591 | }); 592 | 593 | // Schedule a task to run after a millisecond delay 594 | var delay = /*#__PURE__*/curry3(function (delay, task, scheduler) { 595 | return scheduler.scheduleTask(0, delay, -1, task); 596 | }); 597 | 598 | // Schedule a task to run periodically, with the 599 | // first run starting asap 600 | var periodic = /*#__PURE__*/curry3(function (period, task, scheduler) { 601 | return scheduler.scheduleTask(0, 0, period, task); 602 | }); 603 | 604 | // Cancel a scheduledTask 605 | var cancelTask = function cancelTask(scheduledTask) { 606 | return scheduledTask.dispose(); 607 | }; 608 | 609 | var schedulerRelativeTo = /*#__PURE__*/curry2(function (offset, scheduler) { 610 | return new RelativeScheduler(offset, scheduler); 611 | }); 612 | 613 | var classCallCheck$1 = function (instance, Constructor) { 614 | if (!(instance instanceof Constructor)) { 615 | throw new TypeError("Cannot call a class as a function"); 616 | } 617 | }; 618 | 619 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 620 | 621 | var disposeNone = function disposeNone() { 622 | return NONE; 623 | }; 624 | var NONE = /*#__PURE__*/new (function () { 625 | function DisposeNone() { 626 | classCallCheck$1(this, DisposeNone); 627 | } 628 | 629 | DisposeNone.prototype.dispose = function dispose() {}; 630 | 631 | return DisposeNone; 632 | }())(); 633 | 634 | var isDisposeNone = function isDisposeNone(d) { 635 | return d === NONE; 636 | }; 637 | 638 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 639 | 640 | // Wrap an existing disposable (which may not already have been once()d) 641 | // so that it will only dispose its underlying resource at most once. 642 | var disposeOnce = function disposeOnce(disposable) { 643 | return new DisposeOnce(disposable); 644 | }; 645 | 646 | var DisposeOnce = /*#__PURE__*/function () { 647 | function DisposeOnce(disposable) { 648 | classCallCheck$1(this, DisposeOnce); 649 | 650 | this.disposed = false; 651 | this.disposable = disposable; 652 | } 653 | 654 | DisposeOnce.prototype.dispose = function dispose() { 655 | if (!this.disposed) { 656 | this.disposed = true; 657 | this.disposable.dispose(); 658 | this.disposable = undefined; 659 | } 660 | }; 661 | 662 | return DisposeOnce; 663 | }(); 664 | 665 | // Disposable represents a resource that must be 666 | // disposed/released. It aggregates a function to dispose 667 | // the resource and a handle to a key/id/handle/reference 668 | // that identifies the resource 669 | 670 | var DisposeWith = /*#__PURE__*/function () { 671 | function DisposeWith(dispose, resource) { 672 | classCallCheck$1(this, DisposeWith); 673 | 674 | this._dispose = dispose; 675 | this._resource = resource; 676 | } 677 | 678 | DisposeWith.prototype.dispose = function dispose() { 679 | this._dispose(this._resource); 680 | }; 681 | 682 | return DisposeWith; 683 | }(); 684 | 685 | /** @license MIT License (c) copyright 2010 original author or authors */ 686 | // Aggregate a list of disposables into a DisposeAll 687 | var disposeAll = function disposeAll(ds) { 688 | var merged = reduce(merge, [], ds); 689 | return merged.length === 0 ? disposeNone() : new DisposeAll(merged); 690 | }; 691 | 692 | // Convenience to aggregate 2 disposables 693 | var disposeBoth = /*#__PURE__*/curry2(function (d1, d2) { 694 | return disposeAll([d1, d2]); 695 | }); 696 | 697 | var merge = function merge(ds, d) { 698 | return isDisposeNone(d) ? ds : d instanceof DisposeAll ? ds.concat(d.disposables) : append(d, ds); 699 | }; 700 | 701 | var DisposeAll = /*#__PURE__*/function () { 702 | function DisposeAll(disposables) { 703 | classCallCheck$1(this, DisposeAll); 704 | 705 | this.disposables = disposables; 706 | } 707 | 708 | DisposeAll.prototype.dispose = function dispose() { 709 | throwIfErrors(disposeCollectErrors(this.disposables)); 710 | }; 711 | 712 | return DisposeAll; 713 | }(); 714 | 715 | // Dispose all, safely collecting errors into an array 716 | 717 | 718 | var disposeCollectErrors = function disposeCollectErrors(disposables) { 719 | return reduce(appendIfError, [], disposables); 720 | }; 721 | 722 | // Call dispose and if throws, append thrown error to errors 723 | var appendIfError = function appendIfError(errors, d) { 724 | try { 725 | d.dispose(); 726 | } catch (e) { 727 | errors.push(e); 728 | } 729 | return errors; 730 | }; 731 | 732 | // Throw DisposeAllError if errors is non-empty 733 | var throwIfErrors = function throwIfErrors(errors) { 734 | if (errors.length > 0) { 735 | throw new DisposeAllError(errors.length + ' errors', errors); 736 | } 737 | }; 738 | 739 | var DisposeAllError = /*#__PURE__*/function (Error) { 740 | function DisposeAllError(message, errors) { 741 | Error.call(this, message); 742 | this.message = message; 743 | this.name = DisposeAllError.name; 744 | this.errors = errors; 745 | 746 | if (Error.captureStackTrace) { 747 | Error.captureStackTrace(this, DisposeAllError); 748 | } 749 | 750 | this.stack = '' + this.stack + formatErrorStacks(this.errors); 751 | } 752 | 753 | DisposeAllError.prototype = /*#__PURE__*/Object.create(Error.prototype); 754 | 755 | return DisposeAllError; 756 | }(Error); 757 | 758 | var formatErrorStacks = function formatErrorStacks(errors) { 759 | return reduce(formatErrorStack, '', errors); 760 | }; 761 | 762 | var formatErrorStack = function formatErrorStack(s, e, i) { 763 | return s + ('\n[' + (i + 1) + '] ' + e.stack); 764 | }; 765 | 766 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 767 | // Try to dispose the disposable. If it throws, send 768 | // the error to sink.error with the provided Time value 769 | var tryDispose = /*#__PURE__*/curry3(function (t, disposable, sink) { 770 | try { 771 | disposable.dispose(); 772 | } catch (e) { 773 | sink.error(t, e); 774 | } 775 | }); 776 | 777 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 778 | /** @author Brian Cavalier */ 779 | /** @author John Hann */ 780 | 781 | function fatalError(e) { 782 | setTimeout(rethrow, 0, e); 783 | } 784 | 785 | function rethrow(e) { 786 | throw e; 787 | } 788 | 789 | 790 | 791 | 792 | 793 | var classCallCheck$2 = function (instance, Constructor) { 794 | if (!(instance instanceof Constructor)) { 795 | throw new TypeError("Cannot call a class as a function"); 796 | } 797 | }; 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | var inherits = function (subClass, superClass) { 810 | if (typeof superClass !== "function" && superClass !== null) { 811 | throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); 812 | } 813 | 814 | subClass.prototype = Object.create(superClass && superClass.prototype, { 815 | constructor: { 816 | value: subClass, 817 | enumerable: false, 818 | writable: true, 819 | configurable: true 820 | } 821 | }); 822 | if (superClass) { Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } 823 | }; 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | var possibleConstructorReturn = function (self, call) { 836 | if (!self) { 837 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); 838 | } 839 | 840 | return call && (typeof call === "object" || typeof call === "function") ? call : self; 841 | }; 842 | 843 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 844 | /** @author Brian Cavalier */ 845 | /** @author John Hann */ 846 | 847 | var propagateTask$1 = function propagateTask(run, value, sink) { 848 | return new PropagateTask(run, value, sink); 849 | }; 850 | 851 | var propagateEventTask$1 = function propagateEventTask(value, sink) { 852 | return propagateTask$1(runEvent, value, sink); 853 | }; 854 | 855 | var propagateEndTask = function propagateEndTask(sink) { 856 | return propagateTask$1(runEnd, undefined, sink); 857 | }; 858 | 859 | var propagateErrorTask$1 = function propagateErrorTask(value, sink) { 860 | return propagateTask$1(runError, value, sink); 861 | }; 862 | 863 | var PropagateTask = /*#__PURE__*/function () { 864 | function PropagateTask(run, value, sink) { 865 | classCallCheck$2(this, PropagateTask); 866 | 867 | this._run = run; 868 | this.value = value; 869 | this.sink = sink; 870 | this.active = true; 871 | } 872 | 873 | PropagateTask.prototype.dispose = function dispose$$1() { 874 | this.active = false; 875 | }; 876 | 877 | PropagateTask.prototype.run = function run(t) { 878 | if (!this.active) { 879 | return; 880 | } 881 | var run = this._run; 882 | run(t, this.value, this.sink); 883 | }; 884 | 885 | PropagateTask.prototype.error = function error(t, e) { 886 | // TODO: Remove this check and just do this.sink.error(t, e)? 887 | if (!this.active) { 888 | return fatalError(e); 889 | } 890 | this.sink.error(t, e); 891 | }; 892 | 893 | return PropagateTask; 894 | }(); 895 | 896 | var runEvent = function runEvent(t, x, sink) { 897 | return sink.event(t, x); 898 | }; 899 | 900 | var runEnd = function runEnd(t, _, sink) { 901 | return sink.end(t); 902 | }; 903 | 904 | var runError = function runError(t, e, sink) { 905 | return sink.error(t, e); 906 | }; 907 | 908 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 909 | 910 | var empty = function empty() { 911 | return EMPTY; 912 | }; 913 | 914 | var isCanonicalEmpty = function isCanonicalEmpty(stream) { 915 | return stream === EMPTY; 916 | }; 917 | 918 | var Empty = /*#__PURE__*/function () { 919 | function Empty() { 920 | classCallCheck$2(this, Empty); 921 | } 922 | 923 | Empty.prototype.run = function run(sink, scheduler$$1) { 924 | return asap(propagateEndTask(sink), scheduler$$1); 925 | }; 926 | 927 | return Empty; 928 | }(); 929 | 930 | var EMPTY = /*#__PURE__*/new Empty(); 931 | 932 | var Never = /*#__PURE__*/function () { 933 | function Never() { 934 | classCallCheck$2(this, Never); 935 | } 936 | 937 | Never.prototype.run = function run() { 938 | return disposeNone(); 939 | }; 940 | 941 | return Never; 942 | }(); 943 | 944 | var NEVER = /*#__PURE__*/new Never(); 945 | 946 | var At = /*#__PURE__*/function () { 947 | function At(t, x) { 948 | classCallCheck$2(this, At); 949 | 950 | this.time = t; 951 | this.value = x; 952 | } 953 | 954 | At.prototype.run = function run(sink, scheduler$$1) { 955 | return delay(this.time, propagateTask$1(runAt, this.value, sink), scheduler$$1); 956 | }; 957 | 958 | return At; 959 | }(); 960 | 961 | function runAt(t, x, sink) { 962 | sink.event(t, x); 963 | sink.end(t); 964 | } 965 | 966 | var Periodic = /*#__PURE__*/function () { 967 | function Periodic(period) { 968 | classCallCheck$2(this, Periodic); 969 | 970 | this.period = period; 971 | } 972 | 973 | Periodic.prototype.run = function run(sink, scheduler$$1) { 974 | return periodic(this.period, propagateEventTask$1(undefined, sink), scheduler$$1); 975 | }; 976 | 977 | return Periodic; 978 | }(); 979 | 980 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 981 | /** @author Brian Cavalier */ 982 | 983 | var Pipe = /*#__PURE__*/function () { 984 | function Pipe(sink) { 985 | classCallCheck$2(this, Pipe); 986 | 987 | this.sink = sink; 988 | } 989 | 990 | Pipe.prototype.event = function event(t, x) { 991 | return this.sink.event(t, x); 992 | }; 993 | 994 | Pipe.prototype.end = function end(t) { 995 | return this.sink.end(t); 996 | }; 997 | 998 | Pipe.prototype.error = function error(t, e) { 999 | return this.sink.error(t, e); 1000 | }; 1001 | 1002 | return Pipe; 1003 | }(); 1004 | 1005 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 1006 | /** @author Brian Cavalier */ 1007 | /** @author John Hann */ 1008 | 1009 | var Filter = /*#__PURE__*/function () { 1010 | function Filter(p, source) { 1011 | classCallCheck$2(this, Filter); 1012 | 1013 | this.p = p; 1014 | this.source = source; 1015 | } 1016 | 1017 | Filter.prototype.run = function run(sink, scheduler$$1) { 1018 | return this.source.run(new FilterSink(this.p, sink), scheduler$$1); 1019 | }; 1020 | 1021 | /** 1022 | * Create a filtered source, fusing adjacent filter.filter if possible 1023 | * @param {function(x:*):boolean} p filtering predicate 1024 | * @param {{run:function}} source source to filter 1025 | * @returns {Filter} filtered source 1026 | */ 1027 | 1028 | 1029 | Filter.create = function create(p, source) { 1030 | if (isCanonicalEmpty(source)) { 1031 | return source; 1032 | } 1033 | 1034 | if (source instanceof Filter) { 1035 | return new Filter(and(source.p, p), source.source); 1036 | } 1037 | 1038 | return new Filter(p, source); 1039 | }; 1040 | 1041 | return Filter; 1042 | }(); 1043 | 1044 | var FilterSink = /*#__PURE__*/function (_Pipe) { 1045 | inherits(FilterSink, _Pipe); 1046 | 1047 | function FilterSink(p, sink) { 1048 | classCallCheck$2(this, FilterSink); 1049 | 1050 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1051 | 1052 | _this.p = p; 1053 | return _this; 1054 | } 1055 | 1056 | FilterSink.prototype.event = function event(t, x) { 1057 | var p = this.p; 1058 | p(x) && this.sink.event(t, x); 1059 | }; 1060 | 1061 | return FilterSink; 1062 | }(Pipe); 1063 | 1064 | var and = function and(p, q) { 1065 | return function (x) { 1066 | return p(x) && q(x); 1067 | }; 1068 | }; 1069 | 1070 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 1071 | /** @author Brian Cavalier */ 1072 | /** @author John Hann */ 1073 | 1074 | var FilterMap = /*#__PURE__*/function () { 1075 | function FilterMap(p, f, source) { 1076 | classCallCheck$2(this, FilterMap); 1077 | 1078 | this.p = p; 1079 | this.f = f; 1080 | this.source = source; 1081 | } 1082 | 1083 | FilterMap.prototype.run = function run(sink, scheduler$$1) { 1084 | return this.source.run(new FilterMapSink(this.p, this.f, sink), scheduler$$1); 1085 | }; 1086 | 1087 | return FilterMap; 1088 | }(); 1089 | 1090 | var FilterMapSink = /*#__PURE__*/function (_Pipe) { 1091 | inherits(FilterMapSink, _Pipe); 1092 | 1093 | function FilterMapSink(p, f, sink) { 1094 | classCallCheck$2(this, FilterMapSink); 1095 | 1096 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1097 | 1098 | _this.p = p; 1099 | _this.f = f; 1100 | return _this; 1101 | } 1102 | 1103 | FilterMapSink.prototype.event = function event(t, x) { 1104 | var f = this.f; 1105 | var p = this.p; 1106 | p(x) && this.sink.event(t, f(x)); 1107 | }; 1108 | 1109 | return FilterMapSink; 1110 | }(Pipe); 1111 | 1112 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 1113 | /** @author Brian Cavalier */ 1114 | /** @author John Hann */ 1115 | 1116 | var Map = /*#__PURE__*/function () { 1117 | function Map(f, source) { 1118 | classCallCheck$2(this, Map); 1119 | 1120 | this.f = f; 1121 | this.source = source; 1122 | } 1123 | 1124 | Map.prototype.run = function run(sink, scheduler$$1) { 1125 | // eslint-disable-line no-extend-native 1126 | return this.source.run(new MapSink(this.f, sink), scheduler$$1); 1127 | }; 1128 | 1129 | /** 1130 | * Create a mapped source, fusing adjacent map.map, filter.map, 1131 | * and filter.map.map if possible 1132 | * @param {function(*):*} f mapping function 1133 | * @param {{run:function}} source source to map 1134 | * @returns {Map|FilterMap} mapped source, possibly fused 1135 | */ 1136 | 1137 | 1138 | Map.create = function create(f, source) { 1139 | if (isCanonicalEmpty(source)) { 1140 | return empty(); 1141 | } 1142 | 1143 | if (source instanceof Map) { 1144 | return new Map(compose(f, source.f), source.source); 1145 | } 1146 | 1147 | if (source instanceof Filter) { 1148 | return new FilterMap(source.p, f, source.source); 1149 | } 1150 | 1151 | return new Map(f, source); 1152 | }; 1153 | 1154 | return Map; 1155 | }(); 1156 | 1157 | var MapSink = /*#__PURE__*/function (_Pipe) { 1158 | inherits(MapSink, _Pipe); 1159 | 1160 | function MapSink(f, sink) { 1161 | classCallCheck$2(this, MapSink); 1162 | 1163 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1164 | 1165 | _this.f = f; 1166 | return _this; 1167 | } 1168 | 1169 | MapSink.prototype.event = function event(t, x) { 1170 | var f = this.f; 1171 | this.sink.event(t, f(x)); 1172 | }; 1173 | 1174 | return MapSink; 1175 | }(Pipe); 1176 | 1177 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 1178 | 1179 | var SettableDisposable = /*#__PURE__*/function () { 1180 | function SettableDisposable() { 1181 | classCallCheck$2(this, SettableDisposable); 1182 | 1183 | this.disposable = undefined; 1184 | this.disposed = false; 1185 | } 1186 | 1187 | SettableDisposable.prototype.setDisposable = function setDisposable(disposable$$1) { 1188 | if (this.disposable !== void 0) { 1189 | throw new Error('setDisposable called more than once'); 1190 | } 1191 | 1192 | this.disposable = disposable$$1; 1193 | 1194 | if (this.disposed) { 1195 | disposable$$1.dispose(); 1196 | } 1197 | }; 1198 | 1199 | SettableDisposable.prototype.dispose = function dispose$$1() { 1200 | if (this.disposed) { 1201 | return; 1202 | } 1203 | 1204 | this.disposed = true; 1205 | 1206 | if (this.disposable !== void 0) { 1207 | this.disposable.dispose(); 1208 | } 1209 | }; 1210 | 1211 | return SettableDisposable; 1212 | }(); 1213 | 1214 | var Slice = /*#__PURE__*/function () { 1215 | function Slice(bounds, source) { 1216 | classCallCheck$2(this, Slice); 1217 | 1218 | this.source = source; 1219 | this.bounds = bounds; 1220 | } 1221 | 1222 | Slice.prototype.run = function run(sink, scheduler$$1) { 1223 | var disposable$$1 = new SettableDisposable(); 1224 | var sliceSink = new SliceSink(this.bounds.min, this.bounds.max - this.bounds.min, sink, disposable$$1); 1225 | 1226 | disposable$$1.setDisposable(this.source.run(sliceSink, scheduler$$1)); 1227 | 1228 | return disposable$$1; 1229 | }; 1230 | 1231 | return Slice; 1232 | }(); 1233 | 1234 | var SliceSink = /*#__PURE__*/function (_Pipe) { 1235 | inherits(SliceSink, _Pipe); 1236 | 1237 | function SliceSink(skip, take, sink, disposable$$1) { 1238 | classCallCheck$2(this, SliceSink); 1239 | 1240 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1241 | 1242 | _this.skip = skip; 1243 | _this.take = take; 1244 | _this.disposable = disposable$$1; 1245 | return _this; 1246 | } 1247 | 1248 | SliceSink.prototype.event = function event(t, x) { 1249 | /* eslint complexity: [1, 4] */ 1250 | if (this.skip > 0) { 1251 | this.skip -= 1; 1252 | return; 1253 | } 1254 | 1255 | if (this.take === 0) { 1256 | return; 1257 | } 1258 | 1259 | this.take -= 1; 1260 | this.sink.event(t, x); 1261 | if (this.take === 0) { 1262 | this.disposable.dispose(); 1263 | this.sink.end(t); 1264 | } 1265 | }; 1266 | 1267 | return SliceSink; 1268 | }(Pipe); 1269 | 1270 | var TakeWhile = /*#__PURE__*/function () { 1271 | function TakeWhile(p, source) { 1272 | classCallCheck$2(this, TakeWhile); 1273 | 1274 | this.p = p; 1275 | this.source = source; 1276 | } 1277 | 1278 | TakeWhile.prototype.run = function run(sink, scheduler$$1) { 1279 | var disposable$$1 = new SettableDisposable(); 1280 | var takeWhileSink = new TakeWhileSink(this.p, sink, disposable$$1); 1281 | 1282 | disposable$$1.setDisposable(this.source.run(takeWhileSink, scheduler$$1)); 1283 | 1284 | return disposable$$1; 1285 | }; 1286 | 1287 | return TakeWhile; 1288 | }(); 1289 | 1290 | var TakeWhileSink = /*#__PURE__*/function (_Pipe2) { 1291 | inherits(TakeWhileSink, _Pipe2); 1292 | 1293 | function TakeWhileSink(p, sink, disposable$$1) { 1294 | classCallCheck$2(this, TakeWhileSink); 1295 | 1296 | var _this2 = possibleConstructorReturn(this, _Pipe2.call(this, sink)); 1297 | 1298 | _this2.p = p; 1299 | _this2.active = true; 1300 | _this2.disposable = disposable$$1; 1301 | return _this2; 1302 | } 1303 | 1304 | TakeWhileSink.prototype.event = function event(t, x) { 1305 | if (!this.active) { 1306 | return; 1307 | } 1308 | 1309 | var p = this.p; 1310 | this.active = p(x); 1311 | 1312 | if (this.active) { 1313 | this.sink.event(t, x); 1314 | } else { 1315 | this.disposable.dispose(); 1316 | this.sink.end(t); 1317 | } 1318 | }; 1319 | 1320 | return TakeWhileSink; 1321 | }(Pipe); 1322 | 1323 | var SkipWhile = /*#__PURE__*/function () { 1324 | function SkipWhile(p, source) { 1325 | classCallCheck$2(this, SkipWhile); 1326 | 1327 | this.p = p; 1328 | this.source = source; 1329 | } 1330 | 1331 | SkipWhile.prototype.run = function run(sink, scheduler$$1) { 1332 | return this.source.run(new SkipWhileSink(this.p, sink), scheduler$$1); 1333 | }; 1334 | 1335 | return SkipWhile; 1336 | }(); 1337 | 1338 | var SkipWhileSink = /*#__PURE__*/function (_Pipe3) { 1339 | inherits(SkipWhileSink, _Pipe3); 1340 | 1341 | function SkipWhileSink(p, sink) { 1342 | classCallCheck$2(this, SkipWhileSink); 1343 | 1344 | var _this3 = possibleConstructorReturn(this, _Pipe3.call(this, sink)); 1345 | 1346 | _this3.p = p; 1347 | _this3.skipping = true; 1348 | return _this3; 1349 | } 1350 | 1351 | SkipWhileSink.prototype.event = function event(t, x) { 1352 | if (this.skipping) { 1353 | var p = this.p; 1354 | this.skipping = p(x); 1355 | if (this.skipping) { 1356 | return; 1357 | } 1358 | } 1359 | 1360 | this.sink.event(t, x); 1361 | }; 1362 | 1363 | return SkipWhileSink; 1364 | }(Pipe); 1365 | 1366 | var SkipAfter = /*#__PURE__*/function () { 1367 | function SkipAfter(p, source) { 1368 | classCallCheck$2(this, SkipAfter); 1369 | 1370 | this.p = p; 1371 | this.source = source; 1372 | } 1373 | 1374 | SkipAfter.prototype.run = function run(sink, scheduler$$1) { 1375 | return this.source.run(new SkipAfterSink(this.p, sink), scheduler$$1); 1376 | }; 1377 | 1378 | return SkipAfter; 1379 | }(); 1380 | 1381 | var SkipAfterSink = /*#__PURE__*/function (_Pipe4) { 1382 | inherits(SkipAfterSink, _Pipe4); 1383 | 1384 | function SkipAfterSink(p, sink) { 1385 | classCallCheck$2(this, SkipAfterSink); 1386 | 1387 | var _this4 = possibleConstructorReturn(this, _Pipe4.call(this, sink)); 1388 | 1389 | _this4.p = p; 1390 | _this4.skipping = false; 1391 | return _this4; 1392 | } 1393 | 1394 | SkipAfterSink.prototype.event = function event(t, x) { 1395 | if (this.skipping) { 1396 | return; 1397 | } 1398 | 1399 | var p = this.p; 1400 | this.skipping = p(x); 1401 | this.sink.event(t, x); 1402 | 1403 | if (this.skipping) { 1404 | this.sink.end(t); 1405 | } 1406 | }; 1407 | 1408 | return SkipAfterSink; 1409 | }(Pipe); 1410 | 1411 | var ZipItems = /*#__PURE__*/function () { 1412 | function ZipItems(f, items, source) { 1413 | classCallCheck$2(this, ZipItems); 1414 | 1415 | this.f = f; 1416 | this.items = items; 1417 | this.source = source; 1418 | } 1419 | 1420 | ZipItems.prototype.run = function run(sink, scheduler$$1) { 1421 | return this.source.run(new ZipItemsSink(this.f, this.items, sink), scheduler$$1); 1422 | }; 1423 | 1424 | return ZipItems; 1425 | }(); 1426 | 1427 | var ZipItemsSink = /*#__PURE__*/function (_Pipe) { 1428 | inherits(ZipItemsSink, _Pipe); 1429 | 1430 | function ZipItemsSink(f, items, sink) { 1431 | classCallCheck$2(this, ZipItemsSink); 1432 | 1433 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1434 | 1435 | _this.f = f; 1436 | _this.items = items; 1437 | _this.index = 0; 1438 | return _this; 1439 | } 1440 | 1441 | ZipItemsSink.prototype.event = function event(t, b) { 1442 | var f = this.f; 1443 | this.sink.event(t, f(this.items[this.index], b)); 1444 | this.index += 1; 1445 | }; 1446 | 1447 | return ZipItemsSink; 1448 | }(Pipe); 1449 | 1450 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 1451 | 1452 | var runEffects$1 = /*#__PURE__*/curry2(function (stream, scheduler$$1) { 1453 | return new Promise(function (resolve, reject) { 1454 | return runStream(stream, scheduler$$1, resolve, reject); 1455 | }); 1456 | }); 1457 | 1458 | function runStream(stream, scheduler$$1, resolve, reject) { 1459 | var disposable$$1 = new SettableDisposable(); 1460 | var observer = new RunEffectsSink(resolve, reject, disposable$$1); 1461 | 1462 | disposable$$1.setDisposable(stream.run(observer, scheduler$$1)); 1463 | } 1464 | 1465 | var RunEffectsSink = /*#__PURE__*/function () { 1466 | function RunEffectsSink(end, error, disposable$$1) { 1467 | classCallCheck$2(this, RunEffectsSink); 1468 | 1469 | this._end = end; 1470 | this._error = error; 1471 | this._disposable = disposable$$1; 1472 | this.active = true; 1473 | } 1474 | 1475 | RunEffectsSink.prototype.event = function event(t, x) {}; 1476 | 1477 | RunEffectsSink.prototype.end = function end(t) { 1478 | if (!this.active) { 1479 | return; 1480 | } 1481 | this._dispose(this._error, this._end, undefined); 1482 | }; 1483 | 1484 | RunEffectsSink.prototype.error = function error(t, e) { 1485 | this._dispose(this._error, this._error, e); 1486 | }; 1487 | 1488 | RunEffectsSink.prototype._dispose = function _dispose(error, end, x) { 1489 | this.active = false; 1490 | tryDispose$1(error, end, x, this._disposable); 1491 | }; 1492 | 1493 | return RunEffectsSink; 1494 | }(); 1495 | 1496 | function tryDispose$1(error, end, x, disposable$$1) { 1497 | try { 1498 | disposable$$1.dispose(); 1499 | } catch (e) { 1500 | error(e); 1501 | return; 1502 | } 1503 | 1504 | end(x); 1505 | } 1506 | 1507 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 1508 | 1509 | // Run a Stream, sending all its events to the 1510 | // provided Sink. 1511 | var run$1 = function run(sink, scheduler$$1, stream) { 1512 | return stream.run(sink, scheduler$$1); 1513 | }; 1514 | 1515 | var RelativeSink = /*#__PURE__*/function () { 1516 | function RelativeSink(offset, sink) { 1517 | classCallCheck$2(this, RelativeSink); 1518 | 1519 | this.sink = sink; 1520 | this.offset = offset; 1521 | } 1522 | 1523 | RelativeSink.prototype.event = function event(t, x) { 1524 | this.sink.event(t + this.offset, x); 1525 | }; 1526 | 1527 | RelativeSink.prototype.error = function error(t, e) { 1528 | this.sink.error(t + this.offset, e); 1529 | }; 1530 | 1531 | RelativeSink.prototype.end = function end(t) { 1532 | this.sink.end(t + this.offset); 1533 | }; 1534 | 1535 | return RelativeSink; 1536 | }(); 1537 | 1538 | // Create a stream with its own local clock 1539 | // This transforms time from the provided scheduler's clock to a stream-local 1540 | // clock (which starts at 0), and then *back* to the scheduler's clock before 1541 | // propagating events to sink. In other words, upstream sources will see local times, 1542 | // and downstream sinks will see non-local (original) times. 1543 | var withLocalTime$1 = function withLocalTime(origin, stream) { 1544 | return new WithLocalTime(origin, stream); 1545 | }; 1546 | 1547 | var WithLocalTime = /*#__PURE__*/function () { 1548 | function WithLocalTime(origin, source) { 1549 | classCallCheck$2(this, WithLocalTime); 1550 | 1551 | this.origin = origin; 1552 | this.source = source; 1553 | } 1554 | 1555 | WithLocalTime.prototype.run = function run(sink, scheduler$$1) { 1556 | return this.source.run(relativeSink(this.origin, sink), schedulerRelativeTo(this.origin, scheduler$$1)); 1557 | }; 1558 | 1559 | return WithLocalTime; 1560 | }(); 1561 | 1562 | // Accumulate offsets instead of nesting RelativeSinks, which can happen 1563 | // with higher-order stream and combinators like continueWith when they're 1564 | // applied recursively. 1565 | 1566 | 1567 | var relativeSink = function relativeSink(origin, sink) { 1568 | return sink instanceof RelativeSink ? new RelativeSink(origin + sink.offset, sink.sink) : new RelativeSink(origin, sink); 1569 | }; 1570 | 1571 | var Loop = /*#__PURE__*/function () { 1572 | function Loop(stepper, seed, source) { 1573 | classCallCheck$2(this, Loop); 1574 | 1575 | this.step = stepper; 1576 | this.seed = seed; 1577 | this.source = source; 1578 | } 1579 | 1580 | Loop.prototype.run = function run(sink, scheduler$$1) { 1581 | return this.source.run(new LoopSink(this.step, this.seed, sink), scheduler$$1); 1582 | }; 1583 | 1584 | return Loop; 1585 | }(); 1586 | 1587 | var LoopSink = /*#__PURE__*/function (_Pipe) { 1588 | inherits(LoopSink, _Pipe); 1589 | 1590 | function LoopSink(stepper, seed, sink) { 1591 | classCallCheck$2(this, LoopSink); 1592 | 1593 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1594 | 1595 | _this.step = stepper; 1596 | _this.seed = seed; 1597 | return _this; 1598 | } 1599 | 1600 | LoopSink.prototype.event = function event(t, x) { 1601 | var result = this.step(this.seed, x); 1602 | this.seed = result.seed; 1603 | this.sink.event(t, result.value); 1604 | }; 1605 | 1606 | return LoopSink; 1607 | }(Pipe); 1608 | 1609 | var Scan = /*#__PURE__*/function () { 1610 | function Scan(f, z, source) { 1611 | classCallCheck$2(this, Scan); 1612 | 1613 | this.source = source; 1614 | this.f = f; 1615 | this.value = z; 1616 | } 1617 | 1618 | Scan.prototype.run = function run(sink, scheduler$$1) { 1619 | var d1 = asap(propagateEventTask$1(this.value, sink), scheduler$$1); 1620 | var d2 = this.source.run(new ScanSink(this.f, this.value, sink), scheduler$$1); 1621 | return disposeBoth(d1, d2); 1622 | }; 1623 | 1624 | return Scan; 1625 | }(); 1626 | 1627 | var ScanSink = /*#__PURE__*/function (_Pipe) { 1628 | inherits(ScanSink, _Pipe); 1629 | 1630 | function ScanSink(f, z, sink) { 1631 | classCallCheck$2(this, ScanSink); 1632 | 1633 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1634 | 1635 | _this.f = f; 1636 | _this.value = z; 1637 | return _this; 1638 | } 1639 | 1640 | ScanSink.prototype.event = function event(t, x) { 1641 | var f = this.f; 1642 | this.value = f(this.value, x); 1643 | this.sink.event(t, this.value); 1644 | }; 1645 | 1646 | return ScanSink; 1647 | }(Pipe); 1648 | 1649 | var ContinueWith = /*#__PURE__*/function () { 1650 | function ContinueWith(f, source) { 1651 | classCallCheck$2(this, ContinueWith); 1652 | 1653 | this.f = f; 1654 | this.source = source; 1655 | } 1656 | 1657 | ContinueWith.prototype.run = function run(sink, scheduler$$1) { 1658 | return new ContinueWithSink(this.f, this.source, sink, scheduler$$1); 1659 | }; 1660 | 1661 | return ContinueWith; 1662 | }(); 1663 | 1664 | var ContinueWithSink = /*#__PURE__*/function (_Pipe) { 1665 | inherits(ContinueWithSink, _Pipe); 1666 | 1667 | function ContinueWithSink(f, source, sink, scheduler$$1) { 1668 | classCallCheck$2(this, ContinueWithSink); 1669 | 1670 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1671 | 1672 | _this.f = f; 1673 | _this.scheduler = scheduler$$1; 1674 | _this.active = true; 1675 | _this.disposable = disposeOnce(source.run(_this, scheduler$$1)); 1676 | return _this; 1677 | } 1678 | 1679 | ContinueWithSink.prototype.event = function event(t, x) { 1680 | if (!this.active) { 1681 | return; 1682 | } 1683 | this.sink.event(t, x); 1684 | }; 1685 | 1686 | ContinueWithSink.prototype.end = function end(t) { 1687 | if (!this.active) { 1688 | return; 1689 | } 1690 | 1691 | tryDispose(t, this.disposable, this.sink); 1692 | 1693 | this._startNext(t, this.sink); 1694 | }; 1695 | 1696 | ContinueWithSink.prototype._startNext = function _startNext(t, sink) { 1697 | try { 1698 | this.disposable = this._continue(this.f, t, sink); 1699 | } catch (e) { 1700 | sink.error(t, e); 1701 | } 1702 | }; 1703 | 1704 | ContinueWithSink.prototype._continue = function _continue(f, t, sink) { 1705 | return run$1(sink, this.scheduler, withLocalTime$1(t, f())); 1706 | }; 1707 | 1708 | ContinueWithSink.prototype.dispose = function dispose$$1() { 1709 | this.active = false; 1710 | return this.disposable.dispose(); 1711 | }; 1712 | 1713 | return ContinueWithSink; 1714 | }(Pipe); 1715 | 1716 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 1717 | /** @author Brian Cavalier */ 1718 | /** @author John Hann */ 1719 | 1720 | /** 1721 | * Transform each value in the stream by applying f to each 1722 | * @param {function(*):*} f mapping function 1723 | * @param {Stream} stream stream to map 1724 | * @returns {Stream} stream containing items transformed by f 1725 | */ 1726 | var map$2 = function map$$1(f, stream) { 1727 | return Map.create(f, stream); 1728 | }; 1729 | 1730 | /** 1731 | * Perform a side effect for each item in the stream 1732 | * @param {function(x:*):*} f side effect to execute for each item. The 1733 | * return value will be discarded. 1734 | * @param {Stream} stream stream to tap 1735 | * @returns {Stream} new stream containing the same items as this stream 1736 | */ 1737 | var tap$1 = function tap(f, stream) { 1738 | return new Tap(f, stream); 1739 | }; 1740 | 1741 | var Tap = /*#__PURE__*/function () { 1742 | function Tap(f, source) { 1743 | classCallCheck$2(this, Tap); 1744 | 1745 | this.source = source; 1746 | this.f = f; 1747 | } 1748 | 1749 | Tap.prototype.run = function run(sink, scheduler$$1) { 1750 | return this.source.run(new TapSink(this.f, sink), scheduler$$1); 1751 | }; 1752 | 1753 | return Tap; 1754 | }(); 1755 | 1756 | var TapSink = /*#__PURE__*/function (_Pipe) { 1757 | inherits(TapSink, _Pipe); 1758 | 1759 | function TapSink(f, sink) { 1760 | classCallCheck$2(this, TapSink); 1761 | 1762 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1763 | 1764 | _this.f = f; 1765 | return _this; 1766 | } 1767 | 1768 | TapSink.prototype.event = function event(t, x) { 1769 | var f = this.f; 1770 | f(x); 1771 | this.sink.event(t, x); 1772 | }; 1773 | 1774 | return TapSink; 1775 | }(Pipe); 1776 | 1777 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 1778 | /** @author Brian Cavalier */ 1779 | /** @author John Hann */ 1780 | 1781 | var IndexSink = /*#__PURE__*/function (_Sink) { 1782 | inherits(IndexSink, _Sink); 1783 | 1784 | function IndexSink(i, sink) { 1785 | classCallCheck$2(this, IndexSink); 1786 | 1787 | var _this = possibleConstructorReturn(this, _Sink.call(this, sink)); 1788 | 1789 | _this.index = i; 1790 | _this.active = true; 1791 | _this.value = undefined; 1792 | return _this; 1793 | } 1794 | 1795 | IndexSink.prototype.event = function event(t, x) { 1796 | if (!this.active) { 1797 | return; 1798 | } 1799 | this.value = x; 1800 | this.sink.event(t, this); 1801 | }; 1802 | 1803 | IndexSink.prototype.end = function end(t) { 1804 | if (!this.active) { 1805 | return; 1806 | } 1807 | this.active = false; 1808 | this.sink.event(t, this); 1809 | }; 1810 | 1811 | return IndexSink; 1812 | }(Pipe); 1813 | 1814 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 1815 | /** @author Brian Cavalier */ 1816 | /** @author John Hann */ 1817 | 1818 | function invoke(f, args) { 1819 | /* eslint complexity: [2,7] */ 1820 | switch (args.length) { 1821 | case 0: 1822 | return f(); 1823 | case 1: 1824 | return f(args[0]); 1825 | case 2: 1826 | return f(args[0], args[1]); 1827 | case 3: 1828 | return f(args[0], args[1], args[2]); 1829 | case 4: 1830 | return f(args[0], args[1], args[2], args[3]); 1831 | case 5: 1832 | return f(args[0], args[1], args[2], args[3], args[4]); 1833 | default: 1834 | return f.apply(void 0, args); 1835 | } 1836 | } 1837 | 1838 | var Combine = /*#__PURE__*/function () { 1839 | function Combine(f, sources) { 1840 | classCallCheck$2(this, Combine); 1841 | 1842 | this.f = f; 1843 | this.sources = sources; 1844 | } 1845 | 1846 | Combine.prototype.run = function run(sink, scheduler$$1) { 1847 | var l = this.sources.length; 1848 | var disposables = new Array(l); 1849 | var sinks = new Array(l); 1850 | 1851 | var mergeSink = new CombineSink(disposables, sinks, sink, this.f); 1852 | 1853 | for (var indexSink, i = 0; i < l; ++i) { 1854 | indexSink = sinks[i] = new IndexSink(i, mergeSink); 1855 | disposables[i] = this.sources[i].run(indexSink, scheduler$$1); 1856 | } 1857 | 1858 | return disposeAll(disposables); 1859 | }; 1860 | 1861 | return Combine; 1862 | }(); 1863 | 1864 | var CombineSink = /*#__PURE__*/function (_Pipe) { 1865 | inherits(CombineSink, _Pipe); 1866 | 1867 | function CombineSink(disposables, sinks, sink, f) { 1868 | classCallCheck$2(this, CombineSink); 1869 | 1870 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 1871 | 1872 | _this.disposables = disposables; 1873 | _this.sinks = sinks; 1874 | _this.f = f; 1875 | 1876 | var l = sinks.length; 1877 | _this.awaiting = l; 1878 | _this.values = new Array(l); 1879 | _this.hasValue = new Array(l).fill(false); 1880 | _this.activeCount = sinks.length; 1881 | return _this; 1882 | } 1883 | 1884 | CombineSink.prototype.event = function event(t, indexedValue) { 1885 | if (!indexedValue.active) { 1886 | this._dispose(t, indexedValue.index); 1887 | return; 1888 | } 1889 | 1890 | var i = indexedValue.index; 1891 | var awaiting = this._updateReady(i); 1892 | 1893 | this.values[i] = indexedValue.value; 1894 | if (awaiting === 0) { 1895 | this.sink.event(t, invoke(this.f, this.values)); 1896 | } 1897 | }; 1898 | 1899 | CombineSink.prototype._updateReady = function _updateReady(index) { 1900 | if (this.awaiting > 0) { 1901 | if (!this.hasValue[index]) { 1902 | this.hasValue[index] = true; 1903 | this.awaiting -= 1; 1904 | } 1905 | } 1906 | return this.awaiting; 1907 | }; 1908 | 1909 | CombineSink.prototype._dispose = function _dispose(t, index) { 1910 | tryDispose(t, this.disposables[index], this.sink); 1911 | if (--this.activeCount === 0) { 1912 | this.sink.end(t); 1913 | } 1914 | }; 1915 | 1916 | return CombineSink; 1917 | }(Pipe); 1918 | 1919 | /** @license MIT License (c) copyright 2010 original author or authors */ 1920 | 1921 | /** 1922 | * Doubly linked list 1923 | * @constructor 1924 | */ 1925 | var LinkedList = /*#__PURE__*/function () { 1926 | function LinkedList() { 1927 | classCallCheck$2(this, LinkedList); 1928 | 1929 | this.head = null; 1930 | this.length = 0; 1931 | } 1932 | 1933 | /** 1934 | * Add a node to the end of the list 1935 | * @param {{prev:Object|null, next:Object|null, dispose:function}} x node to add 1936 | */ 1937 | 1938 | 1939 | LinkedList.prototype.add = function add(x) { 1940 | if (this.head !== null) { 1941 | this.head.prev = x; 1942 | x.next = this.head; 1943 | } 1944 | this.head = x; 1945 | ++this.length; 1946 | }; 1947 | 1948 | /** 1949 | * Remove the provided node from the list 1950 | * @param {{prev:Object|null, next:Object|null, dispose:function}} x node to remove 1951 | */ 1952 | 1953 | 1954 | LinkedList.prototype.remove = function remove$$1(x) { 1955 | // eslint-disable-line complexity 1956 | --this.length; 1957 | if (x === this.head) { 1958 | this.head = this.head.next; 1959 | } 1960 | if (x.next !== null) { 1961 | x.next.prev = x.prev; 1962 | x.next = null; 1963 | } 1964 | if (x.prev !== null) { 1965 | x.prev.next = x.next; 1966 | x.prev = null; 1967 | } 1968 | }; 1969 | 1970 | /** 1971 | * @returns {boolean} true iff there are no nodes in the list 1972 | */ 1973 | 1974 | 1975 | LinkedList.prototype.isEmpty = function isEmpty() { 1976 | return this.length === 0; 1977 | }; 1978 | 1979 | /** 1980 | * Dispose all nodes 1981 | * @returns {void} 1982 | */ 1983 | 1984 | 1985 | LinkedList.prototype.dispose = function dispose$$1() { 1986 | if (this.isEmpty()) { 1987 | return; 1988 | } 1989 | 1990 | var head = this.head; 1991 | this.head = null; 1992 | this.length = 0; 1993 | 1994 | while (head !== null) { 1995 | head.dispose(); 1996 | head = head.next; 1997 | } 1998 | }; 1999 | 2000 | return LinkedList; 2001 | }(); 2002 | 2003 | var MergeConcurrently = /*#__PURE__*/function () { 2004 | function MergeConcurrently(f, concurrency, source) { 2005 | classCallCheck$2(this, MergeConcurrently); 2006 | 2007 | this.f = f; 2008 | this.concurrency = concurrency; 2009 | this.source = source; 2010 | } 2011 | 2012 | MergeConcurrently.prototype.run = function run(sink, scheduler$$1) { 2013 | return new Outer(this.f, this.concurrency, this.source, sink, scheduler$$1); 2014 | }; 2015 | 2016 | return MergeConcurrently; 2017 | }(); 2018 | 2019 | var Outer = /*#__PURE__*/function () { 2020 | function Outer(f, concurrency, source, sink, scheduler$$1) { 2021 | classCallCheck$2(this, Outer); 2022 | 2023 | this.f = f; 2024 | this.concurrency = concurrency; 2025 | this.sink = sink; 2026 | this.scheduler = scheduler$$1; 2027 | this.pending = []; 2028 | this.current = new LinkedList(); 2029 | this.disposable = disposeOnce(source.run(this, scheduler$$1)); 2030 | this.active = true; 2031 | } 2032 | 2033 | Outer.prototype.event = function event(t, x) { 2034 | this._addInner(t, x); 2035 | }; 2036 | 2037 | Outer.prototype._addInner = function _addInner(t, x) { 2038 | if (this.current.length < this.concurrency) { 2039 | this._startInner(t, x); 2040 | } else { 2041 | this.pending.push(x); 2042 | } 2043 | }; 2044 | 2045 | Outer.prototype._startInner = function _startInner(t, x) { 2046 | try { 2047 | this._initInner(t, x); 2048 | } catch (e) { 2049 | this.error(t, e); 2050 | } 2051 | }; 2052 | 2053 | Outer.prototype._initInner = function _initInner(t, x) { 2054 | var innerSink = new Inner(t, this, this.sink); 2055 | innerSink.disposable = mapAndRun(this.f, t, x, innerSink, this.scheduler); 2056 | this.current.add(innerSink); 2057 | }; 2058 | 2059 | Outer.prototype.end = function end(t) { 2060 | this.active = false; 2061 | tryDispose(t, this.disposable, this.sink); 2062 | this._checkEnd(t); 2063 | }; 2064 | 2065 | Outer.prototype.error = function error(t, e) { 2066 | this.active = false; 2067 | this.sink.error(t, e); 2068 | }; 2069 | 2070 | Outer.prototype.dispose = function dispose$$1() { 2071 | this.active = false; 2072 | this.pending.length = 0; 2073 | this.disposable.dispose(); 2074 | this.current.dispose(); 2075 | }; 2076 | 2077 | Outer.prototype._endInner = function _endInner(t, inner) { 2078 | this.current.remove(inner); 2079 | tryDispose(t, inner, this); 2080 | 2081 | if (this.pending.length === 0) { 2082 | this._checkEnd(t); 2083 | } else { 2084 | this._startInner(t, this.pending.shift()); 2085 | } 2086 | }; 2087 | 2088 | Outer.prototype._checkEnd = function _checkEnd(t) { 2089 | if (!this.active && this.current.isEmpty()) { 2090 | this.sink.end(t); 2091 | } 2092 | }; 2093 | 2094 | return Outer; 2095 | }(); 2096 | 2097 | var mapAndRun = function mapAndRun(f, t, x, sink, scheduler$$1) { 2098 | return f(x).run(sink, schedulerRelativeTo(t, scheduler$$1)); 2099 | }; 2100 | 2101 | var Inner = /*#__PURE__*/function () { 2102 | function Inner(time, outer, sink) { 2103 | classCallCheck$2(this, Inner); 2104 | 2105 | this.prev = this.next = null; 2106 | this.time = time; 2107 | this.outer = outer; 2108 | this.sink = sink; 2109 | this.disposable = void 0; 2110 | } 2111 | 2112 | Inner.prototype.event = function event(t, x) { 2113 | this.sink.event(t + this.time, x); 2114 | }; 2115 | 2116 | Inner.prototype.end = function end(t) { 2117 | this.outer._endInner(t + this.time, this); 2118 | }; 2119 | 2120 | Inner.prototype.error = function error(t, e) { 2121 | this.outer.error(t + this.time, e); 2122 | }; 2123 | 2124 | Inner.prototype.dispose = function dispose$$1() { 2125 | return this.disposable.dispose(); 2126 | }; 2127 | 2128 | return Inner; 2129 | }(); 2130 | 2131 | var Merge = /*#__PURE__*/function () { 2132 | function Merge(sources) { 2133 | classCallCheck$2(this, Merge); 2134 | 2135 | this.sources = sources; 2136 | } 2137 | 2138 | Merge.prototype.run = function run(sink, scheduler$$1) { 2139 | var l = this.sources.length; 2140 | var disposables = new Array(l); 2141 | var sinks = new Array(l); 2142 | 2143 | var mergeSink = new MergeSink(disposables, sinks, sink); 2144 | 2145 | for (var indexSink, i = 0; i < l; ++i) { 2146 | indexSink = sinks[i] = new IndexSink(i, mergeSink); 2147 | disposables[i] = this.sources[i].run(indexSink, scheduler$$1); 2148 | } 2149 | 2150 | return disposeAll(disposables); 2151 | }; 2152 | 2153 | return Merge; 2154 | }(); 2155 | 2156 | var MergeSink = /*#__PURE__*/function (_Pipe) { 2157 | inherits(MergeSink, _Pipe); 2158 | 2159 | function MergeSink(disposables, sinks, sink) { 2160 | classCallCheck$2(this, MergeSink); 2161 | 2162 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 2163 | 2164 | _this.disposables = disposables; 2165 | _this.activeCount = sinks.length; 2166 | return _this; 2167 | } 2168 | 2169 | MergeSink.prototype.event = function event(t, indexValue) { 2170 | if (!indexValue.active) { 2171 | this._dispose(t, indexValue.index); 2172 | return; 2173 | } 2174 | this.sink.event(t, indexValue.value); 2175 | }; 2176 | 2177 | MergeSink.prototype._dispose = function _dispose(t, index) { 2178 | tryDispose(t, this.disposables[index], this.sink); 2179 | if (--this.activeCount === 0) { 2180 | this.sink.end(t); 2181 | } 2182 | }; 2183 | 2184 | return MergeSink; 2185 | }(Pipe); 2186 | 2187 | var Snapshot = /*#__PURE__*/function () { 2188 | function Snapshot(f, values, sampler) { 2189 | classCallCheck$2(this, Snapshot); 2190 | 2191 | this.f = f; 2192 | this.values = values; 2193 | this.sampler = sampler; 2194 | } 2195 | 2196 | Snapshot.prototype.run = function run(sink, scheduler$$1) { 2197 | var sampleSink = new SnapshotSink(this.f, sink); 2198 | var valuesDisposable = this.values.run(sampleSink.latest, scheduler$$1); 2199 | var samplerDisposable = this.sampler.run(sampleSink, scheduler$$1); 2200 | 2201 | return disposeBoth(samplerDisposable, valuesDisposable); 2202 | }; 2203 | 2204 | return Snapshot; 2205 | }(); 2206 | 2207 | var SnapshotSink = /*#__PURE__*/function (_Pipe) { 2208 | inherits(SnapshotSink, _Pipe); 2209 | 2210 | function SnapshotSink(f, sink) { 2211 | classCallCheck$2(this, SnapshotSink); 2212 | 2213 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 2214 | 2215 | _this.f = f; 2216 | _this.latest = new LatestValueSink(_this); 2217 | return _this; 2218 | } 2219 | 2220 | SnapshotSink.prototype.event = function event(t, x) { 2221 | if (this.latest.hasValue) { 2222 | var f = this.f; 2223 | this.sink.event(t, f(this.latest.value, x)); 2224 | } 2225 | }; 2226 | 2227 | return SnapshotSink; 2228 | }(Pipe); 2229 | 2230 | var LatestValueSink = /*#__PURE__*/function (_Pipe2) { 2231 | inherits(LatestValueSink, _Pipe2); 2232 | 2233 | function LatestValueSink(sink) { 2234 | classCallCheck$2(this, LatestValueSink); 2235 | 2236 | var _this2 = possibleConstructorReturn(this, _Pipe2.call(this, sink)); 2237 | 2238 | _this2.hasValue = false; 2239 | return _this2; 2240 | } 2241 | 2242 | LatestValueSink.prototype.event = function event(t, x) { 2243 | this.value = x; 2244 | this.hasValue = true; 2245 | }; 2246 | 2247 | LatestValueSink.prototype.end = function end() {}; 2248 | 2249 | return LatestValueSink; 2250 | }(Pipe); 2251 | 2252 | // Copied and modified from https://github.com/invertase/denque 2253 | // MIT License 2254 | 2255 | // These constants were extracted directly from denque's shift() 2256 | // It's not clear exactly why the authors chose these particular 2257 | // values, but given denque's stated goals, it seems likely that 2258 | // they were chosen for speed/memory reasons. 2259 | 2260 | // Max value of _head at which Queue is willing to shink 2261 | // its internal array 2262 | var HEAD_MAX_SHRINK = 2; 2263 | 2264 | // Min value of _tail at which Queue is willing to shink 2265 | // its internal array 2266 | var TAIL_MIN_SHRINK = 10000; 2267 | 2268 | var Queue = /*#__PURE__*/function () { 2269 | function Queue() { 2270 | classCallCheck$2(this, Queue); 2271 | 2272 | this._head = 0; 2273 | this._tail = 0; 2274 | this._capacityMask = 0x3; 2275 | this._list = new Array(4); 2276 | } 2277 | 2278 | Queue.prototype.push = function push(x) { 2279 | var tail$$1 = this._tail; 2280 | this._list[tail$$1] = x; 2281 | this._tail = tail$$1 + 1 & this._capacityMask; 2282 | if (this._tail === this._head) { 2283 | this._growArray(); 2284 | } 2285 | 2286 | if (this._head < this._tail) { 2287 | return this._tail - this._head; 2288 | } else { 2289 | return this._capacityMask + 1 - (this._head - this._tail); 2290 | } 2291 | }; 2292 | 2293 | Queue.prototype.shift = function shift() { 2294 | var head = this._head; 2295 | if (head === this._tail) { 2296 | return undefined; 2297 | } 2298 | 2299 | var x = this._list[head]; 2300 | this._list[head] = undefined; 2301 | this._head = head + 1 & this._capacityMask; 2302 | if (head < HEAD_MAX_SHRINK && this._tail > TAIL_MIN_SHRINK && this._tail <= this._list.length >>> 2) { 2303 | this._shrinkArray(); 2304 | } 2305 | 2306 | return x; 2307 | }; 2308 | 2309 | Queue.prototype.isEmpty = function isEmpty() { 2310 | return this._head === this._tail; 2311 | }; 2312 | 2313 | Queue.prototype.length = function length() { 2314 | if (this._head === this._tail) { 2315 | return 0; 2316 | } else if (this._head < this._tail) { 2317 | return this._tail - this._head; 2318 | } else { 2319 | return this._capacityMask + 1 - (this._head - this._tail); 2320 | } 2321 | }; 2322 | 2323 | Queue.prototype._growArray = function _growArray() { 2324 | if (this._head) { 2325 | // copy existing data, head to end, then beginning to tail. 2326 | this._list = this._copyArray(); 2327 | this._head = 0; 2328 | } 2329 | 2330 | // head is at 0 and array is now full, safe to extend 2331 | this._tail = this._list.length; 2332 | 2333 | this._list.length *= 2; 2334 | this._capacityMask = this._capacityMask << 1 | 1; 2335 | }; 2336 | 2337 | Queue.prototype._shrinkArray = function _shrinkArray() { 2338 | this._list.length >>>= 1; 2339 | this._capacityMask >>>= 1; 2340 | }; 2341 | 2342 | Queue.prototype._copyArray = function _copyArray() { 2343 | var newArray = []; 2344 | var list = this._list; 2345 | var len = list.length; 2346 | 2347 | var i = void 0; 2348 | for (i = this._head; i < len; i++) { 2349 | newArray.push(list[i]); 2350 | } 2351 | for (i = 0; i < this._tail; i++) { 2352 | newArray.push(list[i]); 2353 | } 2354 | 2355 | return newArray; 2356 | }; 2357 | 2358 | return Queue; 2359 | }(); 2360 | 2361 | var Zip = /*#__PURE__*/function () { 2362 | function Zip(f, sources) { 2363 | classCallCheck$2(this, Zip); 2364 | 2365 | this.f = f; 2366 | this.sources = sources; 2367 | } 2368 | 2369 | Zip.prototype.run = function run(sink, scheduler$$1) { 2370 | var l = this.sources.length; 2371 | var disposables = new Array(l); 2372 | var sinks = new Array(l); 2373 | var buffers = new Array(l); 2374 | 2375 | var zipSink = new ZipSink(this.f, buffers, sinks, sink); 2376 | 2377 | for (var indexSink, i = 0; i < l; ++i) { 2378 | buffers[i] = new Queue(); 2379 | indexSink = sinks[i] = new IndexSink(i, zipSink); 2380 | disposables[i] = this.sources[i].run(indexSink, scheduler$$1); 2381 | } 2382 | 2383 | return disposeAll(disposables); 2384 | }; 2385 | 2386 | return Zip; 2387 | }(); 2388 | 2389 | var ZipSink = /*#__PURE__*/function (_Pipe) { 2390 | inherits(ZipSink, _Pipe); 2391 | 2392 | function ZipSink(f, buffers, sinks, sink) { 2393 | classCallCheck$2(this, ZipSink); 2394 | 2395 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 2396 | 2397 | _this.f = f; 2398 | _this.sinks = sinks; 2399 | _this.buffers = buffers; 2400 | return _this; 2401 | } 2402 | 2403 | ZipSink.prototype.event = function event(t, indexedValue) { 2404 | /* eslint complexity: [1, 5] */ 2405 | if (!indexedValue.active) { 2406 | this._dispose(t, indexedValue.index); 2407 | return; 2408 | } 2409 | 2410 | var buffers = this.buffers; 2411 | var buffer = buffers[indexedValue.index]; 2412 | 2413 | buffer.push(indexedValue.value); 2414 | 2415 | if (buffer.length() === 1) { 2416 | if (!ready(this.buffers)) { 2417 | return; 2418 | } 2419 | 2420 | emitZipped(this.f, t, buffers, this.sink); 2421 | 2422 | if (ended(this.buffers, this.sinks)) { 2423 | this.sink.end(t); 2424 | } 2425 | } 2426 | }; 2427 | 2428 | ZipSink.prototype._dispose = function _dispose(t, index) { 2429 | var buffer = this.buffers[index]; 2430 | if (buffer.isEmpty()) { 2431 | this.sink.end(t); 2432 | } 2433 | }; 2434 | 2435 | return ZipSink; 2436 | }(Pipe); 2437 | 2438 | var emitZipped = function emitZipped(f, t, buffers, sink) { 2439 | return sink.event(t, invoke(f, map(head, buffers))); 2440 | }; 2441 | 2442 | var head = function head(buffer) { 2443 | return buffer.shift(); 2444 | }; 2445 | 2446 | function ended(buffers, sinks) { 2447 | for (var i = 0, l = buffers.length; i < l; ++i) { 2448 | if (buffers[i].isEmpty() && !sinks[i].active) { 2449 | return true; 2450 | } 2451 | } 2452 | return false; 2453 | } 2454 | 2455 | function ready(buffers) { 2456 | for (var i = 0, l = buffers.length; i < l; ++i) { 2457 | if (buffers[i].isEmpty()) { 2458 | return false; 2459 | } 2460 | } 2461 | return true; 2462 | } 2463 | 2464 | var Switch = /*#__PURE__*/function () { 2465 | function Switch(source) { 2466 | classCallCheck$2(this, Switch); 2467 | 2468 | this.source = source; 2469 | } 2470 | 2471 | Switch.prototype.run = function run(sink, scheduler$$1) { 2472 | var switchSink = new SwitchSink(sink, scheduler$$1); 2473 | return disposeBoth(switchSink, this.source.run(switchSink, scheduler$$1)); 2474 | }; 2475 | 2476 | return Switch; 2477 | }(); 2478 | 2479 | var SwitchSink = /*#__PURE__*/function () { 2480 | function SwitchSink(sink, scheduler$$1) { 2481 | classCallCheck$2(this, SwitchSink); 2482 | 2483 | this.sink = sink; 2484 | this.scheduler = scheduler$$1; 2485 | this.current = null; 2486 | this.ended = false; 2487 | } 2488 | 2489 | SwitchSink.prototype.event = function event(t, stream) { 2490 | this._disposeCurrent(t); 2491 | this.current = new Segment(stream, t, Infinity, this, this.sink, this.scheduler); 2492 | }; 2493 | 2494 | SwitchSink.prototype.end = function end(t) { 2495 | this.ended = true; 2496 | this._checkEnd(t); 2497 | }; 2498 | 2499 | SwitchSink.prototype.error = function error(t, e) { 2500 | this.ended = true; 2501 | this.sink.error(t, e); 2502 | }; 2503 | 2504 | SwitchSink.prototype.dispose = function dispose$$1() { 2505 | return this._disposeCurrent(currentTime(this.scheduler)); 2506 | }; 2507 | 2508 | SwitchSink.prototype._disposeCurrent = function _disposeCurrent(t) { 2509 | if (this.current !== null) { 2510 | return this.current._dispose(t); 2511 | } 2512 | }; 2513 | 2514 | SwitchSink.prototype._disposeInner = function _disposeInner(t, inner) { 2515 | inner._dispose(t); 2516 | if (inner === this.current) { 2517 | this.current = null; 2518 | } 2519 | }; 2520 | 2521 | SwitchSink.prototype._checkEnd = function _checkEnd(t) { 2522 | if (this.ended && this.current === null) { 2523 | this.sink.end(t); 2524 | } 2525 | }; 2526 | 2527 | SwitchSink.prototype._endInner = function _endInner(t, inner) { 2528 | this._disposeInner(t, inner); 2529 | this._checkEnd(t); 2530 | }; 2531 | 2532 | SwitchSink.prototype._errorInner = function _errorInner(t, e, inner) { 2533 | this._disposeInner(t, inner); 2534 | this.sink.error(t, e); 2535 | }; 2536 | 2537 | return SwitchSink; 2538 | }(); 2539 | 2540 | var Segment = /*#__PURE__*/function () { 2541 | function Segment(source, min, max, outer, sink, scheduler$$1) { 2542 | classCallCheck$2(this, Segment); 2543 | 2544 | this.min = min; 2545 | this.max = max; 2546 | this.outer = outer; 2547 | this.sink = sink; 2548 | this.disposable = source.run(this, schedulerRelativeTo(min, scheduler$$1)); 2549 | } 2550 | 2551 | Segment.prototype.event = function event(t, x) { 2552 | var time = Math.max(0, t + this.min); 2553 | if (time < this.max) { 2554 | this.sink.event(time, x); 2555 | } 2556 | }; 2557 | 2558 | Segment.prototype.end = function end(t) { 2559 | this.outer._endInner(t + this.min, this); 2560 | }; 2561 | 2562 | Segment.prototype.error = function error(t, e) { 2563 | this.outer._errorInner(t + this.min, e, this); 2564 | }; 2565 | 2566 | Segment.prototype._dispose = function _dispose(t) { 2567 | tryDispose(t + this.min, this.disposable, this.sink); 2568 | }; 2569 | 2570 | return Segment; 2571 | }(); 2572 | 2573 | var SkipRepeats = /*#__PURE__*/function () { 2574 | function SkipRepeats(equals, source) { 2575 | classCallCheck$2(this, SkipRepeats); 2576 | 2577 | this.equals = equals; 2578 | this.source = source; 2579 | } 2580 | 2581 | SkipRepeats.prototype.run = function run(sink, scheduler$$1) { 2582 | return this.source.run(new SkipRepeatsSink(this.equals, sink), scheduler$$1); 2583 | }; 2584 | 2585 | return SkipRepeats; 2586 | }(); 2587 | 2588 | var SkipRepeatsSink = /*#__PURE__*/function (_Pipe) { 2589 | inherits(SkipRepeatsSink, _Pipe); 2590 | 2591 | function SkipRepeatsSink(equals, sink) { 2592 | classCallCheck$2(this, SkipRepeatsSink); 2593 | 2594 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 2595 | 2596 | _this.equals = equals; 2597 | _this.value = void 0; 2598 | _this.init = true; 2599 | return _this; 2600 | } 2601 | 2602 | SkipRepeatsSink.prototype.event = function event(t, x) { 2603 | if (this.init) { 2604 | this.init = false; 2605 | this.value = x; 2606 | this.sink.event(t, x); 2607 | } else if (!this.equals(this.value, x)) { 2608 | this.value = x; 2609 | this.sink.event(t, x); 2610 | } 2611 | }; 2612 | 2613 | return SkipRepeatsSink; 2614 | }(Pipe); 2615 | 2616 | var Until = /*#__PURE__*/function () { 2617 | function Until(maxSignal, source) { 2618 | classCallCheck$2(this, Until); 2619 | 2620 | this.maxSignal = maxSignal; 2621 | this.source = source; 2622 | } 2623 | 2624 | Until.prototype.run = function run(sink, scheduler$$1) { 2625 | var min = new Bound(-Infinity, sink); 2626 | var max = new UpperBound(this.maxSignal, sink, scheduler$$1); 2627 | var disposable$$1 = this.source.run(new TimeWindowSink(min, max, sink), scheduler$$1); 2628 | 2629 | return disposeAll([min, max, disposable$$1]); 2630 | }; 2631 | 2632 | return Until; 2633 | }(); 2634 | 2635 | var Since = /*#__PURE__*/function () { 2636 | function Since(minSignal, source) { 2637 | classCallCheck$2(this, Since); 2638 | 2639 | this.minSignal = minSignal; 2640 | this.source = source; 2641 | } 2642 | 2643 | Since.prototype.run = function run(sink, scheduler$$1) { 2644 | var min = new LowerBound(this.minSignal, sink, scheduler$$1); 2645 | var max = new Bound(Infinity, sink); 2646 | var disposable$$1 = this.source.run(new TimeWindowSink(min, max, sink), scheduler$$1); 2647 | 2648 | return disposeAll([min, max, disposable$$1]); 2649 | }; 2650 | 2651 | return Since; 2652 | }(); 2653 | 2654 | var Bound = /*#__PURE__*/function (_Pipe) { 2655 | inherits(Bound, _Pipe); 2656 | 2657 | function Bound(value, sink) { 2658 | classCallCheck$2(this, Bound); 2659 | 2660 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 2661 | 2662 | _this.value = value; 2663 | return _this; 2664 | } 2665 | 2666 | Bound.prototype.event = function event() {}; 2667 | 2668 | Bound.prototype.end = function end() {}; 2669 | 2670 | Bound.prototype.dispose = function dispose$$1() {}; 2671 | 2672 | return Bound; 2673 | }(Pipe); 2674 | 2675 | var TimeWindowSink = /*#__PURE__*/function (_Pipe2) { 2676 | inherits(TimeWindowSink, _Pipe2); 2677 | 2678 | function TimeWindowSink(min, max, sink) { 2679 | classCallCheck$2(this, TimeWindowSink); 2680 | 2681 | var _this2 = possibleConstructorReturn(this, _Pipe2.call(this, sink)); 2682 | 2683 | _this2.min = min; 2684 | _this2.max = max; 2685 | return _this2; 2686 | } 2687 | 2688 | TimeWindowSink.prototype.event = function event(t, x) { 2689 | if (t >= this.min.value && t < this.max.value) { 2690 | this.sink.event(t, x); 2691 | } 2692 | }; 2693 | 2694 | return TimeWindowSink; 2695 | }(Pipe); 2696 | 2697 | var LowerBound = /*#__PURE__*/function (_Pipe3) { 2698 | inherits(LowerBound, _Pipe3); 2699 | 2700 | function LowerBound(signal, sink, scheduler$$1) { 2701 | classCallCheck$2(this, LowerBound); 2702 | 2703 | var _this3 = possibleConstructorReturn(this, _Pipe3.call(this, sink)); 2704 | 2705 | _this3.value = Infinity; 2706 | _this3.disposable = signal.run(_this3, scheduler$$1); 2707 | return _this3; 2708 | } 2709 | 2710 | LowerBound.prototype.event = function event(t /*, x */) { 2711 | if (t < this.value) { 2712 | this.value = t; 2713 | } 2714 | }; 2715 | 2716 | LowerBound.prototype.end = function end() {}; 2717 | 2718 | LowerBound.prototype.dispose = function dispose$$1() { 2719 | return this.disposable.dispose(); 2720 | }; 2721 | 2722 | return LowerBound; 2723 | }(Pipe); 2724 | 2725 | var UpperBound = /*#__PURE__*/function (_Pipe4) { 2726 | inherits(UpperBound, _Pipe4); 2727 | 2728 | function UpperBound(signal, sink, scheduler$$1) { 2729 | classCallCheck$2(this, UpperBound); 2730 | 2731 | var _this4 = possibleConstructorReturn(this, _Pipe4.call(this, sink)); 2732 | 2733 | _this4.value = Infinity; 2734 | _this4.disposable = signal.run(_this4, scheduler$$1); 2735 | return _this4; 2736 | } 2737 | 2738 | UpperBound.prototype.event = function event(t, x) { 2739 | if (t < this.value) { 2740 | this.value = t; 2741 | this.sink.end(t); 2742 | } 2743 | }; 2744 | 2745 | UpperBound.prototype.end = function end() {}; 2746 | 2747 | UpperBound.prototype.dispose = function dispose$$1() { 2748 | return this.disposable.dispose(); 2749 | }; 2750 | 2751 | return UpperBound; 2752 | }(Pipe); 2753 | 2754 | var Delay = /*#__PURE__*/function () { 2755 | function Delay(dt, source) { 2756 | classCallCheck$2(this, Delay); 2757 | 2758 | this.dt = dt; 2759 | this.source = source; 2760 | } 2761 | 2762 | Delay.prototype.run = function run(sink, scheduler$$1) { 2763 | var delaySink = new DelaySink(this.dt, sink, scheduler$$1); 2764 | return disposeBoth(delaySink, this.source.run(delaySink, scheduler$$1)); 2765 | }; 2766 | 2767 | return Delay; 2768 | }(); 2769 | 2770 | var DelaySink = /*#__PURE__*/function (_Pipe) { 2771 | inherits(DelaySink, _Pipe); 2772 | 2773 | function DelaySink(dt, sink, scheduler$$1) { 2774 | classCallCheck$2(this, DelaySink); 2775 | 2776 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 2777 | 2778 | _this.dt = dt; 2779 | _this.scheduler = scheduler$$1; 2780 | _this.tasks = []; 2781 | return _this; 2782 | } 2783 | 2784 | DelaySink.prototype.dispose = function dispose$$1() { 2785 | this.tasks.forEach(cancelTask); 2786 | }; 2787 | 2788 | DelaySink.prototype.event = function event(t, x) { 2789 | this.tasks.push(delay(this.dt, propagateEventTask$1(x, this.sink), this.scheduler)); 2790 | }; 2791 | 2792 | DelaySink.prototype.end = function end(t) { 2793 | this.tasks.push(delay(this.dt, propagateEndTask(this.sink), this.scheduler)); 2794 | }; 2795 | 2796 | return DelaySink; 2797 | }(Pipe); 2798 | 2799 | var Throttle = /*#__PURE__*/function () { 2800 | function Throttle(period, source) { 2801 | classCallCheck$2(this, Throttle); 2802 | 2803 | this.period = period; 2804 | this.source = source; 2805 | } 2806 | 2807 | Throttle.prototype.run = function run(sink, scheduler$$1) { 2808 | return this.source.run(new ThrottleSink(this.period, sink), scheduler$$1); 2809 | }; 2810 | 2811 | return Throttle; 2812 | }(); 2813 | 2814 | var ThrottleSink = /*#__PURE__*/function (_Pipe) { 2815 | inherits(ThrottleSink, _Pipe); 2816 | 2817 | function ThrottleSink(period, sink) { 2818 | classCallCheck$2(this, ThrottleSink); 2819 | 2820 | var _this = possibleConstructorReturn(this, _Pipe.call(this, sink)); 2821 | 2822 | _this.time = 0; 2823 | _this.period = period; 2824 | return _this; 2825 | } 2826 | 2827 | ThrottleSink.prototype.event = function event(t, x) { 2828 | if (t >= this.time) { 2829 | this.time = t + this.period; 2830 | this.sink.event(t, x); 2831 | } 2832 | }; 2833 | 2834 | return ThrottleSink; 2835 | }(Pipe); 2836 | 2837 | var Debounce = /*#__PURE__*/function () { 2838 | function Debounce(dt, source) { 2839 | classCallCheck$2(this, Debounce); 2840 | 2841 | this.dt = dt; 2842 | this.source = source; 2843 | } 2844 | 2845 | Debounce.prototype.run = function run(sink, scheduler$$1) { 2846 | return new DebounceSink(this.dt, this.source, sink, scheduler$$1); 2847 | }; 2848 | 2849 | return Debounce; 2850 | }(); 2851 | 2852 | var DebounceSink = /*#__PURE__*/function () { 2853 | function DebounceSink(dt, source, sink, scheduler$$1) { 2854 | classCallCheck$2(this, DebounceSink); 2855 | 2856 | this.dt = dt; 2857 | this.sink = sink; 2858 | this.scheduler = scheduler$$1; 2859 | this.value = void 0; 2860 | this.timer = null; 2861 | 2862 | this.disposable = source.run(this, scheduler$$1); 2863 | } 2864 | 2865 | DebounceSink.prototype.event = function event(t, x) { 2866 | this._clearTimer(); 2867 | this.value = x; 2868 | this.timer = delay(this.dt, new DebounceTask(this, x), this.scheduler); 2869 | }; 2870 | 2871 | DebounceSink.prototype._event = function _event(t, x) { 2872 | this._clearTimer(); 2873 | this.sink.event(t, x); 2874 | }; 2875 | 2876 | DebounceSink.prototype.end = function end(t) { 2877 | if (this._clearTimer()) { 2878 | this.sink.event(t, this.value); 2879 | this.value = undefined; 2880 | } 2881 | this.sink.end(t); 2882 | }; 2883 | 2884 | DebounceSink.prototype.error = function error(t, x) { 2885 | this._clearTimer(); 2886 | this.sink.error(t, x); 2887 | }; 2888 | 2889 | DebounceSink.prototype.dispose = function dispose$$1() { 2890 | this._clearTimer(); 2891 | this.disposable.dispose(); 2892 | }; 2893 | 2894 | DebounceSink.prototype._clearTimer = function _clearTimer() { 2895 | if (this.timer === null) { 2896 | return false; 2897 | } 2898 | this.timer.dispose(); 2899 | this.timer = null; 2900 | return true; 2901 | }; 2902 | 2903 | return DebounceSink; 2904 | }(); 2905 | 2906 | var DebounceTask = /*#__PURE__*/function () { 2907 | function DebounceTask(debounce, value) { 2908 | classCallCheck$2(this, DebounceTask); 2909 | 2910 | this.debounce = debounce; 2911 | this.value = value; 2912 | } 2913 | 2914 | DebounceTask.prototype.run = function run(t) { 2915 | this.debounce._event(t, this.value); 2916 | }; 2917 | 2918 | DebounceTask.prototype.error = function error(t, e) { 2919 | this.debounce.error(t, e); 2920 | }; 2921 | 2922 | DebounceTask.prototype.dispose = function dispose$$1() {}; 2923 | 2924 | return DebounceTask; 2925 | }(); 2926 | 2927 | var Await = /*#__PURE__*/function () { 2928 | function Await(source) { 2929 | classCallCheck$2(this, Await); 2930 | 2931 | this.source = source; 2932 | } 2933 | 2934 | Await.prototype.run = function run(sink, scheduler$$1) { 2935 | return this.source.run(new AwaitSink(sink, scheduler$$1), scheduler$$1); 2936 | }; 2937 | 2938 | return Await; 2939 | }(); 2940 | 2941 | var AwaitSink = /*#__PURE__*/function () { 2942 | function AwaitSink(sink, scheduler$$1) { 2943 | var _this = this; 2944 | 2945 | classCallCheck$2(this, AwaitSink); 2946 | 2947 | this.sink = sink; 2948 | this.scheduler = scheduler$$1; 2949 | this.queue = Promise.resolve(); 2950 | 2951 | // Pre-create closures, to avoid creating them per event 2952 | this._eventBound = function (x) { 2953 | return _this.sink.event(currentTime(_this.scheduler), x); 2954 | }; 2955 | this._endBound = function () { 2956 | return _this.sink.end(currentTime(_this.scheduler)); 2957 | }; 2958 | this._errorBound = function (e) { 2959 | return _this.sink.error(currentTime(_this.scheduler), e); 2960 | }; 2961 | } 2962 | 2963 | AwaitSink.prototype.event = function event(t, promise) { 2964 | var _this2 = this; 2965 | 2966 | this.queue = this.queue.then(function () { 2967 | return _this2._event(promise); 2968 | }).catch(this._errorBound); 2969 | }; 2970 | 2971 | AwaitSink.prototype.end = function end(t) { 2972 | this.queue = this.queue.then(this._endBound).catch(this._errorBound); 2973 | }; 2974 | 2975 | AwaitSink.prototype.error = function error(t, e) { 2976 | var _this3 = this; 2977 | 2978 | // Don't resolve error values, propagate directly 2979 | this.queue = this.queue.then(function () { 2980 | return _this3._errorBound(e); 2981 | }).catch(fatalError); 2982 | }; 2983 | 2984 | AwaitSink.prototype._event = function _event(promise) { 2985 | return promise.then(this._eventBound); 2986 | }; 2987 | 2988 | return AwaitSink; 2989 | }(); 2990 | 2991 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 2992 | /** @author Brian Cavalier */ 2993 | /** @author John Hann */ 2994 | 2995 | var SafeSink = /*#__PURE__*/function () { 2996 | function SafeSink(sink) { 2997 | classCallCheck$2(this, SafeSink); 2998 | 2999 | this.sink = sink; 3000 | this.active = true; 3001 | } 3002 | 3003 | SafeSink.prototype.event = function event(t, x) { 3004 | if (!this.active) { 3005 | return; 3006 | } 3007 | this.sink.event(t, x); 3008 | }; 3009 | 3010 | SafeSink.prototype.end = function end(t, x) { 3011 | if (!this.active) { 3012 | return; 3013 | } 3014 | this.disable(); 3015 | this.sink.end(t, x); 3016 | }; 3017 | 3018 | SafeSink.prototype.error = function error(t, e) { 3019 | this.disable(); 3020 | this.sink.error(t, e); 3021 | }; 3022 | 3023 | SafeSink.prototype.disable = function disable() { 3024 | this.active = false; 3025 | return this.sink; 3026 | }; 3027 | 3028 | return SafeSink; 3029 | }(); 3030 | 3031 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 3032 | /** @author Brian Cavalier */ 3033 | /** @author John Hann */ 3034 | 3035 | function tryEvent(t, x, sink) { 3036 | try { 3037 | sink.event(t, x); 3038 | } catch (e) { 3039 | sink.error(t, e); 3040 | } 3041 | } 3042 | 3043 | function tryEnd(t, sink) { 3044 | try { 3045 | sink.end(t); 3046 | } catch (e) { 3047 | sink.error(t, e); 3048 | } 3049 | } 3050 | 3051 | var ErrorStream = /*#__PURE__*/function () { 3052 | function ErrorStream(e) { 3053 | classCallCheck$2(this, ErrorStream); 3054 | 3055 | this.value = e; 3056 | } 3057 | 3058 | ErrorStream.prototype.run = function run(sink, scheduler$$1) { 3059 | return asap(propagateErrorTask$1(this.value, sink), scheduler$$1); 3060 | }; 3061 | 3062 | return ErrorStream; 3063 | }(); 3064 | 3065 | var RecoverWith = /*#__PURE__*/function () { 3066 | function RecoverWith(f, source) { 3067 | classCallCheck$2(this, RecoverWith); 3068 | 3069 | this.f = f; 3070 | this.source = source; 3071 | } 3072 | 3073 | RecoverWith.prototype.run = function run(sink, scheduler$$1) { 3074 | return new RecoverWithSink(this.f, this.source, sink, scheduler$$1); 3075 | }; 3076 | 3077 | return RecoverWith; 3078 | }(); 3079 | 3080 | var RecoverWithSink = /*#__PURE__*/function () { 3081 | function RecoverWithSink(f, source, sink, scheduler$$1) { 3082 | classCallCheck$2(this, RecoverWithSink); 3083 | 3084 | this.f = f; 3085 | this.sink = new SafeSink(sink); 3086 | this.scheduler = scheduler$$1; 3087 | this.disposable = source.run(this, scheduler$$1); 3088 | } 3089 | 3090 | RecoverWithSink.prototype.event = function event(t, x) { 3091 | tryEvent(t, x, this.sink); 3092 | }; 3093 | 3094 | RecoverWithSink.prototype.end = function end(t) { 3095 | tryEnd(t, this.sink); 3096 | }; 3097 | 3098 | RecoverWithSink.prototype.error = function error(t, e) { 3099 | var nextSink = this.sink.disable(); 3100 | 3101 | tryDispose(t, this.disposable, this.sink); 3102 | 3103 | this._startNext(t, e, nextSink); 3104 | }; 3105 | 3106 | RecoverWithSink.prototype._startNext = function _startNext(t, x, sink) { 3107 | try { 3108 | this.disposable = this._continue(this.f, t, x, sink); 3109 | } catch (e) { 3110 | sink.error(t, e); 3111 | } 3112 | }; 3113 | 3114 | RecoverWithSink.prototype._continue = function _continue(f, t, x, sink) { 3115 | return run$1(sink, this.scheduler, withLocalTime$1(t, f(x))); 3116 | }; 3117 | 3118 | RecoverWithSink.prototype.dispose = function dispose$$1() { 3119 | return this.disposable.dispose(); 3120 | }; 3121 | 3122 | return RecoverWithSink; 3123 | }(); 3124 | 3125 | var Multicast = /*#__PURE__*/function () { 3126 | function Multicast(source) { 3127 | classCallCheck$2(this, Multicast); 3128 | 3129 | this.source = new MulticastSource(source); 3130 | } 3131 | 3132 | Multicast.prototype.run = function run(sink, scheduler$$1) { 3133 | return this.source.run(sink, scheduler$$1); 3134 | }; 3135 | 3136 | return Multicast; 3137 | }(); 3138 | 3139 | var MulticastSource = /*#__PURE__*/function () { 3140 | function MulticastSource(source) { 3141 | classCallCheck$2(this, MulticastSource); 3142 | 3143 | this.source = source; 3144 | this.sinks = []; 3145 | this.disposable = disposeNone(); 3146 | } 3147 | 3148 | MulticastSource.prototype.run = function run(sink, scheduler$$1) { 3149 | var n = this.add(sink); 3150 | if (n === 1) { 3151 | this.disposable = this.source.run(this, scheduler$$1); 3152 | } 3153 | return disposeOnce(new MulticastDisposable(this, sink)); 3154 | }; 3155 | 3156 | MulticastSource.prototype.dispose = function dispose$$1() { 3157 | var disposable$$1 = this.disposable; 3158 | this.disposable = disposeNone(); 3159 | return disposable$$1.dispose(); 3160 | }; 3161 | 3162 | MulticastSource.prototype.add = function add(sink) { 3163 | this.sinks = append(sink, this.sinks); 3164 | return this.sinks.length; 3165 | }; 3166 | 3167 | MulticastSource.prototype.remove = function remove$$1(sink) { 3168 | var i = findIndex(sink, this.sinks); 3169 | // istanbul ignore next 3170 | if (i >= 0) { 3171 | this.sinks = remove(i, this.sinks); 3172 | } 3173 | 3174 | return this.sinks.length; 3175 | }; 3176 | 3177 | MulticastSource.prototype.event = function event(time, value) { 3178 | var s = this.sinks; 3179 | if (s.length === 1) { 3180 | return s[0].event(time, value); 3181 | } 3182 | for (var i = 0; i < s.length; ++i) { 3183 | tryEvent(time, value, s[i]); 3184 | } 3185 | }; 3186 | 3187 | MulticastSource.prototype.end = function end(time) { 3188 | var s = this.sinks; 3189 | for (var i = 0; i < s.length; ++i) { 3190 | tryEnd(time, s[i]); 3191 | } 3192 | }; 3193 | 3194 | MulticastSource.prototype.error = function error(time, err) { 3195 | var s = this.sinks; 3196 | for (var i = 0; i < s.length; ++i) { 3197 | s[i].error(time, err); 3198 | } 3199 | }; 3200 | 3201 | return MulticastSource; 3202 | }(); 3203 | 3204 | var MulticastDisposable = /*#__PURE__*/function () { 3205 | function MulticastDisposable(source, sink) { 3206 | classCallCheck$2(this, MulticastDisposable); 3207 | 3208 | this.source = source; 3209 | this.sink = sink; 3210 | } 3211 | 3212 | MulticastDisposable.prototype.dispose = function dispose$$1() { 3213 | if (this.source.remove(this.sink) === 0) { 3214 | this.source.dispose(); 3215 | } 3216 | }; 3217 | 3218 | return MulticastDisposable; 3219 | }(); 3220 | 3221 | // ----------------------------------------------------------------------- 3222 | // Observing 3223 | 3224 | var runEffects$$1 = /*#__PURE__*/curry2(runEffects$1); 3225 | 3226 | // ----------------------------------------------------------------------- 3227 | // Transforming 3228 | 3229 | var map$1 = /*#__PURE__*/curry2(map$2); 3230 | var tap$$1 = /*#__PURE__*/curry2(tap$1); 3231 | 3232 | // 3233 | 3234 | // 3235 | 3236 | // 3237 | 3238 | 3239 | 3240 | var snapshot$$2 = function (b , s ) { return b(s); }; 3241 | 3242 | var sample = function (b , s ) { return map$1(function (ab) { return ab[0]; }, snapshot$$2(b, s)); }; 3243 | 3244 | var always = function (a ) { return function (sb ) { return map$1(function (b) { return [a, b]; }, sb); }; }; 3245 | 3246 | var map$3 = function (f , ba ) { return function (sc ) { return map$1(function (ref) { 3247 | var a = ref[0]; 3248 | var c = ref[1]; 3249 | 3250 | return [f(a), c]; 3251 | }, snapshot$$2(ba, sc)); }; }; 3252 | 3253 | var liftA2 = function (f , ba , bb ) { return function (sd ) { return map$1(function (ref) { 3254 | var a = ref[0]; 3255 | var ref_1 = ref[1]; 3256 | var b = ref_1[0]; 3257 | var d = ref_1[1]; 3258 | 3259 | return [f(a, b), d]; 3260 | }, snapshot$$2(ba, snapshot$$2(bb, sd))); }; }; 3261 | 3262 | /** @license MIT License (c) copyright 2010-2016 original author or authors */ 3263 | 3264 | // removeAll :: (a -> boolean) -> [a] -> [a] 3265 | // remove all elements matching a predicate 3266 | function removeAll$1 (f, a) { 3267 | var l = a.length; 3268 | var b = new Array(l); 3269 | var j = 0; 3270 | for (var x = (void 0), i = 0; i < l; ++i) { 3271 | x = a[i]; 3272 | if (!f(x)) { 3273 | b[j] = x; 3274 | ++j; 3275 | } 3276 | } 3277 | 3278 | b.length = j; 3279 | return b 3280 | } 3281 | 3282 | // findIndex :: a -> [a] -> Int 3283 | // find index of x in a, from the left 3284 | function findIndex$1 (x, a) { 3285 | for (var i = 0, l = a.length; i < l; ++i) { 3286 | if (x === a[i]) { 3287 | return i 3288 | } 3289 | } 3290 | return -1 3291 | } 3292 | 3293 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 3294 | 3295 | var ScheduledTask$1 = function ScheduledTask (time, localOffset, period, task, scheduler) { 3296 | this.time = time; 3297 | this.localOffset = localOffset; 3298 | this.period = period; 3299 | this.task = task; 3300 | this.scheduler = scheduler; 3301 | this.active = true; 3302 | }; 3303 | 3304 | ScheduledTask$1.prototype.run = function run () { 3305 | return this.task.run(this.time - this.localOffset) 3306 | }; 3307 | 3308 | ScheduledTask$1.prototype.error = function error (e) { 3309 | return this.task.error(this.time - this.localOffset, e) 3310 | }; 3311 | 3312 | ScheduledTask$1.prototype.dispose = function dispose () { 3313 | this.scheduler.cancel(this); 3314 | return this.task.dispose() 3315 | }; 3316 | 3317 | var RelativeScheduler$1 = function RelativeScheduler (origin, scheduler) { 3318 | this.origin = origin; 3319 | this.scheduler = scheduler; 3320 | }; 3321 | 3322 | RelativeScheduler$1.prototype.currentTime = function currentTime () { 3323 | return this.scheduler.currentTime() - this.origin 3324 | }; 3325 | 3326 | RelativeScheduler$1.prototype.scheduleTask = function scheduleTask (localOffset, delay, period, task) { 3327 | return this.scheduler.scheduleTask(localOffset + this.origin, delay, period, task) 3328 | }; 3329 | 3330 | RelativeScheduler$1.prototype.relative = function relative (origin) { 3331 | return new RelativeScheduler$1(origin + this.origin, this.scheduler) 3332 | }; 3333 | 3334 | RelativeScheduler$1.prototype.cancel = function cancel (task) { 3335 | return this.scheduler.cancel(task) 3336 | }; 3337 | 3338 | RelativeScheduler$1.prototype.cancelAll = function cancelAll (f) { 3339 | return this.scheduler.cancelAll(f) 3340 | }; 3341 | 3342 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 3343 | 3344 | var defer$1 = function (task) { return Promise.resolve(task).then(runTask$1); }; 3345 | 3346 | function runTask$1 (task) { 3347 | try { 3348 | return task.run() 3349 | } catch (e) { 3350 | return task.error(e) 3351 | } 3352 | } 3353 | 3354 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 3355 | 3356 | var Scheduler$1 = function Scheduler (timer, timeline) { 3357 | var this$1 = this; 3358 | 3359 | this.timer = timer; 3360 | this.timeline = timeline; 3361 | 3362 | this._timer = null; 3363 | this._nextArrival = Infinity; 3364 | 3365 | this._runReadyTasksBound = function () { return this$1._runReadyTasks(this$1.currentTime()); }; 3366 | }; 3367 | 3368 | Scheduler$1.prototype.currentTime = function currentTime () { 3369 | return this.timer.now() 3370 | }; 3371 | 3372 | Scheduler$1.prototype.scheduleTask = function scheduleTask (localOffset, delay, period, task) { 3373 | var time = this.currentTime() + Math.max(0, delay); 3374 | var st = new ScheduledTask$1(time, localOffset, period, task, this); 3375 | 3376 | this.timeline.add(st); 3377 | this._scheduleNextRun(); 3378 | return st 3379 | }; 3380 | 3381 | Scheduler$1.prototype.relative = function relative (offset) { 3382 | return new RelativeScheduler$1(offset, this) 3383 | }; 3384 | 3385 | Scheduler$1.prototype.cancel = function cancel (task) { 3386 | task.active = false; 3387 | if (this.timeline.remove(task)) { 3388 | this._reschedule(); 3389 | } 3390 | }; 3391 | 3392 | Scheduler$1.prototype.cancelAll = function cancelAll (f) { 3393 | this.timeline.removeAll(f); 3394 | this._reschedule(); 3395 | }; 3396 | 3397 | Scheduler$1.prototype._reschedule = function _reschedule () { 3398 | if (this.timeline.isEmpty()) { 3399 | this._unschedule(); 3400 | } else { 3401 | this._scheduleNextRun(this.currentTime()); 3402 | } 3403 | }; 3404 | 3405 | Scheduler$1.prototype._unschedule = function _unschedule () { 3406 | this.timer.clearTimer(this._timer); 3407 | this._timer = null; 3408 | }; 3409 | 3410 | Scheduler$1.prototype._scheduleNextRun = function _scheduleNextRun () { // eslint-disable-line complexity 3411 | if (this.timeline.isEmpty()) { 3412 | return 3413 | } 3414 | 3415 | var nextArrival = this.timeline.nextArrival(); 3416 | 3417 | if (this._timer === null) { 3418 | this._scheduleNextArrival(nextArrival); 3419 | } else if (nextArrival < this._nextArrival) { 3420 | this._unschedule(); 3421 | this._scheduleNextArrival(nextArrival); 3422 | } 3423 | }; 3424 | 3425 | Scheduler$1.prototype._scheduleNextArrival = function _scheduleNextArrival (nextArrival) { 3426 | this._nextArrival = nextArrival; 3427 | var delay = Math.max(0, nextArrival - this.currentTime()); 3428 | this._timer = this.timer.setTimer(this._runReadyTasksBound, delay); 3429 | }; 3430 | 3431 | Scheduler$1.prototype._runReadyTasks = function _runReadyTasks () { 3432 | this._timer = null; 3433 | this.timeline.runTasks(this.currentTime(), runTask$1); 3434 | this._scheduleNextRun(); 3435 | }; 3436 | 3437 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 3438 | 3439 | var Timeline$1 = function Timeline () { 3440 | this.tasks = []; 3441 | }; 3442 | 3443 | Timeline$1.prototype.nextArrival = function nextArrival () { 3444 | return this.isEmpty() ? Infinity : this.tasks[0].time 3445 | }; 3446 | 3447 | Timeline$1.prototype.isEmpty = function isEmpty () { 3448 | return this.tasks.length === 0 3449 | }; 3450 | 3451 | Timeline$1.prototype.add = function add (st) { 3452 | insertByTime$1(st, this.tasks); 3453 | }; 3454 | 3455 | Timeline$1.prototype.remove = function remove (st) { 3456 | var i = binarySearch$1(getTime$1(st), this.tasks); 3457 | 3458 | if (i >= 0 && i < this.tasks.length) { 3459 | var at = findIndex$1(st, this.tasks[i].events); 3460 | if (at >= 0) { 3461 | this.tasks[i].events.splice(at, 1); 3462 | return true 3463 | } 3464 | } 3465 | 3466 | return false 3467 | }; 3468 | 3469 | Timeline$1.prototype.removeAll = function removeAll$$1 (f) { 3470 | var this$1 = this; 3471 | 3472 | for (var i = 0; i < this.tasks.length; ++i) { 3473 | removeAllFrom$1(f, this$1.tasks[i]); 3474 | } 3475 | }; 3476 | 3477 | Timeline$1.prototype.runTasks = function runTasks (t, runTask) { 3478 | var this$1 = this; 3479 | 3480 | var tasks = this.tasks; 3481 | var l = tasks.length; 3482 | var i = 0; 3483 | 3484 | while (i < l && tasks[i].time <= t) { 3485 | ++i; 3486 | } 3487 | 3488 | this.tasks = tasks.slice(i); 3489 | 3490 | // Run all ready tasks 3491 | for (var j = 0; j < i; ++j) { 3492 | this$1.tasks = runReadyTasks$1(runTask, tasks[j].events, this$1.tasks); 3493 | } 3494 | }; 3495 | 3496 | function runReadyTasks$1 (runTask, events, tasks) { // eslint-disable-line complexity 3497 | for (var i = 0; i < events.length; ++i) { 3498 | var task = events[i]; 3499 | 3500 | if (task.active) { 3501 | runTask(task); 3502 | 3503 | // Reschedule periodic repeating tasks 3504 | // Check active again, since a task may have canceled itself 3505 | if (task.period >= 0 && task.active) { 3506 | task.time = task.time + task.period; 3507 | insertByTime$1(task, tasks); 3508 | } 3509 | } 3510 | } 3511 | 3512 | return tasks 3513 | } 3514 | 3515 | function insertByTime$1 (task, timeslots) { 3516 | var l = timeslots.length; 3517 | var time = getTime$1(task); 3518 | 3519 | if (l === 0) { 3520 | timeslots.push(newTimeslot$1(time, [task])); 3521 | return 3522 | } 3523 | 3524 | var i = binarySearch$1(time, timeslots); 3525 | 3526 | if (i >= l) { 3527 | timeslots.push(newTimeslot$1(time, [task])); 3528 | } else { 3529 | insertAtTimeslot$1(task, timeslots, time, i); 3530 | } 3531 | } 3532 | 3533 | function insertAtTimeslot$1 (task, timeslots, time, i) { 3534 | var timeslot = timeslots[i]; 3535 | if (time === timeslot.time) { 3536 | addEvent$1(task, timeslot.events, time); 3537 | } else { 3538 | timeslots.splice(i, 0, newTimeslot$1(time, [task])); 3539 | } 3540 | } 3541 | 3542 | function addEvent$1 (task, events) { 3543 | if (events.length === 0 || task.time >= events[events.length - 1].time) { 3544 | events.push(task); 3545 | } else { 3546 | spliceEvent$1(task, events); 3547 | } 3548 | } 3549 | 3550 | function spliceEvent$1 (task, events) { 3551 | for (var j = 0; j < events.length; j++) { 3552 | if (task.time < events[j].time) { 3553 | events.splice(j, 0, task); 3554 | break 3555 | } 3556 | } 3557 | } 3558 | 3559 | function getTime$1 (scheduledTask) { 3560 | return Math.floor(scheduledTask.time) 3561 | } 3562 | 3563 | function removeAllFrom$1 (f, timeslot) { 3564 | timeslot.events = removeAll$1(f, timeslot.events); 3565 | } 3566 | 3567 | function binarySearch$1 (t, sortedArray) { // eslint-disable-line complexity 3568 | var lo = 0; 3569 | var hi = sortedArray.length; 3570 | var mid, y; 3571 | 3572 | while (lo < hi) { 3573 | mid = Math.floor((lo + hi) / 2); 3574 | y = sortedArray[mid]; 3575 | 3576 | if (t === y.time) { 3577 | return mid 3578 | } else if (t < y.time) { 3579 | hi = mid; 3580 | } else { 3581 | lo = mid + 1; 3582 | } 3583 | } 3584 | return hi 3585 | } 3586 | 3587 | var newTimeslot$1 = function (t, events) { return ({ time: t, events: events }); }; 3588 | 3589 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 3590 | 3591 | /* global setTimeout, clearTimeout */ 3592 | 3593 | var ClockTimer$1 = function ClockTimer (clock) { 3594 | this._clock = clock; 3595 | }; 3596 | 3597 | ClockTimer$1.prototype.now = function now () { 3598 | return this._clock.now() 3599 | }; 3600 | 3601 | ClockTimer$1.prototype.setTimer = function setTimer (f, dt) { 3602 | return dt <= 0 ? runAsap$1(f) : setTimeout(f, dt) 3603 | }; 3604 | 3605 | ClockTimer$1.prototype.clearTimer = function clearTimer (t) { 3606 | return t instanceof Asap$1 ? t.cancel() : clearTimeout(t) 3607 | }; 3608 | 3609 | var Asap$1 = function Asap (f) { 3610 | this.f = f; 3611 | this.active = true; 3612 | }; 3613 | 3614 | Asap$1.prototype.run = function run () { 3615 | return this.active && this.f() 3616 | }; 3617 | 3618 | Asap$1.prototype.error = function error (e) { 3619 | throw e 3620 | }; 3621 | 3622 | Asap$1.prototype.cancel = function cancel () { 3623 | this.active = false; 3624 | }; 3625 | 3626 | function runAsap$1 (f) { 3627 | var task = new Asap$1(f); 3628 | defer$1(task); 3629 | return task 3630 | } 3631 | 3632 | /** @license MIT License (c) copyright 2010-2017 original author or authors */ 3633 | 3634 | /* global performance, process */ 3635 | 3636 | var RelativeClock$1 = function RelativeClock (clock, origin) { 3637 | this.origin = origin; 3638 | this.clock = clock; 3639 | }; 3640 | 3641 | RelativeClock$1.prototype.now = function now () { 3642 | return this.clock.now() - this.origin 3643 | }; 3644 | 3645 | var HRTimeClock$1 = function HRTimeClock (hrtime, origin) { 3646 | this.origin = origin; 3647 | this.hrtime = hrtime; 3648 | }; 3649 | 3650 | HRTimeClock$1.prototype.now = function now () { 3651 | var hrt = this.hrtime(this.origin); 3652 | return (hrt[0] * 1e9 + hrt[1]) / 1e6 3653 | }; 3654 | 3655 | var clockRelativeTo$1 = function (clock) { return new RelativeClock$1(clock, clock.now()); }; 3656 | 3657 | var newPerformanceClock$1 = function () { return clockRelativeTo$1(performance); }; 3658 | 3659 | var newDateClock$1 = function () { return clockRelativeTo$1(Date); }; 3660 | 3661 | var newHRTimeClock$1 = function () { return new HRTimeClock$1(process.hrtime, process.hrtime()); }; 3662 | 3663 | var newPlatformClock$1 = function () { 3664 | if (typeof performance !== 'undefined' && typeof performance.now === 'function') { 3665 | return newPerformanceClock$1() 3666 | } else if (typeof process !== 'undefined' && typeof process.hrtime === 'function') { 3667 | return newHRTimeClock$1() 3668 | } 3669 | 3670 | return newDateClock$1() 3671 | }; 3672 | 3673 | // Read the current time from the provided Scheduler 3674 | var currentTime$1 = function (scheduler) { return scheduler.currentTime(); }; 3675 | 3676 | var newDefaultScheduler$1 = function () { return new Scheduler$1(newDefaultTimer$1(), new Timeline$1()); }; 3677 | 3678 | var newDefaultTimer$1 = function () { return new ClockTimer$1(newPlatformClock$1()); }; 3679 | 3680 | /** @license MIT License (c) copyright 2015-2016 original author or authors */ 3681 | /** @author Brian Cavalier */ 3682 | // domEvent :: (EventTarget t, Event e) => String -> t -> boolean=false -> Stream e 3683 | var domEvent = function (event, node, capture) { 3684 | if ( capture === void 0 ) { capture = false; } 3685 | 3686 | return new DomEvent(event, node, capture); 3687 | }; 3688 | var input = function (node, capture) { 3689 | if ( capture === void 0 ) { capture = false; } 3690 | 3691 | return domEvent('input', node, capture); 3692 | }; 3693 | 3694 | var DomEvent = function DomEvent (event, node, capture) { 3695 | this.event = event; 3696 | this.node = node; 3697 | this.capture = capture; 3698 | }; 3699 | 3700 | DomEvent.prototype.run = function run (sink, scheduler$$1) { 3701 | var this$1 = this; 3702 | 3703 | var send = function (e) { return tryEvent$1(currentTime$1(scheduler$$1), e, sink); }; 3704 | var dispose = function () { return this$1.node.removeEventListener(this$1.event, send, this$1.capture); }; 3705 | 3706 | this.node.addEventListener(this.event, send, this.capture); 3707 | 3708 | return { dispose: dispose } 3709 | }; 3710 | 3711 | function tryEvent$1 (t, x, sink) { 3712 | try { 3713 | sink.event(t, x); 3714 | } catch (e) { 3715 | sink.error(t, e); 3716 | } 3717 | } 3718 | 3719 | // inputById :: String -> HTMLInputElement 3720 | // Type-safe helper to get an HTMLInputElement by id 3721 | var inputById = function (id$$1) { 3722 | var el = document.getElementById(id$$1); 3723 | if(!(el instanceof HTMLInputElement)) { throw new Error(("input #" + id$$1 + " not found")) } 3724 | return el 3725 | }; 3726 | 3727 | // numberValue :: HTMLInputElement -> Behavior Number 3728 | var numberValue = function (input$$1) { return map$3(function (input$$1) { return Number(input$$1.value); }, always(input$$1)); }; 3729 | 3730 | // add :: Number -> Number -> Number 3731 | var add = function (x, y) { return x + y; }; 3732 | 3733 | // x :: Behavior Number 3734 | // x is the value of the #x input at all times 3735 | // note that x is not a change event, it's the value over time 3736 | var x = numberValue(inputById('x')); 3737 | 3738 | // y :: Behavior Number 3739 | // Similarly, y is the value of the #y input at all times 3740 | var y = numberValue(inputById('y')); 3741 | 3742 | // z :: Behavior Number 3743 | // z is x + y at all times 3744 | var z = liftA2(add, x, y); 3745 | 3746 | // inputEvents :: Stream InputEvent 3747 | // All the input events that will occur in container 3748 | var inputEvents = input(document.getElementById('container')); 3749 | 3750 | // render :: HTMLInputElement -> Number -> void 3751 | var render = function (el) { return function (result) { return el.value = String(result); }; }; 3752 | 3753 | // updateFrom :: Behavior Number -> Stream void 3754 | // Sample z at all the instants input events occur in container 3755 | // and render the sampled value into the `#z` input element's value 3756 | var updates = tap$$1(render(inputById('z')), sample(z, inputEvents)); 3757 | 3758 | // Run the app 3759 | runEffects$$1(updates, newDefaultScheduler$1()); 3760 | 3761 | }()); 3762 | -------------------------------------------------------------------------------- /examples/add-inputs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Add 7 | 29 | 30 | 31 | 32 | + 33 | = 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /examples/add-inputs/index.ts: -------------------------------------------------------------------------------- 1 | import { always, map, liftA2, sample, Behavior } from '../../src/index' 2 | import { input } from '@most/dom-event' 3 | import { newDefaultScheduler } from '@most/scheduler' 4 | import { tap, runEffects } from '@most/core' 5 | 6 | // inputById :: String -> HTMLInputElement 7 | // Type-safe helper to get an HTMLInputElement by id 8 | const byId = (id: string): E => { 9 | const el = document.getElementById(id) 10 | if (!el) { throw new Error(`#${id} not found`) } 11 | return el as E 12 | } 13 | 14 | // numberValue :: HTMLInputElement -> Behavior Number 15 | const numberValue = (input: HTMLInputElement): Behavior => 16 | map((input: HTMLInputElement) => Number(input.value), always(input)) 17 | 18 | // add :: Number -> Number -> Number 19 | const add = (x: number, y: number): number => x + y 20 | 21 | // x :: Behavior Number 22 | // x is the value of the #x input at all times 23 | // note that x is not a change event, it's the value over time 24 | const x = numberValue(byId('x')) 25 | 26 | // y :: Behavior Number 27 | // Similarly, y is the value of the #y input at all times 28 | const y = numberValue(byId('y')) 29 | 30 | // z :: Behavior Number 31 | // z is x + y at all times 32 | const z = liftA2(add, x, y) 33 | 34 | // inputEvents :: Stream InputEvent 35 | // All the input events that will occur in container 36 | const inputEvents = input(byId('container')) 37 | 38 | // render :: HTMLInputElement -> Number -> void 39 | const render = (el: HTMLInputElement) => 40 | (result: number) => el.value = String(result) 41 | 42 | // updateFrom :: Behavior Number -> Stream void 43 | // Sample z at all the instants input events occur in container 44 | // and render the sampled value into the `#z` input element's value 45 | const updates = tap(render(byId('z')), sample(z, inputEvents)) 46 | 47 | // Run the app 48 | runEffects(updates, newDefaultScheduler()) 49 | -------------------------------------------------------------------------------- /examples/secret-combination/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Secret Combination 6 | 7 | 8 |
9 | 10 | 11 |

12 |
13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /examples/secret-combination/index.ts: -------------------------------------------------------------------------------- 1 | import { Stream, Time } from '@most/types' 2 | import { time, snapshot } from '../../src' 3 | import { scan, merge, skip, constant, map, tap, runEffects } from '@most/core' 4 | import { newDefaultScheduler } from '@most/scheduler' 5 | import { click } from '@most/dom-event' 6 | import { compose, append } from '@most/prelude' 7 | 8 | type TimeAndValue = [Time, A] 9 | 10 | const timeOf = (tv: TimeAndValue): Time => tv[0] 11 | const valueOf = (tv: TimeAndValue): A => tv[1] 12 | 13 | type TimeAndLetter = TimeAndValue 14 | 15 | type CodeAndElapsed = { code: string, elapsed: Time } 16 | 17 | type MatchResult = CodeAndElapsed & { match: boolean } 18 | 19 | // DOM Event helpers 20 | const fail = (s: string): never => { throw new Error(s) } 21 | const qs = (s: string): HTMLElement => document.querySelector(s) || fail(`${s} not found`) 22 | 23 | const aClicks = constant('A', click(qs('[name=a]'))) 24 | const bClicks = constant('B', click(qs('[name=b]'))) 25 | const value = qs('.value') 26 | 27 | const isMatch = ({ code, elapsed }: CodeAndElapsed): boolean => 28 | elapsed < 5000 && code === 'ABBABA' 29 | 30 | const verify = (isMatch: (ce: CodeAndElapsed) => boolean) => 31 | (ce: CodeAndElapsed): MatchResult => ({ ...ce, match: isMatch(ce) }) 32 | 33 | const codeAndTime = (pairs: TimeAndLetter[]): CodeAndElapsed => ({ 34 | code: pairs.map(valueOf).join(''), 35 | elapsed: timeOf(pairs[pairs.length - 1]) - timeOf(pairs[0]) 36 | }) 37 | 38 | const slidingWindow = (size: number): ((s: Stream) => Stream) => 39 | compose(skip(1), scan((events, event) => 40 | append(event, events).slice(-size), [])) 41 | 42 | const withTime = (s: Stream): Stream> => 43 | snapshot(time, s) 44 | 45 | const render = ({ code, elapsed, match }: MatchResult): string => 46 | `${code} ${(elapsed / 1000).toFixed(2)} secs ${match ? 'MATCHED' : ''}` 47 | 48 | const results = 49 | compose(tap((result: MatchResult): void => { value.innerText = render(result) }), 50 | compose(map(verify(isMatch)), 51 | compose(map(codeAndTime), 52 | compose(slidingWindow(6), 53 | withTime)))) 54 | 55 | runEffects(results(merge(aClicks, bClicks)), newDefaultScheduler()) 56 | -------------------------------------------------------------------------------- /examples/simple-clock/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Clock 9 | 10 | 11 |

Seconds since start:

12 |
13 |

14 | Update every: 15 | 16 | 17 | 18 |

19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /examples/simple-clock/index.ts: -------------------------------------------------------------------------------- 1 | import { Stream, Time } from '@most/types' 2 | import { time, sample } from '../../src' 3 | import { periodic, mergeArray, constant, switchLatest, startWith, tap, runEffects } from '@most/core' 4 | import { newDefaultScheduler } from '@most/scheduler' 5 | import { click } from '@most/dom-event' 6 | 7 | // DOM Event helpers 8 | const fail = (s: string): never => { throw new Error(s) } 9 | const qs = (s: string): HTMLElement => document.querySelector(s) || fail(`${s} not found`) 10 | 11 | // Each button click is a higher-order event stream carrying a periodic 12 | // event stream representing a sample rate value. The event values are 13 | // all void because they don't matter. What matters is the *rate* at 14 | // which they occur, as that will be used to sample the current elapsed time. 15 | const click10ms: Stream> = constant(periodic(10), click(qs('[name="10ms"]'))) 16 | const click100ms: Stream> = constant(periodic(100), click(qs('[name="100ms"]'))) 17 | const click1s: Stream> = constant(periodic(1000), click(qs('[name="1s"]'))) 18 | 19 | const clicks: Stream> = mergeArray([click10ms, click100ms, click1s]) 20 | 21 | // Each time a button is clicked, switch to its associated sampling rate. 22 | // Start the app with one second sampling rate, i.e., before any buttons 23 | // have been clicked. 24 | const sampler: Stream = switchLatest(startWith(periodic(1000), clicks)) 25 | 26 | // Get the elapsed time by sampling time() at the associated rate each 27 | // time a button is clicked. 28 | const elapsed: Stream
= (s: Stream) => Stream<[A, B]> 6 | 7 | export const snapshot = (b: Behavior, s: Stream): Stream<[A, B]> => b(s) 8 | 9 | export const sample = (b: Behavior, s: Stream): Stream => mapS((ab: [A, B]) => ab[0], snapshot(b, s)) 10 | 11 | export { snapshotTime as time } 12 | 13 | export const always = (a: A): Behavior => (sb: Stream): Stream<[A, B]> => mapS((b: B) => [a, b], sb) 14 | 15 | export const fromTime = (f: (t: Time) => A): Behavior => map(f, snapshotTime) 16 | 17 | export const step = (a: A, sa: Stream): Behavior => (sb: Stream): Stream<[A, B]> => 18 | snapshotStream((a: A, b: B) => [a, b], startWith(a, sa), sb) 19 | 20 | export const map = (f: (a: A) => B, ba: Behavior): Behavior => (sc: Stream): Stream<[B, C]> => 21 | mapS(([a, c]: [A, C]) => [f(a), c], snapshot(ba, sc)) 22 | 23 | export const apply = (bf: Behavior<(a: A) => B>, ba: Behavior): Behavior => liftA2((f, a) => f(a), bf, ba) 24 | 25 | export const liftA2 = (f: (a: A, b: B) => C, ba: Behavior, bb: Behavior): Behavior => ( 26 | sd: Stream 27 | ): Stream<[C, D]> => mapS(([a, [b, d]]: [A, [B, D]]) => [f(a, b), d], snapshot(ba, snapshot(bb, sd))) 28 | -------------------------------------------------------------------------------- /src/snapshotTime.ts: -------------------------------------------------------------------------------- 1 | import { Disposable, Scheduler, Sink, Stream, Time } from '@most/types' 2 | import { Behavior } from './index' 3 | 4 | export const snapshotTime: Behavior(stream: Stream): Stream<[Time, A]> => new SnapshotTime(stream) 5 | 6 | class SnapshotTime { 7 | source: Stream 8 | 9 | constructor(source: Stream) { 10 | this.source = source 11 | } 12 | 13 | run(sink: Sink<[Time, A]>, scheduler: Scheduler): Disposable { 14 | return this.source.run(new SnapshotTimeSink(sink), scheduler) 15 | } 16 | } 17 | 18 | class SnapshotTimeSink { 19 | sink: Sink<[Time, A]> 20 | 21 | constructor(sink: Sink<[Time, A]>) { 22 | this.sink = sink 23 | } 24 | 25 | event(t: Time, a: A): void { 26 | this.sink.event(t, [t, a]) 27 | } 28 | 29 | error(t: Time, e: Error): void { 30 | this.sink.error(t, e) 31 | } 32 | 33 | end(t: Time): void { 34 | this.sink.end(t) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ 4 | "module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ 5 | "declaration": true, /* Generates corresponding '.d.ts' file. */ 6 | "strict": true, /* Enable all strict type-checking options. */ 7 | "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 8 | "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 9 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ 10 | }, 11 | "include": [ 12 | "src/**/*.ts" 13 | ] 14 | } --------------------------------------------------------------------------------