mongo-locks 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,7 +15,7 @@ const lockManager = new LockManager(client.db().collection("mongo-locks"));
15
15
  async function doStuff() {
16
16
  await using lock = lockManager.lock("unique string key");
17
17
 
18
- if (!lock.locked) {
18
+ if (!lock) {
19
19
  console.log("Lock already taken");
20
20
  return;
21
21
  }
@@ -34,7 +34,7 @@ If you don't want to use the `using` syntax, you can do it this way:
34
34
  async function doStuff() {
35
35
  const lock = await lockManager.lock("unique string key");
36
36
 
37
- if (!lock.locked) {
37
+ if (!lock) {
38
38
  console.log("Lock already taken");
39
39
  return;
40
40
  }
package/dist/index.js CHANGED
@@ -22,11 +22,11 @@ const makeKey = (key) => {
22
22
  return Array.isArray(key) ? { _key_array: key } : key;
23
23
  };
24
24
  class Lock {
25
- constructor(_id, action, collection, locked) {
25
+ constructor(_id, action, collection) {
26
26
  this._id = _id;
27
27
  this.action = action;
28
28
  this.collection = collection;
29
- this.locked = locked;
29
+ this.locked = true;
30
30
  if (this.locked) {
31
31
  (() => __awaiter(this, void 0, void 0, function* () {
32
32
  while (this.locked) {
@@ -48,6 +48,7 @@ class Lock {
48
48
  if (!this.locked) {
49
49
  return false;
50
50
  }
51
+ this.locked = false;
51
52
  const result = yield this.collection.deleteOne({ _id: this._id });
52
53
  return result.deletedCount === 1;
53
54
  });
@@ -103,11 +104,11 @@ class LockManager {
103
104
  refreshedAt: new Date(),
104
105
  expiresAt: new Date(Date.now() + 60000),
105
106
  });
106
- return new Lock(id, lockId, this.collection, true);
107
+ return new Lock(id, lockId, this.collection);
107
108
  }
108
109
  catch (err) {
109
110
  if (err instanceof Error && "code" in err && err.code === 11000) {
110
- return new Lock(id, lockId, this.collection, false);
111
+ return null;
111
112
  }
112
113
  throw err;
113
114
  }
package/index.ts CHANGED
@@ -20,12 +20,7 @@ interface LockModel {
20
20
  }
21
21
 
22
22
  class Lock {
23
- constructor(
24
- public _id: ObjectId,
25
- public action: unknown,
26
- public collection: Collection<LockModel>,
27
- public locked: boolean
28
- ) {
23
+ constructor(public _id: ObjectId, public action: unknown, public collection: Collection<LockModel>) {
29
24
  if (this.locked) {
30
25
  (async () => {
31
26
  while (this.locked) {
@@ -38,6 +33,8 @@ class Lock {
38
33
  }
39
34
  }
40
35
 
36
+ locked = true;
37
+
41
38
  /**
42
39
  * Instead of calling `this.free` directly you can use the `using` syntax
43
40
  *
@@ -47,6 +44,7 @@ class Lock {
47
44
  if (!this.locked) {
48
45
  return false;
49
46
  }
47
+ this.locked = false;
50
48
 
51
49
  const result = await this.collection.deleteOne({ _id: this._id });
52
50
  return result.deletedCount === 1;
@@ -100,7 +98,7 @@ export class LockManager {
100
98
  *
101
99
  * @returns a function that when called will release the lock
102
100
  */
103
- async lock(key: unknown): Promise<Lock> {
101
+ async lock(key: unknown): Promise<Lock | null> {
104
102
  const lockId = makeKey(key);
105
103
  const id = new ObjectId();
106
104
 
@@ -113,10 +111,10 @@ export class LockManager {
113
111
  expiresAt: new Date(Date.now() + 60_000),
114
112
  });
115
113
 
116
- return new Lock(id, lockId, this.collection, true);
114
+ return new Lock(id, lockId, this.collection);
117
115
  } catch (err) {
118
116
  if (err instanceof Error && "code" in err && err.code === 11000) {
119
- return new Lock(id, lockId, this.collection, false);
117
+ return null;
120
118
  }
121
119
 
122
120
  throw err;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-locks",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
4
4
  "description": "Simple and bountiful locks to avoid doing the same operation multiple times",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {