From 670dc42a301db19244d80b75844eeb6059bb9dd0 Mon Sep 17 00:00:00 2001 From: Alexey Ponomarev Date: Tue, 18 Feb 2020 01:33:52 +0100 Subject: [PATCH] [locks] Fixed typo in timedLock function and added some missed methods (#42373) * Fixed typo in timedLock function and added some missed methods * Added some tests for create helpers and timedLock method * Fixed lint issues --- types/locks/index.d.ts | 11 ++++++++++- types/locks/locks-tests.ts | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/types/locks/index.d.ts b/types/locks/index.d.ts index 8c112d65d1..c994138db4 100644 --- a/types/locks/index.d.ts +++ b/types/locks/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for Locks 0.2 // Project: https://github.com/Wizcorp/locks // Definitions by: Joshua Graham +// Alexey Ponomarev // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -13,7 +14,7 @@ export class Mutex { /** Take the lock. */ lock(callback: () => void): void; /** Wait the specified number of milliseconds to take the lock. If error is set, the lock wasn't taken. */ - timedlock(ttl: number, callback: (error: Error) => void): void; + timedLock(ttl: number, callback: (error: Error) => void): void; /** Try taking the lock. If false, the lock wasn't taken. */ tryLock(): boolean; /** Release the current lock. */ @@ -67,3 +68,11 @@ export class CondVariable { /** Set the conditional variable value. */ set(value: any): void; } + +export function createCondVariable(initialValue: any): CondVariable; + +export function createSemaphore(initialValue: number): Semaphore; + +export function createMutex(): Mutex; + +export function createReadWriteLock(): ReadWriteLock; diff --git a/types/locks/locks-tests.ts b/types/locks/locks-tests.ts index 1e7d768a0c..94d66977e1 100644 --- a/types/locks/locks-tests.ts +++ b/types/locks/locks-tests.ts @@ -3,14 +3,17 @@ import locks = require("locks"); // Mutex const mutex: locks.Mutex = new locks.Mutex(); mutex.lock(() => {}); +mutex.timedLock(1000, () => {}); if (mutex.tryLock()) { console.log('Should not happen'); } mutex.unlock(); +const createMutex: locks.Mutex = locks.createMutex(); // Semaphore const semaphore: locks.Semaphore = new locks.Semaphore(1); semaphore.wait(() => {}); semaphore.wait(() => {}); semaphore.signal(); +const createSemaphore: locks.Semaphore = locks.createSemaphore(1); // Read Write Lock const readWriteLock: locks.ReadWriteLock = new locks.ReadWriteLock(); @@ -20,6 +23,7 @@ readWriteLock.unlock(); readWriteLock.writeLock(() => {}); if (readWriteLock.tryWriteLock()) { console.log('Should not happen'); } readWriteLock.unlock(); +const createReadWriteLock: locks.ReadWriteLock = locks.createReadWriteLock(); // Conditional Variable const condVariable: locks.CondVariable = new locks.CondVariable('ho'); @@ -27,3 +31,4 @@ if (condVariable.get() !== 'ho') { console.log('Should not happen'); } condVariable.wait('hi', () => {}); condVariable.set('hi'); if (condVariable.get() !== 'hi') { console.log('Should not happen'); } +const createCondVariable: locks.CondVariable = locks.createCondVariable('ho');