Skip to content

Commit

Permalink
fix: potential memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
chidg committed Dec 11, 2024
1 parent 9880d9f commit 89cca3b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/extension/src/ui/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/ui/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default interface MessageTypes {
newPw: string,
newPwConfirm: string,
cb: (val: ChangePasswordStatusUpdate) => void,
) => Promise<boolean>
) => UnsubscribeFn
checkPassword: (password: string) => Promise<boolean>
authStatus: () => Promise<LoggedinType>
authStatusSubscribe: (cb: (val: LoggedinType) => void) => UnsubscribeFn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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<void>((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 })
Expand Down

0 comments on commit 89cca3b

Please sign in to comment.