Skip to content

Instantly share code, notes, and snippets.

@MikaAK
Forked from icfantv/FilterService.ts
Last active May 27, 2016 01:55
Show Gist options
  • Save MikaAK/ccd57f28640564e8f50c17a449fe221b to your computer and use it in GitHub Desktop.
Save MikaAK/ccd57f28640564e8f50c17a449fe221b to your computer and use it in GitHub Desktop.
Subject Observer Example
import {Observer} from 'rxjs/Observer';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
interface IFilter {
name: string,
tags: CollectionMatcher
}
export class FilterService {
private _filters = new BehaviorSubject<IFilter>({
name: '',
tags: {
values: [],
allMustMatch: false
}
})
public changeFilters(filters) {
this._filters.next(filters)
}
public getFilters(prop?: string): BehaviorSubject<IFilter> {
if (prop)
return this._filters.map((filters: IFilter) => _.get(filters, prop))
else
return this._filters
}
public subscribe(fn) {
return this._filters.subscribe(fn)
}
}
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