Skip to content

Commit

Permalink
cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
amandajliu authored and Sashko Stubailo committed Jul 11, 2016
1 parent 7269ca3 commit 2685c38
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 62 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API.

### vNEXT
- moved refetch(), startPolling(), and stopPolling() methods from QuerySubscription to ObservableQuery [Issue #194] [PR #362]
- moved refetch(), startPolling(), and stopPolling() methods from QuerySubscription to ObservableQuery [Issue #194] (https://github.com/apollostack/apollo-client/issues/194) and [PR #362] (https://github.com/apollostack/apollo-client/pull/362)

- Don't throw on unknown directives, instead just pass them through. This can open the door to implementing `@live`, `@defer`, and `@stream`, if coupled with some changes in the network layer. [PR #372](https://github.com/apollostack/apollo-client/pull/372)

Expand Down
80 changes: 48 additions & 32 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,24 @@ import {
QueryScheduler,
} from './scheduler';

import { Observable, Observer, Subscription } from './util/Observable';
import { Observable, Observer, Subscription, SubscriberFunction } from './util/Observable';

export class ObservableQuery extends Observable<GraphQLResult> {
public subscribe(observer: Observer<GraphQLResult>): QuerySubscription {
return super.subscribe(observer) as QuerySubscription;
public refetch: (variables?: any) => Promise<GraphQLResult>;
public stopPolling: () => void;
public startPolling: (p: number) => void;

constructor(subscriberFunction: SubscriberFunction<GraphQLResult>,
refetch: (variables?: any) => Promise<GraphQLResult>,
stopPolling: () => void, startPolling: (p: number) => void) {
super(subscriberFunction);
this.refetch = refetch;
this.stopPolling = stopPolling;
this.startPolling = startPolling;

}
public subscribe(observer: Observer<GraphQLResult>): Subscription {
return super.subscribe(observer) as Subscription;
}


Expand All @@ -91,9 +104,6 @@ export class ObservableQuery extends Observable<GraphQLResult> {
}
}

export interface QuerySubscription extends Subscription {
}

export interface WatchQueryOptions {
query: Document;
variables?: { [key: string]: any };
Expand Down Expand Up @@ -134,7 +144,7 @@ export class QueryManager {
// with them in case of some destabalizing action (e.g. reset of the Apollo store).
private observableQueries: { [queryId: string]: {
observableQuery: ObservableQuery;
subscriptions: QuerySubscription[];
subscriptions: Subscription[];
} };

constructor({
Expand Down Expand Up @@ -308,6 +318,33 @@ export class QueryManager {
// Call just to get errors synchronously
getQueryDefinition(options.query);
const queryId = this.generateQueryId();

const refetch = (variables?: any) => {
// If no new variables passed, use existing variables
variables = variables || options.variables;

// Use the same options as before, but with new variables and forceFetch true
return this.fetchQuery(queryId, assign(options, {
forceFetch: true,
variables,
}) as WatchQueryOptions);
};

const stopPolling = () => {
if (this.pollingTimers[queryId]) {
clearInterval(this.pollingTimers[queryId]);
}
};

const startPolling = (pollInterval) => {
this.pollingTimers[queryId] = setInterval(() => {
const pollingOptions = assign({}, options) as WatchQueryOptions;
// subsequent fetches from polling always reqeust new data
pollingOptions.forceFetch = true;
this.fetchQuery(queryId, pollingOptions);
}, pollInterval);
};

const observableQuery = new ObservableQuery((observer) => {
const retQuerySubscription = {
unsubscribe: () => {
Expand Down Expand Up @@ -360,32 +397,11 @@ export class QueryManager {
}
}
});

return retQuerySubscription;
},
(variables?: any) => {
// If no new variables passed, use existing variables
variables = variables || options.variables;

// Use the same options as before, but with new variables and forceFetch true
return this.fetchQuery(queryId, assign(options, {
forceFetch: true,
variables,
}) as WatchQueryOptions);
},
() => {
if (this.pollingTimers[queryId]) {
clearInterval(this.pollingTimers[queryId]);
}
},
(pollInterval) => {
this.pollingTimers[queryId] = setInterval(() => {
const pollingOptions = assign({}, options) as WatchQueryOptions;
// subsequent fetches from polling always reqeust new data
pollingOptions.forceFetch = true;
this.fetchQuery(queryId, pollingOptions);
}, pollInterval);
}
refetch,
stopPolling,
startPolling
);

return observableQuery;
Expand Down Expand Up @@ -463,7 +479,7 @@ export class QueryManager {
}

// Associates a query subscription with an ObservableQuery in this.observableQueries
public addQuerySubscription(queryId: string, querySubscription: QuerySubscription) {
public addQuerySubscription(queryId: string, querySubscription: Subscription) {
if (this.observableQueries.hasOwnProperty(queryId)) {
this.observableQueries[queryId].subscriptions.push(querySubscription);
} else {
Expand Down
45 changes: 26 additions & 19 deletions src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ export class QueryScheduler {
throw new Error('Tried to register a non-polling query with the scheduler.');
}
const queryId = this.queryManager.generateQueryId();

const refetch = (variables: any) => {
variables = variables || options.variables;
return this.fetchQuery(queryId, assign(options, {
forceFetch: true,
variables,
}) as WatchQueryOptions);
};

const startPolling = () => {
this.pollingTimers[queryId] = setInterval(() => {
const pollingOptions = assign({}, options) as WatchQueryOptions;
pollingOptions.forceFetch = true;
this.fetchQuery(queryId, pollingOptions).then(() => {
this.removeInFlight(queryId);
});
}, options.pollInterval);
};

const stopPolling = () => {
this.stopPollingQuery(queryId);
};

return new ObservableQuery(
(observer) => {
// "Fire" (i.e. add to the QueryBatcher queue)
Expand All @@ -109,25 +132,9 @@ export class QueryScheduler {
},
};
},
(variables: any) => {
variables = variables || options.variables;
return this.fetchQuery(queryId, assign(options, {
forceFetch: true,
variables,
}) as WatchQueryOptions);
},
() => {
this.pollingTimers[queryId] = setInterval(() => {
const pollingOptions = assign({}, options) as WatchQueryOptions;
pollingOptions.forceFetch = true;
this.fetchQuery(queryId, pollingOptions).then(() => {
this.removeInFlight(queryId);
});
}, options.pollInterval);
},
() => {
this.stopPollingQuery(queryId);
}
refetch,
stopPolling,
startPolling
);
}

Expand Down
11 changes: 1 addition & 10 deletions src/util/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// See https://github.com/zenparsing/es-observable

import * as $$observable from 'symbol-observable';
import { GraphQLResult } from 'graphql';

export type CleanupFunction = () => void;
export type SubscriberFunction<T> = (observer: Observer<T>) => (Subscription | CleanupFunction);
Expand All @@ -12,18 +11,10 @@ function isSubscription(subscription: Function | Subscription): subscription is
}

export class Observable<T> {
public refetch: (variables?: any) => Promise<GraphQLResult>;
public stopPolling: () => void;
public startPolling: (p: number) => void;
private subscriberFunction: SubscriberFunction<T>;

constructor(subscriberFunction: SubscriberFunction<T>,
refetch: (variables?: any) => Promise<GraphQLResult>,
stopPolling: () => void, startPolling: (p: number) => void) {
constructor(subscriberFunction: SubscriberFunction<T>) {
this.subscriberFunction = subscriberFunction;
this.refetch = refetch;
this.stopPolling = stopPolling;
this.startPolling = startPolling;

}

Expand Down

0 comments on commit 2685c38

Please sign in to comment.