From 8178c40b956dff2aa99fedf5e5fd0420a2dbe22d Mon Sep 17 00:00:00 2001 From: Galen Wolfe-Pauly Date: Thu, 31 Jul 2014 16:57:35 -0700 Subject: [PATCH 1/2] urb.js retooling --- arvo/eyre.hoon | 188 +++++++++++++++++++++++++----------- main/lib/urb.js | 249 ++++++++++++++---------------------------------- 2 files changed, 202 insertions(+), 235 deletions(-) diff --git a/arvo/eyre.hoon b/arvo/eyre.hoon index fb55f9b27..ab724d73e 100644 --- a/arvo/eyre.hoon +++ b/arvo/eyre.hoon @@ -1,4 +1,4 @@ -!: :: %eyre, http servant +:: :: %eyre, http servant !? 164 :::: |= pit=vase @@ -291,7 +291,7 @@ %mid =+ str=(trip q.q.luv) =+ scr=|-(^-(tape ?~(mog ~ (xmlt & i.mog $(mog t.mog))))) - =+ rep=(need (repg "" str (weld "" scr))) + =+ rep=(need (repg "" str (weld "" scr))) [%mid p.luv (tact rep)] == ++ lofe :: variables in head @@ -301,7 +301,8 @@ :_ ~ ^- manx :- [%script ~] - (turn vaz |=([a=cord b=tape] :/("var {(trip a)}={b};"))) + :- :/ "window.urb = \{};\0a" + (turn vaz |=([a=cord b=tape] :/("window.urb.{(trip a)}={b};\0a"))) :: ++ lofi :: insert in body |= [mog=(list manx) luv=love] @@ -1104,68 +1105,141 @@ :: ++ duti :: heartbeat script ;script:''' - var heart = { - seqn: 0, - trys: 0, - dely: 30000, - - beat: function() { - var method, perm, url, $this - - method = "put" - perm = "tih" - url = [perm,user,port,heart.seqn] - url = "/"+url.join("/") - - $this = this - - var xhr = new XMLHttpRequest() - xhr.open(method.toUpperCase(), url) - xhr.setRequestHeader("content-type", "text/json") - xhr.send(JSON.stringify({oryx:oryx, xyro: {heart:"beat"}})) - xhr.onload = function () { - heart.seqn++ - heart.trys = 0 - setTimeout(heart.beat,heart.dely) + window.urb.seqn_h = 0 + window.urb.heartbeat = function() { + this.poll({ + type:"heb", + ship:this.ship, + dely:30000, + incs:function() { + window.urb.seqn_h++ } - xhr.onerror = function() { - heart.trys++ - setTimeout(heart.beat,heart.dely*heart.trys) - } - } + },function() { + console.log('heartbeat.') + }) } - heart.beat() + // XX 404 bug + // window.urb.heartbeat() ''' :: ++ duty ;script:''' - if (auto) - { - var tries = 0; - var cnt = 0; - var next = "/gie/"+user+"/"+port+"/"+cnt; - call = function() { - xhr = new XMLHttpRequest(); - xhr.open('GET', next, true); - xhr.addEventListener('load', function() { - if ( this.status >= 500 ) { - return delay(); - } - cnt++; - if ( this.status >= 400 ) { - document.alert("neighbor, please."); - } - document.location.reload(); - }); - xhr.addEventListener('error', delay); - xhr.addEventListener('abort', delay); - xhr.send(); + window.urb.seqn_u = 0 + window.urb.dely = 0 + window.urb.puls = 0 + window.urb.cabs = 0 + window.urb.perms = { + pol:"gie", + sub:"tis", + uns:"tiu", + mes:"tim", + heb:"tih" + } + + window.urb.req = function(method,url,params,json,cb) { + var xhr = new XMLHttpRequest() + xhr.open(method.toUpperCase(), url) + if(json) + xhr.setRequestHeader("content-type", "text/json") + + _data = {} + if(params.data) { _data.data = params.data; } + if(params.ship) { _data.ship = params.ship; } + if(params.path) { _data.path = params.path; } + if(params.appl) { _data.appl = params.appl; } + __data = {oryx: window.urb.oryx, xyro: _data} + xhr.send(JSON.stringify(__data)) + + if(cb) { + xhr.onload = function() { + cb(null,{ + status:this.status, + data:JSON.parse(this.responseText) + }) + } + xhr.onerror = function() { + cb({ + status:this.status, + data:this.responseText + }) + } } - delay = function() { - setTimeout(call,1000*tries); - tries++; + } + + window.urb.reqq = [] + window.urb.qreq = function(method,url,params,json,cb) { + walk = function() { + args = window.urb.reqq.shift() + _args = [].slice.apply(args,[0,4]) + _args.push(function(){ + if(args[4]) + args[4].apply(this,arguments) + if(window.urb.reqq.length > 0) + walk() + }) + window.urb.req.apply(this,_args) } - call(); + l = window.urb.reqq.length + window.urb.reqq.push(arguments); + if(l == 0) { walk() } + } + + window.urb.gsig = function(params) { + return params.appl+","+ + params.path.replace(/[^\x00-\x7F]/g, "")+","+ + params.ship + } + + window.urb.poll = function(params,cb) { + if(!params) + throw new Error("You must supply params to urb.poll.") + + var method, perm, url, $this + + method = "get" + perm = params.type ? this.perms[params.type] : "gie" + url = [perm,this.user,this.port,this.seqn_u] + url = "/"+url.join("/") + + this.puls = 1 + + $this = this + this.req(method,url,params,false,function(err,data) { + if (data.data.reload) { + return document.location.reload() + } else { + fn = $this.gsig(data.data) + if($this.cabs[fn]) { + $this.cabs[fn].call(this,err, + {status: data.status, data: data.data.data}) + } + } + + dely = params.dely ? params.dely : $this.dely + + if(err) + dely = dely+Math.ceil(dely*.02) + else { + $this.dely = 0 + if(params.incs) + params.incs() + else + $this.seqn_u++ + } + + setTimeout(function() { + $this.poll(params,cb) + },dely) + }) + } + + if (window.urb.auto) { + var tries = 0 + var cnt = 0 + var param = { + type:"pol" + } + window.urb.poll(param) } ''' :: diff --git a/main/lib/urb.js b/main/lib/urb.js index 883a7144d..449933c74 100644 --- a/main/lib/urb.js +++ b/main/lib/urb.js @@ -1,187 +1,80 @@ -window.urb = { - ship: ship, - port: port, - auto: auto, - oryx: oryx, - user: user, - seqn_h: 0, - seqn_u: 0, - seqn_s: 0, - dely: 0, - puls: 0, - perms: { - pol:"gie", - sub:"tis", - uns:"tiu", - mes:"tim", - heb:"tih" - }, - cabs: {}, +window.urb.seqn_s = 0 - req: function(method,url,params,json,cb) { - var xhr = new XMLHttpRequest() - xhr.open(method.toUpperCase(), url) - if(json) - xhr.setRequestHeader("content-type", "text/json") +window.urb.send = function(params,cb) { + if(!params) + throw new Error("You must supply params to urb.send.") + if(!params.appl) + throw new Error("You must specify an appl for urb.send.") + if(!params.data) { params.data = {}; } - _data = {} - if(params.data) { _data.data = params.data; } - if(params.ship) { _data.ship = params.ship; } - if(params.path) { _data.path = params.path; } - if(params.appl) { _data.appl = params.appl; } - __data = {oryx: oryx, xyro: _data} - xhr.send(JSON.stringify(__data)) + var method, perm, url, $this - if(cb) { - xhr.onload = function() { - cb(null,{ - "status":this.status, - "data":JSON.parse(this.responseText) - }) - } - xhr.onerror = function() { - cb({ - "status":this.status, - "data":this.responseText - }) - } - } - }, + type = params.type ? params.type : "mes" + perm = this.perms[type] - send: function(params,cb) { - if(!params) - throw new Error("You must supply params to urb.send.") - if(!params.appl) - throw new Error("You must specify an appl for urb.send.") - if(!params.data) { params.data = {}; } + params.ship = params.ship ? params.ship : this.ship - var method, perm, url, $this + method = "put" + url = [perm,this.user,this.port,this.seqn_s] + url = "/"+url.join("/") - type = params.type ? params.type : "mes" - perm = this.perms[type] + this.seqn_s++ - params.ship = params.ship ? params.ship : this.ship - - method = "put" - url = [perm,this.user,this.port,this.seqn_s] - url = "/"+url.join("/") - - this.seqn_s++ - - $this = this - this.req(method,url,params,true,function(err,data) { - if(err) { $this.seqn_s--; } - if(cb) { cb.apply(this,arguments); } - }) - }, - - subscribe: function(params,cb) { - if(!cb) - throw new Error("You must supply a callback to urb.subscribe.") - if(!params) - throw new Error("You must supply params to urb.subscribe.") - if(!params.appl) - throw new Error("You must specify an appl for urb.subscribe.") - if(!params.path) - throw new Error("You must specify a path for urb.subscribe.") - params.ship = params.ship ? params.ship : this.ship - - var method, perm, url, $this - - params.type = "sub" - params.incs = function() { - window.urb.seqn_u++ - } - - this.cabs[params.appl+","+params.path.replace(/[^\x00-\x7F]/g, "")+","+params.ship] = cb - - url = [this.perms["sub"],this.user,this.port] - url = "/"+url.join("/") - method = "put" - - $this = this - this.req(method,url,params,true,function(err,data) { - if(cb) { cb.call(this,err,{status: data.status, data: data.data.data})} - if(!err && $this.puls == 0) { - params.type = "pol" - $this.poll(params) - } - }) - }, - - unsubscribe: function(params,cb) { - if(!params) - throw new Error("You must supply params to urb.unsubscribe.") - if(!params.appl) - throw new Error("You must specify an appl for urb.unsubscribe.") - if(!params.path) - throw new Error("You must specify a path for urb.unsubscribe.") - params.ship = params.ship ? params.ship : this.ship - - method = "put" - type = "uns" - url = [this.perms[type],this.user,this.port] - url = "/"+url.join("/") - - var $this = this - this.req(method,url,params,true,function(err,data) { - fn = params.appl+","+params.path.replace(/[^\x00-\x7F]/g, "")+","+params.ship - $this.cabs[fn]('subscription closed') - }) - }, - - heartbeat: function() { - this.poll({ - type:"heb", - ship:this.ship, - incs:function() { - window.urb.seqn_h++ - } - },function() { - console.log('heartbeat.') - }) - }, - - poll: function(params,cb) { - if(!params) - throw new Error("You must supply params to urb.poll.") - - var method, perm, url, $this - - method = "get" - type = params.type ? params.type : "pol" - perm = this.perms[type] - url = [perm,this.user,this.port,this.seqn_u] - url = "/"+url.join("/") - - this.puls = 1 - - $this = this - this.req(method,url,params,false,function(err,data) { - console.log(data) - if(cb) { - cb.call(this,err,{status: data.status, data: data.data.data}) - } else if (data.data.reload) { - document.location.reload() - } else { - fn = data.data.appl+","+data.data.path.replace(/[^\x00-\x7F]/g, "") - +","+data.data.ship - $this.cabs[fn].call(this,err, - {status: data.status, data: data.data.data}) - } - - if(err) - $this.dely += 1000 - else { - $this.dely = 0 - params.incs() - } - - setTimeout(function() { - $this.poll(params,cb) - },$this.dely) - }) - } + $this = this + this.qreq(method,url,params,true,function(err,data) { + if(err) { $this.seqn_s--; } + if(cb) { cb.apply(this,arguments); } + }) } -auto = false +window.urb.subscribe = function(params,cb) { + if(!cb) + throw new Error("You must supply a callback to urb.subscribe.") + if(!params) + throw new Error("You must supply params to urb.subscribe.") + if(!params.appl) + throw new Error("You must specify an appl for urb.subscribe.") + if(!params.path) + throw new Error("You must specify a path for urb.subscribe.") + params.ship = params.ship ? params.ship : this.ship + + var method, perm, url, $this + + params.type = "sub" + + this.cabs[this.gsig(params)] = cb + + url = [this.perms["sub"],this.user,this.port] + url = "/"+url.join("/") + method = "put" + + $this = this + this.qreq(method,url,params,true,function(err,data) { + if(cb) { cb.apply(this,[err,{status: data.status, data: data.data.data}])} + if(!err && $this.puls == 0) { + params.type = "pol" + $this.poll(params) + } + }) +} + +window.urb.unsubscribe = function(params,cb) { + if(!params) + throw new Error("You must supply params to urb.unsubscribe.") + if(!params.appl) + throw new Error("You must specify an appl for urb.unsubscribe.") + if(!params.path) + throw new Error("You must specify a path for urb.unsubscribe.") + params.ship = params.ship ? params.ship : this.ship + + method = "put" + type = "uns" + url = [this.perms[type],this.user,this.port] + url = "/"+url.join("/") + + var $this = this + this.req(method,url,params,true,function(err,data) { + $this.cabs[$this.gsig(params)]('subscription closed') + delete $this.cabs[$this.gsig(params)] + }) +} From 500acfb39f804eee890eb7794ddb1e8b74f4f80c Mon Sep 17 00:00:00 2001 From: Galen Wolfe-Pauly Date: Fri, 1 Aug 2014 11:21:28 -0700 Subject: [PATCH 2/2] fixed subs --- arvo/eyre.hoon | 51 ++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/arvo/eyre.hoon b/arvo/eyre.hoon index ab724d73e..d35862729 100644 --- a/arvo/eyre.hoon +++ b/arvo/eyre.hoon @@ -1135,21 +1135,20 @@ mes:"tim", heb:"tih" } - + window.urb.req = function(method,url,params,json,cb) { var xhr = new XMLHttpRequest() xhr.open(method.toUpperCase(), url) if(json) xhr.setRequestHeader("content-type", "text/json") - + _data = {} if(params.data) { _data.data = params.data; } if(params.ship) { _data.ship = params.ship; } if(params.path) { _data.path = params.path; } if(params.appl) { _data.appl = params.appl; } __data = {oryx: window.urb.oryx, xyro: _data} - xhr.send(JSON.stringify(__data)) - + if(cb) { xhr.onload = function() { cb(null,{ @@ -1164,45 +1163,53 @@ }) } } + xhr.send(JSON.stringify(__data)) } - + window.urb.reqq = [] + Function.prototype.bind = function(scope) { + var _f = this + return function() { + return _f.apply(scope,arguments) + } + } window.urb.qreq = function(method,url,params,json,cb) { walk = function() { - args = window.urb.reqq.shift() - _args = [].slice.apply(args,[0,4]) - _args.push(function(){ - if(args[4]) - args[4].apply(this,arguments) + qobj = {} + qobj.oargs = window.urb.reqq.shift() + qobj.nargs = [].slice.apply(qobj.oargs,[0,4]) + qobj.nargs.push(function(){ + if(this.oargs[4]) + this.oargs[4].apply(window.urb,arguments) if(window.urb.reqq.length > 0) walk() - }) - window.urb.req.apply(this,_args) + }.bind(qobj)) + window.urb.req.apply(this,qobj.nargs) } l = window.urb.reqq.length window.urb.reqq.push(arguments); if(l == 0) { walk() } } - + window.urb.gsig = function(params) { return params.appl+","+ params.path.replace(/[^\x00-\x7F]/g, "")+","+ params.ship } - + window.urb.poll = function(params,cb) { if(!params) throw new Error("You must supply params to urb.poll.") - + var method, perm, url, $this - + method = "get" perm = params.type ? this.perms[params.type] : "gie" url = [perm,this.user,this.port,this.seqn_u] url = "/"+url.join("/") - + this.puls = 1 - + $this = this this.req(method,url,params,false,function(err,data) { if (data.data.reload) { @@ -1214,9 +1221,9 @@ {status: data.status, data: data.data.data}) } } - + dely = params.dely ? params.dely : $this.dely - + if(err) dely = dely+Math.ceil(dely*.02) else { @@ -1226,13 +1233,13 @@ else $this.seqn_u++ } - + setTimeout(function() { $this.poll(params,cb) },dely) }) } - + if (window.urb.auto) { var tries = 0 var cnt = 0