vue-use-query
Version:
vue use query
84 lines (83 loc) • 3.23 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Subscribable } from './subscribable';
import { isServer } from './utils';
/**
* 判断网络情况
*/
var OnlineManager = /** @class */ (function (_super) {
__extends(OnlineManager, _super);
function OnlineManager() {
return _super !== null && _super.apply(this, arguments) || this;
}
OnlineManager.prototype.onSubscribe = function () {
if (!this.removeEventListener) {
this.setDefaultEventListener();
}
};
OnlineManager.prototype.setEventListener = function (setup) {
var _this = this;
if (this.removeEventListener) {
this.removeEventListener();
}
this.removeEventListener = setup(function (online) {
if (typeof online === 'boolean') {
_this.setOnline(online);
}
else {
_this.onOnline();
}
});
};
OnlineManager.prototype.setOnline = function (online) {
this.online = online;
if (online) {
this.onOnline();
}
};
OnlineManager.prototype.onOnline = function () {
this.listeners.forEach(function (listener) {
listener();
});
};
OnlineManager.prototype.isOnline = function () {
if (typeof this.online === 'boolean') {
return this.online;
}
if (typeof navigator === 'undefined' ||
typeof navigator.onLine === 'undefined') {
return true;
}
return navigator.onLine;
};
OnlineManager.prototype.setDefaultEventListener = function () {
if (!isServer && (window === null || window === void 0 ? void 0 : window.addEventListener)) {
this.setEventListener(function (onOnline) {
var listener = function () { return onOnline(); };
// Listen to online
window.addEventListener('online', listener, false);
window.addEventListener('offline', listener, false);
return function () {
// Be sure to unsubscribe if a new handler is set
window.removeEventListener('online', listener);
window.removeEventListener('offline', listener);
};
});
}
};
return OnlineManager;
}(Subscribable));
export var onlineManager = new OnlineManager();