/* global Counter:true */ Counter = function (name, cursor, interval) { this.name = name; this.cursor = cursor; this.interval = interval || 1000 * 10; this._collectionName = 'counters-collection'; }; // every cursor must provide a collection name via this method Counter.prototype._getCollectionName = function () { return `counter-${this.name}`; }; // the api to publish (async: Meteor 3 awaits _publishCursor) Counter.prototype._publishCursor = async function (sub) { const self = this; sub.added(self._collectionName, self.name, { count: await self.cursor.countAsync() }); const handler = Meteor.setInterval(async () => { try { const count = await self.cursor.countAsync(); sub.changed(self._collectionName, self.name, { count }); } catch (e) { // transient db error: retry on the next tick } }, this.interval); sub.onStop(() => { Meteor.clearInterval(handler); }); return { stop: sub.onStop.bind(sub) }; };