-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): provide
createIterablePromise
helper
- Loading branch information
Showing
15 changed files
with
372 additions
and
190 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 +1 @@ | ||
0.0.11 | ||
0.0.12 |
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
2 changes: 1 addition & 1 deletion
2
clients/algoliasearch-client-javascript/packages/client-common/index.ts
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
220 changes: 220 additions & 0 deletions
220
...ch-client-javascript/packages/client-common/src/__tests__/create-iterable-promise.test.ts
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 |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import { createIterablePromise } from '../createIterablePromise'; | ||
|
||
describe('createIterablePromise', () => { | ||
describe('validate', () => { | ||
it('iterates on a `func` until `validate` is met', async () => { | ||
let calls = 0; | ||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 3, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #3'); | ||
expect(calls).toBe(3); | ||
}); | ||
|
||
it('forward the response of the `func`', async () => { | ||
let calls = 0; | ||
const promise = createIterablePromise<number>({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(calls); | ||
}); | ||
}, | ||
validate: (response) => response >= 3, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual(3); | ||
expect(calls).toBe(3); | ||
}); | ||
}); | ||
|
||
describe('aggregator', () => { | ||
it('is called before iterating', async () => { | ||
let calls = 0; | ||
let count = 0; | ||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 3, | ||
aggregator: () => (count += 3), | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #3'); | ||
expect(calls).toBe(3); | ||
expect(count).toBe(3 * 3); | ||
}); | ||
|
||
it('forward the response of the `func`', async () => { | ||
let calls = 0; | ||
const responses: string[] = []; | ||
const promise = createIterablePromise<string>({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 3, | ||
aggregator: (response) => { | ||
responses.push(response); | ||
}, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #3'); | ||
expect(calls).toBe(3); | ||
expect(responses).toEqual(['success #1', 'success #2', 'success #3']); | ||
}); | ||
}); | ||
|
||
describe('timeout', () => { | ||
it('defaults to no timeout (0)', async () => { | ||
let calls = 0; | ||
const before = Date.now(); | ||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 2, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #2'); | ||
|
||
expect(Date.now() - before).toBeGreaterThanOrEqual(0); | ||
expect(Date.now() - before).toBeLessThan(10); | ||
expect(calls).toBe(2); | ||
}); | ||
|
||
it('waits before calling the `func` again', async () => { | ||
let calls = 0; | ||
const before = Date.now(); | ||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 2, | ||
timeout: () => 2000, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #2'); | ||
|
||
expect(Date.now() - before).toBeGreaterThanOrEqual(2000); | ||
expect(Date.now() - before).toBeLessThan(2010); | ||
expect(calls).toBe(2); | ||
}); | ||
}); | ||
|
||
describe('error', () => { | ||
it('gets the rejection of the given promise via reject', async () => { | ||
let calls = 0; | ||
|
||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve, reject) => { | ||
calls += 1; | ||
if (calls <= 3) { | ||
resolve('okay'); | ||
} else { | ||
reject(new Error('nope')); | ||
} | ||
}); | ||
}, | ||
validate: () => false, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ message: 'nope' }) | ||
); | ||
}); | ||
|
||
it('gets the rejection of the given promise via throw', async () => { | ||
let calls = 0; | ||
|
||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
if (calls <= 3) { | ||
resolve('okay'); | ||
} else { | ||
throw new Error('nope'); | ||
} | ||
}); | ||
}, | ||
validate: () => false, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ message: 'nope' }) | ||
); | ||
}); | ||
|
||
it('rejects with the given `message` when `validate` hits', async () => { | ||
const MAX_RETRIES = 3; | ||
let calls = 0; | ||
|
||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve('okay'); | ||
}); | ||
}, | ||
validate: () => false, | ||
error: { | ||
validate: () => calls >= MAX_RETRIES, | ||
message: () => `Error is thrown: ${calls}/${MAX_RETRIES}`, | ||
}, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ | ||
message: 'Error is thrown: 3/3', | ||
}) | ||
); | ||
expect(calls).toBe(MAX_RETRIES); | ||
}); | ||
|
||
it('forward the response of the `func`', async () => { | ||
const MAX_RETRIES = 3; | ||
let calls = 0; | ||
|
||
const promise = createIterablePromise<number>({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(calls); | ||
}); | ||
}, | ||
validate: () => false, | ||
error: { | ||
validate: (response) => response >= MAX_RETRIES, | ||
message: (response) => `Error is thrown: ${response}/${MAX_RETRIES}`, | ||
}, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ | ||
message: 'Error is thrown: 3/3', | ||
}) | ||
); | ||
expect(calls).toBe(MAX_RETRIES); | ||
}); | ||
}); | ||
}); |
86 changes: 0 additions & 86 deletions
86
...h-client-javascript/packages/client-common/src/__tests__/create-retryable-promise.test.ts
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
clients/algoliasearch-client-javascript/packages/client-common/src/createIterablePromise.ts
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { CreateIterablePromise } from './types/CreateIterablePromise'; | ||
|
||
/** | ||
* Helper: Returns the promise of a given `func` to iterate on, based on a given `validate` condition. | ||
* | ||
* @param createIterator - The createIterator options. | ||
* @param createIterator.func - The function to run, which returns a promise. | ||
* @param createIterator.validate - The validator function. It receives the resolved return of `func`. | ||
* @param createIterator.aggregator - The function that runs right after the `func` method has been executed, allows you to do anything with the response before `validate`. | ||
* @param createIterator.error - The `validate` condition to throw an error, and its message. | ||
* @param createIterator.timeout - The function to decide how long to wait between iterations. | ||
*/ | ||
export function createIterablePromise<TResponse>({ | ||
func, | ||
validate, | ||
aggregator, | ||
error, | ||
timeout = (): number => 0, | ||
}: CreateIterablePromise<TResponse>): Promise<TResponse> { | ||
const retry = (): Promise<TResponse> => { | ||
return new Promise<TResponse>((resolve, reject) => { | ||
func() | ||
.then((response) => { | ||
if (aggregator) { | ||
aggregator(response); | ||
} | ||
|
||
if (validate(response)) { | ||
return resolve(response); | ||
} | ||
|
||
if (error && error.validate(response)) { | ||
return reject(new Error(error.message(response))); | ||
} | ||
|
||
return setTimeout(() => { | ||
retry().then(resolve).catch(reject); | ||
}, timeout()); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
}; | ||
|
||
return retry(); | ||
} |
Oops, something went wrong.