UNPKG

7.91 kBPlain TextView Raw
1/// <reference path="bottleneck.d.ts" />
2
3import Bottleneck from "bottleneck";
4// import * as assert from "assert";
5function assert(b: boolean): void { }
6
7/*
8This file is run by scripts/build.sh.
9It is used to validate the typings in bottleneck.d.ts.
10The command is: tsc --noEmit --strictNullChecks test.ts
11This file cannot be run directly.
12In order to do that, you must comment out the first line,
13and change "bottleneck" to "." on the third line.
14*/
15
16function withCb(foo: number, bar: () => void, cb: (err: any, result: string) => void) {
17 let s: string = `cb ${foo}`;
18 cb(null, s);
19}
20
21console.log(Bottleneck);
22
23let limiter = new Bottleneck({
24 maxConcurrent: 5,
25 minTime: 1000,
26 highWater: 20,
27 strategy: Bottleneck.strategy.LEAK,
28 reservoirRefreshInterval: 1000 * 60,
29 reservoirRefreshAmount: 10,
30 reservoirIncreaseInterval: 1000 * 60,
31 reservoirIncreaseAmount: 2,
32 reservoirIncreaseMaximum: 15
33});
34
35limiter.ready().then(() => { console.log('Ready') });
36limiter.clients().client;
37limiter.disconnect();
38
39limiter.currentReservoir().then(function (x) {
40 if (x != null) {
41 let i: number = x;
42 }
43});
44
45limiter.incrementReservoir(5).then(function (x) {
46 if (x != null) {
47 let i: number = x;
48 }
49});
50
51limiter.running().then(function (x) {
52 let i: number = x;
53});
54
55limiter.clusterQueued().then(function (x) {
56 let i: number = x;
57});
58
59limiter.done().then(function (x) {
60 let i: number = x;
61});
62
63limiter.submit(withCb, 1, () => {}, (err, result) => {
64 let s: string = result;
65 console.log(s);
66 assert(s == "cb 1");
67});
68
69function withPromise(foo: number, bar: () => void): PromiseLike<string> {
70 let s: string = `promise ${foo}`;
71 return Promise.resolve(s);
72}
73
74let foo: Promise<string> = limiter.schedule(withPromise, 1, () => {});
75foo.then(function (result: string) {
76 let s: string = result;
77 console.log(s);
78 assert(s == "promise 1");
79});
80
81limiter.on("message", (msg) => console.log(msg));
82
83limiter.publish(JSON.stringify({ a: "abc", b: { c: 123 }}));
84
85function checkEventInfo(info: Bottleneck.EventInfo) {
86 const numArgs: number = info.args.length;
87 const id: string = info.options.id;
88}
89
90limiter.on('dropped', (info) => {
91 checkEventInfo(info)
92 const task: Function = info.task;
93 const promise: Promise<any> = info.promise;
94})
95
96limiter.on('received', (info) => {
97 checkEventInfo(info)
98})
99
100limiter.on('queued', (info) => {
101 checkEventInfo(info)
102 const blocked: boolean = info.blocked;
103 const reachedHWM: boolean = info.reachedHWM;
104})
105
106limiter.on('scheduled', (info) => {
107 checkEventInfo(info)
108})
109
110limiter.on('executing', (info) => {
111 checkEventInfo(info)
112 const count: number = info.retryCount;
113})
114
115limiter.on('failed', (error, info) => {
116 checkEventInfo(info)
117 const message: string = error.message;
118 const count: number = info.retryCount;
119 return Promise.resolve(10)
120})
121
122limiter.on('failed', (error, info) => {
123 checkEventInfo(info)
124 const message: string = error.message;
125 const count: number = info.retryCount;
126 return Promise.resolve(null)
127})
128
129limiter.on('failed', (error, info) => {
130 checkEventInfo(info)
131 const message: string = error.message;
132 const count: number = info.retryCount;
133 return Promise.resolve()
134})
135
136limiter.on('failed', (error, info) => {
137 checkEventInfo(info)
138 const message: string = error.message;
139 const count: number = info.retryCount;
140 return 10
141})
142
143limiter.on('failed', (error, info) => {
144 checkEventInfo(info)
145 const message: string = error.message;
146 const count: number = info.retryCount;
147 return null
148})
149
150limiter.on('failed', (error, info) => {
151 checkEventInfo(info)
152 const message: string = error.message;
153 const count: number = info.retryCount;
154})
155
156limiter.on('retry', (message: string, info) => {
157 checkEventInfo(info)
158 const count: number = info.retryCount;
159})
160
161limiter.on('done', (info) => {
162 checkEventInfo(info)
163 const count: number = info.retryCount;
164})
165
166let group = new Bottleneck.Group({
167 maxConcurrent: 5,
168 minTime: 1000,
169 highWater: 10,
170 strategy: Bottleneck.strategy.LEAK,
171 datastore: "ioredis",
172 clearDatastore: true,
173 clientOptions: {},
174 clusterNodes: []
175});
176
177group.on('created', (limiter, key) => {
178 assert(limiter.empty())
179 assert(key.length > 0)
180})
181
182group.key("foo").submit(withCb, 2, () => {}, (err, result) => {
183 let s: string = `${result} foo`;
184 console.log(s);
185 assert(s == "cb 2 foo");
186});
187
188group.key("bar").submit({ priority: 4 }, withCb, 3, () => {}, (err, result) => {
189 let s: string = `${result} bar`;
190 console.log(s);
191 assert(s == "cb 3 foo");
192});
193
194let f1: Promise<string> = group.key("pizza").schedule(withPromise, 2, () => {});
195f1.then(function (result: string) {
196 let s: string = result;
197 console.log(s);
198 assert(s == "promise 2");
199});
200
201let f2: Promise<string> = group.key("pie").schedule({ priority: 4 }, withPromise, 3, () => {});
202f2.then(function (result: string) {
203 let s: string = result;
204 console.log(s);
205 assert(s == "promise 3");
206});
207
208let wrapped = limiter.wrap((a: number, b: number) => {
209 let s: string = `Total: ${a + b}`;
210 return Promise.resolve(s);
211});
212
213wrapped(1, 2).then((x) => {
214 let s: string = x;
215 console.log(s);
216 assert(s == "Total: 3");
217});
218
219wrapped.withOptions({ priority: 1, id: 'some-id' }, 9, 9).then((x) => {
220 let s: string = x;
221 console.log(s);
222 assert(s == "Total: 18");
223})
224
225let counts = limiter.counts();
226console.log(`${counts.EXECUTING + 2}`);
227console.log(limiter.jobStatus('some-id'))
228console.log(limiter.jobs());
229console.log(limiter.jobs(Bottleneck.Status.RUNNING));
230
231
232group.deleteKey("pizza")
233.then(function (deleted: boolean) {
234 console.log(deleted)
235});
236group.updateSettings({ timeout: 5, maxConcurrent: null, reservoir: null });
237
238let keys: string[] = group.keys();
239assert(keys.length == 3);
240
241group.clusterKeys()
242.then(function (allKeys: string[]) {
243 let count = allKeys.length;
244})
245
246let queued: number = limiter.chain(group.key("pizza")).queued();
247
248limiter.stop({
249 dropWaitingJobs: true,
250 dropErrorMessage: "Begone!",
251 enqueueErrorMessage: "Denied!"
252}).then(() => {
253 console.log('All stopped.')
254})
255
256wrapped(4, 5).catch((e) => {
257 assert(e.message === "Denied!")
258})
259
260const id: string = limiter.id;
261const datastore: string = limiter.datastore;
262const channel: string = limiter.channel();
263
264const redisConnection = new Bottleneck.RedisConnection({
265 client: "NodeRedis client object",
266 clientOptions: {}
267})
268
269redisConnection.ready()
270.then(function (redisConnectionClients) {
271 const client = redisConnectionClients.client;
272 const subscriber = redisConnectionClients.subscriber;
273})
274
275redisConnection.on("error", (err) => {
276 console.log(err.message)
277})
278
279const limiterWithConn = new Bottleneck({
280 connection: redisConnection
281})
282
283const ioredisConnection = new Bottleneck.IORedisConnection({
284 client: "ioredis client object",
285 clientOptions: {},
286 clusterNodes: []
287})
288
289ioredisConnection.ready()
290.then(function (ioredisConnectionClients) {
291 const client = ioredisConnectionClients.client;
292 const subscriber = ioredisConnectionClients.subscriber;
293})
294
295ioredisConnection.on("error", (err: Bottleneck.BottleneckError) => {
296 console.log(err.message)
297})
298
299const groupWithConn = new Bottleneck.Group({
300 connection: ioredisConnection
301})
302
303const limiterWithConnFromGroup = new Bottleneck({
304 connection: groupWithConn.connection
305})
306
307const groupWithConnFromLimiter = new Bottleneck.Group({
308 connection: limiterWithConn.connection
309})
310
311
312const batcher = new Bottleneck.Batcher({
313 maxTime: 1000,
314 maxSize: 10
315})
316
317batcher.on("batch", (batch) => {
318 const len: number = batch.length
319 console.log("Number of elements:", len)
320})
321
322batcher.on("error", (err: Bottleneck.BottleneckError) => {
323 console.log(err.message)
324})
325
326batcher.add("abc")
327batcher.add({ xyz: 5 })
328.then(() => console.log("Flushed!"))
329
330const object = {}
331const emitter = new Bottleneck.Events(object)
332const listenerCount: number = emitter.listenerCount('info')
333emitter.trigger('info', 'hello', 'world', 123).then(function (result) {
334 console.log(result)
335})