-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add internal implementation of toSignal for State observable
- Loading branch information
1 parent
513b8ea
commit 0ed9f47
Showing
5 changed files
with
39 additions
and
9 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,28 @@ | ||
import { signal, Signal } from '@angular/core'; | ||
import { StateObservable } from './state'; | ||
|
||
/** | ||
* Get the current value of an `StateObservable` as a reactive `Signal`. | ||
* | ||
* `toSignal` returns a `Signal` which provides synchronous reactive access to values produced | ||
* by the `StateObservable`, by subscribing to that `StateObservable`. The returned `Signal` will always | ||
* have the most recent value emitted by the subscription, and will throw an error if the | ||
* `StateObservable` errors. | ||
* | ||
* The subscription will last for the lifetime of the application itself. | ||
* | ||
* This function is for internal use only as it differs from the `toSignal` | ||
* provided by the `@angular/core/rxjs-interop` package with relying on | ||
* the injection context to unsubscribe from the provided observable. | ||
* | ||
*/ | ||
export function toSignal<T, U>(source: StateObservable): Signal<T | U> { | ||
let state = signal<T | U>(undefined as T); | ||
|
||
source.subscribe({ | ||
next: (value) => state.set(value), | ||
error: (error) => state.set(error), | ||
}); | ||
|
||
return state.asReadonly(); | ||
} |
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