@@ -8,7 +8,7 @@ window.React = React
88window . ReactDOM = ReactDOM
99window . 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