Skip to content

Commit 689ee8f

Browse files
author
Alexander Dmitrjuk
committed
Add generics
1 parent 64d7f59 commit 689ee8f

File tree

5 files changed

+39
-40
lines changed

5 files changed

+39
-40
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Redis functions balancer
22
[![NPM](https://nodei.co/npm/redis-functions-balancer.png)](https://nodei.co/npm/redis-functions-balancer/)
33

4-
Balance executable NodeJs function with redis.
4+
Balance executes of NodeJs-functions with redis.
55

66
For example, if you have several functions (A, B, C) doing the same things (http requests, long-running code), and you want to execute it evenly.
77

@@ -37,14 +37,14 @@ let iterator = await balancer.getAsyncIterator();
3737

3838
while ( (foo = await iterator.next()) && !foo.done) {
3939
// Your function A|B|C will be here evenly
40-
let method = foo.value;
40+
let func = foo.value;
4141

4242
try {
4343
// Executing on your way (
44-
foo.value();
44+
func();
4545
} catch (e) {
4646
// something happen badly and you want to postpone executes of the function next 10 runs
47-
balancer.increaseMethodRank(method, 10);
47+
balancer.increaseRank(func, 10);
4848
}
4949
}
5050

index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ var __spreadArrays = (this && this.__spreadArrays) || function () {
5656
};
5757
Object.defineProperty(exports, "__esModule", { value: true });
5858
var util_1 = require("util");
59-
var CallableBalancer = /** @class */ (function () {
59+
var RedisFunctionsBalancer = /** @class */ (function () {
6060
/**
6161
*
6262
* @param methods not empty array of functions
6363
* @param redisClient
6464
*/
65-
function CallableBalancer(methods, redisClient) {
65+
function RedisFunctionsBalancer(methods, redisClient) {
6666
this._STORE_PREFIX = 'balancer';
6767
this.INC_VALUE = 1;
6868
this._redisClient = redisClient;
@@ -76,24 +76,24 @@ var CallableBalancer = /** @class */ (function () {
7676
zIncRbyAsync: util_1.promisify(redisClient.zincrby).bind(this._redisClient),
7777
};
7878
}
79-
CallableBalancer.prototype.setMethods = function (methods) {
79+
RedisFunctionsBalancer.prototype.setMethods = function (methods) {
8080
this._methods = methods;
8181
this._storeKey = this.makeStoreKey(methods);
8282
};
83-
CallableBalancer.prototype.increaseMethodRank = function (method, incValue) {
83+
RedisFunctionsBalancer.prototype.increaseRank = function (func, incValue) {
8484
if (incValue === void 0) { incValue = this.INC_VALUE; }
8585
return __awaiter(this, void 0, void 0, function () {
8686
return __generator(this, function (_a) {
8787
switch (_a.label) {
88-
case 0: return [4 /*yield*/, this._functions.zIncRbyAsync(this._storeKey, incValue, method.name)];
88+
case 0: return [4 /*yield*/, this._functions.zIncRbyAsync(this._storeKey, incValue, func.name)];
8989
case 1:
9090
_a.sent();
9191
return [2 /*return*/];
9292
}
9393
});
9494
});
9595
};
96-
CallableBalancer.prototype.getAsyncIterator = function () {
96+
RedisFunctionsBalancer.prototype.getAsyncIterator = function () {
9797
return __asyncGenerator(this, arguments, function getAsyncIterator_1() {
9898
var storedMethodNames, _i, storedMethodNames_1, methodName, _a, _b, method;
9999
return __generator(this, function (_c) {
@@ -112,7 +112,7 @@ var CallableBalancer = /** @class */ (function () {
112112
if (!(_a < _b.length)) return [3 /*break*/, 8];
113113
method = _b[_a];
114114
if (!(method.name === methodName)) return [3 /*break*/, 7];
115-
return [4 /*yield*/, __await(this.increaseMethodRank(method, this.INC_VALUE))];
115+
return [4 /*yield*/, __await(this.increaseRank(method, this.INC_VALUE))];
116116
case 4:
117117
_c.sent();
118118
return [4 /*yield*/, __await(method)];
@@ -134,7 +134,7 @@ var CallableBalancer = /** @class */ (function () {
134134
/**
135135
* Clear store
136136
*/
137-
CallableBalancer.prototype.resetStore = function () {
137+
RedisFunctionsBalancer.prototype.resetStore = function () {
138138
return __awaiter(this, void 0, void 0, function () {
139139
return __generator(this, function (_a) {
140140
switch (_a.label) {
@@ -146,15 +146,15 @@ var CallableBalancer = /** @class */ (function () {
146146
});
147147
});
148148
};
149-
CallableBalancer.prototype.getStoreKey = function () {
149+
RedisFunctionsBalancer.prototype.getStoreKey = function () {
150150
return this._storeKey;
151151
};
152152
/**
153153
* Return redis key to store list of methods with ranks
154154
* @param methods
155155
* @protected
156156
*/
157-
CallableBalancer.prototype.makeStoreKey = function (methods) {
157+
RedisFunctionsBalancer.prototype.makeStoreKey = function (methods) {
158158
var storeKeyArray = [this._STORE_PREFIX];
159159
methods.forEach(function (method) {
160160
storeKeyArray.push(method.name);
@@ -165,7 +165,7 @@ var CallableBalancer = /** @class */ (function () {
165165
* Returns an Array stored in Redis in Rank order
166166
* @private
167167
*/
168-
CallableBalancer.prototype.getRange = function () {
168+
RedisFunctionsBalancer.prototype.getRange = function () {
169169
return __awaiter(this, void 0, void 0, function () {
170170
var storedMethodNames, args_1, result_1;
171171
var _a;
@@ -190,6 +190,6 @@ var CallableBalancer = /** @class */ (function () {
190190
});
191191
});
192192
};
193-
return CallableBalancer;
193+
return RedisFunctionsBalancer;
194194
}());
195-
exports.default = CallableBalancer;
195+
exports.default = RedisFunctionsBalancer;

index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ type RedisFunctions = {
88
zIncRbyAsync: (key: string, incValue: number, element: string) => Promise<string>;
99
};
1010

11-
export default class CallableBalancer {
11+
export default class RedisFunctionsBalancer<T extends Function> {
1212
private _storeKey: string;
13-
private _methods: Array<Function>;
13+
private _methods: Array<T>;
1414
private readonly _STORE_PREFIX = 'balancer';
1515
private readonly _redisClient: RedisClient;
1616
private readonly INC_VALUE = 1;
@@ -22,7 +22,7 @@ export default class CallableBalancer {
2222
* @param methods not empty array of functions
2323
* @param redisClient
2424
*/
25-
constructor(methods: Array<Function>, redisClient: RedisClient) {
25+
constructor(methods: Array<T>, redisClient: RedisClient) {
2626
this._redisClient = redisClient;
2727
this._methods = methods;
2828
this._storeKey = this.makeStoreKey(methods);
@@ -36,23 +36,23 @@ export default class CallableBalancer {
3636
};
3737
}
3838

39-
public setMethods(methods: Array<Function>) {
39+
public setMethods(methods: Array<T>) {
4040
this._methods = methods;
4141
this._storeKey = this.makeStoreKey(methods);
4242
}
4343

44-
public async increaseMethodRank(method: Function, incValue: number = this.INC_VALUE) {
45-
await this._functions.zIncRbyAsync(this._storeKey, incValue, method.name);
44+
public async increaseRank(func: T, incValue: number = this.INC_VALUE) {
45+
await this._functions.zIncRbyAsync(this._storeKey, incValue, func.name);
4646
}
4747

48-
public async* getAsyncIterator(): AsyncIterableIterator<Function> {
48+
public async* getAsyncIterator(): AsyncIterableIterator<T> {
4949
let storedMethodNames = await this.getRange();
5050

5151
// Redis store defined
5252
for (let methodName of storedMethodNames) {
5353
for (let method of this._methods) {
5454
if (method.name === methodName) {
55-
await this.increaseMethodRank(method, this.INC_VALUE);
55+
await this.increaseRank(method, this.INC_VALUE);
5656
yield method;
5757
}
5858
}
@@ -75,9 +75,9 @@ export default class CallableBalancer {
7575
* @param methods
7676
* @protected
7777
*/
78-
protected makeStoreKey(methods: Array<Function>): string {
78+
protected makeStoreKey(methods: Array<T>): string {
7979
let storeKeyArray: Array<string> = [this._STORE_PREFIX];
80-
methods.forEach((method: Function) => {
80+
methods.forEach((method: T) => {
8181
storeKeyArray.push(method.name);
8282
});
8383

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"name": "redis-functions-balancer",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Balance NodeJs functions with Redis",
55
"repository": "https://github.com/dmitryuk/RedisFunctionsBalancer",
6-
"main": "index.ts",
7-
"type": "module",
6+
"main": "index.js",
87
"scripts": {
98
"test": "./node_modules/mocha/bin/mocha"
109
},

test/iterator.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import CallableBalancer from "../index";
1+
import RedisFunctionsBalancer from "../index";
22
import {beforeEach} from "mocha";
33
import {promisify} from "util";
44
import assert = require("assert");
@@ -11,11 +11,11 @@ const B = () => new Promise(() => console.log('B'));
1111
const C = () => new Promise(() => console.log('C'));
1212

1313
let methods = [A, B, C];
14-
let balancer: CallableBalancer;
15-
let zrangeAsync = promisify(redisClient.zrange).bind(redisClient);
14+
let balancer: RedisFunctionsBalancer<Function>;
15+
let zRangeAsync = promisify(redisClient.zrange).bind(redisClient);
1616
describe('Test Callable Balancer', async function () {
1717
beforeEach(async () => {
18-
balancer = new CallableBalancer(methods, redisClient)
18+
balancer = new RedisFunctionsBalancer(methods, redisClient)
1919
await balancer.resetStore();
2020
});
2121

@@ -37,24 +37,24 @@ describe('Test Callable Balancer', async function () {
3737

3838
it('check redis state with iterator', async () => {
3939
let key = balancer.getStoreKey();
40-
let result = await zrangeAsync(key, 0, -1);
40+
let result = await zRangeAsync(key, 0, -1);
4141
assert.strictEqual(0, result.length);
4242
let iterator = balancer.getAsyncIterator();
43-
result = await zrangeAsync(key, 0, -1);
43+
result = await zRangeAsync(key, 0, -1);
4444
assert.strictEqual(0, result.length);
4545

4646
let data = await iterator.next();
47-
result = await zrangeAsync(key, 0, -1);
47+
result = await zRangeAsync(key, 0, -1);
4848
assert.deepStrictEqual(['B', 'C', 'A'], result);
4949
assert.strictEqual(A, data.value);
5050

5151
data = await iterator.next();
52-
result = await zrangeAsync(key, 0, -1);
52+
result = await zRangeAsync(key, 0, -1);
5353
assert.deepStrictEqual(['C', 'A', 'B'], result);
5454
assert.strictEqual(B, data.value);
5555

5656
data = await iterator.next();
57-
result = await zrangeAsync(key, 0, -1);
57+
result = await zRangeAsync(key, 0, -1);
5858
assert.deepStrictEqual(['A', 'B', 'C'], result);
5959
assert.strictEqual(C, data.value);
6060
});
@@ -63,7 +63,7 @@ describe('Test Callable Balancer', async function () {
6363
let iterator = await balancer.getAsyncIterator(),
6464
data;
6565
await iterator.next();
66-
await balancer.increaseMethodRank(B, 2);
66+
await balancer.increaseRank(B, 2);
6767

6868
iterator = await balancer.getAsyncIterator();
6969
data = await iterator.next();

0 commit comments

Comments
 (0)