diff --git a/dist/MeteorObservable.js b/dist/MeteorObservable.js index 82a42cfb..19e65e26 100644 --- a/dist/MeteorObservable.js +++ b/dist/MeteorObservable.js @@ -1,6 +1,7 @@ 'use strict'; import { Observable } from 'rxjs'; import { isMeteorCallbacks, forkZone, removeObserver } from './utils'; +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!"); } @@ -157,9 +158,23 @@ export 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(); + }); }; }); }; diff --git a/dist/MeteorObservable.js.map b/dist/MeteorObservable.js.map index 40bdd642..81b7ed93 100644 --- a/dist/MeteorObservable.js.map +++ b/dist/MeteorObservable.js.map @@ -1 +1 @@ -{"version":3,"file":"MeteorObservable.js","sourceRoot":"","sources":["../src/MeteorObservable.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;OAEN,EAAC,UAAU,EAAa,MAAM,MAAM;OACpC,EAAC,iBAAiB,EAAE,QAAQ,EAAE,cAAc,EAAC,MAAM,SAAS;AAEnE,8BAA8B,MAAc;IAC1C,MAAM,IAAI,KAAK,CACb,aAAW,MAAM,sIAEyC,CAAC,CAAC;AAChE,CAAC;AAED;;;;;;;;;GASG;AACH;IAAA;IAmMA,CAAC;IAjMC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACW,qBAAI,GAAlB,UAAsB,IAAY;QAAE,cAAc;aAAd,WAAc,CAAd,s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CAAC,CAAC,CAAC,CAAC,CAAC;wBACX,iBAAiB,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;oBACjC,CAAC;oBAED,UAAU,CAAC,IAAI,EAAE,CAAC;gBAEpB,CAAC,CAAC,CAAC;YACP,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACW,wBAAO,GAArB;QACE,IAAI,IAAI,GAAG,QAAQ,EAAE,CAAC;QACtB,IAAI,SAAS,GAAG,EAAE,CAAC;QACnB,IAAI,OAAO,GAAG;YACZ,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,UAAC,WAAgC;gBACtD,IAAI,CAAC,GAAG,CAAC;oBACP,SAAS,CAAC,OAAO,CAAC,UAAA,QAAQ,IAAI,OAAA,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,EAA1B,CAA0B,CAAC,CAAC;gBAC5D,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,IAAI,OAAO,GAAG,IAAI,CAAC;QACnB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAC,QAAwD;YAChF,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACzB,0BAA0B;YAC1B,EAAE,CAAC,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC;gBACrB,OAAO,GAAG,OAAO,EAAE,CAAC;YACtB,CAAC;YACD,MAAM,CAAC;gBACL,cAAc,CAAC,SAAS,EACtB,QAAQ,EAAE,cAAM,OAAA,OAAO,CAAC,IAAI,EAAE,EAAd,CAAc,CAAC,CAAC;YACpC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IACH,uBAAC;AAAD,CAAC,AArND,IAqNC"} \ No newline at end of file diff --git a/dist/bundles/index.umd.js b/dist/bundles/index.umd.js index c4928c6a..4c308229 100644 --- a/dist/bundles/index.umd.js +++ b/dist/bundles/index.umd.js @@ -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!"); } @@ -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(); + }); }; }); }; diff --git a/src/MeteorObservable.ts b/src/MeteorObservable.ts index 8e05161e..4916f91d 100644 --- a/src/MeteorObservable.ts +++ b/src/MeteorObservable.ts @@ -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: @@ -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(); + + }); }; }); }