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

Add info event to propagate protocol-level events #43

Merged
merged 1 commit into from
Jul 31, 2024

Conversation

palkan
Copy link
Member

@palkan palkan commented Jul 23, 2024

Context

Our extended protocol supports automatic stream history retrieval but we do not provide any APIs to react on the corresponding events (more precisely, protocol messages: confirm_history and reject_history).

This PR introduces a new info event for cables and channels that can be used to signal arbitrary protocol-level events to users, including history_not_found and history_received events:

import { createCable, Channel } from '@anycable/web'

const cable = createCable({protocol: 'actioncable-v1-ext-json'});

const ch = cable.subscribeTo("ChatChannel");

ch.on("info", (evt) => {
  if (evt.type === "history_not_found") {
    // Restore state by performing an action
    ch.perform("resetState")
    
    // Or hard-reset the clients by reloading the page
    window.location.reload();
  }

  // Successful history retrieval is also notified
  if (evt.type === "history_received") {
    // ...
  }
});

@palkan palkan added the enhancement New feature or request label Jul 23, 2024
Copy link
Contributor

@cmdoptesc cmdoptesc left a comment

Choose a reason for hiding this comment

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

Thanks for this speedy enhancement!

@@ -19,11 +19,18 @@ type ConnectEvent = Partial<{
reconnect: boolean
}>

export type InfoEvent = {
type: string
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it safe to assume this can actually just be:

Suggested change
type: string
type: 'info'

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not 'info', but a protocol-level event ('history_received' or 'history_not_found').

The idea is that we allow the protocol to propagate anything up to the cable and channels, so the possible event types are not known beforehand.

We can probably add some generics and TS magic to make the InfoEvent.typed inferred from the Cable.protocol, but... let's leave it as a potential contribution for some TS enthusiast :)

@@ -32,11 +32,17 @@ type ConnectEvent = Partial<{
reconnect: boolean
}>

export type InfoEvent = {
type: string
Copy link
Contributor

Choose a reason for hiding this comment

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

And here, could we possibly use a string union type to be more specific?

Suggested change
type: string
type: 'history_received' | 'history_not_found'

@palkan palkan merged commit 2908314 into master Jul 31, 2024
4 checks passed
@palkan palkan deleted the feat/protocol-notification-event branch July 31, 2024 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants