aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristian Lyngstol <kly@kly.no>2016-03-10 23:02:03 +0000
committerKristian Lyngstol <kly@kly.no>2016-03-10 23:02:03 +0000
commit5b000365dd6ebbc9d1dc0b17c024cdc627289c5e (patch)
tree643880ec5aa48bf50248cf97d86665d3e0c9065e
parent10be27c7185c5e68fee49657f780d9ad614922f0 (diff)
NMS: front: Prototyping of new generic poller
Not used yet
-rw-r--r--web/nms.gathering.org/js/nms-data.js142
-rw-r--r--web/nms.gathering.org/test.html9
2 files changed, 151 insertions, 0 deletions
diff --git a/web/nms.gathering.org/js/nms-data.js b/web/nms.gathering.org/js/nms-data.js
new file mode 100644
index 0000000..4dda8e4
--- /dev/null
+++ b/web/nms.gathering.org/js/nms-data.js
@@ -0,0 +1,142 @@
+"use strict";
+
+/**************************************************************************
+ * *
+ * THIS IS WORK IN PROGRESS, NOT CURRENTLY USED! *
+ * *
+ * It WILL eventually replace large chunks of nms.js. But we're not there *
+ * yet. *
+ * *
+ **************************************************************************/
+
+
+/*
+ * This file/module/whatever is an attempt to gather all data collection in
+ * one place.
+ *
+ * It is work in progress.
+ *
+ * The basic idea is to have all periodic data updates unified here, with
+ * stats, tracking of "ajax overflows" and general-purpose error handling
+ * and callbacks and whatnot, instead of all the custom stuff that we
+ * started out with.
+ *
+ * Public interfaces:
+ * nmsData.data[name] - actual data
+ * nmsData.registerSource() - add a source, will be polled periodicall
+ * nmsData.updateSource() - issue a one-off update, outside of whatever
+ * periodic polling might take place
+ */
+
+
+var nmsData = nmsData || {
+ data: {}, // Actual data
+ sources: {},
+
+ // Tracks metdata (hashes/timestamps)
+ poller: {
+ hashes:{},
+ time:{}
+ },
+ // setInterval handlers (and more?)
+ pollers: {
+
+ },
+ stats: {
+ identicalFetches:0,
+ outstandingAjaxRequests:0,
+ ajaxOverflow:0,
+ pollClears:0,
+ pollSets:0
+ }
+};
+
+/*
+ * Register a source.
+ *
+ * name: "Local" name. Maps to nmsData.data[name]
+ * remotename: The primary attribute to get from the remote source.
+ * target: URL of the source
+ * cb: Optional callback
+ * cbdata: Optional callback data
+ *
+ * Update frequency will (eventually) be handled by parsing max-age from
+ * the source. Right now it's hardcoded.
+ *
+ * FIXME: Should be unified with nmsTimers() somehow.
+ */
+nmsData.registerSource = function(name, remotename, target, cb, cbdata) {
+ if(this.pollers[name]) {
+ clearInterval(this.pollers[name]);
+ this.stats.pollClears++;
+ }
+ this.sources[name] = { remotename: remotename, target: target, cb: cb, cbdata: cbdata };
+ this.pollers[name] = setInterval(function(){nmsData.updateSource(name)}, 1000);
+ this.stats.pollSets++;
+}
+
+/*
+ * Updates a source.
+ *
+ * Called on interval, but can also be used to update a source after a
+ * known action that updates the underlying data (e.g: update comments
+ * after a comment is posted).
+ */
+nmsData.updateSource = function(name) {
+ nmsData.genericUpdater(name,
+ this.sources[name].remotename,
+ this.sources[name].target,
+ this.sources[name].cb,
+ this.sources[name].cbdata);
+}
+
+/*
+ * Updates nmsData.data[name] with data fetched from remote target in
+ * variable "remotename". If a callback is provided, it is called with
+ * argument meh.
+ *
+ * This also populates nms.pollers[name] with the server-provided hash.
+ * Only if a change is detected is the callback issued.
+ *
+ * Used by registerSource.
+ */
+nmsData.genericUpdater = function(name, remotename, target, cb, meh) {
+ if (this.stats.outstandingAjaxRequests > 5) {
+ this.stats.ajaxOverflow++;
+ return;
+ }
+ this.stats.outstandingAjaxRequests++;
+ var now = "";
+ /*
+ if (nms.now != false)
+ now = "now=" + nms.now;
+ if (now != "") {
+ if (target.match("\\?"))
+ now = "&" + now;
+ else
+ now = "?" + now;
+ }
+ */
+ $.ajax({
+ type: "GET",
+ url: target + now,
+ dataType: "text",
+ success: function (data, textStatus, jqXHR) {
+ var indata = JSON.parse(data);
+ if (nmsData.poller.hashes[name] != indata['hash']) {
+ nmsData.data[name] = indata[remotename];
+ nmsData.poller.hashes[name] = indata['hash'];
+ nmsData.poller.time[name] = indata['time'];
+ if (cb != undefined) {
+ cb(meh);
+ }
+ } else {
+ nmsData.stats.identicalFetches++;
+ }
+ },
+ complete: function(jqXHR, textStatus) {
+ nmsData.stats.outstandingAjaxRequests--;
+ //updateAjaxInfo();
+ }
+ });
+};
diff --git a/web/nms.gathering.org/test.html b/web/nms.gathering.org/test.html
new file mode 100644
index 0000000..49b240b
--- /dev/null
+++ b/web/nms.gathering.org/test.html
@@ -0,0 +1,9 @@
+<html>
+<body>
+this is just used for prototyping js/nms-data.js and similar.
+<script src="js/jquery.min.js" >
+</script>
+<script src="js/nms-data.js" >
+</script>
+</body>
+</html>