playwright-performance-reporter
Version:
Measure and publish performance metrics from browser dev-tools when running playwright
70 lines (69 loc) • 1.66 kB
JavaScript
import { Stream, } from 'node:stream';
/**
* Events for lock status
*/
var LockEvents;
(function (LockEvents) {
LockEvents[LockEvents["lock"] = 0] = "lock";
LockEvents[LockEvents["unlock"] = 1] = "unlock";
})(LockEvents || (LockEvents = {}));
/**
* Exclusive Lock for a Resource
*/
export class Lock {
/**
* Stream of lock status
*/
lockStatus = new Stream().setMaxListeners(0);
/**
* Track status of lock
*/
_isLocked = false;
/**
* Requests a lock
*
* @returns callback for unlock or false if lock couldn't be performed
*/
lock() {
if (this._isLocked) {
return false;
}
this._isLocked = true;
this.lockStatus.emit('event', LockEvents.lock);
return () => {
this.unlock();
};
}
/**
* Get current lock status
*
* @returns lock status
*/
isLocked() {
return this._isLocked;
}
/**
* Notify caller about unlock status.
* Does not guarantee that lock can be performed.
*
* @returns Promise to notify about unlock event
*/
async notifyOnUnlock() {
return new Promise(resolve => {
const callback = (status) => {
if (status === LockEvents.unlock) {
this.lockStatus.removeListener('event', callback);
resolve();
}
};
this.lockStatus.on('event', callback);
});
}
/**
* Emit unlock event
*/
unlock() {
this._isLocked = false;
this.lockStatus.emit('event', LockEvents.unlock);
}
}