Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: retry request status #22

Merged
merged 8 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/handlers/wallet.handlers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {retryRequestStatus} from './wallet.handlers';

describe('Wallet handlers', () => {
const testId = '1234_test';

let popup: Window;
let isReady: () => boolean;

beforeEach(() => {
vi.useFakeTimers();

popup = {
postMessage: vi.fn()
} as unknown as Window;
});

afterEach(() => {
vi.useRealTimers();
vi.clearAllMocks();
});

describe('Success', () => {
beforeEach(() => {
let counter = 0;
isReady = vi.fn(() => {
counter++;
return counter > 1;
});
});

it('should call icrc29_status postMessage and returns ready', async () =>
// eslint-disable-next-line @typescript-eslint/return-await
new Promise<void>((resolve) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
retryRequestStatus({popup, isReady, id: testId}).then((result) => {
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(popup.postMessage).toHaveBeenCalledWith(
{
id: testId,
jsonrpc: '2.0',
method: 'icrc29_status'
},
'*'
);

expect(result).toEqual('ready');

resolve();
});

vi.advanceTimersByTime(1000);
peterpeterparker marked this conversation as resolved.
Show resolved Hide resolved
}));
});

describe('Failure', () => {
beforeEach(() => {
isReady = vi.fn(() => false);
});

it('should timeout after 30 seconds', async () =>
// eslint-disable-next-line @typescript-eslint/return-await, no-async-promise-executor, @typescript-eslint/no-misused-promises
new Promise<void>(async (resolve) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
retryRequestStatus({popup, isReady, id: testId}).then((result) => {
expect(result).toEqual('timeout');

resolve();
});

await vi.advanceTimersByTimeAsync(60 * 500);
}));
});
});
31 changes: 31 additions & 0 deletions src/handlers/wallet.handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {ICRC29_STATUS} from '../types/icrc';
import type {IcrcWalletStatusRequestType} from '../types/icrc-requests';
import {JSON_RPC_VERSION_2, type RpcIdType} from '../types/rpc';
import {retryUntilReady} from '../utils/timeout.utils';

export const retryRequestStatus = async ({
popup,
isReady,
id
}: {
popup: Window;
isReady: () => boolean;
id: RpcIdType;
}): Promise<'ready' | 'timeout'> => {
const requestStatus = (): void => {
const msg: IcrcWalletStatusRequestType = {
jsonrpc: JSON_RPC_VERSION_2,
id,
method: ICRC29_STATUS
};

popup.postMessage(msg, '*');
};

return await retryUntilReady({
// TODO: extract a variable and default value for the Wallet
retries: 60, // 30 seconds
peterpeterparker marked this conversation as resolved.
Show resolved Hide resolved
isReady,
fn: requestStatus
});
};