-
-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(subscriptions): The subscription resolvers were firing too often (#…
…609) * fix(subscriptions): The subscription resolvers were firing too often The underlying subscription in a remote schema was not terminated properly, causing memory leaks. In addition, multiple subscriptions caused duplicate payloads. Both is fixed by removing the PubSub dependency and directly transforming the Observer returned by the ApolloLink to an async iterable. This allows the async iterable to propagate to the underlying observable to cancel the suscription once the subscription is not needed anymore. * refactor(test): remove timeout from test * chore(changelog): add fix to changelog
- Loading branch information
1 parent
e9c0eb5
commit 2868fe2
Showing
5 changed files
with
135 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Observable } from 'apollo-link'; | ||
import { $$asyncIterator } from 'iterall'; | ||
type Callback = (value?: any) => any; | ||
|
||
export function observableToAsyncIterable<T>(observable: Observable<T>): AsyncIterator<T> { | ||
const pullQueue: Callback[] = []; | ||
const pushQueue: any[] = []; | ||
|
||
let listening = true; | ||
|
||
const pushValue = ({ data }: any) => { | ||
if (pullQueue.length !== 0) { | ||
pullQueue.shift()({ value: data, done: false }); | ||
} else { | ||
pushQueue.push({ value: data }); | ||
} | ||
}; | ||
|
||
const pushError = (error: any) => { | ||
if (pullQueue.length !== 0) { | ||
pullQueue.shift()({ value: { errors: [error] }, done: false }); | ||
} else { | ||
pushQueue.push({ value: { errors: [error] } }); | ||
} | ||
}; | ||
|
||
const pullValue = () => { | ||
return new Promise(resolve => { | ||
if (pushQueue.length !== 0) { | ||
const element = pushQueue.shift(); | ||
// either {value: {errors: [...]}} or {value: ...} | ||
resolve({ | ||
...element, | ||
done: false, | ||
}); | ||
} else { | ||
pullQueue.push(resolve); | ||
} | ||
}); | ||
}; | ||
|
||
const subscription = observable.subscribe({ | ||
next(value: any) { | ||
pushValue(value); | ||
}, | ||
error(err: Error) { | ||
pushError(err); | ||
}, | ||
}); | ||
|
||
const emptyQueue = () => { | ||
if (listening) { | ||
listening = false; | ||
subscription.unsubscribe(); | ||
pullQueue.forEach(resolve => resolve({ value: undefined, done: true })); | ||
pullQueue.length = 0; | ||
pushQueue.length = 0; | ||
} | ||
}; | ||
|
||
return { | ||
async next() { | ||
return listening ? pullValue() : this.return(); | ||
}, | ||
return() { | ||
emptyQueue(); | ||
return Promise.resolve({ value: undefined, done: true }); | ||
}, | ||
throw(error) { | ||
emptyQueue(); | ||
return Promise.reject(error); | ||
}, | ||
[$$asyncIterator]() { | ||
return this; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters