Skip to content

Commit f669438

Browse files
committed
Better subscription handling
1 parent 02c8362 commit f669438

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-meteor-tracker",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Use Meteor Tracker reactivity inside Vue components",
55
"main": "index.js",
66
"scripts": {

src/vue-plugin.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default {
2424

2525
function prepare() {
2626
this._trackerHandles = [];
27+
this._subsAutorun = {};
28+
this._subs = {};
2729

2830
// $subReady state
2931
defineReactive(this, '$subReady', {});
@@ -95,17 +97,10 @@ export default {
9597
if (meteor.subscribe) {
9698
for (let key in meteor.subscribe) {
9799
((key, options) => {
98-
let sub;
99-
100-
let subscribe = (params) => {
101-
if (sub) {
102-
this.$stopHandle(sub);
103-
}
104-
sub = this.$subscribe(key, ...params);
105-
};
100+
let subscribe = params => this.$subscribe(key, ...params);
106101

107102
if (typeof options === 'function') {
108-
this.$watch(options, (params) => {
103+
this.$watch(options, params => {
109104
subscribe(params);
110105
}, {
111106
immediate: true,
@@ -143,15 +138,28 @@ export default {
143138
methods: {
144139
$subscribe(...args) {
145140
if(args.length > 0) {
141+
const key = args[0];
142+
const oldSub = this._subs[key]
146143
let handle = Vue.config.meteor.subscribe.apply(this, args);
147144
this._trackerHandles.push(handle);
145+
this._subs[key] = handle
146+
147+
// Readiness
148148
if(typeof handle.ready === 'function') {
149-
const key = args[0];
150149
defineReactive(this.$subReady, key, false);
151-
this.$autorun(() => {
152-
this.$subReady[key] = handle.ready();
150+
if (this._subsAutorun[key]) {
151+
this._subsAutorun[key].stop();
152+
}
153+
const autorun = this.$autorun(() => {
154+
const ready = this.$subReady[key] = handle.ready();
155+
// Wait for the new subscription to be ready before stoping the old one
156+
if (ready && oldSub) {
157+
this.$stopHandle(oldSub)
158+
}
153159
});
160+
this._subsAutorun[key] = autorun;
154161
}
162+
155163
return handle;
156164
} else {
157165
throw new Error('You must provide the publication name to $subscribe.');

0 commit comments

Comments
 (0)