-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FI-1512 feat: restore
waitForAllRequestsComplete
action
- Loading branch information
Showing
12 changed files
with
177 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type {Request as PlaywrightRequest} from '@playwright/test'; | ||
|
||
import type {RequestHookContextId} from '../../types/internal'; | ||
|
||
/** | ||
* Get `RequestHookContextId` from Playwright request. | ||
* @internal | ||
*/ | ||
export const getRequestHookContextId = ( | ||
playwrightRequest: PlaywrightRequest, | ||
): RequestHookContextId => { | ||
const request = playwrightRequest as {_guid?: string; url: () => string}; | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
return String(request._guid) as RequestHookContextId; | ||
}; |
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
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,23 @@ | ||
import {processAllRequestsCompletePredicates} from './processAllRequestsCompletePredicates'; | ||
|
||
import type { | ||
RequestHookContextId, | ||
RequestWithUtcTimeInMs, | ||
WaitForEventsState, | ||
} from '../../types/internal'; | ||
|
||
/** | ||
* Adds new request to hash of not complete requests. | ||
* @internal | ||
*/ | ||
export const addNotCompleteRequest = async ( | ||
request: RequestWithUtcTimeInMs, | ||
requestHookContextId: RequestHookContextId, | ||
waitForEventsState: WaitForEventsState, | ||
): Promise<void> => { | ||
const {hashOfNotCompleteRequests} = waitForEventsState; | ||
|
||
hashOfNotCompleteRequests[requestHookContextId] = request; | ||
|
||
await processAllRequestsCompletePredicates(requestHookContextId, waitForEventsState); | ||
}; |
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,26 @@ | ||
import type {RequestHookContextId, WaitForEventsState} from '../../types/internal'; | ||
|
||
/** | ||
* Completes request on getting of its response. | ||
* @internal | ||
*/ | ||
export const completeRequest = ( | ||
requestHookContextId: RequestHookContextId, | ||
waitForEventsState: WaitForEventsState, | ||
): void => { | ||
const {allRequestsCompletePredicates, hashOfNotCompleteRequests} = waitForEventsState; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete hashOfNotCompleteRequests[requestHookContextId]; | ||
|
||
for (const allRequestsCompletePredicateWithPromise of allRequestsCompletePredicates) { | ||
const {requestHookContextIds, setResolveTimeout} = allRequestsCompletePredicateWithPromise; | ||
const requestWasWaited = requestHookContextIds.has(requestHookContextId); | ||
|
||
requestHookContextIds.delete(requestHookContextId); | ||
|
||
if (requestHookContextIds.size === 0 && requestWasWaited) { | ||
setResolveTimeout(); | ||
} | ||
} | ||
}; |
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
46 changes: 46 additions & 0 deletions
46
src/utils/waitForEvents/processAllRequestsCompletePredicate.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,46 @@ | ||
import {E2edError} from '../error'; | ||
|
||
import type { | ||
AllRequestsCompletePredicateWithPromise, | ||
RequestHookContextId, | ||
RequestWithUtcTimeInMs, | ||
} from '../../types/internal'; | ||
|
||
/** | ||
* Processes one `waitForAllRequestsComplete` predicate for new request. | ||
* Returns `true` if the promise was fulfilled, and `false` otherwise. | ||
* @internal | ||
*/ | ||
export const processAllRequestsCompletePredicate = async ( | ||
allRequestsCompletePredicateWithPromise: AllRequestsCompletePredicateWithPromise, | ||
request: RequestWithUtcTimeInMs, | ||
requestHookContextId: RequestHookContextId, | ||
): Promise<boolean> => { | ||
const {clearResolveTimeout, predicate, reject, requestHookContextIds} = | ||
allRequestsCompletePredicateWithPromise; | ||
|
||
try { | ||
const isRequestMatched = await predicate(request); | ||
|
||
if (isRequestMatched !== true) { | ||
return false; | ||
} | ||
|
||
clearResolveTimeout(); | ||
|
||
requestHookContextIds.add(requestHookContextId); | ||
} catch (cause) { | ||
clearResolveTimeout(); | ||
|
||
const error = new E2edError( | ||
'waitForAllRequestsComplete promise rejected due to error in predicate function', | ||
{cause, predicate, request}, | ||
); | ||
|
||
reject(error); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}; |
35 changes: 35 additions & 0 deletions
35
src/utils/waitForEvents/processAllRequestsCompletePredicates.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,35 @@ | ||
import {processAllRequestsCompletePredicate} from './processAllRequestsCompletePredicate'; | ||
|
||
import type {RequestHookContextId, WaitForEventsState} from '../../types/internal'; | ||
|
||
/** | ||
* Processes `waitForAllRequestsComplete` predicates for new request. | ||
* @internal | ||
*/ | ||
export const processAllRequestsCompletePredicates = async ( | ||
requestHookContextId: RequestHookContextId, | ||
waitForEventsState: WaitForEventsState, | ||
): Promise<void> => { | ||
const {allRequestsCompletePredicates, hashOfNotCompleteRequests} = waitForEventsState; | ||
const request = hashOfNotCompleteRequests[requestHookContextId]; | ||
|
||
if (request === undefined) { | ||
return; | ||
} | ||
|
||
const promises = [...allRequestsCompletePredicates].map( | ||
async (allRequestsCompletePredicateWithPromise) => { | ||
const isFulfilled = await processAllRequestsCompletePredicate( | ||
allRequestsCompletePredicateWithPromise, | ||
request, | ||
requestHookContextId, | ||
); | ||
|
||
if (isFulfilled) { | ||
allRequestsCompletePredicates.delete(allRequestsCompletePredicateWithPromise); | ||
} | ||
}, | ||
); | ||
|
||
await Promise.all(promises); | ||
}; |