{ "version": 3, "sources": ["../ui/app/components/mn_poll.js"], "sourcesContent": ["/*\nCopyright 2015-Present Couchbase, Inc.\n\nUse of this software is governed by the Business Source License included in\nthe file licenses/BSL-Couchbase.txt. As of the Change Date specified in that\nfile, in accordance with the Business Source License, use of this software will\nbe governed by the Apache License, Version 2.0, included in the file\nlicenses/APL2.txt.\n*/\n\nimport angular from 'angular';\nimport _ from 'lodash';\n\nimport mnPromiseHelper from './mn_promise_helper.js';\n\nexport default 'mnPoll';\n\nangular\n .module(\"mnPoll\", [mnPromiseHelper])\n .factory(\"mnPoller\", [\"$q\", \"$timeout\", \"mnPromiseHelper\", mnPollerFactory])\n .factory(\"mnEtagPoller\", [\"mnPoller\", mnEtagPollerFactory]);\n\nfunction mnEtagPollerFactory(mnPoller) {\n function EtagPoller(scope, request, doNotListenVisibilitychange) {\n mnPoller.call(this, scope, request, doNotListenVisibilitychange);\n }\n\n EtagPoller.prototype = Object.create(mnPoller.prototype);\n EtagPoller.prototype.constructor = EtagPoller;\n EtagPoller.prototype.cycle = cycle;\n\n return EtagPoller;\n\n function cycle() {\n var self = this;\n var timestamp = new Date();\n self.doCallPromise = self.doCall(timestamp);\n self.doCallPromise.then(function () {\n if ((self.doCallPromise !== this) || self.isStopped(timestamp)) {\n return;\n }\n self.cycle();\n }.bind(self.doCallPromise));\n return self;\n }\n}\n\nfunction mnPollerFactory($q, $timeout, mnPromiseHelper) {\n\n function Poller(scope, request, doNotListenVisibilitychange) {\n this.deferred = $q.defer();\n this.request = request;\n this.scope = scope;\n\n var self = this;\n function onVisibilitychange() {\n if (document.hidden) {\n self.pause();\n } else {\n self.resume();\n }\n }\n\n scope.$on('$destroy', function () {\n if (!doNotListenVisibilitychange) {\n document.removeEventListener('visibilitychange', onVisibilitychange);\n }\n self.onDestroy();\n });\n\n if (!doNotListenVisibilitychange) {\n document.addEventListener('visibilitychange', onVisibilitychange);\n }\n\n this.latestResult = undefined;\n this.stopTimestamp = undefined;\n this.extractInterval = undefined;\n this.timeout = undefined;\n this.doCallPromise = undefined;\n this.throttledReload = _.debounce(this.reload.bind(this), 100);\n }\n\n Poller.prototype.isStopped = isStopped;\n Poller.prototype.doCall = doCall;\n Poller.prototype.setInterval = setInterval;\n Poller.prototype.cycle = cycle;\n Poller.prototype.doCycle = doCycle;\n Poller.prototype.stop = stop;\n Poller.prototype.subscribe = subscribe;\n Poller.prototype.showSpinner = showSpinner;\n Poller.prototype.reload = reload;\n Poller.prototype.resume = resume;\n Poller.prototype.pause = pause;\n Poller.prototype.reloadOnScopeEvent = reloadOnScopeEvent;\n Poller.prototype.onDestroy = onDestroy;\n Poller.prototype.getLatestResult = getLatestResult;\n Poller.prototype.onResum = onResum;\n\n return Poller;\n\n function onDestroy() {\n this.stop();\n }\n\n function getLatestResult() {\n return this.latestResult;\n }\n\n function isStopped(startTimestamp) {\n return !(angular.isUndefined(this.stopTimestamp) || startTimestamp >= this.stopTimestamp);\n }\n function reloadOnScopeEvent(eventName, vm, spinnerName) {\n var self = this;\n function action() {\n self.reload();\n if (vm) {\n self.showSpinner(vm, spinnerName);\n }\n }\n if (angular.isArray(eventName)) {\n angular.forEach(eventName, function (event) {\n self.scope.$on(event, action);\n });\n } else {\n self.scope.$on(eventName, action);\n }\n return this;\n }\n function setInterval(interval) {\n this.extractInterval = interval;\n return this;\n }\n function reload(keepLatestResult) {\n if (!keepLatestResult) {\n delete this.latestResult;\n }\n this.stop();\n this.cycle();\n return this;\n }\n function showSpinner(vm, name) {\n var self = this;\n mnPromiseHelper(vm, self.doCallPromise).showSpinner(name);\n return self;\n }\n function doCall(timestamp) {\n var self = this;\n var query = angular.isFunction(self.request) ? self.request(self.latestResult) : self.request;\n query.then(function (result) {\n if ((query !== this) || self.isStopped(timestamp)) {\n return;\n }\n self.deferred.notify(result);\n }.bind(query));\n return query;\n }\n function cycle() {\n if (this.isLaunched) {\n return this;\n }\n delete this.stopTimestamp\n this.isLaunched = true;\n this.doCycle();\n return this;\n }\n function doCycle() {\n var self = this;\n var timestamp = new Date();\n self.doCallPromise = self.doCall(timestamp);\n\n if (self.extractInterval) {\n self.doCallPromise.then(function (result) {\n if ((self.doCallPromise !== this) || self.isStopped(timestamp)) {\n return;\n }\n var interval = angular.isFunction(self.extractInterval) ?\n self.extractInterval(result) :\n self.extractInterval;\n self.timeout = $timeout(self.doCycle.bind(self), interval);\n }.bind(self.doCallPromise));\n }\n self.doCallPromise.then(null, function () {\n self.stop(); //stop cycle on any http error;\n });\n return this;\n }\n function onResum(fn) {\n this.onResumCallback = fn;\n return this;\n }\n function resume() {\n this.isPaused = false;\n if (this.state) {\n this.onResumCallback && this.onResumCallback();\n this.reload();\n }\n }\n function pause() {\n if (this.isPaused) {\n return;\n }\n this.state = this.isLaunched;\n this.isPaused = true;\n this.stop();\n }\n function stop() {\n var self = this;\n self.isLaunched = false;\n self.stopTimestamp = new Date();\n $timeout.cancel(self.timeout);\n }\n function subscribe(subscriber, keeper) {\n var self = this;\n self.deferred.promise.then(null, null, angular.isFunction(subscriber) ? function (value) {\n subscriber(value, self.latestResult);\n self.latestResult = value;\n } : function (value) {\n (keeper || self.scope)[subscriber] = value;\n self.latestResult = value;\n });\n return self;\n }\n}\n"], "mappings": "8LAeA,GAAO,iBAAQ,SAEf,UACG,OAAO,SAAU,CAAC,4BAClB,QAAQ,WAAY,CAAC,KAAM,WAAY,kBAAmB,kBAC1D,QAAQ,eAAgB,CAAC,WAAY,sBAExC,6BAA6B,SAAU,CACrC,oBAAoB,MAAO,QAAS,4BAA6B,CAC/D,SAAS,KAAK,KAAM,MAAO,QAAS,6BAD7B,uCAIT,WAAW,UAAY,OAAO,OAAO,SAAS,WAC9C,WAAW,UAAU,YAAc,WACnC,WAAW,UAAU,MAAQ,MAEtB,WAEP,gBAAiB,CACf,GAAI,MAAO,KACP,UAAY,GAAI,MACpB,YAAK,cAAgB,KAAK,OAAO,WACjC,KAAK,cAAc,KAAK,UAAY,CAClC,AAAK,KAAK,gBAAkB,MAAS,KAAK,UAAU,YAGpD,KA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