Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
5.0 KiB

  1. /// <reference path="bottleneck.d.ts" />
  2. import Bottleneck from "bottleneck";
  3. // import * as assert from "assert";
  4. function assert(b: boolean): void { }
  5. /*
  6. This file is run by scripts/build.sh.
  7. It is used to validate the typings in bottleneck.d.ts.
  8. The command is: tsc --noEmit --strictNullChecks test.ts
  9. This file cannot be run directly.
  10. In order to do that, you must comment out the first line,
  11. and change "bottleneck" to "." on the third line.
  12. */
  13. function withCb(foo: number, bar: () => void, cb: (err: any, result: string) => void) {
  14. let s: string = `cb ${foo}`;
  15. cb(null, s);
  16. }
  17. console.log(Bottleneck);
  18. let limiter = new Bottleneck({
  19. maxConcurrent: 5,
  20. minTime: 1000,
  21. highWater: 20,
  22. strategy: Bottleneck.strategy.LEAK,
  23. reservoirRefreshInterval: 1000 * 60,
  24. reservoirRefreshAmount: 10
  25. });
  26. limiter.ready().then(() => { console.log('Ready') });
  27. limiter.clients().client;
  28. limiter.disconnect();
  29. limiter.currentReservoir().then(function (x) {
  30. if (x != null) {
  31. let i: number = x;
  32. }
  33. });
  34. limiter.incrementReservoir(5).then(function (x) {
  35. if (x != null) {
  36. let i: number = x;
  37. }
  38. });
  39. limiter.running().then(function (x) {
  40. let i: number = x;
  41. });
  42. limiter.done().then(function (x) {
  43. let i: number = x;
  44. });
  45. limiter.submit(withCb, 1, () => {}, (err, result) => {
  46. let s: string = result;
  47. console.log(s);
  48. assert(s == "cb 1");
  49. });
  50. function withPromise(foo: number, bar: () => void): PromiseLike<string> {
  51. let s: string = `promise ${foo}`;
  52. return Promise.resolve(s);
  53. }
  54. let foo: Promise<string> = limiter.schedule(withPromise, 1, () => {});
  55. foo.then(function (result: string) {
  56. let s: string = result;
  57. console.log(s);
  58. assert(s == "promise 1");
  59. });
  60. limiter.on("message", (msg) => console.log(msg));
  61. limiter.publish(JSON.stringify({ a: "abc", b: { c: 123 }}));
  62. let group = new Bottleneck.Group({
  63. maxConcurrent: 5,
  64. minTime: 1000,
  65. highWater: 10,
  66. strategy: Bottleneck.strategy.LEAK,
  67. datastore: "ioredis",
  68. clearDatastore: true,
  69. clientOptions: {},
  70. clusterNodes: []
  71. });
  72. group.on('created', (limiter, key) => {
  73. assert(limiter.empty())
  74. assert(key.length > 0)
  75. })
  76. group.key("foo").submit(withCb, 2, () => {}, (err, result) => {
  77. let s: string = `${result} foo`;
  78. console.log(s);
  79. assert(s == "cb 2 foo");
  80. });
  81. group.key("bar").submit({ priority: 4 }, withCb, 3, () => {}, (err, result) => {
  82. let s: string = `${result} bar`;
  83. console.log(s);
  84. assert(s == "cb 3 foo");
  85. });
  86. let f1: Promise<string> = group.key("pizza").schedule(withPromise, 2, () => {});
  87. f1.then(function (result: string) {
  88. let s: string = result;
  89. console.log(s);
  90. assert(s == "promise 2");
  91. });
  92. let f2: Promise<string> = group.key("pie").schedule({ priority: 4 }, withPromise, 3, () => {});
  93. f2.then(function (result: string) {
  94. let s: string = result;
  95. console.log(s);
  96. assert(s == "promise 3");
  97. });
  98. let wrapped = limiter.wrap((a: number, b: number) => {
  99. let s: string = `Total: ${a + b}`;
  100. return Promise.resolve(s);
  101. });
  102. wrapped(1, 2).then((x) => {
  103. let s: string = x;
  104. console.log(s);
  105. assert(s == "Total: 3");
  106. });
  107. wrapped.withOptions({ priority: 1, id: 'some-id' }, 9, 9).then((x) => {
  108. let s: string = x;
  109. console.log(s);
  110. assert(s == "Total: 18");
  111. })
  112. let counts = limiter.counts();
  113. console.log(`${counts.EXECUTING + 2}`);
  114. console.log(limiter.jobStatus('some-id'))
  115. console.log(limiter.jobs());
  116. console.log(limiter.jobs(Bottleneck.Status.RUNNING));
  117. group.deleteKey("pizza");
  118. group.updateSettings({ timeout: 5, maxConcurrent: null, reservoir: null });
  119. let keys: string[] = group.keys();
  120. assert(keys.length == 3);
  121. let queued: number = limiter.chain(group.key("pizza")).queued();
  122. limiter.stop({
  123. dropWaitingJobs: true,
  124. dropErrorMessage: "Begone!",
  125. enqueueErrorMessage: "Denied!"
  126. }).then(() => {
  127. console.log('All stopped.')
  128. })
  129. wrapped(4, 5).catch((e) => {
  130. assert(e.message === "Denied!")
  131. })
  132. const id: string = limiter.id;
  133. const datastore: string = limiter.datastore;
  134. const channel: string = limiter.channel();
  135. const redisConnection = new Bottleneck.RedisConnection({
  136. client: "NodeRedis client object",
  137. clientOptions: {}
  138. })
  139. redisConnection.on("error", (err) => {
  140. console.log(err.message)
  141. })
  142. const limiterWithConn = new Bottleneck({
  143. connection: redisConnection
  144. })
  145. const ioredisConnection = new Bottleneck.IORedisConnection({
  146. client: "ioredis client object",
  147. clientOptions: {},
  148. clusterNodes: []
  149. })
  150. ioredisConnection.on("error", (err: Bottleneck.BottleneckError) => {
  151. console.log(err.message)
  152. })
  153. const groupWithConn = new Bottleneck.Group({
  154. connection: ioredisConnection
  155. })
  156. const limiterWithConnFromGroup = new Bottleneck({
  157. connection: groupWithConn.connection
  158. })
  159. const groupWithConnFromLimiter = new Bottleneck.Group({
  160. connection: limiterWithConn.connection
  161. })
  162. const batcher = new Bottleneck.Batcher({
  163. maxTime: 1000,
  164. maxSize: 10
  165. })
  166. batcher.on("batch", (batch) => {
  167. const len: number = batch.length
  168. console.log("Number of elements:", len)
  169. })
  170. batcher.on("error", (err: Bottleneck.BottleneckError) => {
  171. console.log(err.message)
  172. })
  173. batcher.add("abc")
  174. batcher.add({ xyz: 5 })
  175. .then(() => console.log("Flushed!"))