-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new helper: effect().
- Loading branch information
Showing
7 changed files
with
98 additions
and
23 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,42 @@ | ||
import { isObservable, Observable, of, Subject, Subscription } from 'rxjs'; | ||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||
import { DestroyRef, inject } from '@angular/core'; | ||
|
||
/** | ||
* This code is copied from NgRx ComponentStore (just a few lines edited to add `takeUntilDestroyed()`) | ||
* Credits: NgRx Team | ||
* https://ngrx.io/ | ||
* Source: https://github.com/ngrx/platform/blob/main/modules/component-store/src/component-store.ts#L382 | ||
* Docs: | ||
* https://ngrx.io/guide/component-store/effect#effect-method | ||
*/ | ||
export function effect< | ||
ProvidedType = void, | ||
OriginType extends | Observable<ProvidedType> | unknown = Observable<ProvidedType>, | ||
ObservableType = OriginType extends Observable<infer A> ? A : never, | ||
ReturnType = ProvidedType | ObservableType extends void | ||
? ( | ||
observableOrValue?: ObservableType | Observable<ObservableType> | ||
) => Subscription | ||
: ( | ||
observableOrValue: ObservableType | Observable<ObservableType> | ||
) => Subscription | ||
>(generator: (origin$: OriginType) => Observable<unknown>): ReturnType { | ||
|
||
const destroyRef = inject(DestroyRef); | ||
const origin$ = new Subject<ObservableType>(); | ||
generator(origin$ as OriginType) | ||
.pipe(takeUntilDestroyed(destroyRef)) | ||
.subscribe(); | ||
|
||
return (( | ||
observableOrValue?: ObservableType | Observable<ObservableType> | ||
): Subscription => { | ||
const observable$ = isObservable(observableOrValue) | ||
? observableOrValue | ||
: of(observableOrValue); | ||
return observable$.pipe(takeUntilDestroyed(destroyRef)).subscribe((value) => { | ||
origin$.next(value as ObservableType); | ||
}); | ||
}) as unknown as ReturnType; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './collection'; | ||
export * from './comparator'; | ||
export * from './status.pipes'; | ||
export * from './effect'; | ||
export type * from './types'; |