From 89cca3bc2e00c7002abf8f935aa0dfa37a633e74 Mon Sep 17 00:00:00 2001 From: Chid Gilovitz Date: Wed, 11 Dec 2024 13:09:30 +0800 Subject: [PATCH] fix: potential memory leak --- apps/extension/src/ui/api/api.ts | 2 +- apps/extension/src/ui/api/types.ts | 2 +- .../Settings/ChangePasswordPage/index.tsx | 29 ++++++++++++------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/apps/extension/src/ui/api/api.ts b/apps/extension/src/ui/api/api.ts index 69ab527796..9b43d51860 100644 --- a/apps/extension/src/ui/api/api.ts +++ b/apps/extension/src/ui/api/api.ts @@ -19,7 +19,7 @@ export const api: MessageTypes = { changePassword: (currentPw, newPw, newPwConfirm) => messageService.sendMessage("pri(app.changePassword)", { currentPw, newPw, newPwConfirm }), changePasswordSubscribe: (currentPw, newPw, newPwConfirm, cb) => - messageService.sendMessage( + messageService.subscribe( "pri(app.changePassword.subscribe)", { currentPw, newPw, newPwConfirm }, cb, diff --git a/apps/extension/src/ui/api/types.ts b/apps/extension/src/ui/api/types.ts index 0270240bf4..b35d35cced 100644 --- a/apps/extension/src/ui/api/types.ts +++ b/apps/extension/src/ui/api/types.ts @@ -83,7 +83,7 @@ export default interface MessageTypes { newPw: string, newPwConfirm: string, cb: (val: ChangePasswordStatusUpdate) => void, - ) => Promise + ) => UnsubscribeFn checkPassword: (password: string) => Promise authStatus: () => Promise authStatusSubscribe: (cb: (val: LoggedinType) => void) => UnsubscribeFn diff --git a/apps/extension/src/ui/apps/dashboard/routes/Settings/ChangePasswordPage/index.tsx b/apps/extension/src/ui/apps/dashboard/routes/Settings/ChangePasswordPage/index.tsx index b62ad36cae..114e6dd312 100644 --- a/apps/extension/src/ui/apps/dashboard/routes/Settings/ChangePasswordPage/index.tsx +++ b/apps/extension/src/ui/apps/dashboard/routes/Settings/ChangePasswordPage/index.tsx @@ -92,17 +92,24 @@ const Content = () => { const subscribeChangePassword = useCallback( async ({ currentPw, newPw, newPwConfirm }: FormData) => { // sets up a custom promise, resolving when the password change is done or there is an error - return await new Promise((resolve, reject) => - api.changePasswordSubscribe(currentPw, newPw, newPwConfirm, ({ status, message }) => { - setProgress(status) - if (status === ChangePasswordStatusUpdateStatus.ERROR) { - reject(new Error(message)) - } - if (status === ChangePasswordStatusUpdateStatus.DONE) { - resolve() - } - }), - ).catch((err) => { + return await new Promise((resolve, reject) => { + const unsub = api.changePasswordSubscribe( + currentPw, + newPw, + newPwConfirm, + ({ status, message }) => { + setProgress(status) + if (status === ChangePasswordStatusUpdateStatus.ERROR) { + unsub() + reject(new Error(message)) + } + if (status === ChangePasswordStatusUpdateStatus.DONE) { + unsub() + resolve() + } + }, + ) + }).catch((err) => { switch (err.message) { case "Incorrect password": setError("currentPw", { message: err.message })