Skip to content

Commit 573597e

Browse files
GozalaCopilot
andauthored
fix: use preloaded data in useDoc (#1096)
* fix: use preloaded data in useDoc * Update jumble/public/module/charm/sandbox/bootstrap.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a9e18e5 commit 573597e

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ window.React = React
88
window.ReactDOM = ReactDOM
99
window.Babel = Babel
1010

11-
window.useDoc = function (key, defaultValue = null) {
11+
window.useDoc = function (key) {
1212
// Track if we've received a response from the parent
1313
const [received, setReceived] = React.useState(false)
1414
// Initialize state with defaultValue
@@ -44,18 +44,6 @@ window.useDoc = function (key, defaultValue = null) {
4444
}
4545
}, [key])
4646

47-
// After we've received a response, apply default value if needed
48-
React.useEffect(() => {
49-
if (received && doc === undefined && defaultValue !== undefined) {
50-
// Only write the default value if we've confirmed no data exists
51-
console.log("useDoc", key, "default", defaultValue)
52-
window.parent.postMessage(
53-
{ type: "write", data: [key, defaultValue] },
54-
"*"
55-
)
56-
}
57-
}, [received, doc, defaultValue, key])
58-
5947
// Update function
6048
const updateDoc = newValue => {
6149
if (typeof newValue === "function") {
@@ -66,11 +54,13 @@ window.useDoc = function (key, defaultValue = null) {
6654
window.parent.postMessage({ type: "write", data: [key, newValue] }, "*")
6755
}
6856

69-
// Return the current document value or the default if we haven't received data yet
70-
return [
71-
received ? (doc === undefined ? defaultValue : doc) : defaultValue,
72-
updateDoc,
73-
]
57+
// If we have not yet received response from the host we use field from the
58+
// sourceData which was preloaded via `subscribeToSource` during initialization
59+
// in the `initializeApp`.
60+
// ⚠️ Please note that value we prefetched still could be out of date because
61+
// `*` subscription is removed in the iframe-ctx.ts file which could lead
62+
// charm to make wrong conclusion and overwrite key.
63+
return [received ? doc : window.sourceData[key], updateDoc]
7464
}
7565

7666
// Define llm utility with React available

0 commit comments

Comments
 (0)