Skip to content

Commit cd72748

Browse files
authored
Merge pull request #17 from nicolas-van/2.0
Major redesign
2 parents 543af9f + f011f48 commit cd72748

File tree

94 files changed

+2187
-2668
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+2187
-2668
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ Or use [jsDelivr](https://www.jsdelivr.com/package/npm/modern-async) to get the
3535
## Usage
3636

3737
```javascript
38-
import { map, sleep } from 'modern-async'
38+
import { asyncMap, asyncSleep } from 'modern-async'
3939

4040
const array = [1, 2, 3]
41-
const result = await map(array, async (v) => {
42-
await sleep(10)
41+
const result = await asyncMap(array, async (v) => {
42+
await asyncSleep(10)
4343
return v * 2
4444
})
4545
console.log(result)

modern-async.d.ts

Lines changed: 83 additions & 153 deletions
Large diffs are not rendered by default.

src/Deferred.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* internally and can be useful to code other asynchronous helpers.
77
*
88
* @example
9-
* import { Deferred, sleep } from 'modern-async'
9+
* import { Deferred, asyncSleep } from 'modern-async'
1010
*
1111
* const deferred = new Deferred()
1212
*
13-
* sleep(10).then(() => {
13+
* asyncSleep(10).then(() => {
1414
* deferred.resolve('test')
1515
* })
1616
*

src/Delayer.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import delay from './delay.mjs'
2+
import asyncDelay from './asyncDelay.mjs'
33
import assert from 'nanoassert'
44

55
/**
@@ -15,7 +15,7 @@ import assert from 'nanoassert'
1515
* at the end of every loop. `checkDelay()` will check the amount of time that ellasped since the last time
1616
* it triggered a new task in the event loop. If the amount of time is below the trigger time it returns
1717
* an already resolved promise and the remaining computation will be able to continue processing in a
18-
* microtask. If not it will call the `delay()` function that will retrigger the operation in a later task
18+
* microtask. If not it will call the `asyncDelay()` function that will retrigger the operation in a later task
1919
* of the event loop.
2020
*
2121
* @example
@@ -69,14 +69,14 @@ class Delayer {
6969

7070
/**
7171
* Checks if a delay must be applied according to the internal timer. If that's the case this method
72-
* will call `delay()` and return `true`. If not it will do nothing and return `false`.
72+
* will call `asyncDelay()` and return `true`. If not it will do nothing and return `false`.
7373
*
7474
* @returns {boolean} `true` if a new task was scheduled in the event loop, `false` otherwise.
7575
*/
7676
async checkDelay () {
7777
const current = new Date().getTime()
7878
if (current - this._last >= this.triggerTime) {
79-
await delay()
79+
await asyncDelay()
8080
this.reset()
8181
return true
8282
} else {

src/Queue.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import queueMicrotask from './queueMicrotask.mjs'
1515
* Once a task is completed, its corresponding promise is terminated accordingly.
1616
*
1717
* @example
18-
* import { Queue, sleep } from 'modern-async'
18+
* import { Queue, asyncSleep } from 'modern-async'
1919
*
2020
* const queue = new Queue(3) // create a queue with concurrency 3
2121
*
@@ -25,7 +25,7 @@ import queueMicrotask from './queueMicrotask.mjs'
2525
* for (const i of array) {
2626
* promises.push(queue.exec(async () => {
2727
* console.log(`Starting task ${i}`)
28-
* await sleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
28+
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
2929
* console.log(`Ending task ${i}`)
3030
* return i;
3131
* }))

src/Queue.test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Queue from './Queue.mjs'
44
import Deferred from './Deferred.mjs'
55
import CancelledError from './CancelledError.mjs'
66
import { range } from 'itertools'
7-
import delay from './delay.mjs'
7+
import asyncDelay from './asyncDelay.mjs'
88

99
test('Queue base 1', async () => {
1010
const queue = new Queue(1)
@@ -565,7 +565,7 @@ test('Queue concurrency 1 priority', async () => {
565565
})
566566

567567
test('Queue all resolve in micro tasks', async () => {
568-
const del = delay()
568+
const del = asyncDelay()
569569
const queue = new Queue(1)
570570
const ps = []
571571
ps.push(queue.exec(async () => {}))
@@ -580,7 +580,7 @@ test('Queue all resolve in micro tasks', async () => {
580580
})
581581

582582
test('Queue infinity all resolve in micro tasks', async () => {
583-
const del = delay()
583+
const del = asyncDelay()
584584
const queue = new Queue(Number.POSITIVE_INFINITY)
585585
const ps = []
586586
ps.push(queue.exec(async () => {}))

src/Scheduler.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import sleepCancellable from './sleepCancellable.mjs'
2+
import asyncSleepCancellable from './asyncSleepCancellable.mjs'
33
import Queue from './Queue.mjs'
44
import assert from 'nanoassert'
55
import asyncWrap from './asyncWrap.mjs'
@@ -12,22 +12,22 @@ import CancelledError from './CancelledError.mjs'
1212
* tasks. Notably it can limit the concurrency of asynchronous tasks running in parallel.
1313
*
1414
* @example
15-
* import { Scheduler, sleep } from 'modern-async'
15+
* import { Scheduler, asyncSleep } from 'modern-async'
1616
*
1717
* let i = 0
1818
* const scheduler = new Scheduler(async () => {
1919
* const taskNbr = i
2020
* i += 1
2121
* console.log(`Starting task ${taskNbr}`)
22-
* await sleep(10) // waits 10ms
22+
* await asyncSleep(10) // waits 10ms
2323
* console.log(`Ending task ${taskNbr}`)
2424
* }, 100) // a scheduler that triggers every 100ms
2525
* // the default configuration uses a maximum concurrency of 1 and doesn't allow pending
2626
* // tasks, which mean that if a task takes more time to complete than the delay it will be skipped
2727
*
2828
* scheduler.start() // starts the scheduler
2929
*
30-
* await sleep(1000) // waits 1s, during that time the task should trigger ~ 9 times
30+
* await asyncSleep(1000) // waits 1s, during that time the task should trigger ~ 9 times
3131
*
3232
* scheduler.stop() // stops the scheduler
3333
* console.log('Scheduler stopped')
@@ -198,13 +198,13 @@ class Scheduler {
198198
this._nbrTriggering += 1
199199
const nextTime = this._initialTime + (this.delay * this._nbrTriggering)
200200
const currentTime = new Date().getTime()
201-
const [promise, cancel] = sleepCancellable(nextTime - currentTime)
201+
const [promise, cancel] = asyncSleepCancellable(nextTime - currentTime)
202202
this._cancelSleep = cancel
203203
promise.then(() => {
204204
this._triggerTask()
205205
this._scheduleSleep()
206206
}, () => {
207-
// ignore cancelled sleep
207+
// ignore cancelled asyncSleep
208208
})
209209
}
210210

src/Scheduler.test.mjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { expect, test } from '@jest/globals'
3-
import sleepPrecise from './sleepPrecise.mjs'
3+
import asyncSleepPrecise from './asyncSleepPrecise.mjs'
44
import Scheduler, { exceptionHandler } from './Scheduler.mjs'
55
import CancelledError from './CancelledError.mjs'
66
import Deferred from './Deferred.mjs'
@@ -21,7 +21,7 @@ test('Scheduler base', async () => {
2121
scheduler.start()
2222
expect(scheduler.started).toBe(true)
2323
d.resolve()
24-
await sleepPrecise(unit * 1.5 * 3)
24+
await asyncSleepPrecise(unit * 1.5 * 3)
2525
scheduler.stop()
2626
expect(scheduler.started).toBe(false)
2727
expect(callCount).toBeGreaterThanOrEqual(3)
@@ -45,7 +45,7 @@ test('Scheduler multi start stop', async () => {
4545
scheduler.start()
4646
expect(scheduler.started).toBe(true)
4747
d.resolve()
48-
await sleepPrecise(unit * 1.5 * 3)
48+
await asyncSleepPrecise(unit * 1.5 * 3)
4949
scheduler.stop()
5050
expect(scheduler.started).toBe(false)
5151
scheduler.stop()
@@ -71,7 +71,7 @@ test('Scheduler startImmediate', async () => {
7171
scheduler.start()
7272
expect(scheduler.started).toBe(true)
7373
d.resolve()
74-
await sleepPrecise(unit * 1.5 * 3)
74+
await asyncSleepPrecise(unit * 1.5 * 3)
7575
scheduler.stop()
7676
expect(scheduler.started).toBe(false)
7777
expect(callCount).toBeGreaterThanOrEqual(4)
@@ -87,13 +87,13 @@ test('Scheduler concurrency 1', async () => {
8787
expect(scheduler.started).toBe(false)
8888
scheduler.start()
8989
expect(scheduler.started).toBe(true)
90-
await sleepPrecise(100)
90+
await asyncSleepPrecise(100)
9191
d.resolve()
9292
expect(scheduler._queue.pending).toBe(0)
9393
scheduler.stop()
9494
expect(scheduler.started).toBe(false)
9595
expect(callCount).toBe(1)
96-
await sleepPrecise(100)
96+
await asyncSleepPrecise(100)
9797
expect(callCount).toBe(1)
9898
})
9999

@@ -109,14 +109,14 @@ test('Scheduler pending', async () => {
109109
expect(scheduler.started).toBe(false)
110110
scheduler.start()
111111
expect(scheduler.started).toBe(true)
112-
await sleepPrecise(150)
112+
await asyncSleepPrecise(150)
113113
expect(scheduler._queue.pending).toBeGreaterThan(3)
114114
scheduler.stop()
115115
expect(scheduler._queue.pending).toBe(0)
116116
expect(scheduler.started).toBe(false)
117117
expect(callCount).toBe(1)
118118
d.resolve()
119-
await sleepPrecise(150)
119+
await asyncSleepPrecise(150)
120120
expect(callCount).toBe(1)
121121
})
122122

@@ -132,13 +132,13 @@ test('Scheduler concurrency 2', async () => {
132132
expect(scheduler.started).toBe(false)
133133
scheduler.start()
134134
expect(scheduler.started).toBe(true)
135-
await sleepPrecise(150)
135+
await asyncSleepPrecise(150)
136136
expect(scheduler._queue.pending).toBe(0)
137137
scheduler.stop()
138138
expect(scheduler.started).toBe(false)
139139
expect(callCount).toBe(2)
140140
d.resolve()
141-
await sleepPrecise(150)
141+
await asyncSleepPrecise(150)
142142
expect(callCount).toBe(2)
143143
})
144144

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import delayCancellable from './delayCancellable.mjs'
2+
import asyncDelayCancellable from './asyncDelayCancellable.mjs'
33

44
/**
55
* A function returning a promise that will be resolved in a later task of the event loop.
@@ -8,14 +8,14 @@ import delayCancellable from './delayCancellable.mjs'
88
*
99
* @returns {Promise<void>} A promise that will be resolved on a later tick of the event loop.
1010
* @example
11-
* import { delay } from 'modern-async'
11+
* import { asyncDelay } from 'modern-async'
1212
*
1313
* console.log('this executes in a tick of the event loop')
14-
* await delay()
14+
* await asyncDelay()
1515
* console.log('this executes in another tick of the event loop')
1616
*/
17-
async function delay () {
18-
return delayCancellable()[0]
17+
async function asyncDelay () {
18+
return asyncDelayCancellable()[0]
1919
}
2020

21-
export default delay
21+
export default asyncDelay

src/delay.test.mjs renamed to src/asyncDelay.test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
import { test, expect } from '@jest/globals'
3-
import delay from './delay.mjs'
3+
import asyncDelay from './asyncDelay.mjs'
44

5-
test('delay', async () => {
5+
test('asyncDelay', async () => {
66
const events = []
7-
const p = delay().then(() => {
7+
const p = asyncDelay().then(() => {
88
events.push('resolved')
99
})
1010
Promise.resolve().then(() => {

0 commit comments

Comments
 (0)