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

feat(client): Event listeners for both operation modes #84

Merged
merged 5 commits into from
Nov 29, 2023

Conversation

enisdenjo
Copy link
Owner

@enisdenjo enisdenjo commented Nov 29, 2023

Closes #77

Distinct connections mode

Use the on option during client setup for all events. Use the on argument for each subscription (in subscribe and iterate) for focused events.

const client = createClient({
  singleConnection: false,
  on: {
    connecting(reconnecting) {
      if (reconnecting) {
        // reconnection pending
        // ⚠️ event will be repeated for each subscription
      }
    },
    connected(reconnected) {
      if (reconnected) {
        // successful reconnection
        // ⚠️ event will be repeated for each subscription
      }
    },
  },
});

client.subscribe(request, sink, {
  connecting(reconnecting) {
    if (reconnecting) {
      // reconnection pending for this specific subscription
    }
  },
  connected(reconnected) {
    if (reconnected) {
      // successful reconnection for this specific subscription
    }
  },
});

// or

const iterable = client.iterate(request, {
  connecting(reconnecting) {
    if (reconnecting) {
      // reconnection pending for this specific subscription
    }
  },
  connected(reconnected) {
    if (reconnected) {
      // successful reconnection for this specific subscription
    }
  },
});

Single connection mode

Use the on option during client setup.

createClient({
  singleConnection: true,
  on: {
    connecting(reconnecting) {
      if (reconnecting) {
        // reconnection pending
      }
    },
    connected(reconnected) {
      if (reconnected) {
        // successful reconnection
      }
    },
  },
});

client.subscribe(request, sink, {
  connecting() {
    // ⚠️ will NOT be called in "single connection mode"
  },
  connected() {
    // ⚠️ will NOT be called in "single connection mode"
  },
});

// or

const interable = client.iterate(request, {
  connecting() {
    // ⚠️ will NOT be called in "single connection mode"
  },
  connected() {
    // ⚠️ will NOT be called in "single connection mode"
  },
});

@enisdenjo enisdenjo merged commit 6274f44 into master Nov 29, 2023
6 checks passed
@enisdenjo enisdenjo deleted the event-listeners branch November 29, 2023 17:36
enisdenjo pushed a commit that referenced this pull request Nov 29, 2023
# [2.4.0](v2.3.0...v2.4.0) (2023-11-29)

### Bug Fixes

* **client:** Use closures instead of bindings (with `this`) ([8ecdf3c](8ecdf3c))

### Features

* **client:** Event listeners for both operation modes ([#84](#84)) ([6274f44](6274f44))
@enisdenjo
Copy link
Owner Author

🎉 This PR is included in version 2.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@enisdenjo enisdenjo added the released Has been released and published label Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Has been released and published
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant