Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 3c6f5a8

Browse files
author
Sam Graber
authored
Merge pull request #366 from TheOriginalJosh/de-rxjs
De-RxJS-ing
2 parents 93a4fd9 + 20cf5a0 commit 3c6f5a8

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

source/behaviors/autosave/autosave.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Directive, Input, Self, AfterViewInit, HostListener } from '@angular/core';
2-
import { Observable, Subject } from 'rxjs';
32

43
import { FormComponent } from '../../components/form/form';
54
import { AutosaveActionService } from '../../services/autosaveAction/autosaveAction.service';
@@ -14,12 +13,10 @@ export class AutosaveDirective implements AfterViewInit {
1413
@Input() saveWhenInvalid: boolean;
1514
@HostListener('keyup') keyupListener = this.resetDebounce;
1615

16+
timer: any;
1717
form: FormComponent;
1818
autosaveAction: AutosaveActionService;
1919

20-
autosaveStart$: Subject<void> = new Subject<void>();
21-
autosaveCancel$: Subject<void> = new Subject<void>();
22-
2320
constructor( @Self() form: FormComponent
2421
, autosaveAction: AutosaveActionService) {
2522
this.form = form;
@@ -31,34 +28,34 @@ export class AutosaveDirective implements AfterViewInit {
3128
}
3229

3330
ngOnDestroy(): void {
34-
this.autosaveCancel$.next();
31+
if (this.timer) {
32+
clearTimeout(this.timer);
33+
}
3534
}
3635

3736
setDebounce = (): void => {
3837
if (this.canAutosave()) {
39-
this.autosaveCancel$.next();
40-
this.autosaveStart$.debounceTime(DEFAULT_AUTOSAVE_DEBOUNCE).takeUntil(this.autosaveCancel$).subscribe(() => this.autosave());
41-
this.autosaveStart$.next();
42-
} else {
43-
this.autosaveCancel$.next();
38+
if (!this.timer && this.form.dirty && (this.saveWhenInvalid || this.form.validate())) {
39+
this.timer = setTimeout(this.autosave, DEFAULT_AUTOSAVE_DEBOUNCE)
40+
}
4441
}
4542
}
4643

4744
resetDebounce(): void {
48-
if (this.canAutosave()) {
49-
this.autosaveStart$.next();
45+
if (this.timer && this.canAutosave()) {
46+
clearTimeout(this.timer);
47+
this.timer = null;
48+
this.setDebounce();
5049
}
5150
}
5251

5352
autosave = (): void => {
54-
if (!this.canAutosave()) {
55-
return;
56-
}
57-
58-
const waitOn = this.submitAndWait();
59-
if (waitOn) {
60-
// subscribes to kick off the stream
61-
this.autosaveAction.waitOn(waitOn).subscribe();
53+
if (this.canAutosave()) {
54+
const waitOn = this.submitAndWait();
55+
if (waitOn) {
56+
this.autosaveAction.waitOn(waitOn).subscribe();
57+
}
58+
clearTimeout(this.timer);
6259
}
6360
}
6461

@@ -71,6 +68,6 @@ export class AutosaveDirective implements AfterViewInit {
7168
}
7269

7370
private canAutosave(): boolean {
74-
return this.form.dirty && (this.saveWhenInvalid || this.form.validate());
75-
}
76-
}
71+
return this.form.dirty && (this.saveWhenInvalid || this.form.validate());
72+
}
73+
}

0 commit comments

Comments
 (0)