Comparing version 0.1.1 to 0.2.0
@@ -56,6 +56,6 @@ export interface ConstructorOptions { | ||
protected callServer<T = ServerSentEventData>(data: ClientSentEventData, { callTimeoutInMs }?: CallServerOptions): Promise<T>; | ||
lock(name: string, { unlockWebhookUrl, waitTimeoutInMs, lockExpirationInSeconds }?: LockActionOptions): Promise<boolean>; | ||
unlock(name: string, { waitTimeoutInMs }?: UnlockActionOptions): Promise<boolean>; | ||
check(name: string, { waitTimeoutInMs }?: CheckActionOptions): Promise<boolean>; | ||
lock(nameOrNames: string | string[], { unlockWebhookUrl, waitTimeoutInMs, lockExpirationInSeconds }?: LockActionOptions): Promise<boolean>; | ||
unlock(nameOrNames: string | string[], { waitTimeoutInMs }?: UnlockActionOptions): Promise<boolean>; | ||
check(nameOrNames: string | string[], { waitTimeoutInMs }?: CheckActionOptions): Promise<boolean>; | ||
} | ||
export {}; |
@@ -46,23 +46,58 @@ const defaultServerUrl = 'https://api.lockdb.com'; | ||
} | ||
async lock(name, { unlockWebhookUrl, waitTimeoutInMs = 30000, lockExpirationInSeconds = 300 } = {}) { | ||
const data = { | ||
eventName: 'lock', | ||
lockName: name, | ||
waitForLockInMs: waitTimeoutInMs, | ||
unlockWebhookUrl, | ||
lockExpirationInSeconds, | ||
}; | ||
await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
async lock(nameOrNames, { unlockWebhookUrl, waitTimeoutInMs = 30000, lockExpirationInSeconds = 300 } = {}) { | ||
const lockNames = []; | ||
if (!Array.isArray(nameOrNames)) { | ||
lockNames.push(nameOrNames); | ||
} | ||
else { | ||
nameOrNames.forEach((name) => lockNames.push(name)); | ||
} | ||
for (const name of lockNames) { | ||
const data = { | ||
eventName: 'lock', | ||
lockName: name, | ||
waitForLockInMs: waitTimeoutInMs, | ||
unlockWebhookUrl, | ||
lockExpirationInSeconds, | ||
}; | ||
await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
} | ||
return true; | ||
} | ||
async unlock(name, { waitTimeoutInMs = 5000 } = {}) { | ||
const data = { eventName: 'unlock', lockName: name }; | ||
const result = await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
return Boolean(result.wasLocked); | ||
async unlock(nameOrNames, { waitTimeoutInMs = 5000 } = {}) { | ||
const lockNames = []; | ||
if (!Array.isArray(nameOrNames)) { | ||
lockNames.push(nameOrNames); | ||
} | ||
else { | ||
nameOrNames.forEach((name) => lockNames.push(name)); | ||
} | ||
let wasAnyLocked = false; | ||
for (const name of lockNames) { | ||
const data = { eventName: 'unlock', lockName: name }; | ||
const result = await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
if (result.wasLocked) { | ||
wasAnyLocked = true; | ||
} | ||
} | ||
return wasAnyLocked; | ||
} | ||
async check(name, { waitTimeoutInMs = 5000 } = {}) { | ||
const data = { eventName: 'check', lockName: name }; | ||
const result = await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
return Boolean(result.isLocked); | ||
async check(nameOrNames, { waitTimeoutInMs = 5000 } = {}) { | ||
const lockNames = []; | ||
if (!Array.isArray(nameOrNames)) { | ||
lockNames.push(nameOrNames); | ||
} | ||
else { | ||
nameOrNames.forEach((name) => lockNames.push(name)); | ||
} | ||
let isAnyLocked = false; | ||
for (const name of lockNames) { | ||
const data = { eventName: 'check', lockName: name }; | ||
const result = await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
if (result.isLocked) { | ||
isAnyLocked = true; | ||
} | ||
} | ||
return isAnyLocked; | ||
} | ||
} |
{ | ||
"module": "./esm/mod.js", | ||
"main": "./script/mod.js", | ||
"name": "lockdb", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "LockDB is a cross-platform tool you can use to handle process/event locking and avoid race conditions. It is sometimes also known as a semaphore.", | ||
"license": "AGPL-3.0", | ||
"author": "Bruno Bernardino <me@brunobernardino.com>", | ||
"keywords": [ | ||
@@ -19,2 +15,3 @@ "lockdb", | ||
], | ||
"author": "Bruno Bernardino <me@brunobernardino.com>", | ||
"homepage": "https://lockdb.com", | ||
@@ -25,8 +22,8 @@ "repository": { | ||
}, | ||
"license": "AGPL-3.0", | ||
"bugs": { | ||
"url": "https://github.com/BrunoBernardino/lockdb/issues" | ||
}, | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"main": "./script/mod.js", | ||
"module": "./esm/mod.js", | ||
"exports": { | ||
@@ -38,5 +35,9 @@ ".": { | ||
}, | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.11.9" | ||
} | ||
"@types/node": "^20.9.0" | ||
}, | ||
"_generatedBy": "dnt@0.39.0" | ||
} |
@@ -7,5 +7,5 @@ # LockDB | ||
There are packages for [Node.js/Browser/Bun/NPM](https://npmjs.org/package/lockdb), [Deno](https://deno.land/x/lockdb), and you can also use it as a [CLI](#cli). | ||
There are packages for [Node.js/Browser/Bun/NPM](https://npmjs.org/package/lockdb), [Deno](https://deno.land/x/lockdb), and you can also use it as a [CLI](https://lockdb.com/docs/cli#install). | ||
It has no dependencies, and is very simple to use, with effectively 3 commands/actions/methods: `lock('name')`, `unlock('name')`, and `check('name')`. | ||
It has no dependencies, and is very simple to use, with effectively 3 commands/actions/methods: `lock('name')`, `unlock('name')`, and `check('name')`. It also supports locking/unlocking/checking multiple locks at once. | ||
@@ -28,3 +28,3 @@ You can get your `apiKey` at [lockdb.com](https://lockdb.com) or connect `LockDB` to your own server/API. | ||
// import LockDB from 'lockdb'; | ||
// import LockDB from 'https://deno.land/x/lockdb@0.1.0/mod.ts'; | ||
// import LockDB from 'https://deno.land/x/lockdb@0.2.0/mod.ts'; | ||
const LockDB = require('lockdb'); | ||
@@ -82,2 +82,6 @@ | ||
# Outputs `true` | ||
# Check on multiple locks | ||
lockdb check sales,report,cleanup | ||
# Outputs `false` | ||
``` | ||
@@ -87,3 +91,3 @@ | ||
Requires [`deno@1.36.0`](https://deno.land) (other versions will probably work). | ||
Requires [`deno@1.37.0`](https://deno.land) (other versions will probably work). | ||
@@ -90,0 +94,0 @@ ```bash |
@@ -56,6 +56,6 @@ export interface ConstructorOptions { | ||
protected callServer<T = ServerSentEventData>(data: ClientSentEventData, { callTimeoutInMs }?: CallServerOptions): Promise<T>; | ||
lock(name: string, { unlockWebhookUrl, waitTimeoutInMs, lockExpirationInSeconds }?: LockActionOptions): Promise<boolean>; | ||
unlock(name: string, { waitTimeoutInMs }?: UnlockActionOptions): Promise<boolean>; | ||
check(name: string, { waitTimeoutInMs }?: CheckActionOptions): Promise<boolean>; | ||
lock(nameOrNames: string | string[], { unlockWebhookUrl, waitTimeoutInMs, lockExpirationInSeconds }?: LockActionOptions): Promise<boolean>; | ||
unlock(nameOrNames: string | string[], { waitTimeoutInMs }?: UnlockActionOptions): Promise<boolean>; | ||
check(nameOrNames: string | string[], { waitTimeoutInMs }?: CheckActionOptions): Promise<boolean>; | ||
} | ||
export {}; |
@@ -48,24 +48,59 @@ "use strict"; | ||
} | ||
async lock(name, { unlockWebhookUrl, waitTimeoutInMs = 30000, lockExpirationInSeconds = 300 } = {}) { | ||
const data = { | ||
eventName: 'lock', | ||
lockName: name, | ||
waitForLockInMs: waitTimeoutInMs, | ||
unlockWebhookUrl, | ||
lockExpirationInSeconds, | ||
}; | ||
await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
async lock(nameOrNames, { unlockWebhookUrl, waitTimeoutInMs = 30000, lockExpirationInSeconds = 300 } = {}) { | ||
const lockNames = []; | ||
if (!Array.isArray(nameOrNames)) { | ||
lockNames.push(nameOrNames); | ||
} | ||
else { | ||
nameOrNames.forEach((name) => lockNames.push(name)); | ||
} | ||
for (const name of lockNames) { | ||
const data = { | ||
eventName: 'lock', | ||
lockName: name, | ||
waitForLockInMs: waitTimeoutInMs, | ||
unlockWebhookUrl, | ||
lockExpirationInSeconds, | ||
}; | ||
await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
} | ||
return true; | ||
} | ||
async unlock(name, { waitTimeoutInMs = 5000 } = {}) { | ||
const data = { eventName: 'unlock', lockName: name }; | ||
const result = await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
return Boolean(result.wasLocked); | ||
async unlock(nameOrNames, { waitTimeoutInMs = 5000 } = {}) { | ||
const lockNames = []; | ||
if (!Array.isArray(nameOrNames)) { | ||
lockNames.push(nameOrNames); | ||
} | ||
else { | ||
nameOrNames.forEach((name) => lockNames.push(name)); | ||
} | ||
let wasAnyLocked = false; | ||
for (const name of lockNames) { | ||
const data = { eventName: 'unlock', lockName: name }; | ||
const result = await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
if (result.wasLocked) { | ||
wasAnyLocked = true; | ||
} | ||
} | ||
return wasAnyLocked; | ||
} | ||
async check(name, { waitTimeoutInMs = 5000 } = {}) { | ||
const data = { eventName: 'check', lockName: name }; | ||
const result = await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
return Boolean(result.isLocked); | ||
async check(nameOrNames, { waitTimeoutInMs = 5000 } = {}) { | ||
const lockNames = []; | ||
if (!Array.isArray(nameOrNames)) { | ||
lockNames.push(nameOrNames); | ||
} | ||
else { | ||
nameOrNames.forEach((name) => lockNames.push(name)); | ||
} | ||
let isAnyLocked = false; | ||
for (const name of lockNames) { | ||
const data = { eventName: 'check', lockName: name }; | ||
const result = await this.callServer(data, { callTimeoutInMs: waitTimeoutInMs }); | ||
if (result.isLocked) { | ||
isAnyLocked = true; | ||
} | ||
} | ||
return isAnyLocked; | ||
} | ||
} | ||
exports.default = LockDB; |
50583
327
104