It’s the first day I removed my autopublish package to get into the “real world”. Now, I am facing some conceptional problems I did not anticipate. Not really a surprise…
Here’s my scenario:
I have a main object, let’s call it “Thing”. These things are stored in their own collection, Things.
As part of its content, a Thing references a bunch of images. To allow for easy replacement of the actual image storage, I do not store URLs in the Thing itself. Instead, I built an Image Manager package. This image manager maintains it’s own collection named Images, where the actual URLs pointing to the backend storage (S3 based on slingshot in my case) are maintained.
The Thing does not know about this hidden implementation, it just stores references (foreign) keys pointing to images maintained by the media manager, and this media manager is responsible for querying the actual image URLs in S3.
So far, so good.
Now to the subscriptions: As there are quadrillions of potential Things in the database (not yet, but maybe in the future), I cannot subscribe to all of them in the template that shall render a Thing. Instead, I will only subscribe for one Thing at a time. So I place a subscription inside an autorun of the onCreated() method of the Thing’s display template.
This works well for the thing collection itself. But how do I manage the subscriptions for the referenced image objects? Of course, I could place a subscription to the image store collection right inside the same onCreated() method. But this would break separation of concerns, as the Thing should not know anything about the actual implementation of the underlying image store.
So I would rather like to call the Image Manager in the autorun and ask for the URLs matching the referenced keys. There are two ways of doing this that sprang to my mind:
Version 1: Explicit client-server communication using a Method
In the autorun, I call the Image Manager’s API on the client-side. The API method (inside the Image Manager package) picks up this call and hands it over to a Meteor method on the server side which retrieves the image URLs and sends them back via a callback chain.
Version 2: Implicit client-server communication
Instead of calling a Meteor method, the Image Manager API method itself subscribes to the relevant images as soon as it picks up the method call, still on the client side. This would require the autorun dependency management to work across function calls in different packages. I think this is the case, since calling a function in another package is not really different from calling a closure or other function in the originating template, and I think that reactive sources are detected there as well (which is the reason why the very first autorun’s run has to touch all reactive sources at least once).
Which of these two ways would be the best way to do this kind of subscription delegation – or is there a third alternative I am not aware of and that might be much more elegant?
Thanks for reading this long question