UNPKG

5.86 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});
31
32limiter.ready().then(() => { console.log('Ready') });
33limiter.clients().client;
34limiter.disconnect();
35
36limiter.currentReservoir().then(function (x) {
37 if (x != null) {
38 let i: number = x;
39 }
40});
41
42limiter.incrementReservoir(5).then(function (x) {
43 if (x != null) {
44 let i: number = x;
45 }
46});
47
48limiter.running().then(function (x) {
49 let i: number = x;
50});
51
52limiter.done().then(function (x) {
53 let i: number = x;
54});
55
56limiter.submit(withCb, 1, () => {}, (err, result) => {
57 let s: string = result;
58 console.log(s);
59 assert(s == "cb 1");
60});
61
62function withPromise(foo: number, bar: () => void): PromiseLike<string> {
63 let s: string = `promise ${foo}`;
64 return Promise.resolve(s);
65}
66
67let foo: Promise<string> = limiter.schedule(withPromise, 1, () => {});
68foo.then(function (result: string) {
69 let s: string = result;
70 console.log(s);
71 assert(s == "promise 1");
72});
73
74limiter.on("message", (msg) => console.log(msg));
75
76limiter.publish(JSON.stringify({ a: "abc", b: { c: 123 }}));
77
78let group = new Bottleneck.Group({
79 maxConcurrent: 5,
80 minTime: 1000,
81 highWater: 10,
82 strategy: Bottleneck.strategy.LEAK,
83 datastore: "ioredis",
84 clearDatastore: true,
85 clientOptions: {},
86 clusterNodes: []
87});
88
89group.on('created', (limiter, key) => {
90 assert(limiter.empty())
91 assert(key.length > 0)
92})
93
94group.key("foo").submit(withCb, 2, () => {}, (err, result) => {
95 let s: string = `${result} foo`;
96 console.log(s);
97 assert(s == "cb 2 foo");
98});
99
100group.key("bar").submit({ priority: 4 }, withCb, 3, () => {}, (err, result) => {
101 let s: string = `${result} bar`;
102 console.log(s);
103 assert(s == "cb 3 foo");
104});
105
106let f1: Promise<string> = group.key("pizza").schedule(withPromise, 2, () => {});
107f1.then(function (result: string) {
108 let s: string = result;
109 console.log(s);
110 assert(s == "promise 2");
111});
112
113let f2: Promise<string> = group.key("pie").schedule({ priority: 4 }, withPromise, 3, () => {});
114f2.then(function (result: string) {
115 let s: string = result;
116 console.log(s);
117 assert(s == "promise 3");
118});
119
120let wrapped = limiter.wrap((a: number, b: number) => {
121 let s: string = `Total: ${a + b}`;
122 return Promise.resolve(s);
123});
124
125wrapped(1, 2).then((x) => {
126 let s: string = x;
127 console.log(s);
128 assert(s == "Total: 3");
129});
130
131wrapped.withOptions({ priority: 1, id: 'some-id' }, 9, 9).then((x) => {
132 let s: string = x;
133 console.log(s);
134 assert(s == "Total: 18");
135})
136
137let counts = limiter.counts();
138console.log(`${counts.EXECUTING + 2}`);
139console.log(limiter.jobStatus('some-id'))
140console.log(limiter.jobs());
141console.log(limiter.jobs(Bottleneck.Status.RUNNING));
142
143
144group.deleteKey("pizza")
145.then(function (deleted: boolean) {
146 console.log(deleted)
147});
148group.updateSettings({ timeout: 5, maxConcurrent: null, reservoir: null });
149
150let keys: string[] = group.keys();
151assert(keys.length == 3);
152
153group.clusterKeys()
154.then(function (allKeys: string[]) {
155 let count = allKeys.length;
156})
157
158let queued: number = limiter.chain(group.key("pizza")).queued();
159
160limiter.stop({
161 dropWaitingJobs: true,
162 dropErrorMessage: "Begone!",
163 enqueueErrorMessage: "Denied!"
164}).then(() => {
165 console.log('All stopped.')
166})
167
168wrapped(4, 5).catch((e) => {
169 assert(e.message === "Denied!")
170})
171
172const id: string = limiter.id;
173const datastore: string = limiter.datastore;
174const channel: string = limiter.channel();
175
176const redisConnection = new Bottleneck.RedisConnection({
177 client: "NodeRedis client object",
178 clientOptions: {}
179})
180
181redisConnection.ready()
182.then(function (redisConnectionClients) {
183 const client = redisConnectionClients.client;
184 const subscriber = redisConnectionClients.subscriber;
185})
186
187redisConnection.on("error", (err) => {
188 console.log(err.message)
189})
190
191const limiterWithConn = new Bottleneck({
192 connection: redisConnection
193})
194
195const ioredisConnection = new Bottleneck.IORedisConnection({
196 client: "ioredis client object",
197 clientOptions: {},
198 clusterNodes: []
199})
200
201ioredisConnection.ready()
202.then(function (ioredisConnectionClients) {
203 const client = ioredisConnectionClients.client;
204 const subscriber = ioredisConnectionClients.subscriber;
205})
206
207ioredisConnection.on("error", (err: Bottleneck.BottleneckError) => {
208 console.log(err.message)
209})
210
211const groupWithConn = new Bottleneck.Group({
212 connection: ioredisConnection
213})
214
215const limiterWithConnFromGroup = new Bottleneck({
216 connection: groupWithConn.connection
217})
218
219const groupWithConnFromLimiter = new Bottleneck.Group({
220 connection: limiterWithConn.connection
221})
222
223
224const batcher = new Bottleneck.Batcher({
225 maxTime: 1000,
226 maxSize: 10
227})
228
229batcher.on("batch", (batch) => {
230 const len: number = batch.length
231 console.log("Number of elements:", len)
232})
233
234batcher.on("error", (err: Bottleneck.BottleneckError) => {
235 console.log(err.message)
236})
237
238batcher.add("abc")
239batcher.add({ xyz: 5 })
240.then(() => console.log("Flushed!"))
241
242const object = {}
243const emitter = new Bottleneck.Events(object)
244const listenerCount: number = emitter.listenerCount('info')
245emitter.trigger('info', 'hello', 'world', 123).then(function (result) {
246 console.log(result)
247})