@@ -20,7 +20,13 @@ class CrossOriginInterface {
2020 this . disable_scroll = false ;
2121 this . updateId = 0
2222 this . enroute = false ;
23+ this . untrackedanchors = [ ] ;
2324 window . addEventListener ( "message" , this . handleMessage . bind ( this ) ) ;
25+
26+ //Try to track untracked anchors every 200ms
27+ setInterval ( ( ) => {
28+ this . poll_untrackedanchors ( ) ;
29+ } , 200 ) ;
2430 }
2531
2632 register ( component , autoUpdateAnchor , emphasisStyle ) {
@@ -187,17 +193,22 @@ class CrossOriginInterface {
187193 for ( const anchorId of anchor_ids ) {
188194 if ( this . trackedAnchors . has ( anchorId ) ) {
189195 console . debug ( 'Anchor is already being tracked' , anchorId ) ;
190- return ;
196+ continue ;
191197 }
192198
193199 const anchor = document . getElementById ( anchorId ) ;
194-
195200 if ( ! anchor ) {
196- console . error ( 'Anchor does not exist' , anchorId ) ;
197- return
201+ console . warn ( 'Anchor does not exist in document: ' , anchorId , ". Queueing for later." ) ;
202+ this . untrackedanchors . push ( anchorId ) ;
203+ continue
198204 }
199205 this . trackedAnchors . add ( anchorId ) ;
200206
207+ //If no active anchor, set this anchor as active
208+ if ( this . activeAnchorId === null ) {
209+ this . activeAnchorId = anchorId ;
210+ }
211+
201212 //Insert anchor into sortedAnchors based on its position in the document
202213 let inserted = false ;
203214 for ( let i = 0 ; i < this . sortedAnchors . length ; i ++ ) {
@@ -211,13 +222,21 @@ class CrossOriginInterface {
211222 if ( ! inserted ) {
212223 this . sortedAnchors . push ( anchorId ) ;
213224 }
214- this . sortedAnchors . push ( anchorId ) ;
215225
216226 this . observer . observe ( anchor ) ;
217227 console . debug ( 'Started tracking anchor' , anchorId ) ;
218228 }
219229 }
220-
230+ poll_untrackedanchors ( ) {
231+ //If there are untracked anchors, try to track them
232+ if ( this . untrackedanchors . length > 0 ) {
233+ const untrackedanchors = this . untrackedanchors ;
234+ this . untrackedanchors = [ ] ;
235+
236+ this . trackAnchors ( untrackedanchors ) ;
237+ console . log ( "ASDFASDF" )
238+ }
239+ }
221240 //Handle messages from the component
222241 handleMessage ( event ) {
223242 const { COI_method, key} = event . data ;
@@ -226,12 +245,13 @@ class CrossOriginInterface {
226245 if ( ! COI_method || ! key ) {
227246 return ;
228247 }
248+
229249 //Check if message is intended for this COI instance
230250 if ( key !== this . key ) {
231251 return ;
232252 }
233253 console . debug ( "COI with key" , key , "received message" , event . data ) ;
234-
254+
235255 //If component is not registered, only allow registration method
236256 if ( this . component === null ) {
237257 if ( COI_method === 'register' ) {
0 commit comments