Skip to content

Commit

Permalink
bugfix: persist not working on iOS (#836)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
jmyrland authored Jun 14, 2023
1 parent 0b16f86 commit 76226a7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 6 additions & 2 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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) => {
Expand Down
6 changes: 0 additions & 6 deletions website/docs/docs/recipes/connecting-to-reactotron.md
Original file line number Diff line number Diff line change
Expand Up @@ -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__) {
Expand Down

1 comment on commit 76226a7

@vercel
Copy link

@vercel vercel bot commented on 76226a7 Jun 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

easy-peasy – ./

easy-peasy-git-master-ctrlplusb.vercel.app
easy-peasy.dev
easy-peasy-ctrlplusb.vercel.app

Please sign in to comment.