Skip to content
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

Question on init mutations #38

Closed
tslocke opened this issue Mar 22, 2022 · 5 comments
Closed

Question on init mutations #38

tslocke opened this issue Mar 22, 2022 · 5 comments

Comments

@tslocke
Copy link

tslocke commented Mar 22, 2022

https://github.com/rocicorp/replidraw-do/blob/d4f191ea47a885636214db1451feccac807d25d2/src/pages/d/%5Bid%5D.tsx#L47-L56

Why is it OK here to call initClientState directly, while initShapes is delayed until after the first sync?

(Also, LMK if this is/isn't a good place to ask questions)

@aboodman
Copy link
Contributor

Good question. It's because for this application each new page load is a different (anonymous) "user" and so it's right to create a new client state for each page load. We don't need to wait for initial sync to see if there's any data there.

The shapes on the other hand should only be created once for the entire room.

@aboodman
Copy link
Contributor

aboodman commented Mar 23, 2022

PS: Fine to ask questions here, but also discord is available. You can ask reflect questions there too: https://discord.replicache.dev.

@tslocke
Copy link
Author

tslocke commented Mar 23, 2022

The shapes on the other hand should only be created once for the entire room.

I'm entirely confused about how the above code achieves this : )

The docs say

onSync is called when a sync begins, and again when the sync ends. The parameter syncing is set to true when onSync is called at the beginning of a sync, and false when it is called at the end of a sync.

So I would expect that callback to be called on the first sync, regardless of whether this is a new room or a reload of an existing one. The only reason it's not called over and over, as I understand, is the callback de-registers itself (53).

Every time the page loads it's going to assign that callback to r.onSync, so it's going to be called once.

I mean, I can see it behaves as you say, but I have no idea why!

Maybe my mental model of what a "sync" is, is wrong. I understand that to mean any time mutations from some other client are applied to the state of this client.

Related question: how is a room created anyway? Is it this? new Client(r, roomID, workerURL); (if it's a newly seen roomID, the new room is created?)

@aboodman
Copy link
Contributor

aboodman commented Mar 23, 2022

The initShapes method runs on every page load after sync, but the first thing it does is check the stored state to see whether its run before:

https://github.com/rocicorp/replidraw-do/blob/main/src/datamodel/shape.ts#L104

If it finds the initialized key in the synced data it does nothing.

Actually now that I think of it this code dates to a version of Replidraw where persistence was turned off for performance, so I think we could now run this mutator before sync and it would work the same way. Because we now store the synced data locally so we can detect whether we've already initialized without waiting for first sync.

@aboodman
Copy link
Contributor

Related question: how is a room created anyway? Is it this? new Client(r, roomID, workerURL); (if it's a newly seen roomID, the new room is created?)

Yes, this is right. The client-side API for reflect is still a bit wonky - we will soon merge Client and Replicache, but that's how it works conceptually. You just declare the room you want to connect to, and if it's new, it's created.

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