Files
shaka-player/lib/util/destroyer.js
T
Joey Parrish 4dc2c65578 Add type assertions for Errors
In many places, we check error codes on shaka.util.Error.  But the
compiler doesn't know that what is caught in "catch" is that type, so
we add type assertions.

In some cases, we know that other types may also be thrown, so there
are also some runtime checks.  Some of these had to be refactored to
allow the compiler to correctly infer types.

Change-Id: I053bd7e96213c689aae3889315052dd402124690
2020-04-29 10:11:21 -07:00

93 lines
2.2 KiB
JavaScript

/** @license
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.Destroyer');
goog.require('shaka.util.PublicPromise');
/**
* @summary
* A utility class to help work with |shaka.util.IDestroyable| objects.
*
* @final
*/
shaka.util.Destroyer = class {
/**
* @param {function():!Promise} callback
* A callback to destroy an object. This callback will only be called once
* regardless of how many times |destroy| is called.
*/
constructor(callback) {
/** @private {boolean} */
this.destroyed_ = false;
/** @private {!shaka.util.PublicPromise} */
this.waitOnDestroy_ = new shaka.util.PublicPromise();
/** @private {function():!Promise} */
this.onDestroy_ = callback;
}
/**
* Check if |destroy| has been called. This returning |true| does not mean
* that the promise returned by |destroy| has resolved yet.
*
* @return {boolean}
* @final
*/
destroyed() {
return this.destroyed_;
}
/**
* Request that the destroy callback be called. Will return a promise that
* will resolve once the callback terminates. The promise will never be
* rejected.
*
* @return {!Promise}
* @final
*/
destroy() {
if (this.destroyed_) {
return this.waitOnDestroy_;
}
// We have started destroying this object, so we should never get here
// again.
this.destroyed_ = true;
return this.onDestroy_().then(
() => { this.waitOnDestroy_.resolve(); },
() => { this.waitOnDestroy_.resolve(); });
}
/**
* Checks if the object is destroyed and throws an error if it is.
* @param {*=} error The inner error, if any.
*/
ensureNotDestroyed(error) {
if (this.destroyed_) {
if (error instanceof shaka.util.Error &&
error.code == shaka.util.Error.Code.OBJECT_DESTROYED) {
throw error;
}
throw shaka.util.Destroyer.destroyedError(error);
}
}
/**
* @param {*=} error The inner error, if any.
* @return {!shaka.util.Error}
*/
static destroyedError(error) {
return new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.PLAYER,
shaka.util.Error.Code.OBJECT_DESTROYED,
error);
}
};