graphql-ruby-client
Advanced tools
Comparing version 1.6.2 to 1.6.3
# graphql-ruby-client | ||
## 1.6.3 (11 Jan 2019) | ||
- Fix `.unsubscribe()` for PusherLink #2042 | ||
## 1.6.2 (14 Dec 2018) | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "graphql-ruby-client", | ||
"version": "1.6.2", | ||
"version": "1.6.3", | ||
"description": "JavaScript client for graphql-ruby", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,29 +7,9 @@ var ActionCableLink = require("../ActionCableLink") | ||
var log = [] | ||
var subscription | ||
var cable = { | ||
subscriptions: { | ||
create: function(channelName, options) { | ||
var subscription = Object.assign(options, { | ||
subscription = Object.assign(options, { | ||
perform: function(actionName, options) { | ||
log.push(["perform", { actionName: actionName, options: options }]) | ||
this.received({ | ||
result: { | ||
data: null | ||
}, | ||
more: true | ||
}) | ||
this.received({ | ||
result: { | ||
data: "data 1" | ||
}, | ||
more: true | ||
}) | ||
this.received({ | ||
result: { | ||
data: "data 2" | ||
}, | ||
more: false | ||
}) | ||
}, | ||
@@ -62,2 +42,25 @@ unsubscribe: function() { | ||
subscription.received({ | ||
result: { | ||
data: null | ||
}, | ||
more: true | ||
}) | ||
subscription.received({ | ||
result: { | ||
data: "data 1" | ||
}, | ||
more: true | ||
}) | ||
subscription.received({ | ||
result: { | ||
data: "data 2" | ||
}, | ||
more: false | ||
}) | ||
expect(log).toEqual([ | ||
@@ -64,0 +67,0 @@ [ |
@@ -48,10 +48,38 @@ // An Apollo Link for using graphql-pro's Pusher subscriptions | ||
request(operation, forward) { | ||
return new Observable((observer) => { | ||
const subscribeObservable = new Observable((observer) => { }) | ||
// Capture the super method | ||
const prevSubscribe = subscribeObservable.subscribe.bind(subscribeObservable) | ||
// Override subscribe to return an `unsubscribe` object, see | ||
// https://github.com/apollographql/subscriptions-transport-ws/blob/master/src/client.ts#L182-L212 | ||
subscribeObservable.subscribe = (observerOrNext, onError, onComplete) => { | ||
// Call super | ||
prevSubscribe(observerOrNext, onError, onComplete) | ||
const observer = getObserver(observerOrNext, onError, onComplete) | ||
var subscriptionChannel = null | ||
// Check the result of the operation | ||
forward(operation).subscribe({ next: (data) => { | ||
const resultObservable = forward(operation) | ||
// When the operation is done, try to get the subscription ID from the server | ||
resultObservable.subscribe({ next: (data) => { | ||
// If the operation has the subscription header, it's a subscription | ||
const subscriptionChannel = this._getSubscriptionChannel(operation) | ||
const response = operation.getContext().response | ||
// Check to see if the response has the header | ||
subscriptionChannel = response.headers.get("X-Subscription-ID") | ||
if (subscriptionChannel) { | ||
// This will keep pushing to `.next` | ||
this._createSubscription(subscriptionChannel, observer) | ||
// Set up the pusher subscription for updates from the server | ||
const pusherChannel = this.pusher.subscribe(subscriptionChannel) | ||
// Subscribe for more update | ||
pusherChannel.bind("update", function(payload) { | ||
if (!payload.more) { | ||
// This is the end, the server says to unsubscribe | ||
pusher.unsubscribe(subscriptionChannel) | ||
observer.complete() | ||
} | ||
const result = payload.result | ||
if (result) { | ||
// Send the new response to listeners | ||
observer.next(result) | ||
} | ||
}) | ||
} | ||
@@ -65,27 +93,31 @@ else { | ||
}}) | ||
}) | ||
} | ||
// Return an object that will unsubscribe _if_ the query was a subscription. | ||
return { | ||
unsubscribe: () => { | ||
subscriptionChannel && this.pusher.unsubscribe(subscriptionChannel) | ||
} | ||
} | ||
} | ||
_getSubscriptionChannel(operation) { | ||
const response = operation.getContext().response | ||
// Check to see if the response has the header | ||
const subscriptionChannel = response.headers.get("X-Subscription-ID") | ||
return subscriptionChannel | ||
return subscribeObservable | ||
} | ||
} | ||
_createSubscription(subscriptionChannel, observer) { | ||
const pusherChannel = this.pusher.subscribe(subscriptionChannel) | ||
// Subscribe for more update | ||
pusherChannel.bind("update", function(payload) { | ||
if (!payload.more) { | ||
// This is the end, the server says to unsubscribe | ||
pusher.unsubscribe(subscriptionChannel) | ||
observer.complete() | ||
} | ||
const result = payload.result | ||
if (result) { | ||
// Send the new response to listeners | ||
observer.next(result) | ||
} | ||
}) | ||
// Turn `subscribe` arguments into an observer-like thing, see getObserver | ||
// https://github.com/apollographql/subscriptions-transport-ws/blob/master/src/client.ts#L329-L343 | ||
function getObserver(observerOrNext, onError, onComplete) { | ||
if (typeof observerOrNext === 'function') { | ||
// Duck-type an observer | ||
return { | ||
next: (v) => observerOrNext(v), | ||
error: (e) => onError && onError(e), | ||
complete: () => onComplete && onComplete(), | ||
} | ||
} else { | ||
// Make an object that calls to the given object, with safety checks | ||
return { | ||
next: (v) => observerOrNext.next && observerOrNext.next(v), | ||
error: (e) => observerOrNext.error && observerOrNext.error(e), | ||
complete: () => observerOrNext.complete && observerOrNext.complete(), | ||
} | ||
} | ||
@@ -92,0 +124,0 @@ } |
Sorry, the diff of this file is not supported yet
181679
2321