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

Provide exactly one handler for events and another for fatal errors #234

Merged
merged 2 commits into from
Mar 8, 2024

Conversation

ptpaterson
Copy link
Contributor

@ptpaterson ptpaterson commented Mar 5, 2024

Ticket(s): FE-4969

Small breaking change, but looking for feedback first. Compared to the callback example in #232, this would remove the concept of registering one or more callbacks before the start method. There is no adding or removing of callbacks, and no more no ambiguity about how the async interface should work if you have registered callbacks. Here is a modified example:

import { Client, fql } from "fauna"
const client = new Client({ secret: FAUNA_SECRET })

const stream = client.stream(fql`MyCollection.all().toStream()`)

stream.start(
  function onEvent(event) {
    switch (event.type) {
      case "start":
        console.log("Stream start", event);
        // ...
        break;

      case "update":
      case "add":
      case "remove":
        console.log("Stream update:", event);
        // ...
        break;
    }
  },
  function onFatalError(error) {
    // An error will be handled here if Fauna returns a terminal, "error" event, or
    // if Fauna returns a non-200 response when trying to connect, or
    // if the max number of retries on network errors is reached.

    // ... handle fatal error
  }
);

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ptpaterson
Copy link
Contributor Author

Personally, I like how this harmonizes the sync and async APIs

async

const stream = client.stream(fql`MyCollection.all().toStream()`)

try {
  for await (const event of stream) {
    handleEvent(event)
  }
} catch (error) {
  handleFatalError(error)
}

sync

const stream = client.stream(fql`MyCollection.all().toStream()`)

stream.start(
  handleEvent(event),
  handleFatalError(error)
)

@ptpaterson ptpaterson force-pushed the streaming-fetch-single-callback branch from ab13765 to 8e0bd6d Compare March 6, 2024 01:16
@ptpaterson ptpaterson merged commit 6f10dec into streaming-fetch Mar 8, 2024
5 of 6 checks passed
@ptpaterson ptpaterson deleted the streaming-fetch-single-callback branch March 8, 2024 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant