-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(lib): simulate late initialization of events stream on element st…
…rategies
- Loading branch information
Showing
4 changed files
with
48 additions
and
22 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 |
---|---|---|
@@ -1,4 +1,36 @@ | ||
import { ReplaySubject, Observable, ObservedValueOf } from 'rxjs'; | ||
import { switchAll } from 'rxjs/operators'; | ||
|
||
/** @internal */ | ||
export function isDefined<T>(value: T): value is NonNullable<T> { | ||
return value !== null && value !== undefined; | ||
} | ||
|
||
/** | ||
* Allows to defer initialization of the stream | ||
* by creating initial stream before hand and intercept the value | ||
* once it's available in setter and switch to it from initial stream | ||
* | ||
* @internal | ||
*/ | ||
export function maybeLateInitStream< | ||
T, | ||
P extends keyof T, | ||
R = ObservedValueOf<T[P]> | ||
>(object: T, prop: P): Observable<R> { | ||
if (object[prop]) { | ||
return object[prop] as any; | ||
} | ||
|
||
const setValue$ = new ReplaySubject<Observable<R>>(1); | ||
const value$ = setValue$.pipe(switchAll()); | ||
|
||
Object.defineProperty(object, prop, { | ||
configurable: true, | ||
enumerable: true, | ||
get: () => value$, | ||
set: (value) => setValue$.next(value), | ||
}); | ||
|
||
return value$; | ||
} |
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