mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-16 16:16:40 +03:00
4425dca283
The state engine mechanism, designed for the player class, was over-engineered. The structure of the class makes debugging player errors unnecessarily annoying, by obfuscating the code-path the error followed, and in general has created a significant amount of technical debt. This changes the player to use an async-await setup for the top-level operations, laying things out much more cleanly and linearly. --------- Co-authored-by: Álvaro Velad Galván <ladvan91@hotmail.com>
60 lines
1.2 KiB
JavaScript
60 lines
1.2 KiB
JavaScript
/*! @license
|
|
* Shaka Player
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
goog.provide('shaka.util.Mutex');
|
|
|
|
goog.require('shaka.log');
|
|
|
|
|
|
/**
|
|
* @summary A simple mutex.
|
|
*/
|
|
shaka.util.Mutex = class {
|
|
/** Constructs the mutex. */
|
|
constructor() {
|
|
/** @private {?string} */
|
|
this.acquiredIdentifier = null;
|
|
|
|
/** @private {!Array.<function()>} */
|
|
this.unlockQueue = [];
|
|
}
|
|
|
|
/**
|
|
* Acquires the mutex, as soon as possible.
|
|
* @param {string} identifier
|
|
* @return {!Promise}
|
|
*/
|
|
async acquire(identifier) {
|
|
shaka.log.v2(identifier + ' has requested mutex');
|
|
if (this.acquiredIdentifier) {
|
|
await new Promise((resolve) => this.unlockQueue.push(resolve));
|
|
}
|
|
this.acquiredIdentifier = identifier;
|
|
shaka.log.v2(identifier + ' has acquired mutex');
|
|
}
|
|
|
|
/**
|
|
* Releases your hold on the mutex.
|
|
*/
|
|
release() {
|
|
shaka.log.v2(this.acquiredIdentifier + ' has released mutex');
|
|
if (this.unlockQueue.length > 0) {
|
|
this.unlockQueue.shift()();
|
|
} else {
|
|
this.acquiredIdentifier = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Completely releases the mutex. Meant for use by the tests.
|
|
*/
|
|
releaseAll() {
|
|
while (this.acquiredIdentifier) {
|
|
this.release();
|
|
}
|
|
}
|
|
};
|