Created
December 17, 2019 15:32
-
-
Save JoviDeCroock/d089de4b5cf6ccbd2f349464c2d2fe3a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Exchange } from 'urql'; | |
import { fromDomEvent, subscribe, pipe, filter } from 'wonka'; | |
export const offlineExchange = (): Exchange => ({ | |
forward, | |
client, | |
}) => { | |
const queue = []; | |
const flushQueue = () => { | |
queue.forEach(op => { | |
client.reexecuteOperation(op); | |
}); | |
} | |
return ops$ => { | |
pipe( | |
fromDomEvent(document.body, 'online'), | |
subscribe(flushQueue) | |
); | |
return pipe( | |
ops$, | |
filter(op => { | |
const isOnline = navigator.onLine; | |
if (!isOnline) { | |
queue.push(op); | |
} | |
return !isOnline; | |
}), | |
forward, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment