Skip to content

Conversation

@Gozala
Copy link
Contributor

@Gozala Gozala commented Apr 23, 2025

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the useDoc hook by removing the defaultValue parameter and instead returning the preloaded data from window.sourceData.

  • Removed the defaultValue parameter from useDoc and its related effect block
  • Updated the return logic to use preloaded data instead of a default value
Comments suppressed due to low confidence (1)

jumble/public/module/charm/sandbox/bootstrap.js:11

  • Removing the defaultValue parameter changes the fallback behavior of useDoc. Confirm that all callsites have been updated to rely on window.sourceData for preloaded data instead of passing a default value.
window.useDoc = function (key) {

@Gozala
Copy link
Contributor Author

Gozala commented Apr 23, 2025

@seefeldb would be great to get your input on this because we went back and forth in regards to what we want to do if key was not preloaded here https://linear.app/common-tools/issue/CT-308/usedoc-should-return-data-read-from and https://discord.com/channels/1232452732963655720/1252747522678460466/1364692637533208727

I also have since realized that useDoc could still return stale value and end up overwriting data just like it did here https://linear.app/common-tools/issue/CT-273/data-seems-to-get-lost-when-opening-charm-in-another-window

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

@bfollington bfollington left a comment

Choose a reason for hiding this comment

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

I originally wanted to achieve this kind of behaviour by blocking the initial React render until we had received the first copy of the Charm's data, then useDoc wouldn't need to have such complex fallback behaviour.

This PR seems cleaner to me in achieving some of that, but I actually don't know how to protect against stale data completely... not without @seefeldb's input

@seefeldb
Copy link
Contributor

Were you able to verify what happens when the schema defines a key, where the value undefined is allowed (e.g. an optional key with no default value) and it is undefined? Will sourceData include it?

@seefeldb
Copy link
Contributor

Ah, since we're not throwing, this is fine.

One thing to watch for: The way to set default values is via the schema, but if the LLM doesn't do that, then it might instead use useEffect, which might exacerbate the issues.

@Gozala
Copy link
Contributor Author

Gozala commented Apr 25, 2025

Ah, since we're not throwing, this is fine.

One thing to watch for: The way to set default values is via the schema, but if the LLM doesn't do that, then it might instead use useEffect, which might exacerbate the issues.

Perhaps alternative tack would be to kickoff schema alteration when useDoc is called with unknown key ? Which may be aligned with more broader idea of lazy bindings.

@Gozala Gozala merged commit 573597e into main Apr 25, 2025
5 checks passed
@Gozala Gozala deleted the fix/use-doc-without-default branch April 25, 2025 15:33
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

Successfully merging this pull request may close these issues.

4 participants