observableCoroutine.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Observable } from "./observable.js";
  2. import { runCoroutineAsync, inlineScheduler } from "./coroutine.js";
  3. function CreateObservableScheduler(observable) {
  4. const coroutines = new Array();
  5. const onSteps = new Array();
  6. const onErrors = new Array();
  7. const observer = observable.add(() => {
  8. const count = coroutines.length;
  9. for (let i = 0; i < count; i++) {
  10. inlineScheduler(coroutines.shift(), onSteps.shift(), onErrors.shift());
  11. }
  12. });
  13. const scheduler = (coroutine, onStep, onError) => {
  14. coroutines.push(coroutine);
  15. onSteps.push(onStep);
  16. onErrors.push(onError);
  17. };
  18. return {
  19. scheduler: scheduler,
  20. dispose: () => {
  21. observable.remove(observer);
  22. },
  23. };
  24. }
  25. Observable.prototype.runCoroutineAsync = function (coroutine) {
  26. if (!this._coroutineScheduler) {
  27. const schedulerAndDispose = CreateObservableScheduler(this);
  28. this._coroutineScheduler = schedulerAndDispose.scheduler;
  29. this._coroutineSchedulerDispose = schedulerAndDispose.dispose;
  30. }
  31. return runCoroutineAsync(coroutine, this._coroutineScheduler);
  32. };
  33. Observable.prototype.cancelAllCoroutines = function () {
  34. if (this._coroutineSchedulerDispose) {
  35. this._coroutineSchedulerDispose();
  36. }
  37. this._coroutineScheduler = undefined;
  38. this._coroutineSchedulerDispose = undefined;
  39. };
  40. //# sourceMappingURL=observableCoroutine.js.map