Skip to content

Commit

Permalink
Merge pull request #174 from manueltimita/fix-ready-too-early
Browse files Browse the repository at this point in the history
Fixes this.ready() being fired too early
  • Loading branch information
StorytellerCZ authored Jan 2, 2024
2 parents f55f001 + 64af66a commit 856e194
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .versions
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
38 changes: 26 additions & 12 deletions lib/publication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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}`)
Expand All @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions lib/publish_composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})

Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "publish-composite",
"version": "1.8.4",
"version": "1.8.5-beta.2",
"private": true,
"repository": {
"type": "git",
Expand Down

0 comments on commit 856e194

Please sign in to comment.