Executing TextSearchInit multiple times

Hey there,

I tried a more realistic test case and now I can reproduce this issue. I believe this is due to the JS feature where callback functions are executed synchronously. onDocumentEnd is executed synchronously, and therefore the internal search state is not being reset before the next call to textSearchInit.

Refactoring the code to use a promise chain fixes the issue for me. (This is basically the same fix as your setTimeout, except a bit less “hacky”)

    const queue = ['pdf', 'viewer'];

    function drawAnnotations() {
      if (queue && queue.length) {
        let chain = Promise.resolve();
        for (const nextAnnotation of queue) {
          chain = chain.then(() => {
            return drawAnnotation(nextAnnotation)
          })
        }
      }
    }
  
    function drawAnnotation(phrase) {
      return new Promise(resolve => {
        console.log('drawAnnotation(): ' + phrase);
      
        const searchMode = CoreControls.Search.Mode.HIGHLIGHT;
    
        const searchOptions = {
          fullSearch: true,
          onResult: (result) => {
            if (result.resultCode === CoreControls.Search.ResultCode.FOUND) {
              console.log('onResult: ' + result);
            }
          },
          onDocumentEnd: (result) => {
            resolve();
          }
        }
    
        docViewer.textSearchInit(phrase, searchMode, searchOptions);

      })

    }

    drawAnnotations();

Thanks!
Logan