Skip to content

Instantly share code, notes, and snippets.

@rc1
Last active June 23, 2017 17:01
Show Gist options
  • Save rc1/dd7a25d2ce0af5daf95acbd2e6fdfcc3 to your computer and use it in GitHub Desktop.
Save rc1/dd7a25d2ce0af5daf95acbd2e6fdfcc3 to your computer and use it in GitHub Desktop.
UniRx's Reactive Property for JavaScript/RxJS
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