subsiphon
Version:
RxJS subscription manager
3 lines (2 loc) • 3.81 kB
JavaScript
var e=require("rxjs");function n(e,n){(null==n||n>e.length)&&(n=e.length);for(var r=0,t=Array(n);r<n;r++)t[r]=e[r];return t}function r(e){var n=function(e){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof n?n:n+""}var t=/*#__PURE__*/function(){function n(){return this.subs={},this.index=0,new Proxy(this,{set:function(n,r,t){return r in n?"add"===r?(n.add=t,!0):(console.error("Attempted to add Subscription value to SubSiphon class methods: "+String(r)+" =",t),!1):t instanceof e.Subscription?(r in n.subs&&(n.subs[r].unsubscribe(),delete n.subs[r]),n.subs[r]=t,!0):(console.error("Attempted to add non-Subscription value to SubSiphon: "+String(r)+" =",t),!1)},get:function(e,n){return n in e?e[n]:e.subs.hasOwnProperty(n)?e.subs[n]:void 0},has:function(e,n){return n in e.subs},deleteProperty:function(e,n){return e.subs[n]&&(e.subs[n].unsubscribe(),delete e.subs[n]),!0},ownKeys:function(e){return Object.keys(e.subs)},getOwnPropertyDescriptor:function(e,n){if(n in e.subs)return{configurable:!0,enumerable:!0,writable:!0,value:e.subs[n]}}})}var t,o,i=n.prototype;return i.unsubscribe=function(){for(var e in this.subs)this.subs[e].unsubscribe()},i.destroy=function(){for(var e in this.subs)this.subs[e].unsubscribe(),delete this.subs[e]},t=n,(o=[{key:"add",set:function(e){for(var n=this.index;n in this.subs;)n+=1;this.index=n,this.subs[n]=e}}])&&function(e,n){for(var t=0;t<n.length;t++){var o=n[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,r(o.key),o)}}(t.prototype,o),Object.defineProperty(t,"prototype",{writable:!1}),t}(),o=new WeakMap,i=function(e){return e&&"function"==typeof e.unsubscribe},s=function(e,n,r){if(i(e))try{e.unsubscribe(),r.log&&console.debug("[Siphon] Unsubscribed: "+n)}catch(e){console.error("[Siphon] Error unsubscribing "+n,e)}else if(Array.isArray(e))for(var t=0;t<e.length;t++){var o=e[t];if(i(o))try{o.unsubscribe(),r.log&&console.debug("[Siphon] Unsubscribed: "+n+"["+t+"]")}catch(e){console.error("[Siphon] Error unsubscribing "+n+"["+t+"]",e)}else r.log&&console.warn("[Siphon] Skipped non-unsubscribable item in "+n+"["+t+"]")}else r.log&&console.warn("[Siphon] "+n+" is not unsubscribable")};exports.Siphon=function(e){return void 0===e&&(e={}),function(r){var t=r.prototype.ngOnDestroy;r.prototype.ngOnDestroy=function(){for(var r,o=Object.keys(this),i=e.include?e.include:o.filter(function(n){var r;return!(null!=(r=e.exclude)&&r.includes(n))}),u=function(e){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,r){if(e){if("string"==typeof e)return n(e,r);var t={}.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(e,r):void 0}}(e))){r&&(e=r);var t=0;return function(){return t>=e.length?{done:!0}:{done:!1,value:e[t++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(i);!(r=u()).done;){var c=r.value;o.includes(c)?s(this[c],c,e):console.warn('[Siphon] Property "'+c+'" does not exist on instance.')}"function"==typeof t&&t.apply(this)}}},exports.SubSiphon=t,exports.createSubSiphon=function(){return new t},exports.siphon=function(n){if(!o.has(n)){var r=new e.Subject;o.set(n,r);var t=n.ngOnDestroy;n.ngOnDestroy=function(){r.next(),r.complete(),o.delete(n),"function"==typeof t&&t.apply(this)}}return e.takeUntil(o.get(n))};
//# sourceMappingURL=index.js.map