Some of you know I recently launched https://honoringthem.com and one feature it has is real-time collaboration to allow multiple people to work on a memorial together. This is actually the only area where I’m doing pub/sub, the rest is Methods.
Right now I have my subscription housed within an autorun. Something like:
Template.someTemplate.onRendered(function(){
import('./some-imports.js').then((modules)=>{
// bringing in scripts for lightboxes, Quill, etc
({lightGallery, lgHash, lgShare, lgThumbnail, Quill, Sortable, WaveSurfer} = modules);
this.autorun(()=>{
const token = FlowRouter.getParam('token');
if(Meteor.userId() && token && token !== 'new'){
this.subscribe('somePublication', token, ()=>{
// perform initial logic and then set a Session variable that will relate to a helper
Of the various features/sections people can add to a memorial, they’re simply stored in an array. And depending on the feature, I may need to instantiate a lightbox (for images), or in the case of the Obituary section, I need to instantiate a Quill JS editor.
So in my helper it looks sort of like:
Template.someTemplate.helpers({
someHelper: function() {
let token = FlowRouter.getParam('token');
if(Meteor.userId() && token && token !== 'new'){
let memorial = Memorials.findOne();
if(memorial){
// run some checks to see if the ordering of sections has changed
// if so we need to instantiate things like lightboxes, Quill editor, etc.
For the most part, this is all working. My helper does a lot of checks each time the data changes. For example, if a collaborator moved a photos section to come before the Obituary, then for all collaborators currently working in real-time, I need to instantiate lightbox functionality for the newly ordered photos section, and likewise the Quill editor would have been destroyed and would need to be re-instantiated.
The issue: on annoyingly rare and difficult to troubleshoot conditions, the Quill editor will be instantiated twice, resulting in two toolbars. In trying to narrow down the issue, I’m seeing that my helper above is being called dozen of times, which makes it more challenging to debug.
I’ve tried to move all my helper logic to within .autorun
, so that the helper just returns the contents of a Session variable, but the autorun
only seems to fire once when the Template is rendered, not each time the data changes in the subscription.
Any insight on whether my approach is good or bad, and/or how to make .autorun
work in my scenario?