Skip to content

Commit

Permalink
Merge pull request #5109 from rmosolgo/relay-action-cable-unsubscribe…
Browse files Browse the repository at this point in the history
…-once

Make sure ActionCableHandler only unsubscribes once
  • Loading branch information
rmosolgo authored Nov 5, 2024
2 parents f788527 + f27004a commit 9ab852f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type { Consumer } from "@rails/actioncable"

describe("createActionCableHandler", () => {
it("returns a function producing a disposable subscription", () => {
var wasDisposed = false
var wasDisposedCount = 0

var subscription = {
unsubscribe: () => (wasDisposed = true)
unsubscribe: () => (wasDisposedCount += 1)
}
var dummyActionCableConsumer = {
subscriptions: {
Expand All @@ -18,9 +18,12 @@ describe("createActionCableHandler", () => {
cable: (dummyActionCableConsumer as unknown) as Consumer
}
var producer = createActionCableHandler(options)
producer({text: "", name: ""}, {}, {}, { onError: () => {}, onNext: () => {}, onCompleted: () => {} }).dispose()
var relaySubscription = producer({text: "", name: ""}, {}, {}, { onError: () => {}, onNext: () => {}, onCompleted: () => {} })

expect(wasDisposed).toEqual(true)
relaySubscription.dispose()
relaySubscription.dispose()

expect(wasDisposedCount).toEqual(1)
})

it("uses a provided clientName and operation.id", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function createActionCableHandler(options: ActionCableHandlerOptions) {
var channelId = Math.round(Date.now() + Math.random() * 100000).toString(16)
var cable = options.cable
var operations = options.operations
var subscribed = true

// Register the subscription by subscribing to the channel
const channel = cable.subscriptions.create({
Expand Down Expand Up @@ -66,7 +67,10 @@ function createActionCableHandler(options: ActionCableHandlerOptions) {
// Return an object for Relay to unsubscribe with
return {
dispose: function() {
channel.unsubscribe()
if (subscribed) {
subscribed = false
channel.unsubscribe()
}
}
}
}
Expand Down

0 comments on commit 9ab852f

Please sign in to comment.