Created
February 20, 2018 05:09
-
-
Save amcdnl/9795a413f9df006e92afa50190a446fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { Input } from '@angular/core'; | |
| import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
| export function InputObservable<T>(inputName?: string) { | |
| return (target: object, name: string): void => { | |
| const subject = new BehaviorSubject(this[name]); | |
| if (delete target[name]) { | |
| Object.defineProperty(target, name, { | |
| set(value: T): void { | |
| subject.next(value); | |
| }, | |
| get(): BehaviorSubject<T> { | |
| return subject; | |
| }, | |
| }); | |
| } | |
| Input(inputName)(target, name); | |
| }; | |
| } |
Nice! unfortunately, it will not work. When you initiate the BehaviorSubject the this context is not what you think it is, it's undefined.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clever!
I would replace
return subject;withreturn subject.asObservable();so you don't 'leak' the subject into your class.Perhaps even pipe in a
distictUntillChangedto prevent repeated invocations that might occur.