Skip to content

Commit

Permalink
fix(extension): debounce on CIP-30 endpoint calls now starts after fi…
Browse files Browse the repository at this point in the history
…rst event
  • Loading branch information
AngelCastilloB committed Oct 2, 2024
1 parent 828a18c commit e85915f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
82 changes: 47 additions & 35 deletions apps/browser-extension-wallet/src/lib/scripts/background/cip30.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,54 @@ export const confirmationCallback: walletCip30.CallbackConfirmation = {
// Also transactions can be submitted by the dApps externally
// once they've got the witnesss keys if they construct their own transactions
Promise.resolve(true),
signTx: pDebounce(async () => {
try {
const tab = await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/sign-tx');
const ready = await userPromptService.readyToSignTx();
if (!ready) return false;
return cancelOnTabClose(tab);
} catch (error) {
console.error(error);
return Promise.reject(new ApiError(APIErrorCode.InternalError, 'Unable to sign transaction'));
}
}, DEBOUNCE_THROTTLE),
signData: pDebounce(async () => {
try {
const tab = await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/sign-data');
const ready = await userPromptService.readyToSignData();
if (!ready) return false;
return cancelOnTabClose(tab);
} catch (error) {
console.error(error);
// eslint-disable-next-line unicorn/no-useless-undefined
return Promise.reject(new ApiError(APIErrorCode.InternalError, 'Unable to sign data'));
}
}, DEBOUNCE_THROTTLE),
getCollateral: pDebounce(async (args) => {
try {
const dappInfo = await senderToDappInfo(args.sender);
dappSetCollateral$.next({ dappInfo, collateralRequest: args.data });
await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/set-collateral');
signTx: pDebounce(
async () => {
try {
const tab = await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/sign-tx');
const ready = await userPromptService.readyToSignTx();
if (!ready) return false;
return cancelOnTabClose(tab);
} catch (error) {
console.error(error);
return Promise.reject(new ApiError(APIErrorCode.InternalError, 'Unable to sign transaction'));
}
},
DEBOUNCE_THROTTLE,
{ before: true }
),
signData: pDebounce(
async () => {
try {
const tab = await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/sign-data');
const ready = await userPromptService.readyToSignData();
if (!ready) return false;
return cancelOnTabClose(tab);
} catch (error) {
console.error(error);
// eslint-disable-next-line unicorn/no-useless-undefined
return Promise.reject(new ApiError(APIErrorCode.InternalError, 'Unable to sign data'));
}
},
DEBOUNCE_THROTTLE,
{ before: true }
),
getCollateral: pDebounce(
async (args) => {
try {
const dappInfo = await senderToDappInfo(args.sender);
dappSetCollateral$.next({ dappInfo, collateralRequest: args.data });
await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/set-collateral');

return userPromptService.getCollateralRequest();
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-undefined
dappSetCollateral$.next(undefined);
throw new Error(error);
}
}, DEBOUNCE_THROTTLE)
return userPromptService.getCollateralRequest();
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-undefined
dappSetCollateral$.next(undefined);
throw new Error(error);
}
},
DEBOUNCE_THROTTLE,
{ before: true }
)
};

const walletApi = walletCip30.createWalletApi(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ export const requestAccess: RequestAccess = async (sender: Runtime.MessageSender
return Promise.resolve(true);
};

export const requestAccessDebounced = pDebounce(requestAccess, DEBOUNCE_THROTTLE);
export const requestAccessDebounced = pDebounce(requestAccess, DEBOUNCE_THROTTLE, { before: true });

0 comments on commit e85915f

Please sign in to comment.