Skip to content

Commit

Permalink
Update publication.js
Browse files Browse the repository at this point in the history
Promise executor functions should not be async. (no-async-promise-executor)
  • Loading branch information
manueltimita authored Dec 12, 2023
1 parent 59b4d0a commit d45800d
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions lib/publication.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,11 @@ class Publication {
// It's only needed when publish is being recursively run.
this.observeHandle = this.cursor.observe({
added: Meteor.bindEnvironment(async (doc) => {
const addedPromise = new Promise(async (resolve) => {
const alreadyPublished = this.publishedDocs.has(doc._id)

if (alreadyPublished) {
debugLog('Publication.observeHandle.added', `${collectionName}:${doc._id} already published`)
this.publishedDocs.unflagForRemoval(doc._id)
this._republishChildrenOf(doc)
this.subscription.changed(collectionName, doc._id, doc)
} else {
this.publishedDocs.add(collectionName, doc._id)
await this._publishChildrenOf(doc)
this.subscription.added(collectionName, doc)
}

// resolve the promise at the end of the added callback
resolve()
const addedPromise = new Promise((resolve, reject) => {
// call the async function to handle the 'added' logic
this._handleAddedAsync(doc, collectionName)
.then(resolve) // resolve the promise at the end of the 'added' callback
.catch(reject)
})

// store the promise
Expand All @@ -75,6 +64,21 @@ class Publication {
this._unpublishAllDocuments()
}

async _handleAddedAsync(doc, collectionName) {
const alreadyPublished = this.publishedDocs.has(doc._id)

if (alreadyPublished) {
debugLog('Publication.observeHandle.added', `${collectionName}:${doc._id} already published`)
this.publishedDocs.unflagForRemoval(doc._id)
this._republishChildrenOf(doc)
this.subscription.changed(collectionName, doc._id, doc)
} else {
this.publishedDocs.add(collectionName, doc._id)
await this._publishChildrenOf(doc)
this.subscription.added(collectionName, doc)
}
}

async _republish () {
this._stopObservingCursor()

Expand Down

0 comments on commit d45800d

Please sign in to comment.