@@ -68,13 +68,11 @@ function createAblyHandler(options: AblyHandlerOptions) {
6868 observer . onCompleted ( )
6969 }
7070 }
71-
7271 ; ( async ( ) => {
7372 try {
7473 // POST the subscription like a normal query
7574 const response = await fetchOperation ( operation , variables , cacheConfig )
7675
77- dispatchResult ( response . body )
7876 const channelName = response . headers . get ( "X-Subscription-ID" )
7977 if ( ! channelName ) {
8078 throw new Error ( "Missing X-Subscription-ID header" )
@@ -123,6 +121,8 @@ function createAblyHandler(options: AblyHandlerOptions) {
123121 }
124122 // When you get an update from ably, give it to Relay
125123 channel . subscribe ( "update" , updateHandler )
124+
125+ dispatchResult ( response . body )
126126 } catch ( error ) {
127127 observer . onError ( error )
128128 }
@@ -133,28 +133,25 @@ function createAblyHandler(options: AblyHandlerOptions) {
133133 try {
134134 if ( channel ) {
135135 const disposedChannel = channel
136- disposedChannel . unsubscribe ( "update" , updateHandler )
137-
138- const leavePromise = new Promise ( ( resolve , reject ) => {
139- const callback = ( err : Types . ErrorInfo ) => {
140- if ( err ) {
141- reject ( new AblyError ( err ) )
142- } else {
143- resolve ( )
136+ disposedChannel . unsubscribe ( )
137+
138+ // Ensure channel is no longer attaching, as otherwise detach does
139+ // nothing
140+ if ( disposedChannel . state === "attaching" ) {
141+ await new Promise ( ( resolve , _reject ) => {
142+ const onStateChange = (
143+ stateChange : Types . ChannelStateChange
144+ ) => {
145+ if ( stateChange . current !== "attaching" ) {
146+ disposedChannel . off ( onStateChange )
147+ resolve ( )
148+ }
144149 }
145- }
146-
147- if ( isAnonymousClient ( ) ) {
148- disposedChannel . presence . leaveClient (
149- anonymousClientId ,
150- callback
151- )
152- } else {
153- disposedChannel . presence . leave ( callback )
154- }
155- } )
150+ disposedChannel . on ( onStateChange )
151+ } )
152+ }
156153
157- const detachPromise = new Promise ( ( resolve , reject ) => {
154+ await new Promise ( ( resolve , reject ) => {
158155 disposedChannel . detach ( ( err : Types . ErrorInfo ) => {
159156 if ( err ) {
160157 reject ( new AblyError ( err ) )
@@ -164,7 +161,6 @@ function createAblyHandler(options: AblyHandlerOptions) {
164161 } )
165162 } )
166163
167- await Promise . all ( [ leavePromise , detachPromise ] )
168164 ably . channels . release ( disposedChannel . name )
169165 }
170166 } catch ( error ) {
0 commit comments