Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Angular2Apollo): support for Rx.Observable
- Loading branch information
Kamil Kisiela
committed
Jul 20, 2016
1 parent
09f7227
commit d38cbcd
Showing
8 changed files
with
168 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
Observable, | ||
} from 'rxjs/Observable'; | ||
|
||
import { | ||
Operator, | ||
} from 'rxjs/Operator'; | ||
|
||
import { | ||
Subscriber, | ||
} from 'rxjs/Subscriber'; | ||
|
||
import { | ||
Subscription, | ||
} from 'rxjs/Subscription'; | ||
|
||
export class ApolloQueryObservable<T> extends Observable<T> { | ||
constructor(public apollo: any, subscribe?: <R>(subscriber: Subscriber<R>) => Subscription | Function | void) { | ||
super(subscribe); | ||
} | ||
|
||
public lift<T, R>(operator: Operator<T, R>): Observable<R> { | ||
const observable = new ApolloQueryObservable<R>(this.apollo); | ||
|
||
observable.source = this; | ||
observable.operator = operator; | ||
|
||
return observable; | ||
} | ||
|
||
public refetch(variables?: any): Promise<any> { | ||
return this.apollo.refetch(variables); | ||
} | ||
|
||
public stopPolling(): void { | ||
return this.apollo.stopPolling(); | ||
} | ||
|
||
public startPolling(p: number): void { | ||
return this.apollo.startPolling(p); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
Observable, | ||
} from 'rxjs/Observable'; | ||
|
||
import { | ||
ApolloQueryObservable, | ||
} from '../src/apolloQueryObservable'; | ||
|
||
import 'rxjs/add/operator/map'; | ||
|
||
class ObservableQuery<T> extends Observable<T> { | ||
refetch(v?: any) {} | ||
stopPolling() {} | ||
startPolling(n?: any) {} | ||
} | ||
|
||
describe('ApolloQueryObservable', () => { | ||
it('should be able to call refetch()', () => { | ||
const obs = new ObservableQuery(); | ||
const res = new ApolloQueryObservable(obs); | ||
const variables = { foo: true }; | ||
|
||
spyOn(obs, 'refetch').and.returnValue('refetch'); | ||
|
||
expect(res.refetch(variables)).toEqual('refetch'); | ||
expect(obs.refetch).toHaveBeenCalledWith(variables); | ||
}); | ||
|
||
it('should be able to call stopPolling()', () => { | ||
const obs = new ObservableQuery(); | ||
const res = new ApolloQueryObservable(obs); | ||
|
||
spyOn(obs, 'stopPolling').and.returnValue('stopPolling'); | ||
|
||
expect(res.stopPolling()).toEqual('stopPolling'); | ||
expect(obs.stopPolling).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should be able to call custom method after operator', () => { | ||
const obs = new ObservableQuery(); | ||
spyOn(obs, 'refetch').and.returnValue('refetch'); | ||
|
||
const res = new ApolloQueryObservable(obs).map((i) => i); | ||
|
||
|
||
expect(res['refetch']()).toEqual('refetch'); | ||
expect(obs.refetch).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains 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