diff options
author | Kristian Lyngstol <kly@kly.no> | 2016-03-12 23:31:36 +0000 |
---|---|---|
committer | Kristian Lyngstol <kly@kly.no> | 2016-03-12 23:31:36 +0000 |
commit | 93cf4e90320df21215a081a389cd2ab98a8a2d0d (patch) | |
tree | e79fede0d7730b13d3282100f405fac3e4b37762 | |
parent | 83e6b5e193799cabdc0af876178f385faef5e172 (diff) |
NMSjs: Bump map handlers and more
The idea is that map handlers just register for events instead of this
periodic update.
-rw-r--r-- | web/nms.gathering.org/js/nms-data.js | 55 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms-map-handlers.js | 3 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms-map.js | 7 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms.js | 9 |
4 files changed, 55 insertions, 19 deletions
diff --git a/web/nms.gathering.org/js/nms-data.js b/web/nms.gathering.org/js/nms-data.js index 67f71a2..7ef11ba 100644 --- a/web/nms.gathering.org/js/nms-data.js +++ b/web/nms.gathering.org/js/nms-data.js @@ -44,6 +44,7 @@ nmsCore.assert = function(cb) { * * nmsData.data[name] - actual data * nmsData.registerSource() - add a source, will be polled periodicall + * nmsData.addHandler() * nmsData.updateSource() - issue a one-off update, outside of whatever * periodic polling might take place */ @@ -124,8 +125,6 @@ nmsData.removeSource = function (name) { * * name: "Local" name. Maps to nmsData[name] * target: URL of the source - * cb: Optional callback - * cbdata: Optional callback data * * This can be called multiple times to add multiple handlers. There's no * guarantee that they will be run in order, but right now they do. @@ -136,26 +135,47 @@ nmsData.removeSource = function (name) { * * FIXME: Should be unified with nmsTimers() somehow. */ -nmsData.registerSource = function(name, target, cb, cbdata) { - var fresh = false; - var cbob = { - name: name, - cb: cb, - cbdata: cbdata - }; +nmsData.registerSource = function(name, target) { if (this._sources[name] == undefined) { - this._sources[name] = { target: target, cbs: [] }; + this._sources[name] = { target: target, cbs: {}, fresh: true }; this._sources[name]['handle'] = setInterval(function(){nmsData.updateSource(name)}, 1000); this.stats.newSource++; - fresh = true; } else { this.stats.oldSource++; } - this._sources[name].cbs.push(cbob); this.stats.pollSets++; - if (fresh) - this.updateSource(name); +} + +nmsData.addHandler = function(name, id, cb, cbdata) { + var cbob = { + id: id, + name: name, + cb: cb, + fresh: true, + cbdata: cbdata + }; + if (id == undefined) { + return; + } + this._sources[name].cbs[id] = cbob; + this.updateSource(name); +} + +/* + * Unregister all handlers with the "id" for all sources. + * + * Mainly used to avoid fini() functions in the map handlers. E.g.: just + * reuse "mapHandler" as id. + */ +nmsData.unregisterHandlerWildcard = function(id) { + for (var v in nmsData._sources) { + this._unregisterHandler(v, id); + } +} + +nmsData._unregisterHandler = function(name, id) { + delete this._sources[name].cbs[id]; } /* @@ -218,6 +238,13 @@ nmsData._genericUpdater = function(name) { } } } else { + for (var i in nmsData._sources[name].cbs) { + var tmp = nmsData._sources[name].cbs[i]; + if (tmp.cb != undefined && tmp.fresh) { + nmsData._sources[name].cbs[i].fresh = false; + tmp.cb(tmp.cbdata); + } + } nmsData.stats.identicalFetches++; } }, diff --git a/web/nms.gathering.org/js/nms-map-handlers.js b/web/nms.gathering.org/js/nms-map-handlers.js index ce1d6db..ec9e1b5 100644 --- a/web/nms.gathering.org/js/nms-map-handlers.js +++ b/web/nms.gathering.org/js/nms-map-handlers.js @@ -329,12 +329,14 @@ function commentUpdater() function commentInit() { + nmsData.addHandler("comments","mapHandler",commentUpdater); setLegend(1,"white","0 comments"); setLegend(2,blue,"Persistent"); setLegend(3,red, "New"); setLegend(4,orange,"Active"); setLegend(5,green ,"Old/inactive only"); } + /* * Testing-function to randomize colors of linknets and switches */ @@ -354,6 +356,7 @@ function randomizeColors() function discoInit() { + nmsData.addHandler("switches","mapHandler",randomizeColors); setNightMode(true); setLegend(1,blue,"Y"); setLegend(2,red, "M"); diff --git a/web/nms.gathering.org/js/nms-map.js b/web/nms.gathering.org/js/nms-map.js index 69a76e0..65b5b68 100644 --- a/web/nms.gathering.org/js/nms-map.js +++ b/web/nms.gathering.org/js/nms-map.js @@ -43,7 +43,8 @@ var nmsMap = nmsMap || { nmsMap.init = function() { this._initContexts(); this._drawBG(); - nmsData.registerSource("switches","/api/public/switches",function(){nmsMap._drawAllSwitches();}); + nmsData.registerSource("switches","/api/public/switches"); + nmsData.addHandler("switches","nmsMap",function(){nmsMap._drawAllSwitches();}); window.addEventListener('resize',nmsMap._resizeEvent,true); document.addEventListener('load',nmsMap._resizeEvent,true); this._drawAllSwitches(); @@ -153,6 +154,10 @@ nmsMap._getBox = function(sw) { nmsMap._drawSwitch = function(sw) { + // XXX: If a handler sets a color before switches are loaded... The + // color will get set fine so this isn't a problem. + if (nmsData.switches == undefined || nmsData.switches.switches == undefined) + return; var box = this._getBox(sw); var color = nmsMap._color[sw]; if (color == undefined) { diff --git a/web/nms.gathering.org/js/nms.js b/web/nms.gathering.org/js/nms.js index 9300b2d..7564d72 100644 --- a/web/nms.gathering.org/js/nms.js +++ b/web/nms.gathering.org/js/nms.js @@ -289,7 +289,10 @@ nms.playback.tick = function() // Update data and force redraw // FIXME: nmsData merge - nms.updater.updater(); + // nms.updater.updater(); + // FIXME: 2: This should not be necsarry. The updaters should be + // data-driven, not time-driven. E.g.: If nmsData upates, the handlers + // should run. } /* * Helper function for safely getting a valid now-epoch @@ -530,14 +533,12 @@ function setUpdater(fo) { nms.updater = undefined; nmsMap.reset(); + nmsData.unregisterHandlerWildcard("mapHandler"); fo.init(); nms.updater = fo; var foo = document.getElementById("updater_name"); foo.innerHTML = fo.name + " "; document.location.hash = fo.tag; - if (nms.ping_data && nms.switches_then && nmsData.switches) { - nms.updater.updater(); - } } /* |