diff --git a/lib/lock.js b/lib/lock.js index fc5eb1f..2851a5e 100644 --- a/lib/lock.js +++ b/lib/lock.js @@ -13,6 +13,12 @@ const assert = require('bsert'); */ class Lock { + /** @type {Boolean} */ + named; + + /** @type {Map} */ + map; + /** * Create a lock. * @constructor @@ -30,6 +36,7 @@ class Lock { this.busy = false; this.destroyed = false; + // @ts-ignore this.map = CustomMap ? new CustomMap() : new Map(); this.current = null; @@ -94,9 +101,9 @@ class Lock { /** * Lock the parent object and all its methods * which use the lock. Begin to queue calls. - * @param {String?} name - Job name. - * @param {Boolean?} force - Bypass the lock. - * @returns {Promise} - Returns {Function}, must be + * @param {String|Boolean} [arg1] - Job name or bypass the lock. + * @param {Boolean} [arg2=false] - Bypass the lock. + * @returns {Promise} - Returns {Function}, must be * called once the method finishes executing in order * to resolve the queue. */ @@ -201,7 +208,7 @@ class Job { * @constructor * @param {Function} resolve * @param {Function} reject - * @param {String?} name + * @param {String} [name] */ constructor(resolve, reject, name) { diff --git a/lib/maplock.js b/lib/maplock.js index c9361d9..ce892a8 100644 --- a/lib/maplock.js +++ b/lib/maplock.js @@ -13,6 +13,12 @@ const assert = require('bsert'); */ class MapLock { + /** @type {Map} */ + jobs; + + /** @type {Set} */ + busy; + /** * Create a mapped lock. * @param {Function?} CustomMap @@ -27,7 +33,9 @@ class MapLock { this.Map = CustomMap || Map; this.Set = CustomSet || Set; + // @ts-ignore this.jobs = new this.Map(); + // @ts-ignore this.busy = new this.Set(); this.destroyed = false; @@ -75,7 +83,7 @@ class MapLock { * Begin to queue calls. * @param {String|Number} key * @param {Boolean} [force=false] - Force a call. - * @returns {Promise} - Returns {Function}, must be + * @returns {Promise} - Returns {Function}, must be * called once the method finishes executing in order * to resolve the queue. */ @@ -108,7 +116,7 @@ class MapLock { /** * Create an unlock callback. * @private - * @param {String} key + * @param {String|Number} key * @returns {Function} Unlocker. */ @@ -159,7 +167,9 @@ class MapLock { this.destroyed = true; + // @ts-ignore this.jobs = new this.Map(); + // @ts-ignore this.busy = new this.Set(); for (const jobs of map.values()) { diff --git a/package-lock.json b/package-lock.json index b6ebf8c..e491323 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,10 +9,11 @@ "version": "0.1.6", "license": "MIT", "dependencies": { - "bsert": "~0.0.10" + "bsert": "~0.0.12" }, "devDependencies": { - "bmocha": "^2.1.0" + "bmocha": "^2.1.8", + "bts-type-deps": "^0.0.3" }, "engines": { "node": ">=8.0.0" @@ -38,6 +39,12 @@ "engines": { "node": ">=8.0.0" } + }, + "node_modules/bts-type-deps": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/bts-type-deps/-/bts-type-deps-0.0.3.tgz", + "integrity": "sha512-OQHGWhX5amae6Vj6ShlGaQu0sNCICgJ5YspNZPRzfR5RobrD+wjm5vkZK/J3EH5b/ymxqSWo9VkiFNpCxjaG2Q==", + "dev": true } } } diff --git a/package.json b/package.json index 01d74b6..ebee65a 100644 --- a/package.json +++ b/package.json @@ -16,13 +16,15 @@ "main": "./lib/bmutex.js", "scripts": { "lint": "eslint lib/ test/", + "lint-types": "tsc -p .", "test": "bmocha --reporter spec test/*-test.js" }, "dependencies": { - "bsert": "~0.0.10" + "bsert": "~0.0.12" }, "devDependencies": { - "bmocha": "^2.1.0" + "bmocha": "^2.1.8", + "bts-type-deps": "^0.0.3" }, "engines": { "node": ">=8.0.0" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..da7a94b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,31 @@ +{ + "include": [ + "lib/**/*.js" + ], + "compilerOptions": { + "rootDir": ".", + "target": "ES2020", + "lib": [ + "ES2020" + ], + + "noEmit": true, + + "allowJs": true, + "checkJs": true, + "maxNodeModuleJsDepth": 10, + + "module": "commonjs", + "moduleResolution": "node", + "resolveJsonModule": true, + + "stripInternal": true, + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": false, + + "typeRoots": [ + "node_modules/bts-type-deps/types" + ] + } +}