Hey!
ReactiveVars persistence update not working for React Native.
import { makeVar } from '@apollo/client'
import AsyncStorage from '@react-native-community/async-storage'
import { is } from 'ramda'
const isString = (value) => is(value)
const getCleanValueForStorage = (value) =>
isString(value) ? value : JSON.stringify(value)
const makeVarPersisted = (initialValue, storageName) => {
const rv = makeVar(initialValue)
// eslint-disable-next-line func-names
const rvFn = function (newValue) {
if (newValue) {
try {
AsyncStorage.setItem(storageName, getCleanValueForStorage(newValue))
return rv(newValue)
} catch {
// ignore
}
} else {
return rv()
}
}
const restoreSession = async () => {
try {
const previousValue = await AsyncStorage.getItem(storageName)
if (previousValue) {
rv(isString(initialValue) ? previousValue : JSON.parse(previousValue))
}
} catch {
// ignore
}
}
rvFn.onNextChange = rv.onNextChange
return [rvFn, restoreSession]
}
export default makeVarPersisted