@tim-code/my-util 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/promise.js CHANGED
@@ -44,11 +44,14 @@ export function poll({ ms, wait = false, attempts = undefined }, callback) {
44
44
 
45
45
  /**
46
46
  * Sleep for X milliseconds.
47
- * @param {number} milliseconds
47
+ * @param {number} ms Milliseconds; returns immediately if negative
48
48
  */
49
- export async function sleep(milliseconds) {
49
+ export async function sleep(ms) {
50
+ if (ms < 0) {
51
+ return
52
+ }
50
53
  await new Promise((resolve) => {
51
- setTimeout(resolve, milliseconds)
54
+ setTimeout(resolve, ms)
52
55
  })
53
56
  }
54
57
 
@@ -63,12 +66,14 @@ export async function sleep(milliseconds) {
63
66
  * - "errors": The "reason" property for each "result" object that did not have a status of "fulfilled".
64
67
  * @param {Object} $1
65
68
  * @param {Array} $1.array
66
- * @param {number=} $1.limit If not provided, each call is done in parallel.
69
+ * @param {number=} $1.limit The number of calls to do in parallel. If not provided, each call is done in parallel.
70
+ * @param {Function=} $1.limiter A function awaited after a group of parallel calls is processed.
71
+ * It is called with the number of parallel calls processed. Could be as simple as `() => sleep(10000)` if you wanted to wait 10 seconds between.
67
72
  * @param {boolean=} $1.flatten Flattens values before returning; useful if promises return arrays
68
73
  * @param {Function} callback
69
74
  * @returns {Object} {results, values, returned, errors}
70
75
  */
71
- export async function allSettled({ array, limit, flatten = false }, callback) {
76
+ export async function allSettled({ array, limit, limiter, flatten = false }, callback) {
72
77
  const results = []
73
78
  let returned = []
74
79
  let values = []
@@ -87,6 +92,7 @@ export async function allSettled({ array, limit, flatten = false }, callback) {
87
92
  errors.push(reason)
88
93
  }
89
94
  }
95
+ await limiter?.(elements.length)
90
96
  }
91
97
  if (flatten) {
92
98
  values = values.flat()
@@ -95,6 +101,28 @@ export async function allSettled({ array, limit, flatten = false }, callback) {
95
101
  return { values, returned, errors, results }
96
102
  }
97
103
 
104
+ /**
105
+ * Creates a function that can be used with allSettled to limit the number of elements processed in a time interval.
106
+ * Once the limit is reached, waits until the start of a new interval before returning.
107
+ * @param {Object} $1
108
+ * @param {number} $1.limit The maximum number of elements to be processed in the interval
109
+ * @param {Function=} $1.interval The length of the interval in milliseconds. Default is one minute.
110
+ * @returns {Function} Returned function expects to be called with the number of elements added since last call.
111
+ */
112
+ export function intervalLimiter({ limit, interval = 1000 * 60 }) {
113
+ let count = 0
114
+ let startTimestamp = Date.now()
115
+ return async (added) => {
116
+ count += added
117
+ if (count >= limit) {
118
+ const currentTimestamp = Date.now()
119
+ await sleep(interval - (currentTimestamp - startTimestamp))
120
+ startTimestamp = Date.now()
121
+ count = 0
122
+ }
123
+ }
124
+ }
125
+
98
126
  /**
99
127
  * A convenience method to throw the result of allSettled().
100
128
  * Useful in testing contexts when simply propagating the error is enough.
@@ -2,7 +2,7 @@
2
2
  /* eslint-disable prefer-promise-reject-errors */
3
3
  import { jest } from "@jest/globals"
4
4
 
5
- import { alert, allSettled, poll, PollError, sleep, throwFirstReject } from "./promise.js"
5
+ import { alert, allSettled, poll, PollError, sleep, throwFirstReject, intervalLimiter } from "./promise.js"
6
6
 
7
7
  describe("poll", () => {
8
8
  it("resolves immediately if callback returns a non-undefined/null/false value", async () => {
@@ -104,6 +104,14 @@ describe("sleep", () => {
104
104
  // Allow for some jitter
105
105
  expect(after - before).toBeGreaterThanOrEqual(5)
106
106
  })
107
+
108
+ it("resolves immediately if ms is negative", async () => {
109
+ const before = Date.now()
110
+ const promise = sleep(-10)
111
+ await expect(promise).resolves.toBeUndefined()
112
+ const after = Date.now()
113
+ expect(after - before).toBeLessThan(5)
114
+ })
107
115
  })
108
116
 
109
117
  describe("allSettled", () => {
@@ -154,6 +162,60 @@ describe("allSettled", () => {
154
162
  expect(result.errors).toEqual([])
155
163
  expect(result.results).toEqual([])
156
164
  })
165
+
166
+ it("calls limiter after each chunk if provided", async () => {
167
+ const arr = [1, 2, 3, 4, 5]
168
+ const calls = []
169
+ const limiterCalls = []
170
+ const cb = (x) => {
171
+ calls.push(x)
172
+ return x
173
+ }
174
+ const limiter = jest.fn(async (n) => {
175
+ limiterCalls.push(n)
176
+ // simulate async delay
177
+ await sleep(1)
178
+ })
179
+ const result = await allSettled({ array: arr, limit: 2, limiter }, cb)
180
+ expect(result.values).toEqual([1, 2, 3, 4, 5])
181
+ expect(calls).toEqual([1, 2, 3, 4, 5])
182
+ expect(limiter).toHaveBeenCalledTimes(3)
183
+ expect(limiterCalls).toEqual([2, 2, 1])
184
+ })
185
+ })
186
+
187
+ describe("intervalLimiter", () => {
188
+ it("does not delay until limit is reached", async () => {
189
+ const limiter = intervalLimiter({ limit: 3, interval: 10 })
190
+ const before = Date.now()
191
+ await limiter(1)
192
+ await limiter(1)
193
+ await limiter(1) // should reach limit here, triggers wait
194
+ const after = Date.now()
195
+ expect(after - before).toBeGreaterThanOrEqual(10)
196
+ })
197
+
198
+ it("resets count and interval after waiting", async () => {
199
+ const limiter = intervalLimiter({ limit: 2, interval: 5 })
200
+ const before = Date.now()
201
+ await limiter(1)
202
+ await limiter(1) // triggers wait
203
+ const afterFirst = Date.now()
204
+ await limiter(1)
205
+ await limiter(1) // triggers wait again
206
+ const afterSecond = Date.now()
207
+ expect(afterFirst - before).toBeGreaterThanOrEqual(5)
208
+ expect(afterSecond - afterFirst).toBeGreaterThanOrEqual(5)
209
+ })
210
+
211
+ it("handles added < limit with no delay", async () => {
212
+ const limiter = intervalLimiter({ limit: 10, interval: 5 })
213
+ const before = Date.now()
214
+ await limiter(3)
215
+ await limiter(3)
216
+ const after = Date.now()
217
+ expect(after - before).toBeLessThan(5)
218
+ })
157
219
  })
158
220
 
159
221
  describe("alert", () => {