Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/graphql/subscriptions/serialize.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true
require "set"
require "ostruct"

module GraphQL
class Subscriptions
# Serialization helpers for passing subscription data around.
Expand Down
24 changes: 21 additions & 3 deletions spec/dummy/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
var receivedCallback = options.received
// Unique-ish
var uuid = Math.round(Date.now() + Math.random() * 100000).toString(16)
return {
var subscription = {
_subscribed: false,
subscription: App.cable.subscriptions.create({
channel: "GraphqlChannel",
id: uuid,
Expand All @@ -41,7 +42,8 @@
console.log("Connected", query, variables)
},
received: function(payload) {
console.log("received", query, variables, payload)
subscription._subscribed = true
App.logToBody("ActionCable received: " + JSON.stringify(payload))
if (payload.result) {
receivedCallback(payload)
}
Expand All @@ -53,12 +55,27 @@
}
),
trigger: function(options) {
this.subscription.perform("make_trigger", options)
if (!subscription._subscribed) {
options.retries ||= 0
options.retries++
if (options.retries > 5) {
throw new Error("Retried 5 times, failed to trigger: " + JSON.stringify(options))
} else {
App.logToBody("Retrying trigger " + options.retries + " : " + JSON.stringify(options))
setTimeout(function() {
subscription.trigger(options)
}, 500)
}
} else {
App.logToBody("Triggering " + JSON.stringify(options))
this.subscription.perform("make_trigger", options)
}
},
unsubscribe: function() {
this.subscription.unsubscribe()
},
}
return subscription
}

// Add `text` to the HTML body, for debugging
Expand All @@ -67,5 +84,6 @@
var logEntry = document.createElement("p")
logEntry.innerText = text
bodyLog.appendChild(logEntry)
bodyLog.append("\n")
}
}).call(this);
20 changes: 14 additions & 6 deletions spec/dummy/app/views/pages/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,36 @@ <h1>ActionCable Test Page</h1>
fingerprintSubscriptions[key] = []
}
var subs = fingerprintSubscriptions[key]
var number = subs.length + 1
var newSub = App.subscribe({
number: number,
query: "subscription fingerprint" + key + " { counterIncremented { newValue } }",
variables: {},
received: function(data) {
App.logToBody("received from " + key + " " + JSON.stringify(data))

if (data.result.data.counterIncremented) {
appendItem("fingerprint-updates-" + key, "update-" + newSub.number + "-value-" + data.result.data.counterIncremented.newValue)
appendItem("fingerprint-updates-" + key, "update-" + number + "-value-" + data.result.data.counterIncremented.newValue)
} else {
appendItem("fingerprint-updates-" + key, "connected-" + newSub.number)
appendItem("fingerprint-updates-" + key, "connected-" + number)
}
}
})
newSub.number = subs.length + 1
App.logToBody("Appending Subscription, key: " + key + ", number:" + number )
subs.push(newSub)
}

function triggerWithFingerprint(key) {
App.logToBody("triggering " + key)
var subs = fingerprintSubscriptions[key]
var sub = subs && subs[0]
sub && sub.trigger({field: "counterIncremented", arguments: {}, value: null})
if (!subs) {
App.logToBody("No Subscriptions found for Key: " + key + ", in: " + Object.keys(fingerprintSubscriptions))
}
var sub = subs[0]
if (!sub) {
App.logToBody("Empty subscriptions array for key: " + key + ", in: " + Object.keys(fingerprintSubscriptions))
}
App.logToBody("triggering " + key)
sub.trigger({field: "counterIncremented", arguments: {}, value: null})
}

function unsubscribeWithFingerprint(key) {
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/test/system/action_cable_subscription_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
require "application_system_test_case"

class ActionCableSubscriptionsTest < ApplicationSystemTestCase
setup do
ActionCable.server.config.logger = Logger.new(STDOUT)
end
# This test covers a lot of ground!
test "it handles subscriptions" do
# Load the page and let the subscriptions happen
Expand Down
Loading