-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wait for plan selection (#1547)
# Add `wait()` Method to `AccountPlan` Class ## Summary This PR introduces a new `wait()` method to the `AccountPlan` class, which simplifies the process of waiting for a payment plan to be selected. By integrating configurable polling intervals, timeouts, and abort signals, the method abstracts the common logic required in this scenario, making it easier for developers to implement and customize their workflows. ## Changes - **New `wait()` Method**: - The `wait()` method repeatedly checks the account's payment plan status at a given interval until: - A valid plan is selected. - The timeout is reached. - The operation is aborted using an `AbortSignal`. - Available options: - `interval` (default: 1000ms): Sets the polling interval. - `timeout` (default: 15 minutes): Defines the maximum wait time. - `signal`: An optional `AbortSignal` to allow for cancellation. - This method improves the developer experience by abstracting the repetitive logic required to wait for a payment plan and offering flexibility through configurable options. ## How to Test 1. Run the test suite with `npm run build && npm run test`. 2. Ensure all existing tests pass. 3. Verify that the new test cases for the `wait()` method in `account.test.js` are passing, including tests for: - Successful payment plan selection. - Timeout errors. - Aborted operations.
- Loading branch information
Showing
3 changed files
with
127 additions
and
7 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
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 |
---|---|---|
|
@@ -235,6 +235,46 @@ export class AccountPlan { | |
}) | ||
} | ||
|
||
/** | ||
* Waits for a payment plan to be selected. | ||
* This method continuously checks the account's payment plan status | ||
* at a specified interval until a valid plan is selected, or when the timeout is reached, | ||
* or when the abort signal is aborted. | ||
* | ||
* @param {object} [options] | ||
* @param {number} [options.interval=1000] - The polling interval in milliseconds (default is 1000ms). | ||
Check warning on line 245 in packages/w3up-client/src/account.js GitHub Actions / Test (18)
Check warning on line 245 in packages/w3up-client/src/account.js GitHub Actions / Test (20)
|
||
* @param {number} [options.timeout=900000] - The maximum time to wait in milliseconds before throwing a timeout error (default is 15 minutes). | ||
Check warning on line 246 in packages/w3up-client/src/account.js GitHub Actions / Test (18)
Check warning on line 246 in packages/w3up-client/src/account.js GitHub Actions / Test (20)
|
||
* @param {AbortSignal} [options.signal] - An optional AbortSignal to cancel the waiting process. | ||
* @returns {Promise<import('@web3-storage/access').PlanGetSuccess>} - Resolves once a payment plan is selected within the timeout. | ||
* @throws {Error} - Throws an error if there is an issue retrieving the payment plan or if the timeout is exceeded. | ||
*/ | ||
async wait(options) { | ||
const startTime = Date.now() | ||
const interval = options?.interval || 1000 // 1 second | ||
const timeout = options?.timeout || 60 * 15 * 1000 // 15 minutes | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const res = await this.get() | ||
if (res.ok) return res.ok | ||
|
||
if (res.error) { | ||
throw new Error(`Error retrieving payment plan: ${res.error}`) | ||
} | ||
|
||
if (Date.now() - startTime > timeout) { | ||
throw new Error('Timeout: Payment plan selection took too long.') | ||
} | ||
|
||
if (options?.signal?.aborted) { | ||
throw new Error('Aborted: Payment plan selection was aborted.') | ||
} | ||
|
||
console.log('Waiting for payment plan to be selected...') | ||
await new Promise((resolve) => setTimeout(resolve, interval)) | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {import('@web3-storage/access').AccountDID} accountDID | ||
|
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