Skip to content

Commit

Permalink
Merge pull request #187 from arianjahiri/async-children-func
Browse files Browse the repository at this point in the history
Add await for children() option
  • Loading branch information
StorytellerCZ authored Feb 17, 2025
2 parents ec9f077 + 5905b17 commit 936f03e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Arguments

The name of the publication

* **`options`** -- *object literal or callback function*
* **`options`** -- *object literal or callback function (supports async)*

An object literal specifying the configuration of the composite publication **or** a function that returns said object literal. If a function is used, it will receive the arguments passed to `Meteor.subscribe('myPub', arg1, arg2, ...)` (much like the `func` argument of [`Meteor.publish`](http://docs.meteor.com/#meteor_publish)). Basically, if your publication will take **no** arguments, pass an object literal for this argument. If your publication **will** take arguments, use a function that returns an object literal.

Expand All @@ -45,7 +45,7 @@ Arguments

A function that returns a MongoDB cursor (e.g., `return Meteor.users.find({ active: true });`)

* **`children`** -- *array (optional)* or *function*
* **`children`** -- *array (optional)* or *function (supports async)*

- An array containing any number of object literals with this same structure
- A function with top level documents as arguments. It helps dynamically build
Expand Down Expand Up @@ -103,9 +103,17 @@ Arguments
find() {
return Notifications.find();
},
children(parentNotification) {
// children is a function that returns an array of objects.
async children(parentNotification) {
// children is a function (can be asynchronous) that returns an array of objects.
// It takes parent documents as arguments and dynamically builds children array.
// async children function allows the use of await to dynamically build children array
const userAllowsNotifications = await CustomUserLibrary.allowsNotifications();
if (!userAllowsNotifications) {
return [];
}
if (parentNotification.type === 'about_post') {
return [{
find(notification) {
Expand Down Expand Up @@ -254,6 +262,7 @@ publishComposite('postsByUser', async function(userId) {
},
children: [
// This section will be similar to that of the previous example.
// Note from above, children can be a function (optionally async) in order to dynamically build the children array
]
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/publication.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Publication {

async _publishChildrenOf (doc) {
const children = typeof this.childrenOptions === 'function'
? this.childrenOptions(doc, ...this.args)
? await this.childrenOptions(doc, ...this.args)
: this.childrenOptions
await Promise.all(children.map(async (options) => {
const pub = new Publication(this.subscription, options, [doc].concat(this.args))
Expand Down

0 comments on commit 936f03e

Please sign in to comment.