Last active
June 23, 2017 17:01
-
-
Save rc1/dd7a25d2ce0af5daf95acbd2e6fdfcc3 to your computer and use it in GitHub Desktop.
UniRx's Reactive Property for JavaScript/RxJS
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 { Subject } from 'rxjs/Subject'; | |
import { Subscription } from 'rxjs/Subscription'; | |
import { Observable } from 'rxjs/Observable'; | |
export class ReactiveProperty extends Subject { | |
constructor( v ) { | |
super(); | |
var value = v; | |
this.notifyOnSubscribe = arguments.length > 0; | |
Object.defineProperty( this, 'value', { | |
get: () => value, | |
set: function ( v ) { | |
value = v; | |
this.next( v ); | |
} | |
}); | |
} | |
_subscribe( subscriber ) { | |
var result = super._subscribe( subscriber ); | |
if ( result != Subscription.EMPTY && this.notifyOnSubscribe ) { | |
subscriber.next( this.value ); | |
} | |
return result; | |
} | |
} | |
export default ReactiveProperty; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment