Skip to content

Commit

Permalink
Merge pull request #179 from redabourial/master
Browse files Browse the repository at this point in the history
fixed race condition waiting for documents to be added
  • Loading branch information
StorytellerCZ authored Feb 28, 2024
2 parents 540666f + b8de4d0 commit e32ffe5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 9 additions & 3 deletions lib/publication.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Publication {
this.childrenOptions = options.children || []
this.publishedDocs = new PublishedDocumentList()
this.collectionName = options.collectionName
// property to store promises for added callbacks
this.addedPromises = []
this.promises = []
this.childrenPublication = []
}

async publish () {
Expand All @@ -41,7 +41,7 @@ class Publication {
})

// store the promise
this.addedPromises.push(addedPromise)
this.promises.push(addedPromise)
}),
changed: Meteor.bindEnvironment((newDoc, oldDoc) => {
debugLog('Publication.observeHandle.changed', `${collectionName}:${newDoc._id}`)
Expand Down Expand Up @@ -105,6 +105,7 @@ class Publication {
: this.childrenOptions
await Promise.all(children.map(async (options) => {
const pub = new Publication(this.subscription, options, [doc].concat(this.args))
this.childrenPublication.push(pub)
this.publishedDocs.addChildPub(doc._id, pub)
await pub.publish()
}))
Expand Down Expand Up @@ -169,6 +170,11 @@ class Publication {
publication.unpublish()
})
}

async awaitPromises () {
await Promise.all(this.promises)
await Promise.all(this.childrenPublication.map(p => p.awaitPromises()))
}
}

export default Publication
4 changes: 1 addition & 3 deletions lib/publish_composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ function publishComposite (name, options) {
publications.forEach(pub => pub.unpublish())
})

// wait for all publications to finish processing initial added callbacks
await Promise.all(publications.flatMap(pub => pub.addedPromises))

await Promise.all(publications.map(p => p.awaitPromises()))
debugLog('Meteor.publish', 'ready')
this.ready()
})
Expand Down

0 comments on commit e32ffe5

Please sign in to comment.