question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Persist updates slow

See original GitHub issue

Hi @ctrlplusb

First of all. Great job on this lib, i love it!

We run into an issue where persist is updating slowly (approx 2 sec delay). Even on simple/small persisted models.

For example i have this simple model

export const mainMenuModel = {
  isOpen: true,
  toggle: action((state, payload) => {
    state.isOpen = !state.isOpen
  }),
}

const storeModel = {
  mainMenu: persist(mainMenuModel),
}

export const store = createStore(storeModel)

When toggling the menu it takes approx 2 sec to update the local/session storage.

The main issue is where we log out and refresh immediately or refresh 1 second after logging out we got logged back in because it did not update the local-storage on time.

I can get around this by setting the localStorage directly from my model

export const userModel = {
  ...initialState,
  logout: action((state, payload) => {
    localStorage.setItem('[StoreKey]@user.isLoggedIn', false)
    localStorage.setItem('[StoreKey]@user.token', '')
    
    state.isLoggedIn = false
    state.token = ''
  }),
}

But this seems not to be a solution in the long run.

Is this a know issue or is this expected behavior?

i’m using version 3.3.0

Thank you

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ctrlplusbcommented, Apr 2, 2020

Hi @julianklumpers 👋

Glad to hear you are enjoying the lib. 😊

This is a similar problem as is described in #454

There are some API’s which I have both improved and documented within the 3.4.0 alpha, which is available via #459

What is happening is that we debounce the attempts to persist state, currently at an arbitrary 1000ms debounce setting (I plan to make this configurable via a further update in the 3.4.0 release).

So what you need to do prior to refreshing the page is to flush any outstanding calls in the persist layer. You can do so via the API that is attached to your store instance.

For example:

import { useStore, useStoreActions } from 'easy-peasy';

function LogoutButton() {
  const store = useStore();
  const logout = useStoreActions(actions => actions.session.logout);

  const performLogout = async () => {
    logout();
    await store.persist.flush();
    window.location.href = 'http://foo';
  };

  return <button onClick={performLogout}>Logout</button>;
}
1reaction
ctrlplusbcommented, Apr 3, 2020

@julianklumpers I’ve just re-read your comment. I think you need to have the same flush logic after you login and before you refresh the page. Basically any time you refresh the page or navigate out of the site/app you should run a flush to be absolutely sure your state is persisted. I’ll update the docs accordingly with this information, making it more explicit and with some examples.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why hibernate persist() is slow? It is only slow in my case?
I am trying to populate a database by creating entity objects and calling persist() using Hibernate as JPA provider.
Read more >
Windows 10 Cumulative Updates are way too slow to ...
Windows 10 Cumulative Updates are way too slow to download and install. Hi Microsoft, I can no longer tolerate with the Cumulative Updates...
Read more >
Appsheet VERY slow to save changes to googlesheet
Google Sheets recomputes that formula on every update. And if that is a long-running formula, then it delays everything.
Read more >
Slow background tasks process - Persist issues - SonarQube
Hi,. We're using Sonar 8.2.0 developer edition and have ±1.3M LOC, for some time now we're seeing slowness in processing background tasks ...
Read more >
Slow rendering - Android Developers
After looking at the systrace output, there might be methods in your app that you suspect are causing jank. For an example, if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found