-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
551296c
commit 0c32f56
Showing
7 changed files
with
221 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"extends": [ | ||
"@jedwards1211/eslint-config", "@jedwards1211/eslint-config-flow" | ||
] | ||
], | ||
"env": { | ||
"shared-node-browser": true, | ||
"commonjs": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,92 @@ | ||
/* @flow */ | ||
// @flow | ||
|
||
/* eslint-disable no-console, no-undef */ | ||
console.log('Hello world!') | ||
export type UntilCondition<T> = (error: ?Error, result?: T) => boolean | Promise<boolean> | ||
|
||
export type Poller<T> = Promise<T> & { | ||
cancel(): void; | ||
until(condition: UntilCondition<T>): Poller<T>; | ||
timeout(ms: number): Poller<T>; | ||
} | ||
|
||
export type CallContext<T> = { | ||
attemptNumber: number; | ||
elapsedTime: number; | ||
fail(error: Error): void; | ||
pass(value: T): void; | ||
} | ||
|
||
function poll<T>( | ||
fn: (info: CallContext<T>) => T | Promise<T>, | ||
interval: number | ||
): Poller<T> { | ||
let fail, pass | ||
let attemptNumber = 0 | ||
let until = (error: ?Error, result?: T) => !error | ||
let timeout: ?number | ||
let timeoutId: ?number | ||
let lastError: ?Error | ||
|
||
if (!Number.isFinite(interval) || interval < 0) { | ||
throw new Error(`invalid interval: ${interval}`) | ||
} | ||
|
||
const promise = new Promise((resolve: (value: T) => void, reject: (error: Error) => void) => { | ||
const startTime = Date.now() | ||
|
||
fail = (error: Error) => { | ||
if (timeoutId != null) clearTimeout(timeoutId) | ||
reject(error) | ||
} | ||
pass = (value: T) => { | ||
if (timeoutId != null) clearTimeout(timeoutId) | ||
resolve(value) | ||
} | ||
|
||
async function attempt(): Promise<void> { | ||
let result, error | ||
const now = Date.now() | ||
try { | ||
result = await fn({ | ||
attemptNumber: attemptNumber++, | ||
elapsedTime: now - startTime, | ||
fail, | ||
pass, | ||
}) | ||
} catch (err) { | ||
lastError = error = err | ||
} | ||
|
||
if (await until(error, result)) { | ||
if (error) reject(error) | ||
else resolve((result: any)) | ||
} | ||
else { | ||
const nextTime = now + interval | ||
if (timeout != null && nextTime - startTime > timeout) { | ||
let message = "timed out waiting for polling to succeed" | ||
if (lastError) message += `; last error: ${lastError.stack}` | ||
reject(new Error(message)) | ||
} else { | ||
const delay = Math.max(0, nextTime - Date.now()) | ||
timeoutId = setTimeout(attempt, delay) | ||
} | ||
} | ||
} | ||
|
||
attempt() | ||
}) | ||
|
||
;(promise: any).cancel = () => fail(new Error("polling canceled")) | ||
;(promise: any).until = (condition: UntilCondition<T>) => { | ||
until = condition | ||
return promise | ||
} | ||
;(promise: any).timeout = (ms: number) => { | ||
timeout = ms | ||
return promise | ||
} | ||
|
||
return (promise: any) | ||
} | ||
|
||
module.exports = poll |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,80 @@ | ||
import '../src/index' | ||
// @flow | ||
|
||
describe('test setup', () => { | ||
it('works', () => { | ||
import {describe, it} from 'mocha' | ||
import poll from '../src' | ||
import {expect} from 'chai' | ||
|
||
import type {CallContext} from '../src' | ||
|
||
describe('poll', function () { | ||
this.timeout(2000) | ||
it('throws when interval is missing', async () => { | ||
// $FlowFixMe | ||
expect(() => poll(() => {})).to.throw(Error) | ||
}) | ||
it('throws when interval is NaN', async () => { | ||
expect(() => poll(() => {}, NaN)).to.throw(Error) | ||
}) | ||
it('throws when interval is negative', async () => { | ||
expect(() => poll(() => {}, -1)).to.throw(Error) | ||
}) | ||
it('resolves when condition is met', async () => { | ||
let numAttempts | ||
await poll(({attemptNumber, elapsedTime}: CallContext<void>) => { | ||
numAttempts = attemptNumber + 1 | ||
if (elapsedTime < 250) throw new Error() | ||
}, 100).timeout(1000) | ||
expect(numAttempts).to.equal(4) | ||
}) | ||
it('rejects when condition times out', async () => { | ||
let numAttempts | ||
let error | ||
await poll(({attemptNumber, elapsedTime}: CallContext<void>) => { | ||
numAttempts = attemptNumber + 1 | ||
if (elapsedTime < 500) throw new Error('test!') | ||
}, 100).timeout(250).catch(err => error = err) | ||
expect(numAttempts).to.equal(3) | ||
if (!error) throw new Error('expected error to be thrown') | ||
expect(error.message).to.match(/timed out/i) | ||
expect(error.message).to.match(/last error: Error: test!/i) | ||
}) | ||
it('allows fn to manually pass', async () => { | ||
const result = await poll(({attemptNumber, pass}: CallContext<number>) => { | ||
if (attemptNumber === 3) pass(attemptNumber) | ||
}, 20).until((error, value) => value != null) | ||
expect(result).to.equal(3) | ||
}) | ||
it('allows fn to manually fail', async () => { | ||
let numAttempts | ||
let error | ||
await poll(({attemptNumber, elapsedTime, fail}: CallContext<void>) => { | ||
numAttempts = attemptNumber + 1 | ||
if (elapsedTime < 50) throw new Error() | ||
else fail(new Error('manually failed!')) | ||
}, 20).catch(err => error = err) | ||
expect(numAttempts).to.equal(4) | ||
if (!error) throw new Error('expected error to be thrown') | ||
expect(error.message).to.equal('manually failed!') | ||
}) | ||
it('allows until condition to be overridden', async () => { | ||
const value = await poll(({attemptNumber}: CallContext<number>) => attemptNumber, 20) | ||
.until((error, value) => value === 3) | ||
expect(value).to.equal(3) | ||
}) | ||
it('throws if condition becomes true on error', async () => { | ||
let error | ||
await poll(({attemptNumber}: CallContext<void>) => { | ||
if (attemptNumber === 3) throw new Error('done!') | ||
}, 20).until(error => Boolean(error)).catch(err => error = err) | ||
if (!error) throw new Error('expected error to be thrown') | ||
expect(error.message).to.equal('done!') | ||
}) | ||
it('rejects when canceled', async () => { | ||
let error | ||
const promise = poll(() => { throw new Error() }, 20) | ||
promise.cancel() | ||
await promise.catch(err => error = err) | ||
if (!error) throw new Error('expected error to be thrown') | ||
expect(error.message).to.match(/canceled/) | ||
}) | ||
}) |