Skip to content

Commit e96a6d8

Browse files
committed
feat: rewritten in typescript
BREAKING CHANGE: API is the same, moved over to @microfleet, source in typescript
1 parent db89475 commit e96a6d8

13 files changed

Lines changed: 4994 additions & 1920 deletions

.babelrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

.commitlintrc.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
rules: {
3+
'body-leading-blank': [1, 'always'],
4+
'footer-leading-blank': [1, 'always'],
5+
'header-max-length': [2, 'always', 72],
6+
'scope-case': [2, 'always', 'lower-case'],
7+
'subject-case': [
8+
2,
9+
'never',
10+
['sentence-case', 'start-case', 'pascal-case', 'upper-case']
11+
],
12+
'subject-empty': [2, 'never'],
13+
'subject-full-stop': [2, 'never', '.'],
14+
'type-case': [2, 'always', 'lower-case'],
15+
'type-empty': [2, 'never'],
16+
'type-enum': [2, 'always', [
17+
'build',
18+
'ci',
19+
'docs',
20+
'feat',
21+
'fix',
22+
'perf',
23+
'refactor',
24+
'revert',
25+
'style',
26+
'test',
27+
'major',
28+
'minor',
29+
'patch',
30+
'chore'
31+
]
32+
]
33+
}
34+
};

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "makeomatic"
2+
"extends": "makeomatic/typescript"
33
}

.releaserc.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"branches": [
3+
"master",
4+
{ "name": "develop", "prerelease": true }
5+
],
6+
"analyzeCommits": {
7+
"preset": "angular",
8+
"releaseRules": [
9+
{ "type": "docs", "release": "patch" },
10+
{ "type": "refactor", "release": "patch" },
11+
{ "type": "style", "release": "patch" },
12+
{ "type": "minor", "release": "minor" },
13+
{ "type": "patch", "release": "patch" },
14+
{ "type": "major", "release": "major" },
15+
{ "type": "breaking", "release": "major" }
16+
]
17+
}
18+
}

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
The MIT License (MIT)
22

33
Copyright (c) 2015-2016 Vitaly Aminev
4+
Copyright (c) 2021 Makeomatic Inc
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy
67
of this software and associated documentation files (the "Software"), to deal

package.json

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
{
2-
"name": "callback-queue",
2+
"name": "@microfleet/callback-queue",
33
"version": "3.0.0",
44
"description": "Put your callbacks into queue to make sure that concurrent requests that you might want to perform will only be executed once",
55
"main": "./lib/callback-queue.js",
66
"scripts": {
7-
"test": "yarn lint && mocha -r @babel/register -R spec test",
7+
"test": "yarn lint && mocha -r ts-node/register -R spec test/*.spec.ts",
88
"lint": "eslint ./src",
9-
"prepublishOnly": "yarn compile",
109
"pretest": "yarn compile",
11-
"compile": "babel -d ./lib src"
10+
"compile": "tsc --build ./tsconfig.build.json",
11+
"prepublishOnly": "yarn compile",
12+
"semantic-release": "semantic-release"
1213
},
1314
"repository": {
1415
"type": "git",
15-
"url": "git://github.com/AVVS/callback-queue.git"
16+
"url": "git://github.com/microfleet/callback-queue.git"
1617
},
1718
"bugs": {
18-
"url": "https://github.com/AVVS/callback-queue/issues"
19+
"url": "https://github.com/microfleet/callback-queue/issues"
1920
},
2021
"keywords": [
2122
"queue",
@@ -29,22 +30,40 @@
2930
"debug": "^4.1.1"
3031
},
3132
"engine": {
32-
"node": "^10.15.x"
33+
"node": ">= 12.15.x"
3334
},
3435
"devDependencies": {
35-
"@babel/cli": "^7.0.0",
36-
"@babel/core": "^7.0.0",
37-
"@babel/plugin-transform-strict-mode": "^7.2.0",
38-
"@babel/register": "^7.0.0",
36+
"@makeomatic/deploy": "^10.4.0",
37+
"@types/bluebird": "^3.5.33",
38+
"@types/debug": "^4.1.5",
39+
"@types/ioredis": "^4.22.3",
40+
"@types/mocha": "^8.2.2",
41+
"@types/node": "^14.14.39",
42+
"@types/sinon": "^10.0.0",
43+
"@types/uuid": "^8.3.0",
44+
"@typescript-eslint/eslint-plugin": "^4.22.0",
45+
"@typescript-eslint/parser": "^4.22.0",
3946
"chai": "^4.2.0",
4047
"chai-spies": "^1.0.0",
41-
"eslint": "^6.3.0",
42-
"eslint-config-makeomatic": "^3.1.0",
43-
"eslint-plugin-import": "^2.18.2",
44-
"mocha": "^6.2.0"
48+
"cross-env": "^7.0.3",
49+
"eslint": "^7.24.0",
50+
"eslint-config-makeomatic": "^5.0.4",
51+
"eslint-plugin-import": "^2.22.1",
52+
"eslint-plugin-promise": "^5.1.0",
53+
"ioredis": "^4.26.0",
54+
"mocha": "^8.3.2",
55+
"sinon": "^10.0.0",
56+
"ts-node": "^9.1.1",
57+
"typescript": "^4.2.3"
4558
},
4659
"files": [
4760
"src/",
4861
"lib/"
49-
]
62+
],
63+
"husky": {
64+
"hooks": {
65+
"commit-msg": "commitlint -e $HUSKY_GIT_PARAMS",
66+
"prepare-commit-msg": "./node_modules/@makeomatic/deploy/git-hooks/prepare-commit-msg $HUSKY_GIT_PARAMS"
67+
}
68+
}
5069
}
Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
const debug = require('debug')('callback-queue');
2-
const assert = require('assert');
1+
import _debug = require('debug')
2+
import assert = require('assert')
3+
4+
const debug = _debug('callback-queue')
35

46
/**
57
* Callback queue
6-
* @type {Map}
78
*/
8-
const callbackQueue = new Map();
9+
const callbackQueue = new Map<string, Set<Thunk>>()
910

1011
// cache reference
11-
const nextTick = (typeof setImmediate === 'function' && setImmediate) || process.nextTick;
12+
const nextTick = (typeof setImmediate === 'function' && setImmediate) || process.nextTick
13+
export type Thunk = (...args: unknown[]) => unknown
1214

1315
/**
1416
* Iterates over callbacks and calls them with passed args
15-
* @param {Array} bucket
16-
* @param {Array} args
17+
* @param bucket
18+
* @param args
1719
*/
18-
function iterateOverCallbacks(bucket, args) {
20+
function iterateOverCallbacks(bucket: Set<Thunk>, args: unknown[]) {
1921
// set iterator
2022
for (const thunk of bucket) {
21-
nextTick(thunk, ...args);
23+
nextTick(thunk, ...args)
2224
}
2325
}
2426

@@ -33,36 +35,40 @@ function iterateOverCallbacks(bucket, args) {
3335
* @param {String} key - unique key, based on which requests are bucketed
3436
* @param {Function} callback - callback that should be added into requests queue
3537
*/
36-
exports.add = function add(key, callback) {
37-
assert.equal(typeof key, 'string', 'key must be a truthy string');
38-
assert.ok(key, 'key must be a truthy string');
39-
assert.equal(typeof callback, 'function', 'callback must be a function');
38+
export function add(key: string, callback: Thunk): Thunk | false {
39+
assert.strictEqual(typeof key, 'string', 'key must be a truthy string')
40+
assert.ok(key, 'key must be a truthy string')
41+
assert.strictEqual(typeof callback, 'function', 'callback must be a function')
4042

41-
const bucket = callbackQueue.get(key);
43+
const bucket = callbackQueue.get(key)
4244
if (bucket) {
43-
bucket.add(callback);
44-
return false;
45+
bucket.add(callback)
46+
return false
4547
}
4648

4749
// push new set into queue
48-
callbackQueue.set(key, new Set([callback]));
50+
callbackQueue.set(key, new Set([callback]))
4951

50-
return function queuedCallback(...args) {
52+
/**
53+
* Returns forwarder function that needs to be invoked
54+
* when data has been processed
55+
*/
56+
return function forwarder(...args: unknown[]) {
5157
// its essential that we do not use any reference, because of GC
5258
// when object reaches certain number of nullified values - its recreated using compactObject
5359
// function. Therefore we need to grab a reference when callback needs to be invoked and not at
5460
// other time
55-
const callbacks = callbackQueue.get(key);
61+
const callbacks = callbackQueue.get(key)
5662
if (!callbacks) {
57-
debug('Callbacks couldn\'t be invoked');
58-
return null;
63+
debug('Callbacks couldn\'t be invoked')
64+
return null
5965
}
6066

61-
debug('calling callback for key %s', key);
62-
callbackQueue.delete(key);
63-
return iterateOverCallbacks(callbacks, args);
64-
};
65-
};
67+
debug('calling callback for key %s', key)
68+
callbackQueue.delete(key)
69+
return iterateOverCallbacks(callbacks, args)
70+
}
71+
}
6672

6773
/**
6874
* Call this if you are absolutely sure you need to abort the request
@@ -74,13 +80,13 @@ exports.add = function add(key, callback) {
7480
* @param {String} key
7581
* @param {Error} error
7682
*/
77-
exports.remove = function remove(key, error) {
78-
const bucket = callbackQueue.get(key);
83+
export function remove(key: string, error: Error): void | false {
84+
const bucket = callbackQueue.get(key)
7985
if (!bucket) {
80-
return false;
86+
return false
8187
}
8288

83-
assert.ok(error instanceof Error, 'you must pass an instance of Error object when canceling requests');
84-
callbackQueue.delete(key);
85-
return iterateOverCallbacks(bucket, [error]);
86-
};
89+
assert.ok(error instanceof Error, 'you must pass an instance of Error object when canceling requests')
90+
callbackQueue.delete(key)
91+
return iterateOverCallbacks(bucket, [error])
92+
}

test/index.js

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)