Skip to content

Commit 33d188d

Browse files
committed
Use outer references to channel instead of casting this
1 parent cf0a3fe commit 33d188d

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

javascript_client/src/subscriptions/ActionCableLink.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ApolloLink, Observable, FetchResult, Operation, NextLink } from "@apollo/client/core"
2-
import { Cable, Channel } from "actioncable"
2+
import { Cable } from "actioncable"
33
import { print } from "graphql"
44

55
type RequestResult = FetchResult<{ [key: string]: any; }, Record<string, any>, Record<string, any>>
@@ -25,13 +25,12 @@ class ActionCableLink extends ApolloLink {
2525
return new Observable((observer) => {
2626
var channelId = Math.round(Date.now() + Math.random() * 100000).toString(16)
2727
var actionName = this.actionName
28-
var subscription = this.cable.subscriptions.create(Object.assign({},{
28+
var channel = this.cable.subscriptions.create(Object.assign({},{
2929
channel: this.channelName,
3030
channelId: channelId
3131
}, this.connectionParams), {
3232
connected: function() {
33-
// Broken since https://github.com/DefinitelyTyped/DefinitelyTyped/pull/52421 ??
34-
((this as unknown) as Channel).perform(
33+
channel.perform(
3534
actionName,
3635
{
3736
query: operation.query ? print(operation.query) : null,
@@ -52,9 +51,8 @@ class ActionCableLink extends ApolloLink {
5251
}
5352
}
5453
})
55-
5654
// Make the ActionCable subscription behave like an Apollo subscription
57-
return Object.assign(subscription, {closed: false})
55+
return Object.assign(channel, {closed: false})
5856
})
5957
}
6058
}

javascript_client/src/subscriptions/ActionCableSubscriber.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import printer from "graphql/language/printer"
22
import registry from "./registry"
3-
import { Cable, Channel } from "actioncable"
3+
import { Cable } from "actioncable"
44

55
interface ApolloNetworkInterface {
66
applyMiddlewares: Function
@@ -30,14 +30,12 @@ class ActionCableSubscriber {
3030
var networkInterface = this._networkInterface
3131
// unique-ish
3232
var channelId = Math.round(Date.now() + Math.random() * 100000).toString(16)
33-
3433
var channel = this._cable.subscriptions.create({
3534
channel: "GraphqlChannel",
3635
channelId: channelId,
3736
}, {
3837
// After connecting, send the data over ActionCable
3938
connected: function() {
40-
var _this = this
4139
// applyMiddlewares code is inspired by networkInterface internals
4240
var opts = Object.assign({}, networkInterface._opts)
4341
networkInterface
@@ -52,10 +50,8 @@ class ActionCableSubscriber {
5250
variables: variables,
5351
operationId: operationId,
5452
operationName: operationName,
55-
}) as any
56-
// This goes to the #execute method of the channel
57-
// Broken since https://github.com/DefinitelyTyped/DefinitelyTyped/pull/52421
58-
((_this as unknown) as Channel).perform("execute", channelParams)
53+
})
54+
channel.perform("execute", channelParams)
5955
})
6056
},
6157
// Payload from ActionCable should have at least two keys:

javascript_client/src/subscriptions/__tests__/ActionCableLinkTest.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe("ActionCableLink", () => {
1616
cable = {
1717
subscriptions: {
1818
create: function(_channelName: string, options: {connected: Function, received: Function}) {
19+
var alreadyConnected = false
1920
var subscription = Object.assign(
2021
Object.create({
2122
perform: function(actionName: string, options: object) {
@@ -29,8 +30,15 @@ describe("ActionCableLink", () => {
2930
)
3031

3132
subscription.connected = subscription.connected.bind(subscription)
33+
var received = subscription.received
34+
subscription.received = function(data: any) {
35+
if (!alreadyConnected) {
36+
alreadyConnected = true
37+
subscription.connected()
38+
}
39+
received(data)
40+
}
3241
subscription.__proto__.unsubscribe = subscription.__proto__.unsubscribe.bind(subscription)
33-
subscription.connected()
3442
return subscription
3543
}
3644
}

javascript_client/src/subscriptions/createActionCableHandler.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Cable, Channel } from "actioncable"
1+
import { Cable } from "actioncable"
22

33
/**
44
* Create a Relay Modern-compatible subscription handler.
@@ -20,7 +20,7 @@ function createActionCableHandler(options: ActionCableHandlerOptions) {
2020
var operations = options.operations
2121

2222
// Register the subscription by subscribing to the channel
23-
const subscription = cable.subscriptions.create({
23+
const channel = cable.subscriptions.create({
2424
channel: "GraphqlChannel",
2525
channelId: channelId,
2626
}, {
@@ -41,10 +41,8 @@ function createActionCableHandler(options: ActionCableHandlerOptions) {
4141
query: operation.text
4242
}
4343
}
44-
// `this.perform` stopped working after https://github.com/DefinitelyTyped/DefinitelyTyped/pull/52421
45-
((this as unknown) as Channel).perform('send', channelParams);
46-
47-
((this as unknown) as Channel).perform("execute", channelParams)
44+
channel.perform('send', channelParams)
45+
channel.perform("execute", channelParams)
4846
},
4947
// This result is sent back from ActionCable.
5048
received: function(payload: { result: { errors: any[], data: object }, more: boolean}) {
@@ -66,7 +64,7 @@ function createActionCableHandler(options: ActionCableHandlerOptions) {
6664
// Return an object for Relay to unsubscribe with
6765
return {
6866
dispose: function() {
69-
subscription.unsubscribe()
67+
channel.unsubscribe()
7068
}
7169
}
7270
}

0 commit comments

Comments
 (0)