Skip to content

Commit 35e55dc

Browse files
committed
Fix race condition in early subscription disposal
1 parent cda9597 commit 35e55dc

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

javascript_client/src/subscriptions/__tests__/createAblyHandlerTest.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,43 @@ describe("createAblyHandler", () => {
216216
expect(nextInvokedWith).toBeUndefined()
217217
})
218218

219+
it("detaches the channel when the subscription is disposed during initial response", async () => {
220+
let detached = false
221+
222+
const ably = createDummyConsumer({
223+
...channelTemplate,
224+
detach() {
225+
detached = true
226+
}
227+
})
228+
const producer = createAblyHandler({
229+
fetchOperation: () =>
230+
new Promise(resolve =>
231+
resolve({
232+
headers: new Map([["X-Subscription-ID", "foo"]]),
233+
body: { errors: {} }
234+
})
235+
),
236+
ably
237+
})
238+
239+
const { dispose } = producer(
240+
dummyOperation,
241+
{},
242+
{},
243+
{
244+
onError: async () => {
245+
dispose()
246+
},
247+
onNext: async () => {},
248+
onCompleted: () => {}
249+
}
250+
)
251+
252+
await nextTick()
253+
expect(detached).toBe(true)
254+
})
255+
219256
describe("integration with Ably", () => {
220257
const key = process.env.ABLY_KEY
221258
const testWithAblyKey = key ? test : test.skip

javascript_client/src/subscriptions/createAblyHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ function createAblyHandler(options: AblyHandlerOptions) {
7474
// POST the subscription like a normal query
7575
const response = await fetchOperation(operation, variables, cacheConfig)
7676

77-
dispatchResult(response.body)
7877
const channelName = response.headers.get("X-Subscription-ID")
7978
if (!channelName) {
8079
throw new Error("Missing X-Subscription-ID header")
@@ -123,6 +122,8 @@ function createAblyHandler(options: AblyHandlerOptions) {
123122
}
124123
// When you get an update from ably, give it to Relay
125124
channel.subscribe("update", updateHandler)
125+
126+
dispatchResult(response.body)
126127
} catch (error) {
127128
observer.onError(error)
128129
}

0 commit comments

Comments
 (0)