diff --git a/.versions b/.versions index 9a51b09..3edb81c 100644 --- a/.versions +++ b/.versions @@ -32,7 +32,7 @@ htmljs@1.0.11 id-map@1.1.1 inter-process-messaging@0.1.1 jquery@1.11.11 -local-test:reywood:publish-composite@1.8.4 +local-test:reywood:publish-composite@1.8.5-beta.2 logging@1.3.2 meteor@1.11.3 minimongo@1.9.3 @@ -56,7 +56,7 @@ react-fast-refresh@0.2.7 reactive-var@1.0.12 reload@1.3.1 retry@1.1.0 -reywood:publish-composite@1.8.4 +reywood:publish-composite@1.8.5-beta.2 routepolicy@1.1.1 socket-stream-client@0.5.1 spacebars@1.0.13 diff --git a/CHANGELOG.md b/CHANGELOG.md index 912d6de..463835d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v1.8.5 +* Fixed `this.ready()` being fired too early [PR](https://github.com/Meteor-Community-Packages/meteor-publish-composite/pull/174) [manueltimita](https://github.com/manueltimita) + ## v1.8.4 * Added basic TypeScript types [PR](https://github.com/Meteor-Community-Packages/meteor-publish-composite/pull/166) [@storytellercz](https://github.com/sponsors/StorytellerCZ) diff --git a/lib/publication.js b/lib/publication.js index 3ae68f7..5e824de 100644 --- a/lib/publication.js +++ b/lib/publication.js @@ -18,6 +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 = [] } async publish () { @@ -31,18 +33,15 @@ class Publication { // It's only needed when publish is being recursively run. this.observeHandle = this.cursor.observe({ added: Meteor.bindEnvironment(async (doc) => { - 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) - } + 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 + this.addedPromises.push(addedPromise) }), changed: Meteor.bindEnvironment((newDoc, oldDoc) => { debugLog('Publication.observeHandle.changed', `${collectionName}:${newDoc._id}`) @@ -65,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() diff --git a/lib/publish_composite.js b/lib/publish_composite.js index 1188200..8bd74b9 100644 --- a/lib/publish_composite.js +++ b/lib/publish_composite.js @@ -20,6 +20,9 @@ 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)) + debugLog('Meteor.publish', 'ready') this.ready() }) diff --git a/package-lock.json b/package-lock.json index c5243a4..463a3ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "publish-composite", - "version": "1.8.4", + "version": "1.8.5-beta.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.js b/package.js index 3197da5..89e1d32 100644 --- a/package.js +++ b/package.js @@ -2,7 +2,7 @@ Package.describe({ name: 'reywood:publish-composite', summary: 'Publish a set of related documents from multiple collections with a reactive join.', - version: '1.8.4', + version: '1.8.5-beta.2', git: 'https://github.com/Meteor-Community-Packages/meteor-publish-composite' }) @@ -11,7 +11,7 @@ Npm.depends({ }) Package.onUse((api) => { - api.versionsFrom(['1.8.3', '2.8.1', '3.0-alpha.15']) + api.versionsFrom(['1.8.3', '2.8.1', '3.0-alpha.19']) api.use([ 'check', 'ecmascript', diff --git a/package.json b/package.json index 9cd0970..b99a3bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "publish-composite", - "version": "1.8.4", + "version": "1.8.5-beta.2", "private": true, "repository": { "type": "git",