Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/lock-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('assert').strict;
const { locks } = require('..');
const { sleep } = require('./test-utils');
Comment thread
anton-iskryzhytskyi marked this conversation as resolved.
Outdated

const TIME_TO_PROCESS = 2000;
const TIME_TO_PROCESS = 200;
const TIME_TO_LOCK = 100;

module.exports = async () => {
Expand Down
21 changes: 10 additions & 11 deletions web-locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ const UNLOCKED = 1;
let locks = null; // LockManager instance

class Lock {
constructor({ name, mode = 'exclusive', buffer = null, timeout = null }) {
constructor(name, mode = 'exclusive', buffer = null) {
this.name = name;
this.mode = mode; // 'exclusive' or 'shared'
this.queue = [];
this.owner = false;
this.trying = false;
this.timeout = timeout;
this.buffer = buffer ? buffer : new SharedArrayBuffer(4);
this.flag = new Int32Array(this.buffer, 0, 1);
if (!buffer) Atomics.store(this.flag, 0, UNLOCKED);
}

enter(handler) {
enter(handler, timeout) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That it much better, but we need to compare passing timeout and timer instance.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to use clearTimeout on abort?

Hmm, maybe it would be better to throw an error on timeout, similar to abort behavior?
Also, move all resolve / reject invocations to one place.

return new Promise(resolve => {
this.queue.push({ handler, resolve });
this.queue.push({ handler, resolve, timeout });
this.trying = true;
setTimeout(() => {
this.tryEnter();
Expand All @@ -39,15 +38,15 @@ class Lock {
if (prev === LOCKED) return;
this.owner = true;
this.trying = false;
const { handler, resolve } = this.queue.shift();
const { handler, resolve, timeout } = this.queue.shift();

const endWork = () => {
Comment thread
anton-iskryzhytskyi marked this conversation as resolved.
Outdated
this.leave();
resolve();
};

if (typeof this.timeout === 'number') {
setTimeout(endWork, this.timeout);
if (timeout) {
setTimeout(endWork, timeout);
}
handler(this).finally(endWork);
Comment thread
anton-iskryzhytskyi marked this conversation as resolved.
Outdated
}
Expand Down Expand Up @@ -100,14 +99,14 @@ class LockManager {

let lock = this.collection.get(name);
if (!lock) {
lock = new Lock({ name, mode, timeout });
lock = new Lock(name, mode);
this.collection.set(name, lock);
const { buffer } = lock;
const message = { webLocks: true, kind: 'create', name, mode, buffer };
locks.send(message);
}

const finished = lock.enter(handler);
const finished = lock.enter(handler, timeout);
let aborted = null;
if (signal) {
aborted = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -154,9 +153,9 @@ class LockManager {

receive(message) {
if (!message.webLocks) return;
const { kind, name, mode, buffer, timeout } = message;
const { kind, name, mode, buffer } = message;
if (kind === 'create') {
const lock = new Lock({ name, mode, buffer, timeout });
const lock = new Lock(name, mode, buffer);
this.collection.set(name, lock);
return;
}
Expand Down