Skip to content

Commit

Permalink
Store subscriptionIds to handle re-subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ijager committed Dec 2, 2016
1 parent 3ea6542 commit 8ddd5f1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
17 changes: 16 additions & 1 deletion dist/MeteorObservable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/MeteorObservable.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion dist/bundles/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ var ObservableCursor = (function (_super) {
* @property {Boolean} upsert - True to use upsert logic.
*/

var liveSubscriptions = [];
function throwInvalidCallback(method) {
throw new Error("Invalid " + method + " arguments:\n your last param can't be a callback function, \n please remove it and use \".subscribe\" of the Observable!");
}
Expand Down Expand Up @@ -626,9 +627,23 @@ var MeteorObservable = (function () {
// Execute subscribe lazily.
if (subHandler === null) {
subHandler = subscribe();
if (liveSubscriptions.find(function (sub) { return sub === subHandler.subscriptionId; })) {
// subscription already exists, call observer.next() since Meteor won't.
observer.next();
}
else {
liveSubscriptions.push(subHandler.subscriptionId);
}
}
return function () {
removeObserver(observers, observer, function () { return subHandler.stop(); });
removeObserver(observers, observer, function () {
// remove subscription from liveSubscriptions list
var i = liveSubscriptions.findIndex(function (sub) { return sub === subHandler.subscriptionId; });
if (i > -1) {
liveSubscriptions.splice(i, 1);
}
subHandler.stop();
});
};
});
};
Expand Down
22 changes: 21 additions & 1 deletion src/MeteorObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import {Observable, Subscriber} from 'rxjs';
import {isMeteorCallbacks, forkZone, removeObserver} from './utils';

let liveSubscriptions = [];

function throwInvalidCallback(method: string) {
throw new Error(
`Invalid ${method} arguments:
Expand Down Expand Up @@ -161,10 +163,28 @@ export class MeteorObservable {
// Execute subscribe lazily.
if (subHandler === null) {
subHandler = subscribe();
if (liveSubscriptions.find(sub => sub === subHandler.subscriptionId)) {
// subscription already exists, call observer.next() since Meteor won't.
observer.next();
} else {
liveSubscriptions.push(subHandler.subscriptionId);
}
}
return () => {
removeObserver(observers,
observer, () => subHandler.stop());
observer, () => {
// remove subscription from liveSubscriptions list
let i = liveSubscriptions.findIndex(
sub => sub === subHandler.subscriptionId
);

if (i > -1) {
liveSubscriptions.splice(i, 1);
}

subHandler.stop();

});
};
});
}
Expand Down

0 comments on commit 8ddd5f1

Please sign in to comment.