Skip to content

Instantly share code, notes, and snippets.

@JoviDeCroock
Created December 17, 2019 15:32
Show Gist options
  • Save JoviDeCroock/d089de4b5cf6ccbd2f349464c2d2fe3a to your computer and use it in GitHub Desktop.
Save JoviDeCroock/d089de4b5cf6ccbd2f349464c2d2fe3a to your computer and use it in GitHub Desktop.
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