Skip to content

Commit

Permalink
Merge pull request #76 from ijager/master
Browse files Browse the repository at this point in the history
Fix multiple MeteorObservable.subscribe calls with identical arguments (issue #43)
  • Loading branch information
dotansimha authored Sep 13, 2017
2 parents bb3bffc + c40f14e commit b060ad4
Showing 1 changed file with 21 additions and 1 deletion.
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 b060ad4

Please sign in to comment.