From e1ce6b42093d56b5a9bd1b7d81f019a4a09bdb79 Mon Sep 17 00:00:00 2001 From: int0h Date: Sun, 3 Dec 2017 04:16:06 +0300 Subject: [PATCH 1/3] Type definitions for weak 1.0.1 --- types/weak/index.d.ts | 68 ++++++++++++++++++++++++++++++++++++++++ types/weak/tsconfig.json | 22 +++++++++++++ types/weak/tslint.json | 1 + types/weak/weak-tests.ts | 9 ++++++ 4 files changed, 100 insertions(+) create mode 100644 types/weak/index.d.ts create mode 100644 types/weak/tsconfig.json create mode 100644 types/weak/tslint.json create mode 100644 types/weak/weak-tests.ts diff --git a/types/weak/index.d.ts b/types/weak/index.d.ts new file mode 100644 index 0000000000..554e7474d8 --- /dev/null +++ b/types/weak/index.d.ts @@ -0,0 +1,68 @@ +// Type definitions for weak 1.0.1 +// Project: https://github.com/TooTallNate/node-weak +// Definitions by: William Kapp +/// + +interface WeakRef {} + +/** + * Makes weak references to JavaScript Objects + * @param object can be a regular Object, an Array, a Function, a RegExp, or any of the primitive types or constructor function created with new + * @param callback a callback function to be invoked before the object is garbage collected + */ +declare function weak(object: T, callback?: () => void): WeakRef; + +declare namespace weak { + + /** + * Returns the actual reference to the Object that this weak reference was created with. If this is called with a dead reference, undefined is returned. + * @param ref + */ + export function get(ref: WeakRef): T | undefined; + + /** + * Checks to see if ref is a dead reference. Returns true if the original Object has already been GC'd, false otherwise + * @param ref + */ + export function isDead(ref: WeakRef): boolean; + + /** + * Checks to see if ref is "near death". This will be true exactly during the weak reference callback function, and false any other time. + * @param ref + */ + export function isNearDeath(ref: WeakRef): boolean; + + /** + * Checks to see if obj is "weak reference" instance. Returns true if the passed in object is a "weak reference", false otherwise. + * @param obj + */ + export function isWeakRef(obj: Object | WeakRef): boolean; + + /** + * Adds callback to the Array of callback functions that will be invoked before the Object gets garbage collected. The callbacks get executed in the order that they are added. + * @param ref + * @param callback + */ + export function addCallback(ref: WeakRef, callback: () => void): NodeJS.EventEmitter; + + /** + * Removes callback from the Array of callback functions that will be invoked before the Object gets garbage collected. + * @param ref + * @param callback + */ + export function removeCallback(ref: WeakRef, callback: () => void): NodeJS.EventEmitter; + + /** + * Empties the Array of callback functions that will be invoked before the Object gets garbage collected. + * @param ref + */ + export function removeCallbacks(ref: WeakRef): NodeJS.EventEmitter; + + /** + * Returns an Array that ref iterates through to invoke the GC callbacks. This utilizes node's EventEmitter#listeners() function and therefore returns a copy in node 0.10 and newer. + * @param ref + */ + export function callbacks(ref: WeakRef): (() => void)[]; +} + +export = weak; diff --git a/types/weak/tsconfig.json b/types/weak/tsconfig.json new file mode 100644 index 0000000000..a0bb7dd534 --- /dev/null +++ b/types/weak/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../", + "../node" + ], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "weak-tests.ts" + ] +} diff --git a/types/weak/tslint.json b/types/weak/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/weak/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/weak/weak-tests.ts b/types/weak/weak-tests.ts new file mode 100644 index 0000000000..01a4b2adfc --- /dev/null +++ b/types/weak/weak-tests.ts @@ -0,0 +1,9 @@ +import weak = require('weak'); + +const obj = {a: 123}; + +const weakReference = weak(obj, () => { + // collected +}); + +const sameType = weak.get(weakReference); From 09018134f8a51d8ebc63ce8c83da844834624356 Mon Sep 17 00:00:00 2001 From: int0h Date: Sun, 3 Dec 2017 04:36:47 +0300 Subject: [PATCH 2/3] weak lint fixes --- types/weak/index.d.ts | 46 +++++++++++++++++++++------------------- types/weak/tsconfig.json | 5 +++-- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/types/weak/index.d.ts b/types/weak/index.d.ts index 554e7474d8..5620540fae 100644 --- a/types/weak/index.d.ts +++ b/types/weak/index.d.ts @@ -1,68 +1,70 @@ -// Type definitions for weak 1.0.1 +// Type definitions for weak 1.0 // Project: https://github.com/TooTallNate/node-weak // Definitions by: William Kapp +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + /// -interface WeakRef {} +declare class WeakRef {} /** * Makes weak references to JavaScript Objects * @param object can be a regular Object, an Array, a Function, a RegExp, or any of the primitive types or constructor function created with new * @param callback a callback function to be invoked before the object is garbage collected */ -declare function weak(object: T, callback?: () => void): WeakRef; +declare function weak(object: T, callback?: () => void): WeakRef; declare namespace weak { - /** * Returns the actual reference to the Object that this weak reference was created with. If this is called with a dead reference, undefined is returned. - * @param ref + * @param ref weak reference object */ - export function get(ref: WeakRef): T | undefined; + function get(ref: WeakRef): T | undefined; /** * Checks to see if ref is a dead reference. Returns true if the original Object has already been GC'd, false otherwise - * @param ref + * @param ref weak reference object */ - export function isDead(ref: WeakRef): boolean; + function isDead(ref: WeakRef): boolean; /** * Checks to see if ref is "near death". This will be true exactly during the weak reference callback function, and false any other time. - * @param ref + * @param ref weak reference object */ - export function isNearDeath(ref: WeakRef): boolean; + function isNearDeath(ref: WeakRef): boolean; /** * Checks to see if obj is "weak reference" instance. Returns true if the passed in object is a "weak reference", false otherwise. - * @param obj + * @param obj object to check */ - export function isWeakRef(obj: Object | WeakRef): boolean; + function isWeakRef(obj: object | WeakRef): boolean; /** * Adds callback to the Array of callback functions that will be invoked before the Object gets garbage collected. The callbacks get executed in the order that they are added. - * @param ref - * @param callback + * @param ref weak reference object + * @param callback function to be called */ - export function addCallback(ref: WeakRef, callback: () => void): NodeJS.EventEmitter; + function addCallback(ref: WeakRef, callback: () => void): NodeJS.EventEmitter; /** * Removes callback from the Array of callback functions that will be invoked before the Object gets garbage collected. - * @param ref - * @param callback + * @param ref weak reference object + * @param callback function to be called */ - export function removeCallback(ref: WeakRef, callback: () => void): NodeJS.EventEmitter; + function removeCallback(ref: WeakRef, callback: () => void): NodeJS.EventEmitter; /** * Empties the Array of callback functions that will be invoked before the Object gets garbage collected. - * @param ref + * @param ref weak reference object */ - export function removeCallbacks(ref: WeakRef): NodeJS.EventEmitter; + function removeCallbacks(ref: WeakRef): NodeJS.EventEmitter; /** * Returns an Array that ref iterates through to invoke the GC callbacks. This utilizes node's EventEmitter#listeners() function and therefore returns a copy in node 0.10 and newer. - * @param ref + * @param ref weak reference object */ - export function callbacks(ref: WeakRef): (() => void)[]; + function callbacks(ref: WeakRef): Array<(() => void)>; } export = weak; diff --git a/types/weak/tsconfig.json b/types/weak/tsconfig.json index a0bb7dd534..33d1a4cdf3 100644 --- a/types/weak/tsconfig.json +++ b/types/weak/tsconfig.json @@ -7,11 +7,12 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ - "../", - "../node" + "../" ], + "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true }, From dc914efc0d94e88011b7dfc554ad75c4bff1220a Mon Sep 17 00:00:00 2001 From: int0h Date: Sun, 3 Dec 2017 13:42:25 +0300 Subject: [PATCH 3/3] type guards --- types/weak/index.d.ts | 6 +++--- types/weak/weak-tests.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/types/weak/index.d.ts b/types/weak/index.d.ts index 5620540fae..456a578539 100644 --- a/types/weak/index.d.ts +++ b/types/weak/index.d.ts @@ -26,19 +26,19 @@ declare namespace weak { * Checks to see if ref is a dead reference. Returns true if the original Object has already been GC'd, false otherwise * @param ref weak reference object */ - function isDead(ref: WeakRef): boolean; + function isDead(ref: WeakRef): ref is WeakRef; /** * Checks to see if ref is "near death". This will be true exactly during the weak reference callback function, and false any other time. * @param ref weak reference object */ - function isNearDeath(ref: WeakRef): boolean; + function isNearDeath(ref: WeakRef): ref is WeakRef; /** * Checks to see if obj is "weak reference" instance. Returns true if the passed in object is a "weak reference", false otherwise. * @param obj object to check */ - function isWeakRef(obj: object | WeakRef): boolean; + function isWeakRef(obj: any): obj is WeakRef; /** * Adds callback to the Array of callback functions that will be invoked before the Object gets garbage collected. The callbacks get executed in the order that they are added. diff --git a/types/weak/weak-tests.ts b/types/weak/weak-tests.ts index 01a4b2adfc..a77180823e 100644 --- a/types/weak/weak-tests.ts +++ b/types/weak/weak-tests.ts @@ -7,3 +7,20 @@ const weakReference = weak(obj, () => { }); const sameType = weak.get(weakReference); + +function foo(a: {a: number}) {} + +if (sameType) { + foo(sameType); +} + +const anyVar = null as any; + +if (weak.isWeakRef(anyVar)) { + const a = anyVar; // WeakRef +} + +if (weak.isDead(weakReference)) { + const a = weakReference; // WeakRef + const value = weak.get(weakReference); // undefined only possible +}