streams.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict"
  2. define(["bacon", "lib/helper"], function(Bacon, Helper) {
  3. function nodeInfo(ip, ifname) {
  4. return Bacon.fromBinder(function (sink) {
  5. var url = Helper.buildUrl(ip, "dyn/neighbours-nodeinfo", ifname)
  6. var evtSource = new EventSource(url)
  7. evtSource.addEventListener("neighbour", function(e) {
  8. var r = sink(new Bacon.Next(JSON.parse(e.data)))
  9. if (r === Bacon.noMore)
  10. tearDown()
  11. }, false)
  12. evtSource.addEventListener("eot", function() {
  13. evtSource.close()
  14. sink(new Bacon.End())
  15. }, false)
  16. function tearDown() {
  17. evtSource.close()
  18. }
  19. return tearDown
  20. })
  21. }
  22. function simpleStream(url) {
  23. return Bacon.fromBinder(function (sink) {
  24. var evtSource = new EventSource(url)
  25. evtSource.onmessage = function (e) {
  26. var r = sink(new Bacon.Next(JSON.parse(e.data)))
  27. if (r === Bacon.noMore)
  28. tearDown()
  29. }
  30. function tearDown() {
  31. evtSource.close()
  32. }
  33. return tearDown
  34. })
  35. }
  36. function batadv(ip) {
  37. var url = Helper.buildUrl(ip, "dyn/neighbours-batadv")
  38. return simpleStream(url)
  39. }
  40. function stations(ip, ifname) {
  41. var url = Helper.buildUrl(ip, "dyn/stations", ifname)
  42. return simpleStream(url)
  43. }
  44. function statistics(ip) {
  45. var url = Helper.buildUrl(ip, "dyn/statistics")
  46. return simpleStream(url)
  47. }
  48. return { nodeInfo: nodeInfo
  49. , Batadv: batadv
  50. , Stations: stations
  51. , Statistics: statistics
  52. }
  53. })