Source: lib/util/operation_manager.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.OperationManager');
  7. goog.require('shaka.util.ArrayUtils');
  8. goog.require('shaka.util.IDestroyable');
  9. /**
  10. * A utility for cleaning up AbortableOperations, to help simplify common
  11. * patterns and reduce code duplication.
  12. *
  13. * @implements {shaka.util.IDestroyable}
  14. */
  15. shaka.util.OperationManager = class {
  16. /** */
  17. constructor() {
  18. /** @private {!Array<!shaka.extern.IAbortableOperation>} */
  19. this.operations_ = [];
  20. }
  21. /**
  22. * Manage an operation. This means aborting it on destroy() and removing it
  23. * from the management set when it complete.
  24. *
  25. * @param {!shaka.extern.IAbortableOperation} operation
  26. */
  27. manage(operation) {
  28. this.operations_.push(operation.finally(() => {
  29. shaka.util.ArrayUtils.remove(this.operations_, operation);
  30. }));
  31. }
  32. /** @override */
  33. destroy() {
  34. const cleanup = [];
  35. for (const op of this.operations_) {
  36. // Catch and ignore any failures. This silences error logs in the
  37. // JavaScript console about uncaught Promise failures.
  38. op.promise.catch(() => {});
  39. // Now abort the operation.
  40. cleanup.push(op.abort());
  41. }
  42. this.operations_ = [];
  43. return Promise.all(cleanup);
  44. }
  45. };