Skip to content

Commit

Permalink
fix(apollo-link): optimized stack
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Jul 28, 2019
1 parent 6c98134 commit a83db09
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/proxyCacheLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,34 @@ export const proxyCacheLink = (queryCache: Cache<string, Object>, 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)
})
}
}()
Expand Down

0 comments on commit a83db09

Please sign in to comment.