@@ -110,30 +110,30 @@ export class Datacore extends Component {
110110 this . index ( ) ;
111111 }
112112
113- /** Starts the background initializer . */
114- index ( ) {
113+ /** Indexes all documents in the vault. Wait on this if you want to wait for the whole index to be ready . */
114+ async index ( ) {
115115 // Asynchronously initialize actual content in the background using a lifecycle-respecting object.
116116 const init = ( this . initializer = new DatacoreInitializer ( this ) ) ;
117- init . finished ( ) . then ( ( stats ) => {
118- this . initialized = true ;
119- this . initializer = undefined ;
120- this . removeChild ( init ) ;
117+ this . addChild ( init ) ;
121118
122- this . datastore . touch ( ) ;
123- this . trigger ( "update" , this . revision ) ;
124- this . trigger ( "initialized" ) ;
119+ await init . finished ( ) ;
125120
126- // Clean up any documents which no longer exist in the vault.
127- // TODO: I think this may race with other concurrent operations, so
128- // this may need to happen at the start of init and not at the end.
129- const currentFiles = this . vault . getFiles ( ) . map ( ( file ) => file . path ) ;
130- this . persister . synchronize ( currentFiles ) ;
131- } ) ;
121+ this . initialized = true ;
122+ this . initializer = undefined ;
123+ this . removeChild ( init ) ;
132124
133- this . addChild ( init ) ;
125+ this . datastore . touch ( ) ;
126+ this . trigger ( "update" , this . revision ) ;
127+ this . trigger ( "initialized" ) ;
128+
129+ // Clean up any documents which no longer exist in the vault.
130+ // TODO: I think this may race with other concurrent operations, so
131+ // this may need to happen at the start of init and not at the end.
132+ const currentFiles = this . vault . getFiles ( ) . map ( ( file ) => file . path ) ;
133+ this . persister . synchronize ( currentFiles ) ;
134134 }
135135
136- private rename ( file : TAbstractFile , oldPath : string ) {
136+ private async rename ( file : TAbstractFile , oldPath : string ) {
137137 if ( ! ( file instanceof TFile ) ) {
138138 return ;
139139 }
@@ -142,7 +142,9 @@ export class Datacore extends Component {
142142 // This is less optimal than what can probably be done, but paths are used in a bunch of places
143143 // (for sections, tasks, etc to refer to their parent file) and it requires some finesse to fix.
144144 this . datastore . delete ( oldPath ) ;
145- this . reload ( file ) . then ( ( ) => this . trigger ( "rename" , file . path , oldPath ) ) ;
145+ await this . reload ( file ) ;
146+
147+ this . trigger ( "rename" , file . path , oldPath ) ;
146148
147149 // TODO: For correctness, probably have to either fix links in all linked files OR
148150 // just stop normalizing links in the store. We can traverse the links index to do so
@@ -364,9 +366,16 @@ export class DatacoreInitializer extends Component {
364366 const next = this . queue . pop ( ) ;
365367 if ( next ) {
366368 this . current . push ( next ) ;
367- this . init ( next )
368- . then ( ( result ) => this . handleResult ( next , result ) )
369- . catch ( ( result ) => this . handleResult ( next , result ) ) ;
369+
370+ // Run asynchronously to allow for concurrency.
371+ ( async ( ) => {
372+ try {
373+ const result = await this . init ( next ) ;
374+ this . handleResult ( next , result ) ;
375+ } catch ( error ) {
376+ this . handleResult ( next , { status : "skipped" } ) ;
377+ }
378+ } ) ( ) ;
370379
371380 this . runNext ( ) ;
372381 } else if ( ! next && this . current . length == 0 ) {
0 commit comments