-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(signal-store): reimplement toObservable with Subject instead of …
…ReplaySubject
- Loading branch information
Showing
4 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Credits go to Angular | ||
// Copied from with small modifications: https://github.com/angular/angular/blob/16.2.10/packages/core/rxjs-interop/src/take_until_destroyed.ts | ||
|
||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { | ||
assertInInjectionContext, | ||
DestroyRef, | ||
effect, | ||
inject, | ||
Injector, | ||
Signal, | ||
untracked, | ||
} from '@angular/core'; | ||
import { ToObservableOptions } from '@angular/core/rxjs-interop'; | ||
import { Observable, Subject } from 'rxjs'; | ||
|
||
// Reimplemented `toObservable` from Angular | ||
// Use Subject instead of ReplaySubject (which is more lightweight and sufficient for internal use in MiniRx) | ||
export function miniRxToObservable<T>( | ||
source: Signal<T>, | ||
options?: ToObservableOptions | ||
): Observable<T> { | ||
!options?.injector && assertInInjectionContext(miniRxToObservable); | ||
const injector = options?.injector ?? inject(Injector); | ||
const subject = new Subject<T>(); | ||
|
||
const watcher = effect( | ||
() => { | ||
let value: T; | ||
try { | ||
value = source(); | ||
} catch (err) { | ||
untracked(() => subject.error(err)); | ||
return; | ||
} | ||
untracked(() => subject.next(value)); | ||
}, | ||
{ injector, manualCleanup: true } | ||
); | ||
|
||
injector.get(DestroyRef).onDestroy(() => { | ||
watcher.destroy(); | ||
subject.complete(); | ||
}); | ||
|
||
return subject.asObservable(); | ||
} |
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