From 76226a7891c4f03daf15fc4550c6767f6a181aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rn=20A=2E=20Myrland?= Date: Wed, 14 Jun 2023 16:55:39 +0200 Subject: [PATCH] bugfix: persist not working on iOS (#836) `createPersistor` defines a `timingMethod` that won't work on iOS. The `timingMethod` utilizes `requestIdleCallback` if it is defined. It turns out that the pollyfilled requestIdleCallback for RN on iOS, requires a second parameter with a specific timeout - where as the native implementation does not. Setting the specific parameter invokes this function across all platforms. --- package.json | 2 +- src/persistence.js | 8 ++++++-- website/docs/docs/recipes/connecting-to-reactotron.md | 6 ------ 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 481a49e3..f9ccb711 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easy-peasy", - "version": "6.0.0", + "version": "6.0.1", "description": "Vegetarian friendly state for React", "license": "MIT", "main": "dist/index.cjs.js", diff --git a/src/persistence.js b/src/persistence.js index 0e0c641e..a4dda3ef 100644 --- a/src/persistence.js +++ b/src/persistence.js @@ -76,7 +76,9 @@ function createStorageWrapper(storage, transformers = [], migrations = {}) { : data; const hasMigrations = Object.keys(migrations).length > 0; - const result = hasMigrations ? migrate(storageData, migrations) : storageData; + const result = hasMigrations + ? migrate(storageData, migrations) + : storageData; if ( outTransformers.length > 0 && @@ -173,7 +175,9 @@ export function createPersistor(persistKey, _r) { typeof window === 'undefined' ? (fn) => fn() : window.requestIdleCallback != null - ? window.requestIdleCallback + ? // We need to wrap requestIdleCallback, because it doesn't work without + // a second parameter on iOS with ReactNative + (fn) => window.requestIdleCallback(fn, { timeout: 0 }) : window.requestAnimationFrame; const persist = (nextState) => { diff --git a/website/docs/docs/recipes/connecting-to-reactotron.md b/website/docs/docs/recipes/connecting-to-reactotron.md index 1d0328cb..d0620aa7 100644 --- a/website/docs/docs/recipes/connecting-to-reactotron.md +++ b/website/docs/docs/recipes/connecting-to-reactotron.md @@ -30,12 +30,6 @@ Then update the manner in which you create your Easy Peasy store. import { createStore } from 'easy-peasy'; import model from './model'; -// There might be an issue causing `setItem` not being called correctly -// for iOS devices using React Native. The solution for this is currently -// to remove the implemenation of `requestIdleCallback`. -// Read this issue for more information: https://github.com/ctrlplusb/easy-peasy/issues/599 -window.requestIdleCallback = null; - let storeEnhancers = []; if (__DEV__) {