Skip to content

Commit

Permalink
Allow using a query and cursor together when calling client.feed() (#298
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ecooper authored Nov 4, 2024
1 parent 19458fe commit 1c347b8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
40 changes: 40 additions & 0 deletions __tests__/integration/feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,46 @@ describe("Client", () => {
expect(pages).toHaveLength(3);
expect(Array.from(pages[0].events)).toHaveLength(1);
});

it("can resume from a cursor using a query", async () => {
// Query fragment to reuse in the feed
const query = fql<EventSource>`FeedTest.all().eventSource()`;

// First document, which we don't want to include in the feed, but we'll
// use its transaction timestamp to resume from after we create our test
// documents.
const startAt = (await client.query(fql`FeedTest.create({ value: 1})`))
.txn_ts;

// Create a second batch of documents to include in the feed
await client.query(
fql`Set.sequence(0, 3).forEach(v => FeedTest.create({ value: v + 1}));`,
);

// Create a feed that will resume from the transaction timestamp of the
// first document we created above.
const feed = client.feed(query, {
...defaultFeedConfig,
page_size: 1,
start_ts: startAt,
});

// Get the first page of events from the feed
const firstPage = await feed[Symbol.asyncIterator]().next();

// Create a second feed that will resume from the cursor of the first page
const feedWithCursor = client.feed(query, {
...defaultFeedConfig,
cursor: firstPage.value.cursor,
});

// Get the second page of events from the feed
const pages = await fromAsync(feedWithCursor);

// We should get a single page with 2 events in it
expect(pages).toHaveLength(1);
expect(Array.from(pages[0].events)).toHaveLength(2);
});
});

describe("FeedClient", () => {
Expand Down
9 changes: 0 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,6 @@ export class Client {
...options,
};

if (
clientConfiguration.cursor !== undefined &&
tokenOrQuery instanceof Query
) {
throw new ClientError(
"The `cursor` configuration can only be used with a stream token.",
);
}

const tokenOrGetToken =
tokenOrQuery instanceof Query
? () => this.query<EventSource>(tokenOrQuery).then((res) => res.data)
Expand Down

0 comments on commit 1c347b8

Please sign in to comment.