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. constructor() {
  17. /** @private {!Array<!shaka.extern.IAbortableOperation>} */
  18. this.operations_ = [];
  19. }
  20. /**
  21. * Manage an operation. This means aborting it on destroy() and removing it
  22. * from the management set when it complete.
  23. *
  24. * @param {!shaka.extern.IAbortableOperation} operation
  25. */
  26. manage(operation) {
  27. this.operations_.push(operation.finally(() => {
  28. shaka.util.ArrayUtils.remove(this.operations_, operation);
  29. }));
  30. }
  31. /** @override */
  32. destroy() {
  33. const cleanup = [];
  34. for (const op of this.operations_) {
  35. // Catch and ignore any failures. This silences error logs in the
  36. // JavaScript console about uncaught Promise failures.
  37. op.promise.catch(() => {});
  38. // Now abort the operation.
  39. cleanup.push(op.abort());
  40. }
  41. this.operations_ = [];
  42. return Promise.all(cleanup);
  43. }
  44. };