Skip to content

Instantly share code, notes, and snippets.

@alastaircoote
Last active July 16, 2022 17:19
Show Gist options
  • Save alastaircoote/9351643 to your computer and use it in GitHub Desktop.
Save alastaircoote/9351643 to your computer and use it in GitHub Desktop.
OfflineTracker as JS
var OfflineTrack,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
OfflineTrack = (function() {
function OfflineTrack(dataSendFunc) {
var e;
this.dataSendFunc = dataSendFunc;
this.pageVisibilityChanged = __bind(this.pageVisibilityChanged, this);
this.doOnlineOfflineCheck = __bind(this.doOnlineOfflineCheck, this);
this.sendData = __bind(this.sendData, this);
this.endOfflineTrack = __bind(this.endOfflineTrack, this);
this.setTrackingEnd = __bind(this.setTrackingEnd, this);
this.startOfflineTrack = __bind(this.startOfflineTrack, this);
try {
this.trackingArray = JSON.parse(window.localStorage.getItem("offlineTrack")) || [];
} catch (_error) {
e = _error;
return;
}
window.addEventListener("offline", this.startOfflineTrack, false);
document.addEventListener("visibilitychange", this.pageVisibilityChanged, false);
this.doOnlineOfflineCheck();
}
OfflineTrack.prototype.startOfflineTrack = function() {
var lastTrack;
if (this.isTracking) {
return;
}
this.isTracking = true;
lastTrack = this.trackingArray[this.trackingArray.length - 1];
if ((lastTrack != null ? lastTrack.length : void 0) < 2) {
this.trackingArray.splice(this.trackingArray.length - 1, 1);
}
this.trackingArray.push([Date.now()]);
this.trackInterval = setInterval(this.setTrackingEnd, 30000);
return window.addEventListener("online", this.endOfflineTrack, false);
};
OfflineTrack.prototype.setTrackingEnd = function() {
var currentTrack;
currentTrack = this.trackingArray[this.trackingArray.length - 1];
currentTrack[1] = Date.now();
return window.localStorage.setItem("offlineTrack", JSON.stringify(this.trackingArray));
};
OfflineTrack.prototype.endOfflineTrack = function() {
this.isTracking = false;
if (this.trackInterval) {
clearInterval(this.trackInterval);
}
this.setTrackingEnd();
window.removeEventListener("online", this.endOfflineTrack, false);
return this.sendData();
};
OfflineTrack.prototype.sendData = function() {
var mappedResults, result;
if (this.trackingArray.length === 0) {
return;
}
mappedResults = this.trackingArray.map(function(t) {
return {
start: new Date(t[0]),
end: new Date(t[1])
};
});
return result = this.dataSendFunc(mappedResults, (function(_this) {
return function() {
_this.trackingArray = [];
return window.localStorage.removeItem("offlineTrack");
};
})(this));
};
OfflineTrack.prototype.doOnlineOfflineCheck = function() {
if (navigator.onLine) {
return this.sendData();
} else {
return this.startOfflineTrack();
}
};
OfflineTrack.prototype.pageVisibilityChanged = function() {
if (document.hidden && !navigator.onLine) {
return this.endOfflineTrack();
} else if (!navigator.onLine) {
return this.startOfflineTrack();
} else if (!document.hidden && navigator.onLine) {
return this.sendData();
}
};
return OfflineTrack;
})();
new OfflineTrack(function(data, cb) {
console.log(data);
return cb();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment