Skip to content

Instantly share code, notes, and snippets.

@icfantv
Last active May 27, 2016 01:47
Show Gist options
  • Save icfantv/2aca3eeeedae2768ed6454dbf699e2bd to your computer and use it in GitHub Desktop.
Save icfantv/2aca3eeeedae2768ed6454dbf699e2bd to your computer and use it in GitHub Desktop.
Subject Observer Example
import {Observer} from 'rxjs/Observer';
import {Subject} from 'rxjs/Subject';
export class FilterService {
private subject: Subject = new Subject();
private _name: string;
private _tags: CollectionMatcher = {
values: [],
allMustMatch: false
};
get name(): string {
return this._name;
}
set name(name: string) {
this._name = name;
this.subject.next(null);
}
get tags(): CollectionMatcher {
return this._tags;
}
set tags(item: CollectionMatcher) {
this._tags = item;
this.subject.next(null);
}
subscribe(observer: Observer): ISubscription {
return this.subject.subscribe(observer);
}
}
class ObserverImpl implements Observer {
public next(result: any) {
console.log(`result: ${result}`);
};
public error(err: any) {
console.log(`error: ${err}`);
};
public complete() {
console.log('Completed');
}
}
import {FilterService} from './FilterService';
import {ObserverImpl} from './ObserverImpl';
export class SomeClass {
private subscription: ISubscription;
constructor(private filterService: FilterService, private $scope: any) {
this.subscription = this.filterService.subscribe(new ObserverImpl());
$scope.$on('$destroy', () => this.subscription.unsubscribe();
}
/* moar stuff */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment