From 32ee928b6c39500fb26b7fc5428710db723b4650 Mon Sep 17 00:00:00 2001 From: rubikscraft Date: Sun, 4 Sep 2022 20:21:57 +0200 Subject: [PATCH] Fix subscription leak --- .../user/register/register.component.ts | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/frontend/src/app/routes/user/register/register.component.ts b/frontend/src/app/routes/user/register/register.component.ts index c56b907..79d23ef 100644 --- a/frontend/src/app/routes/user/register/register.component.ts +++ b/frontend/src/app/routes/user/register/register.component.ts @@ -37,20 +37,7 @@ export class RegisterComponent implements OnInit { history.replaceState(null, ''); } - this.model.username.valueChanges - .pipe(debounceTime(500)) - .subscribe(async (value) => { - if (this.model.username.errors || value === null) return; - - const result = await this.userService.checkNameIsAvailable(value); - if (HasFailed(result)) - return this.errorService.showFailure(result, this.logger); - - if (!result) { - this.model.username.setErrors({ unavailable: true }); - } - }); - + this.subscribeUsernameCheck(); this.onPermissions(); } @@ -101,4 +88,21 @@ export class RegisterComponent implements OnInit { state: this.model.getRawData(), }); } + + @AutoUnsubscribe() + private subscribeUsernameCheck() { + return this.model.username.valueChanges + .pipe(debounceTime(500)) + .subscribe(async (value) => { + if (this.model.username.errors || value === null) return; + + const result = await this.userService.checkNameIsAvailable(value); + if (HasFailed(result)) + return this.errorService.showFailure(result, this.logger); + + if (!result) { + this.model.username.setErrors({ unavailable: true }); + } + }); + } }