|
3 | 3 | const { |
4 | 4 | ObjectDefineProperties, |
5 | 5 | Promise, |
| 6 | + PromisePrototypeThen, |
6 | 7 | PromiseResolve, |
| 8 | + SafePromisePrototypeFinally, |
7 | 9 | Symbol, |
8 | 10 | SymbolToStringTag, |
9 | 11 | } = primordials; |
@@ -15,6 +17,7 @@ const { |
15 | 17 | const { |
16 | 18 | kEmptyObject, |
17 | 19 | lazyDOMException, |
| 20 | + kEnumerableProperty, |
18 | 21 | } = require('internal/util'); |
19 | 22 | const { |
20 | 23 | validateAbortSignal, |
@@ -86,8 +89,8 @@ class Lock { |
86 | 89 | } |
87 | 90 |
|
88 | 91 | ObjectDefineProperties(Lock.prototype, { |
89 | | - name: { __proto__: null, enumerable: true }, |
90 | | - mode: { __proto__: null, enumerable: true }, |
| 92 | + name: kEnumerableProperty, |
| 93 | + mode: kEnumerableProperty, |
91 | 94 | [SymbolToStringTag]: { |
92 | 95 | __proto__: null, |
93 | 96 | value: 'Lock', |
@@ -126,13 +129,13 @@ class LockManager { |
126 | 129 | * @param {boolean} [options.ifAvailable] - Only grant if immediately available |
127 | 130 | * @param {boolean} [options.steal] - Steal existing locks with same name |
128 | 131 | * @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 |
130 | 133 | * @returns {Promise} Promise that resolves when the lock is released |
131 | 134 | * @throws {TypeError} When name is not a string or callback is not a function |
132 | 135 | * @throws {DOMException} When validation fails or operation is not supported |
133 | 136 | */ |
134 | 137 | // https://w3c.github.io/web-locks/#api-lock-manager-request |
135 | | - async request(name, options, callback) { |
| 138 | + async request(name, options, callback = undefined) { |
136 | 139 | if (callback === undefined) { |
137 | 140 | callback = options; |
138 | 141 | options = undefined; |
@@ -204,7 +207,7 @@ class LockManager { |
204 | 207 | signal.addEventListener('abort', abortListener, { once: true }); |
205 | 208 |
|
206 | 209 | const wrappedCallback = (lock) => { |
207 | | - return PromiseResolve().then(() => { |
| 210 | + return PromisePrototypeThen(PromiseResolve(), () => { |
208 | 211 | if (signal.aborted) { |
209 | 212 | return undefined; |
210 | 213 | } |
@@ -224,11 +227,10 @@ class LockManager { |
224 | 227 | ); |
225 | 228 |
|
226 | 229 | // 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 | + ); |
232 | 234 | } catch (error) { |
233 | 235 | signal.removeEventListener('abort', abortListener); |
234 | 236 | reject(convertLockError(error)); |
@@ -265,8 +267,8 @@ class LockManager { |
265 | 267 | } |
266 | 268 |
|
267 | 269 | ObjectDefineProperties(LockManager.prototype, { |
268 | | - request: { __proto__: null, enumerable: true }, |
269 | | - query: { __proto__: null, enumerable: true }, |
| 270 | + request: kEnumerableProperty, |
| 271 | + query: kEnumerableProperty, |
270 | 272 | [SymbolToStringTag]: { |
271 | 273 | __proto__: null, |
272 | 274 | value: 'LockManager', |
@@ -276,16 +278,6 @@ ObjectDefineProperties(LockManager.prototype, { |
276 | 278 | }, |
277 | 279 | }); |
278 | 280 |
|
279 | | -ObjectDefineProperties(LockManager.prototype.request, { |
280 | | - length: { |
281 | | - __proto__: null, |
282 | | - value: 2, |
283 | | - writable: false, |
284 | | - enumerable: false, |
285 | | - configurable: true, |
286 | | - }, |
287 | | -}); |
288 | | - |
289 | 281 | module.exports = { |
290 | 282 | Lock, |
291 | 283 | LockManager, |
|