Skip to content

Commit

Permalink
Merge 7f57aff into e55170a
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin-Huang committed Jan 8, 2021
2 parents e55170a + 7f57aff commit 032ab27
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ console.log(p2[0]) // 'a'
- [Prray.from(arrayLike)](#prrayfromarraylike)
- [Prray.of(...args)](#prrayofargs)
- [Prray.isPrray(obj)](#prrayisprrayobj)
- [Prray.delay(ms)](#prraydelayms)

#### Prray.from(arrayLike)

Expand Down Expand Up @@ -199,6 +200,22 @@ Prray.isPrray([1, 2, 3]) // false
Prray.isPrray(new Prray(1, 2, 3)) // true
```

#### Prray.delay(ms)

The Prray.delay() method returns a promise (PrrayPromise exactly) that will be resolved after given ms milliseconds.

```javascript
import Prray from 'prray'

await Prray.delay(1000) // resolve after 1 second

const prr = Prray.from([1,2,3])
await prr
.mapAsync(action1)
.delay(500) // delay 500ms between two iteration ations
.forEach(action2)
```

### Specific methods of Prray instance

- [Prray.prototype.toArray()](#prrayprototypetoarray)
Expand Down
9 changes: 9 additions & 0 deletions src/prray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export default class Prray<T> extends Array<T> {
return prr
}

static delay<T>(ms: number): PrrayPromise<T> {
const prray = new Prray<T>()
return new PrrayPromise(resolve => setTimeout(() => resolve(prray), ms))
}

constructor(length: number)
constructor(...args: T[])
constructor(...args: any[]) {
Expand Down Expand Up @@ -195,6 +200,10 @@ export default class Prray<T> extends Array<T> {
toArray(): T[] {
return [...this]
}

delay(ms: number): PrrayPromise<T> {
return new PrrayPromise(resolve => setTimeout(() => resolve(this), ms))
}
}

export function _ensurePrray<T>(arr: T[]): Prray<T> {
Expand Down
5 changes: 5 additions & 0 deletions src/prraypromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ export class PrrayPromise<T> extends Promise<Prray<T>> {
toArray(): Promise<T[]> {
return this.then(prray => prray.toArray())
}

delay(ms: number): PrrayPromise<T> {
const promise = this.then(prray => prray.delay(ms))
return prraypromise(promise)
}
}

export function prraypromise<T>(promise: Promise<Prray<T>>): PrrayPromise<T> {
Expand Down
106 changes: 106 additions & 0 deletions test/delay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import test from 'ava'
import Prray from '../src/prray'
import { PrrayPromise } from '../src/prraypromise'
import { toPrrayPromise, isClose, noop, noopAsync } from './test-utils'

test('prray static delay', async t => {
// delay 0ms
let time = Date.now()
await Prray.delay(0)
t.assert(isClose(time, Date.now(), { threshold: 20 }))

// delay 100ms
time = Date.now()
await Prray.delay(100)
t.assert(isClose(time + 100, Date.now(), { threshold: 20 }))

// delay 1s
time = Date.now()
await Prray.delay(1000)
t.assert(isClose(time + 1000, Date.now(), { threshold: 20 }))

// delay 3s
time = Date.now()
await Prray.delay(3000)
t.assert(isClose(time + 3000, Date.now(), { threshold: 20 }))

// the returned value should be a prraypromise
t.assert(Prray.delay(100) instanceof PrrayPromise)

// the resolved value should be a prray
t.deepEqual(await Prray.delay(100), new Prray())
})

test('prray instance delay', async t => {
const prray = new Prray<number>()

// delay 0s
let time = Date.now()
await prray.delay(0)
t.assert(isClose(time, Date.now(), { threshold: 20 }))

// delay 100ms
time = Date.now()
await prray.delay(100)
t.assert(isClose(time + 100, Date.now(), { threshold: 20 }))

// delay 500ms
time = Date.now()
await prray.delay(500)
t.assert(isClose(time + 500, Date.now(), { threshold: 20 }))

// the resolved value should be the original prray
t.is(await prray.delay(100), prray)
})

test('prraypromise delay', async t => {
const prraypromise = toPrrayPromise([1, 2, 3, 4])

// delay 0ms
let time = Date.now()
await prraypromise.delay(0)
t.assert(isClose(time, Date.now(), { threshold: 20 }))

// delay 100ms
time = Date.now()
await prraypromise.delay(100)
t.assert(isClose(time + 100, Date.now(), { threshold: 20 }))

// delay 500ms
time = Date.now()
await prraypromise.delay(500)
t.assert(isClose(time + 500, Date.now(), { threshold: 20 }))

// the resolved value should be a prray with original items
t.deepEqual(await prraypromise.delay(100), Prray.from([1, 2, 3, 4]))
})

test('delay: real cases', async t => {
// case1: the method chaining of multi-delays should works
let time = Date.now()
await Prray.delay(100)
.delay(100)
.delay(100)
.delay(100)
t.assert(isClose(time + 400, Date.now(), { threshold: 30 }))

// case2: the method chaining of multi-delays should works
const prray = Prray.from([1, 2, 3])
time = Date.now()
await prray
.delay(100)
.delay(100)
.delay(100)
.delay(100)
t.assert(isClose(time + 400, Date.now(), { threshold: 30 }))

// case3: the method chaining with other methods should works
time = Date.now()
await prray
.delay(100)
.map(noop)
.mapAsync(noopAsync)
.delay(100)
.forEach(noop)
t.assert(isClose(time + 200, Date.now(), { threshold: 30 }))
})
8 changes: 8 additions & 0 deletions test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ export function toPrrayPromise<T>(arr: T[]): PrrayPromise<T> {
export function isClose(n1: number, n2: number, opt = { threshold: 100 }): boolean {
return Math.abs(n1 - n2) < opt.threshold
}

export function noop(...args: any[]) {
return
}

export function noopAsync(...args: any[]) {
return new Promise(resolve => resolve(true))
}

0 comments on commit 032ab27

Please sign in to comment.