Skip to content
This repository was archived by the owner on Jan 18, 2019. It is now read-only.

Keep fetching on empty pages #46

Merged
merged 1 commit into from
Oct 11, 2018
Merged
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
31 changes: 25 additions & 6 deletions src/ConnectionLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ const FIRST = '$$FIRST$$';

export default class ConnectionLoader {
constructor(singleConnectionHandler) {
const fetch = async ({ connection, options, ...props }) => {
const result = connection
? await singleConnectionHandler({ connection, options, ...props })
: await singleConnectionHandler({ options, ...props });

if (result.continuationToken && !result.items.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The returned items are not always in a property named "items". Bug 1436212 has some background on why this is hard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every connection loader in taskcluster-web-server returns an items property (https://github.com/taskcluster/taskcluster-web-server/search?utf8=%E2%9C%93&q=items%3A&type=). I guess we can write tests to assert that each connection handler returns an object with an items fields.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, awesome. I didn't know these were results from a loader. One of the downsides of this heavily-functional style of programming is that it's not easy to follow control flow!

return fetch({
...props,
options: {
...options,
continuationToken: result.continuationToken,
},
});
}

return result;
};

return new DataLoader(connections =>
Promise.all(
connections.map(async ({ connection, ...props }) => {
Expand All @@ -22,22 +40,23 @@ export default class ConnectionLoader {
? { limit, continuationToken }
: { limit };
const result = connection
? await singleConnectionHandler({ ...props, connection, options })
: await singleConnectionHandler({ ...props, options });

return this.createPageConnection(result, {
? await fetch({ ...props, connection, options })
: await fetch({ ...props, options });
const pageConnection = this.createPageConnection(result, {
...connection,
...options,
});

return pageConnection;
})
)
);
}

createPageConnection({ continuationToken, items, ...props }, options) {
const pageInfo = {
hasNextPage: !!continuationToken,
hasPreviousPage: !!options.cursor && options.cursor !== FIRST,
hasNextPage: Boolean(continuationToken),
hasPreviousPage: Boolean(options.cursor) && options.cursor !== FIRST,
cursor: options.continuationToken || FIRST,
};

Expand Down