Skip to content

Commit

Permalink
Add basic interoperability with other Observable implementations (#196)
Browse files Browse the repository at this point in the history
Closes #149.
  • Loading branch information
martijnwalraven committed May 10, 2016
1 parent feb63db commit 829180b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -49,7 +49,8 @@
"lodash.isobject": "^3.0.2",
"lodash.isstring": "^4.0.1",
"lodash.isundefined": "^3.0.1",
"redux": "^3.3.1"
"redux": "^3.3.1",
"symbol-observable": "^0.2.4"
},
"devDependencies": {
"async": "^1.5.2",
Expand Down Expand Up @@ -77,6 +78,7 @@
"tslint": "^3.6.0",
"typescript": "^1.8.9",
"typings": "^0.7.9",
"uglify-js": "^2.6.2"
"uglify-js": "^2.6.2",
"rxjs": "^5.0.0-beta.7"
}
}
6 changes: 6 additions & 0 deletions src/util/Observable.ts
@@ -1,6 +1,8 @@
// This simplified polyfill attempts to follow the ECMAScript Observable proposal.
// See https://github.com/zenparsing/es-observable

import * as $$observable from 'symbol-observable'

export type CleanupFunction = () => void;
export type SubscriberFunction<T> = (observer: Observer<T>) => (Subscription | CleanupFunction);

Expand All @@ -15,6 +17,10 @@ export class Observable<T> {
this.subscriberFunction = subscriberFunction;
}

public [$$observable]() {
return this
}

public subscribe(observer: Observer<T>): Subscription {
let subscriptionOrCleanupFunction = this.subscriberFunction(observer);

Expand Down
55 changes: 55 additions & 0 deletions test/QueryManager.ts
Expand Up @@ -25,6 +25,10 @@ import {
Document,
} from 'graphql';

import * as Rx from 'rxjs';

import assign = require('lodash.assign');

import mockNetworkInterface from './mocks/mockNetworkInterface';

describe('QueryManager', () => {
Expand Down Expand Up @@ -391,6 +395,57 @@ describe('QueryManager', () => {
done();
});

it('supports interoperability with other Observable implementations like RxJS', (done) => {
const query = gql`
query people {
allPeople(first: 1) {
people {
name
}
}
}
`;

const data = {
allPeople: {
people: [
{
name: 'Luke Skywalker',
},
],
},
};

const networkInterface = mockNetworkInterface(
{
request: { query },
result: { data },
}
);

const queryManager = new QueryManager({
networkInterface,
store: createApolloStore(),
reduxRootKey: 'apollo',
});

const handle = queryManager.watchQuery({
query,
});

const observable = Rx.Observable.from(handle);

observable
.map(result => (assign({ fromRx: true }, result)))
.subscribe({
next(result) {
const expectedResult = assign({ fromRx: true }, result);
assert.deepEqual(result, expectedResult);
done();
},
});
});

it('allows you to refetch queries', (done) => {
const query = gql`
query fetchLuke($id: String) {
Expand Down

0 comments on commit 829180b

Please sign in to comment.