Created
April 6, 2019 20:27
-
-
Save NetanelBasal/c13cf2460cb38f7dad3904e75b6977e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function dirtyCheck<U>(source: Observable<U>) { | |
let subscription: Subscription; | |
let isDirty = false; | |
return function <T>(valueChanges: Observable<T>): Observable<boolean> { | |
const isDirty$ = combineLatest( | |
source, | |
valueChanges, | |
).pipe( | |
debounceTime(300), | |
map(([a, b]) => { | |
return isDirty = isEqual(a, b) === false; | |
}), | |
finalize(() => subscription.unsubscribe()), | |
startWith(false), | |
shareReplay({ bufferSize: 1, refCount: true }), | |
); | |
subscription = fromEvent(window, 'beforeunload').subscribe(event => { | |
isDirty && (event.returnValue = false); | |
}); | |
return isDirty$; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment