Skip to content

Instantly share code, notes, and snippets.

@akatakritos
Created February 14, 2020 16:06
Show Gist options
  • Save akatakritos/acf26036f105e9657cb069ce7b5a924d to your computer and use it in GitHub Desktop.
Save akatakritos/acf26036f105e9657cb069ce7b5a924d to your computer and use it in GitHub Desktop.
angular.js rxjs misc
/** AngularJS function to unsubscribe from observables when scope destroyed.
* @param $scope - Scope that is listening to an observable.
*/
export function registerDestroyedObservable($scope: any) {
if($scope.destroyed$) {
return;
}
$scope.destroyed$ = new Subject();
$scope.$on('$destroy', () => {
$scope.destroyed$.next();
$scope.destroyed$.complete();
});
}
export function takeUntilScopeDestroyed<T>($scope: any) {
registerDestroyedObservable($scope);
return takeUntil<T>($scope.destroyed$);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment