Skip to content

Commit

Permalink
update pagination container API with new fns that take observer
Browse files Browse the repository at this point in the history
Reviewed By: leebyron

Differential Revision: D5728587

fbshipit-source-id: 764aa1d130ded11d322e50f2391907ee5f858f14
  • Loading branch information
Daniel Woelfel authored and facebook-github-bot committed Sep 8, 2017
1 parent 9afd764 commit ef7aa5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
67 changes: 23 additions & 44 deletions packages/react-relay/modern/ReactRelayPaginationContainer.js
Expand Up @@ -30,6 +30,7 @@ const {getComponentName, getReactComponent} = require('RelayContainerUtils');
const {ConnectionInterface, Observable} = require('RelayRuntime');

import type {
ObserverOrCallback,
GeneratedNodeMap,
RefetchOptions,
RelayPaginationProp,
Expand Down Expand Up @@ -295,6 +296,12 @@ function findConnectionMetadata(fragments): ReactConnectionMetadata {
return foundConnectionMetadata || ({}: any);
}

function toObserver(observerOrCallback: ?ObserverOrCallback): Observer<void> {
return typeof observerOrCallback === 'function'
? {error: observerOrCallback, complete: observerOrCallback}
: observerOrCallback || {};
}

function createContainerWithFragments<
TConfig,
TClass: React.ComponentType<TConfig>,
Expand Down Expand Up @@ -544,84 +551,56 @@ function createContainerWithFragments<

_refetchConnection = (
totalCount: number,
callback: ?(error: ?Error) => void,
observerOrCallback: ?ObserverOrCallback,
refetchVariables: ?Variables,
): ?Disposable => {
const observer = this._refetchConnectionSubscription(
totalCount,
{error: callback, complete: callback},
refetchVariables,
);
if (!observer) {
return null;
}
return {dispose: observer.unsubscribe};
};

_refetchConnectionSubscription(
totalCount: number,
observer: ?Observer<RelayResponsePayload>,
refetchVariables: ?Variables,
): ?Subscription {
const paginatingVariables = {
count: totalCount,
cursor: null,
totalCount,
};
return this._fetchPage(
const fetch = this._fetchPage(
paginatingVariables,
observer,
toObserver(observerOrCallback),
{force: true},
refetchVariables,
);
}
return fetch ? {dispose: fetch.unsubscribe} : null;
};

_loadMore = (
pageSize: number,
callback: ?(error: ?Error) => void,
observerOrCallback: ?ObserverOrCallback,
options: ?RefetchOptions,
): ?Disposable => {
const observer = this._loadMoreObserver(
pageSize,
{error: callback, complete: callback},
options,
);
if (!observer) {
return null;
}
return {
dispose: observer.unsubscribe,
};
};

_loadMoreObserver(
pageSize: number,
observer: ?Observer<RelayResponsePayload>,
options: ?RefetchOptions,
): ?Subscription {
const connectionData = this._getConnectionData();
if (!connectionData) {
return null;
}
const totalCount = connectionData.edgeCount + pageSize;
if (options && options.force) {
return this._refetchConnectionSubscription(totalCount, observer);
return this._refetchConnection(totalCount, observerOrCallback);
}
const paginatingVariables = {
count: pageSize,
cursor: connectionData.cursor,
totalCount,
};
return this._fetchPage(paginatingVariables, observer, options);
}
const fetch = this._fetchPage(
paginatingVariables,
toObserver(observerOrCallback),
options,
);
return fetch ? {dispose: fetch.unsubscribe} : null;
};

_fetchPage(
paginatingVariables: {
count: number,
cursor: ?string,
totalCount: number,
},
observer: ?Observer<RelayResponsePayload>,
observer: Observer<void>,
options: ?RefetchOptions,
refetchVariables: ?Variables,
): ?Subscription {
Expand Down Expand Up @@ -730,7 +709,7 @@ function createContainerWithFragments<
payload =>
new Observable(sink => {
onNext(payload, () => {
sink.next(payload);
sink.next(); // pass void to public observer's `next`
sink.complete();
});
}),
Expand Down
7 changes: 5 additions & 2 deletions packages/react-relay/modern/ReactRelayTypes.js
Expand Up @@ -15,6 +15,7 @@

import type {Disposable} from 'RelayCombinedEnvironmentTypes';
import type {GraphQLTaggedNode} from 'RelayModernGraphQLTag';
import type {Observer} from 'RelayObservable';
import type {Environment} from 'RelayStoreTypes';
import type {RerunParam, Variables} from 'RelayTypes';

Expand All @@ -24,17 +25,19 @@ export type RelayProp = {
environment: Environment,
};

export type ObserverOrCallback = Observer<void> | ((error: ?Error) => void);

export type RelayPaginationProp = RelayProp & {
hasMore: () => boolean,
isLoading: () => boolean,
loadMore: (
pageSize: number,
callback: (error: ?Error) => void,
callbackOrObserver: ?ObserverOrCallback,
options?: RefetchOptions,
) => ?Disposable,
refetchConnection: (
totalCount: number,
callback: (error: ?Error) => void,
callbackOrObserver: ?ObserverOrCallback,
refetchVariables: ?Variables,
) => ?Disposable,
};
Expand Down

0 comments on commit ef7aa5b

Please sign in to comment.