Skip to content

SharedStateServer doesn't send update to client after refresh #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brentbaum opened this issue Apr 22, 2021 · 8 comments
Closed

SharedStateServer doesn't send update to client after refresh #352

brentbaum opened this issue Apr 22, 2021 · 8 comments
Assignees

Comments

@brentbaum
Copy link

I have a simple app with a few buttons. When the user clicks on a button, the text next to the button toggles between "asserted=False" and "asserted=True".

Screen Shot 2021-04-22 at 8 51 47 AM

The first time a user brings up the app everything works as expected. The network tab looks like this.

Screen Shot 2021-04-22 at 8 51 51 AM

After a refresh the app keeps sending events but stops receiving them.

Screen Shot 2021-04-22 at 8 52 07 AM

This behavior is consistent across any components that depend on state. Notably, the user is unable to change the value of inputs.

Server code:

def start_app(dispatch):
    from idom.server.sanic import SharedClientStateServer

    # we use a redux-like dispatch structure to send events to outside the idom event, then update state
    # with a callback stored outside the app. 
    # I've tried removing this particular piece of functionality and the problem persists.
    root = lambda: Panel(dispatch)
    app = SharedClientStateServer(root)
    return app.daemon("0.0.0.0", 3040)

Any ideas on the root cause or a way to mitigate? The PerClientStateServer throws a CannotAccessError due to our rapid state updates, logging (f"Did not update {component} - resources of {self} are closed") so it's a no-go (sorry, I've been waiting to file an issue on that one!)

Thanks!

@rmorshea
Copy link
Collaborator

rmorshea commented Apr 22, 2021

Did this issue arise after the latest update?

And I'm assuming the CannotAccessError comes up when a refresh happens?

@brentbaum
Copy link
Author

@rmorshea I've had this issue since 0.22 and haven't tested before that.

You got it, the CannotAccessError (really, just the log message but that's triggered by a CannotAccessError) happens after refresh.

@rmorshea
Copy link
Collaborator

rmorshea commented Apr 22, 2021

Ok, it looks like I'm able to reproduce, so I'll look into that.

With that said, ignoring the logged CannotAccessError, does PerClientStateServer work for your purposes? Perhaps you could leverage a use_effect hook to avoid attempting to update the layout after it's been closed due to a refresh? It might be similar to this example except instead of a connection, you'd be opening and closing your async thread of execution. For example:

@idom.component
def SyncStateComponent():
    state, set_state = idom.hooks.use_state(None)

    @idom.hooks.use_effect
    def sync_state():
        stop_updating = start_updating(set_state)
        return stop_updating

    return ...

def start_updating(set_state):
    """Start async thread of execution to frequently set state and return callback to stop""" 
    ...
    return callback_to_stop_setting_state

@rmorshea
Copy link
Collaborator

The main purpose of the SharedClientStateServer is that it allows all clients to share one synchronized view. For example, a caused or seen by one client will seen by all other clients too. Is that what you're trying to do?

Unfortunately that information isn't really documented yet.

@brentbaum
Copy link
Author

brentbaum commented Apr 22, 2021

One synchronized view is exactly what we're looking for; the SharedClientStateServer is working fantastically minus the refresh bug. I felt like it was pretty clear what the intent was based on the name :) I was falling back to the PerClientStateServer to allow refreshing (helpful during development).

The PerClientStateServer works as expected minus the log messages. I'm depending on logging heavily at this point so the frequent messages were a big enough annoyance that I moved away from that approach.

FYI I've had difficulty with use_effect cleanup effects not running on window close / refresh before. I don't have anything reproducible at this point but will submit another issue if it becomes a problem.

@rmorshea
Copy link
Collaborator

rmorshea commented Apr 23, 2021

I think I found the problem. Seems related to this issue: #298

@rmorshea
Copy link
Collaborator

Ok, got the PR with the fix passing. Will try and release some time this week.

@rmorshea
Copy link
Collaborator

rmorshea commented May 8, 2021

Fixed by #353

@rmorshea rmorshea closed this as completed May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants