-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): add waitForTask in search client (#510)
* WIP * feat(javascript): add waitForTask in search client * chore: export createRetryablePromise * chore: fix return type * fix: change taskID to number * Update templates/javascript/api-single.mustache Co-authored-by: Clément Vannicatte <[email protected]> * provide type and add comment * fix errors * add comments to the type * return nothing from waitForTask Co-authored-by: Clément Vannicatte <[email protected]>
- Loading branch information
1 parent
0835bc8
commit 4f8d355
Showing
7 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
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
86 changes: 86 additions & 0 deletions
86
...h-client-javascript/packages/client-common/src/__tests__/create-retryable-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,86 @@ | ||
import { createRetryablePromise } from '../createRetryablePromise'; | ||
|
||
describe('createRetryablePromise', () => { | ||
it('resolves promise after some retries', async () => { | ||
let calls = 0; | ||
const promise = createRetryablePromise({ | ||
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('gets the rejection of the given promise via reject', async () => { | ||
let calls = 0; | ||
|
||
const promise = createRetryablePromise({ | ||
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 = createRetryablePromise({ | ||
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('gets the rejection when it exceeds the max trial number', async () => { | ||
const MAX_TRIAL = 3; | ||
let calls = 0; | ||
|
||
const promise = createRetryablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve('okay'); | ||
}); | ||
}, | ||
validate: () => false, | ||
maxTrial: MAX_TRIAL, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ | ||
message: 'The maximum number of trials exceeded. (3/3)', | ||
}) | ||
); | ||
expect(calls).toBe(MAX_TRIAL); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
clients/algoliasearch-client-javascript/packages/client-common/src/createRetryablePromise.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,48 @@ | ||
import type { CreateRetryablePromiseOptions } from './types/CreateRetryablePromise'; | ||
|
||
/** | ||
* Return a promise that retry a task until it meets the condition. | ||
* | ||
* @param createRetryablePromiseOptions - The createRetryablePromise options. | ||
* @param createRetryablePromiseOptions.func - The function to run, which returns a promise. | ||
* @param createRetryablePromiseOptions.validate - The validator function. It receives the resolved return of `func`. | ||
* @param createRetryablePromiseOptions.maxTrial - The maximum number of trials. 10 by default. | ||
* @param createRetryablePromiseOptions.timeout - The function to decide how long to wait between tries. | ||
*/ | ||
export function createRetryablePromise<TResponse>({ | ||
func, | ||
validate, | ||
maxTrial = 10, | ||
timeout = (retryCount: number): number => Math.min(retryCount * 10, 1000), | ||
}: CreateRetryablePromiseOptions<TResponse>): Promise<TResponse> { | ||
let retryCount = 0; | ||
const retry = (): Promise<TResponse> => { | ||
return new Promise<TResponse>((resolve, reject) => { | ||
func() | ||
.then((response) => { | ||
const isValid = validate(response); | ||
if (isValid) { | ||
resolve(response); | ||
} else if (retryCount + 1 >= maxTrial) { | ||
reject( | ||
new Error( | ||
`The maximum number of trials exceeded. (${ | ||
retryCount + 1 | ||
}/${maxTrial})` | ||
) | ||
); | ||
} else { | ||
retryCount += 1; | ||
setTimeout(() => { | ||
retry().then(resolve).catch(reject); | ||
}, timeout(retryCount)); | ||
} | ||
}) | ||
.catch((error) => { | ||
reject(error); | ||
}); | ||
}); | ||
}; | ||
|
||
return retry(); | ||
} |
18 changes: 18 additions & 0 deletions
18
...lgoliasearch-client-javascript/packages/client-common/src/types/CreateRetryablePromise.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,18 @@ | ||
export type CreateRetryablePromiseOptions<TResponse> = { | ||
/** | ||
* The function to run, which returns a promise. | ||
*/ | ||
func: () => Promise<TResponse>; | ||
/** | ||
* The validator function. It receives the resolved return of `func`. | ||
*/ | ||
validate: (response: TResponse) => boolean; | ||
/** | ||
* The maximum number of trials. 10 by default. | ||
*/ | ||
maxTrial?: number; | ||
/** | ||
* The function to decide how long to wait between tries. | ||
*/ | ||
timeout?: (retryCount: number) => number; | ||
}; |
1 change: 1 addition & 0 deletions
1
clients/algoliasearch-client-javascript/packages/client-common/src/types/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './Cache'; | ||
export * from './CreateClient'; | ||
export * from './CreateRetryablePromise'; | ||
export * from './Host'; | ||
export * from './Requester'; | ||
export * from './Transporter'; |
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