worker: implements nits in Web Locks code · nodejs/node@84c3513

@@ -3,7 +3,9 @@

33

const {

44

ObjectDefineProperties,

55

Promise,

6+

PromisePrototypeThen,

67

PromiseResolve,

8+

SafePromisePrototypeFinally,

79

Symbol,

810

SymbolToStringTag,

911

} = primordials;

@@ -15,6 +17,7 @@ const {

1517

const {

1618

kEmptyObject,

1719

lazyDOMException,

20+

kEnumerableProperty,

1821

} = require('internal/util');

1922

const {

2023

validateAbortSignal,

@@ -86,8 +89,8 @@ class Lock {

8689

}

87908891

ObjectDefineProperties(Lock.prototype, {

89-

name: { __proto__: null, enumerable: true },

90-

mode: { __proto__: null, enumerable: true },

92+

name: kEnumerableProperty,

93+

mode: kEnumerableProperty,

9194

[SymbolToStringTag]: {

9295

__proto__: null,

9396

value: 'Lock',

@@ -126,13 +129,13 @@ class LockManager {

126129

* @param {boolean} [options.ifAvailable] - Only grant if immediately available

127130

* @param {boolean} [options.steal] - Steal existing locks with same name

128131

* @param {AbortSignal} [options.signal] - Signal to abort pending lock request

129-

* @param {Function} callback - Function called when lock is granted

132+

* @param {Function} [callback] - Function called when lock is granted

130133

* @returns {Promise} Promise that resolves when the lock is released

131134

* @throws {TypeError} When name is not a string or callback is not a function

132135

* @throws {DOMException} When validation fails or operation is not supported

133136

*/

134137

// https://w3c.github.io/web-locks/#api-lock-manager-request

135-

async request(name, options, callback) {

138+

async request(name, options, callback = undefined) {

136139

if (callback === undefined) {

137140

callback = options;

138141

options = undefined;

@@ -204,7 +207,7 @@ class LockManager {

204207

signal.addEventListener('abort', abortListener, { once: true });

205208206209

const wrappedCallback = (lock) => {

207-

return PromiseResolve().then(() => {

210+

return PromisePrototypeThen(PromiseResolve(), () => {

208211

if (signal.aborted) {

209212

return undefined;

210213

}

@@ -224,11 +227,10 @@ class LockManager {

224227

);

225228226229

// When released promise settles, clean up listener and resolve main promise

227-

released

228-

.then(resolve, (error) => reject(convertLockError(error)))

229-

.finally(() => {

230-

signal.removeEventListener('abort', abortListener);

231-

});

230+

SafePromisePrototypeFinally(

231+

PromisePrototypeThen(released, resolve, (error) => reject(convertLockError(error))),

232+

() => signal.removeEventListener('abort', abortListener),

233+

);

232234

} catch (error) {

233235

signal.removeEventListener('abort', abortListener);

234236

reject(convertLockError(error));

@@ -265,8 +267,8 @@ class LockManager {

265267

}

266268267269

ObjectDefineProperties(LockManager.prototype, {

268-

request: { __proto__: null, enumerable: true },

269-

query: { __proto__: null, enumerable: true },

270+

request: kEnumerableProperty,

271+

query: kEnumerableProperty,

270272

[SymbolToStringTag]: {

271273

__proto__: null,

272274

value: 'LockManager',

@@ -276,16 +278,6 @@ ObjectDefineProperties(LockManager.prototype, {

276278

},

277279

});

278280279-

ObjectDefineProperties(LockManager.prototype.request, {

280-

length: {

281-

__proto__: null,

282-

value: 2,

283-

writable: false,

284-

enumerable: false,

285-

configurable: true,

286-

},

287-

});

288-289281

module.exports = {

290282

Lock,

291283

LockManager,