[redlock] change Lock to class declaration, simplify LockError definition (#18770)

* add constructors and tests for classes exposed in redlock

* [redlock] simplify Lock and LockError definitions
This commit is contained in:
Dimitri Benin
2017-08-09 10:56:58 -07:00
committed by Mohamed Hegazy
parent 135ed17a23
commit afa59841f7
4 changed files with 24 additions and 26 deletions
+6 -11
View File
@@ -13,14 +13,13 @@ export = Redlock;
declare namespace Redlock {
type Callback<T> = (err: any, value?: T) => void;
interface Lock {
class Lock {
redlock: Redlock;
resource: string;
value: string | null;
expiration: number;
constructor(redlock: Redlock, resource: string, value: string | null, expiration: number);
unlock(callback?: Callback<void>): Promise<void>;
extend(ttl: number, callback?: Callback<Lock>): Promise<Lock>;
}
@@ -34,14 +33,9 @@ declare namespace Redlock {
extendScript?(origExtendScript: string): string;
}
interface LockErrorConstructor {
new(message?: string): LockError;
(message?: string): LockError;
readonly prototype: LockError;
}
interface LockError extends Error {
class LockError extends Error {
readonly name: 'LockError';
constructor(message?: string);
}
interface CompatibleRedisClient {
@@ -51,7 +45,8 @@ declare namespace Redlock {
}
declare class Redlock extends EventEmitter {
LockError: Redlock.LockErrorConstructor;
LockError: typeof Redlock.LockError;
Lock: typeof Redlock.Lock;
driftFactor: number;
retryCount: number;
retryDelay: number;
+2
View File
@@ -91,6 +91,8 @@ redlock.lock('locks:account:322456', 1000, (err, lock) => {
}
});
new Object() instanceof redlock.Lock;
new Error() instanceof redlock.LockError;
redlock.LockError.prototype;
+7 -12
View File
@@ -12,14 +12,13 @@ export = Redlock;
declare namespace Redlock {
type Callback<T> = (err: any, value?: T) => void;
interface Lock {
class Lock {
redlock: Redlock;
resource: string;
value: any;
expiration: number;
constructor(redlock: Redlock, resource: string, value: any, expiration: number);
unlock(callback?: Callback<void>): Promise<void>;
extend(ttl: number, callback?: Callback<Lock>): Promise<Lock>;
}
@@ -29,19 +28,15 @@ declare namespace Redlock {
retryDelay?: number;
}
interface LockErrorConstructor {
new(message?: string): LockError;
(message?: string): LockError;
readonly prototype: LockError;
}
interface LockError extends Error {
class LockError extends Error {
readonly name: 'LockError';
constructor(message?: string);
}
}
declare class Redlock {
LockError: Redlock.LockErrorConstructor;
LockError: typeof Redlock.LockError;
Lock: typeof Redlock.Lock;
driftFactor: number;
retryCount: number;
@@ -54,7 +49,7 @@ declare class Redlock {
acquire(resource: string, ttl: number, callback?: Redlock.Callback<Redlock.Lock>): Promise<Redlock.Lock>;
lock(resource: string, ttl: number, callback?: Redlock.Callback<Redlock.Lock>): Promise<Redlock.Lock>;
disposer(resource: string, ttl: number, errorHandler?: Redlock.Callback<void>): any; // bluebird Disposer
disposer(resource: string, ttl: number, errorHandler?: Redlock.Callback<void>): Promise.Disposer<Redlock.Lock>; // bluebird Disposer
release(lock: Redlock.Lock, callback?: Redlock.Callback<void>): Promise<void>;
unlock(lock: Redlock.Lock, callback?: Redlock.Callback<void>): Promise<void>;
+9 -3
View File
@@ -1,6 +1,7 @@
import * as Redlock from 'redlock';
import { Lock } from 'redlock';
import { RedisClient } from 'redis';
import { using } from 'bluebird';
let redlock: Redlock;
let client: RedisClient = <RedisClient> {};
@@ -18,9 +19,7 @@ redlock.acquire('resource', 30, (err: any, lock: Lock) => {});
redlock.lock('resource', 30).then((lock: Lock) => {});
redlock.lock('resource', 30, (err: any, lock: Lock) => {});
// There is currently no way to test the disposer as the bluebird typings does not
// expose the .using method.
// promise.using(redlock.disposer('resource', 30), (lock: Lock) => {});
using(redlock.disposer('resource', 30), (lock: Lock) => Promise.resolve());
redlock.release(lock);
redlock.release(lock, (err: any) => {});
@@ -35,3 +34,10 @@ lock.unlock((err) => {});
lock.extend(30).then((lock: Lock) => {});
lock.extend(30, (err: any, lock: Lock) => {});
new Object() instanceof redlock.Lock;
new Error() instanceof redlock.LockError;
redlock.LockError.prototype;
const lockError: Redlock.LockError = new redlock.LockError();