From 5ad20e47c63ab01e62f926c0367d21e8aabb072f Mon Sep 17 00:00:00 2001 From: Hieu Lam Date: Wed, 17 Jul 2019 05:45:44 +0700 Subject: [PATCH] [weak-napi] Add type for `weak-napi` package (#36869) * Add type for package * Update test for `weak-napi` --- types/weak-napi/index.d.ts | 70 ++++++++++++++++++++++++++++++ types/weak-napi/tsconfig.json | 23 ++++++++++ types/weak-napi/tslint.json | 1 + types/weak-napi/weak-napi-tests.ts | 26 +++++++++++ 4 files changed, 120 insertions(+) create mode 100644 types/weak-napi/index.d.ts create mode 100644 types/weak-napi/tsconfig.json create mode 100644 types/weak-napi/tslint.json create mode 100644 types/weak-napi/weak-napi-tests.ts diff --git a/types/weak-napi/index.d.ts b/types/weak-napi/index.d.ts new file mode 100644 index 0000000000..7330eb2258 --- /dev/null +++ b/types/weak-napi/index.d.ts @@ -0,0 +1,70 @@ +// Type definitions for weak-napi 1.0 +// Project: https://github.com/node-ffi-napi/weak-napi +// Definitions by: Hieu Lam +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +/// + +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 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 weak reference object + */ + 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 weak reference object + */ + 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): false; + + /** + * 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: 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. + * @param ref weak reference object + * @param callback function to be called + */ + 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 weak reference object + * @param callback function to be called + */ + 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 weak reference object + */ + 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 weak reference object + */ + function callbacks(ref: WeakRef): Array<(() => void)>; +} + +export = weak; diff --git a/types/weak-napi/tsconfig.json b/types/weak-napi/tsconfig.json new file mode 100644 index 0000000000..0f7d193276 --- /dev/null +++ b/types/weak-napi/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "weak-napi-tests.ts" + ] +} diff --git a/types/weak-napi/tslint.json b/types/weak-napi/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/weak-napi/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/weak-napi/weak-napi-tests.ts b/types/weak-napi/weak-napi-tests.ts new file mode 100644 index 0000000000..4e93a9c9cd --- /dev/null +++ b/types/weak-napi/weak-napi-tests.ts @@ -0,0 +1,26 @@ +import weak = require('weak-napi'); + +const obj = {a: 123}; + +const weakReference = weak(obj, () => { + // collected +}); + +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; // $ExpectType WeakRef + const value = weak.get(weakReference); // undefined only possible +}