From a83db099cc2cca8ebac6612265cdb1923218ee3b Mon Sep 17 00:00:00 2001 From: David Heidrich Date: Sun, 28 Jul 2019 19:57:48 +0200 Subject: [PATCH] fix(apollo-link): optimized stack --- src/proxyCacheLink.js | 48 ++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/src/proxyCacheLink.js b/src/proxyCacheLink.js index 13ba6ac..9a8b5b7 100644 --- a/src/proxyCacheLink.js +++ b/src/proxyCacheLink.js @@ -27,42 +27,34 @@ export const proxyCacheLink = (queryCache: Cache, cacheKeyModifi const { id, timeout } = calculateArguments(query, operation.variables, cacheKeyModifier, operation.getContext()) return new Observable(observer => { - try { - queryCache.get(id).then(data => { - if (data) { - return observer.next({ data }) - } - const obs = + queryCache.get(id).then(data => { + if (data) { + return observer.next({ data }) + } + const obs = server && forward ? forward(operation) : Observable.of({ data: {} }) - obs.subscribe({ - next: ({ data, errors }) => { - if (!errors) { - try { - queryCache.set(id, data, timeout).then(() => { - observer.next({ - data, - errors - }) - }) - } catch (e) { - errorOnSet(e) - } - } - }, - error: observer.error.bind(observer), - complete: () => { - observer.complete() + obs.subscribe({ + next: ({ data, errors }) => { + if (!errors) { + queryCache.set(id, data, timeout) + .catch(errorOnSet) + observer.next({ + data, + errors + }) } - }) + }, + error: observer.error.bind(observer), + complete: () => { + observer.complete() + } }) - } catch (e) { - errorOnGet(e) - } + }).catch(errorOnGet) }) } }()