redux-saga/effects#ActionPattern TypeScript Examples
The following examples show how to use
redux-saga/effects#ActionPattern.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: sagas.ts From pybricks-code with MIT License | 5 votes |
/**
* Waits for a response action, an error response or timeout, whichever comes
* first.
* @param pattern The action type to wait for.
* @param timeout The timeout in milliseconds.
*/
function* waitForResponse<A extends AnyAction>(
pattern: ActionPattern<A>,
timeout = 500,
): SagaGenerator<A> {
const { response, error, disconnected, timedOut } = yield* race({
response: take(pattern),
error: take(errorResponse),
disconnected: take(didDisconnect),
timedOut: delay(timeout),
});
if (timedOut) {
// istanbul ignore if: this hacks around a hardware/OS issue
if (pattern === (errorResponse as unknown)) {
// It has been observed that sometimes this response is not received
// or gets stuck in the Bluetooth stack until another request is sent.
// So, we ignore the timeout and continue. If there really was a
// problem, then the next request should fail anyway.
console.warn('Timeout waiting for erase response, continuing anyway.');
return eraseResponse(Result.OK) as unknown as A;
}
yield* put(didFailToFinish(FailToFinishReasonType.TimedOut));
yield* disconnectAndCancel();
}
if (error) {
yield* put(
didFailToFinish(FailToFinishReasonType.HubError, HubError.UnknownCommand),
);
yield* disconnectAndCancel();
}
if (disconnected) {
yield* put(didFailToFinish(FailToFinishReasonType.Disconnected));
yield* disconnectAndCancel();
}
defined(response);
return response;
}