From 3e5fb67a6b8d300ad9734c5755fb8773f3aa9da8 Mon Sep 17 00:00:00 2001 From: "TATTSGROUP\\Josh.Graham" Date: Thu, 27 Sep 2018 13:07:33 +1000 Subject: [PATCH 1/4] Initial commit of 'locks' definitions. --- types/locks/index.d.ts | 82 ++++++++++++++++++++++++++++++++++++++ types/locks/locks-tests.ts | 66 ++++++++++++++++++++++++++++++ types/locks/tsconfig.json | 23 +++++++++++ types/locks/tslint.json | 1 + 4 files changed, 172 insertions(+) create mode 100644 types/locks/index.d.ts create mode 100644 types/locks/locks-tests.ts create mode 100644 types/locks/tsconfig.json create mode 100644 types/locks/tslint.json diff --git a/types/locks/index.d.ts b/types/locks/index.d.ts new file mode 100644 index 0000000000..3f0b9bee1c --- /dev/null +++ b/types/locks/index.d.ts @@ -0,0 +1,82 @@ +// Type definitions for Locks 0.2.2 +// Project: https://github.com/Wizcorp/locks +// Definitions by: Joshua Graham +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/* ================== USAGE ================== + + import * as locks from "locks"; + + let mutex: locks.Mutex = new locks.Mutex(); + let readWriteLock: locks.ReadWriteLock = new locks.ReadWriteLock(); + let semaphore: locks.Semaphore = new locks.Semaphore(); + let condVariable: locks.CondVariable = new locks.CondVariable(); + + ============================================= */ + + /** Solitary access lock */ + export class Mutex { + /** Construct a new mutex lock. */ + constructor(); + /** Flag indicating whther the lock is currently taken. */ + isLocked: boolean; + /** 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; + /** Try taking the lock. If false, the lock wasn't taken. */ + tryLock(): boolean; + /** Release the current lock. */ + unlock(): void; + /** Clear any waiting lock callbacks. */ + resetQueue(): void; + +} + +/** Reader writer lock */ +export class ReadWriteLock { + /** Construct a new reader writer lock */ + constructor(); + /** Flag indicating whether the reader writer lock is holding the read lock */ + isReadLocked: boolean; + /** Flag indicating whether the reader writer lock is holding the write lock */ + isWriteLocked: boolean; + /** Get the read lock */ + readLock(callback: () => void): void; + /** Get the write lock */ + writeLock(callback: () => void): void; + /** Wait the specified number of milliseconds to take the read lock. If error is set, the lock wasn't taken. */ + timedReadLock(ttl: number, callback: (error: Error) => void): void; + /** Wait the specified number of milliseconds to take the write lock. If error is set, the lock wasn't taken. */ + timedWriteLock(ttl: number, callback: (error: Error) => void): void; + /** Try taking the read lock. If false, the lock wasn't taken. */ + tryReadLock(): boolean; + /** Try taking the write lock. If false, the lock wasn't taken. */ + tryWriteLock(): boolean; + /** Release the write lock if taken, or one of the taken read locks. */ + unlock(): void; +} + +/** Semaphore signaller. */ +export class Semaphore { + /** Construct a new semaphore. */ + constructor(initialCount: number); + /** Queue a callback. Callbacks are called in sequence on signals. */ + wait(callback: () => void): void; + /** Signal a callback. */ + signal(): void; + +} + +/** Conditional variable instance */ +export class CondVariable { + /** Construct a new conditional variable with the specified initial value. */ + constructor(initialValue: any); + /** Get the current conditional variable value. */ + get(): any; + /** Add a callback when the specified conditional variable value matches the specified value. */ + wait(value: any, callback: ()=>void); + /** Set the conditional variable value. */ + set(value: any): void; +} diff --git a/types/locks/locks-tests.ts b/types/locks/locks-tests.ts new file mode 100644 index 0000000000..c8c5923ea2 --- /dev/null +++ b/types/locks/locks-tests.ts @@ -0,0 +1,66 @@ +import * as locks from "locks"; + + +// Mutex +let mutex: locks.Mutex = new locks.Mutex(); + +mutex.lock(() => {}); + +if (mutex.tryLock()) { + console.log('Should not happen'); +} + +mutex.unlock(); + + +// Semaphore +let semaphore: locks.Semaphore = new locks.Semaphore(1); +semaphore.wait(()=> { + +}); + +semaphore.wait(()=> { + +}); + +semaphore.signal(); + + +// Read Write Lock +let readWriteLock: locks.ReadWriteLock = new locks.ReadWriteLock(); + +readWriteLock.readLock(() => {}); + +if (readWriteLock.tryReadLock()) { + console.log('Should not happen'); +} + +readWriteLock.unlock(); + +readWriteLock.writeLock(() => {}); + +if (readWriteLock.tryWriteLock()) { + console.log('Should not happen'); +} + +readWriteLock.unlock(); + +// Conditional Variable +let condVariable: locks.CondVariable = new locks.CondVariable('ho'); + +if(condVariable.get() !== 'ho') { + console.log('Should not happen'); +} + +condVariable.wait('hi', () => {}); + +condVariable.set('hi'); + +if(condVariable.get() !== 'hi') { + console.log('Should not happen'); +} + + + + + diff --git a/types/locks/tsconfig.json b/types/locks/tsconfig.json new file mode 100644 index 0000000000..85b08b6e3f --- /dev/null +++ b/types/locks/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "locks-tests.ts" + ] +} \ No newline at end of file diff --git a/types/locks/tslint.json b/types/locks/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/locks/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 79059518159812fbf7c423ecac4236c5f026eb9e Mon Sep 17 00:00:00 2001 From: "TATTSGROUP\\Josh.Graham" Date: Thu, 27 Sep 2018 13:44:35 +1000 Subject: [PATCH 2/4] Removed extraneous whitespace. --- types/locks/index.d.ts | 23 ++++----------- types/locks/locks-tests.ts | 59 +++++++------------------------------- types/locks/tsconfig.json | 5 ++-- 3 files changed, 19 insertions(+), 68 deletions(-) diff --git a/types/locks/index.d.ts b/types/locks/index.d.ts index 3f0b9bee1c..44efcac521 100644 --- a/types/locks/index.d.ts +++ b/types/locks/index.d.ts @@ -1,22 +1,11 @@ -// Type definitions for Locks 0.2.2 +// Type definitions for Locks 0.2 // Project: https://github.com/Wizcorp/locks // Definitions by: Joshua Graham // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 -/* ================== USAGE ================== - - import * as locks from "locks"; - - let mutex: locks.Mutex = new locks.Mutex(); - let readWriteLock: locks.ReadWriteLock = new locks.ReadWriteLock(); - let semaphore: locks.Semaphore = new locks.Semaphore(); - let condVariable: locks.CondVariable = new locks.CondVariable(); - - ============================================= */ - - /** Solitary access lock */ - export class Mutex { +/** Solitary access lock */ +export class Mutex { /** Construct a new mutex lock. */ constructor(); /** Flag indicating whther the lock is currently taken. */ @@ -31,7 +20,6 @@ unlock(): void; /** Clear any waiting lock callbacks. */ resetQueue(): void; - } /** Reader writer lock */ @@ -66,7 +54,6 @@ export class Semaphore { wait(callback: () => void): void; /** Signal a callback. */ signal(): void; - } /** Conditional variable instance */ @@ -76,7 +63,7 @@ export class CondVariable { /** Get the current conditional variable value. */ get(): any; /** Add a callback when the specified conditional variable value matches the specified value. */ - wait(value: any, callback: ()=>void); + wait(value: any, callback: ()=>void): void; /** Set the conditional variable value. */ set(value: any): void; -} +} \ No newline at end of file diff --git a/types/locks/locks-tests.ts b/types/locks/locks-tests.ts index c8c5923ea2..a52119b0f9 100644 --- a/types/locks/locks-tests.ts +++ b/types/locks/locks-tests.ts @@ -1,66 +1,29 @@ import * as locks from "locks"; - // Mutex -let mutex: locks.Mutex = new locks.Mutex(); - +const mutex: locks.Mutex = new locks.Mutex(); mutex.lock(() => {}); - -if (mutex.tryLock()) { - console.log('Should not happen'); -} - +if (mutex.tryLock()) { console.log('Should not happen'); } mutex.unlock(); - // Semaphore -let semaphore: locks.Semaphore = new locks.Semaphore(1); -semaphore.wait(()=> { - -}); - -semaphore.wait(()=> { - -}); - +const semaphore: locks.Semaphore = new locks.Semaphore(1); +semaphore.wait(()=> {}); +semaphore.wait(()=> {}); semaphore.signal(); - // Read Write Lock -let readWriteLock: locks.ReadWriteLock = new locks.ReadWriteLock(); - +const readWriteLock: locks.ReadWriteLock = new locks.ReadWriteLock(); readWriteLock.readLock(() => {}); - -if (readWriteLock.tryReadLock()) { - console.log('Should not happen'); -} - +if (readWriteLock.tryReadLock()) { console.log('Should not happen'); } readWriteLock.unlock(); - readWriteLock.writeLock(() => {}); - -if (readWriteLock.tryWriteLock()) { - console.log('Should not happen'); -} - +if (readWriteLock.tryWriteLock()) { console.log('Should not happen'); } readWriteLock.unlock(); // Conditional Variable -let condVariable: locks.CondVariable = new locks.CondVariable('ho'); - -if(condVariable.get() !== 'ho') { - console.log('Should not happen'); -} - +const condVariable: locks.CondVariable = new locks.CondVariable('ho'); +if(condVariable.get() !== 'ho') { console.log('Should not happen'); } condVariable.wait('hi', () => {}); - condVariable.set('hi'); - -if(condVariable.get() !== 'hi') { - console.log('Should not happen'); -} - - - - - +if(condVariable.get() !== 'hi') { console.log('Should not happen'); } \ No newline at end of file diff --git a/types/locks/tsconfig.json b/types/locks/tsconfig.json index 85b08b6e3f..123bb4c13f 100644 --- a/types/locks/tsconfig.json +++ b/types/locks/tsconfig.json @@ -2,11 +2,12 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6" + "es6", + "dom" ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ From a7c782972ca5c5548474f1d704573323f3b20754 Mon Sep 17 00:00:00 2001 From: "TATTSGROUP\\Josh.Graham" Date: Thu, 27 Sep 2018 13:51:38 +1000 Subject: [PATCH 3/4] Tailoring whitespace.. --- types/locks/index.d.ts | 4 ++-- types/locks/locks-tests.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/types/locks/index.d.ts b/types/locks/index.d.ts index 44efcac521..9ab113c5a9 100644 --- a/types/locks/index.d.ts +++ b/types/locks/index.d.ts @@ -63,7 +63,7 @@ export class CondVariable { /** Get the current conditional variable value. */ get(): any; /** Add a callback when the specified conditional variable value matches the specified value. */ - wait(value: any, callback: ()=>void): void; + wait(value: any, callback: () => void): void; /** Set the conditional variable value. */ set(value: any): void; -} \ No newline at end of file +} diff --git a/types/locks/locks-tests.ts b/types/locks/locks-tests.ts index a52119b0f9..ecac6f59e1 100644 --- a/types/locks/locks-tests.ts +++ b/types/locks/locks-tests.ts @@ -8,8 +8,8 @@ mutex.unlock(); // Semaphore const semaphore: locks.Semaphore = new locks.Semaphore(1); -semaphore.wait(()=> {}); -semaphore.wait(()=> {}); +semaphore.wait(() => {}); +semaphore.wait(() => {}); semaphore.signal(); // Read Write Lock @@ -23,7 +23,7 @@ readWriteLock.unlock(); // Conditional Variable const condVariable: locks.CondVariable = new locks.CondVariable('ho'); -if(condVariable.get() !== 'ho') { console.log('Should not happen'); } +if (condVariable.get() !== 'ho') { console.log('Should not happen'); } condVariable.wait('hi', () => {}); condVariable.set('hi'); -if(condVariable.get() !== 'hi') { console.log('Should not happen'); } \ No newline at end of file +if (condVariable.get() !== 'hi') { console.log('Should not happen'); } From 4bb0741b07b8de9bc74276efe2d01569efc81b87 Mon Sep 17 00:00:00 2001 From: "TATTSGROUP\\Josh.Graham" Date: Fri, 28 Sep 2018 09:48:34 +1000 Subject: [PATCH 4/4] Typo fix. Changed import style. --- types/locks/index.d.ts | 2 +- types/locks/locks-tests.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/locks/index.d.ts b/types/locks/index.d.ts index 9ab113c5a9..8c112d65d1 100644 --- a/types/locks/index.d.ts +++ b/types/locks/index.d.ts @@ -8,7 +8,7 @@ export class Mutex { /** Construct a new mutex lock. */ constructor(); - /** Flag indicating whther the lock is currently taken. */ + /** Flag indicating whether the lock is currently taken. */ isLocked: boolean; /** Take the lock. */ lock(callback: () => void): void; diff --git a/types/locks/locks-tests.ts b/types/locks/locks-tests.ts index ecac6f59e1..1e7d768a0c 100644 --- a/types/locks/locks-tests.ts +++ b/types/locks/locks-tests.ts @@ -1,4 +1,4 @@ -import * as locks from "locks"; +import locks = require("locks"); // Mutex const mutex: locks.Mutex = new locks.Mutex();