olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.87k stars 546 forks source link

Waiting for initialization of the index? #515

Closed fireflysemantics closed 2 years ago

fireflysemantics commented 2 years ago

I'm trying to create a lunr index and use it immediately. Another one of the issues showed how to use promises to do this, but IIUC this is also broken because the promise resolves the index before it is initialized:

function createLunrIndex(todos: Todo[], fieldNames: string[]) {
  return new Promise((resolve, reject) => {
    const idx = lunr(function () {
      fieldNames.forEach((fn) => {
        this.field(fn);
      });
      todos.forEach((todo) => {
        this.add(todo);
      });
    });
    resolve(idx);
  });
}

https://github.com/olivernn/lunr.js/issues/497

This is a stackblitz demo: https://stackblitz.com/edit/firefly-semantics-slice-entity-guide-fuodgg

Is there a way to wait for the initialization of the index?

Also asked about this on SO: https://stackoverflow.com/questions/72116666/waiting-for-the-lunr-index-to-initialize

fireflysemantics commented 2 years ago

Ooops I think I searched for the wrong thing. Closing for now ...