Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RxObservableQuery async #13

Merged
merged 1 commit into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### vNEXT

- Use `lodash` instead of individual packages [PR #14](https://github.com/kamilkisiela/apollo-client-rxjs/pull/14)
- Made every `RxObservableQuery` asynchronous [PR #13](https://github.com/kamilkisiela/apollo-client-rxjs/pull/13)

### v0.2.3

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"test-watch": "mocha --reporter spec --full-trace build/tests/index.js --watch",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha -- --reporter dot --full-trace build/tests/index.js",
"postcoverage": "remap-istanbul --input coverage/coverage.json --type lcovonly --output coverage/lcov.info",
"lint": "tslint -e src/*.ts && tslint tests/*.ts",
"lint": "tslint src/**/*.ts && tslint tests/*.ts",
"prebuild": "npm run clean:build",
"build": "tsc",
"watch": "tsc -w",
Expand Down
13 changes: 10 additions & 3 deletions src/rxify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ApolloClient, ApolloQueryResult, ObservableQuery } from 'apollo-client';
import { observeOn } from 'rxjs/operator/observeOn';
import { AsyncAction } from 'rxjs/scheduler/AsyncAction';
import { AsyncScheduler } from 'rxjs/scheduler/AsyncScheduler';

import { createWithObservableVariables } from './utils/variables';
import { RxObservableQuery } from './RxObservableQuery';
Expand Down Expand Up @@ -30,17 +33,21 @@ export function rxify(
throw new Error('Use ApolloClient or a function that returns ObservableQuery');
}

function wrapAsync<T>(obs: RxObservableQuery<T>): RxObservableQuery<T> {
return observeOn.call(obs, new AsyncScheduler(AsyncAction));
}

function wrapWatchQuery(
watchQuery: WatchQueryFn
): WatchQueryRxFn {
return (options) => {
if (typeof options.variables === 'object') {
return createWithObservableVariables(
return wrapAsync(createWithObservableVariables(
options,
(newOptions) => watchQuery(newOptions)
);
));
}

return new RxObservableQuery(watchQuery(options));
return wrapAsync(new RxObservableQuery(watchQuery(options)));
};
}