/gep/hart.js long-poll utils factored out

This commit is contained in:
Anton Dyudin 2014-09-04 14:37:29 -07:00 committed by Anton Dyudin
parent 2d15399137
commit d83db302f6

View File

@ -139,8 +139,7 @@
hen=duct :: event trace
som=seam :: logical request
pez=pest :: request state
vaz=(list ,[p=cord q=tape]) :: variables
sip=(list manx) :: scripts in result
sip=marl :: injected scripts
== ::
++ rote :: remote server
$: cnt=@ud :: number served
@ -152,6 +151,7 @@
[%aph p=ship q=@ud r=@ud s=json] :: app heartbeat
[%apg p=term q=ship r=mark s=path] :: app get/start
[%apm p=ship q=@ud r=@ud s=hasp t=json] :: message send
[%app p=ship q=(unit ,@ud)] :: script by port
[%aps p=ship q=@ud s=hasp t=path] :: subscribe
[%apu p=ship q=@ud s=hasp t=path] :: unsubscribe
[%cog p=@ud q=@ud] :: console get
@ -315,30 +315,6 @@
=+ rep=:(welp p.hop scr q.q.hop)
[%mid p.luv (tact rep)]
==
++ lofe :: variables in head
|= [vaz=(list ,[p=cord q=tape]) sip=(list manx) luv=love]
%- lofa
:_ luv
%- flop
^- (list manx)
:_ sip
^- manx
:- [%script ~]
:- :/ "window.urb = \{};\0a"
(turn vaz |=([a=cord b=tape] :/("window.urb.{(trip a)}={b};\0a")))
::
++ lofi :: XX unused
|= [mog=(list manx) luv=love]
^- love
?: =(~ mog) luv
?+ -.luv luv
%mid
?. =(/text/html p.luv) luv
=+ str=(trip q.q.luv)
=+ scr=|-(^-(tape ?~(mog "</body>" (xmlt & i.mog $(mog t.mog)))))
=+ rep=(need (repg "</body>" str scr))
[%mid p.luv (tact rep)]
==
::
++ loft :: love to response
|= luv=love
@ -610,7 +586,7 @@
=+ cyz=(need (~(get by wup.sef) ses))
~(. ya [our ses] sef cyz)
::
++ galt
++ galt ::
|= [our=ship ses=hole num=@ud mez=(each bead (list tank))]
^+ +>
=+ suf=(~(get by own) our)
@ -920,116 +896,115 @@
|= [our=ship whu=(unit ship) rul=tape ruf=tape]
^- manx
=+ ^= sic ^- manx
;script
;
; var seal = {
; who: goal,
; url: burl,
; pas: null
; }
; var hist = []
; var hind = 0
; $(
; function() {
; $input = $('#input .line')
; $prompt = $('#input .prompt')
; $prompt.addClass('prefix')
; $output = $('#output')
; $input.focus()
; $('body').click(function() { $input.focus() })
; ctrl = false;
;
; start = function(ship) {
; $prompt.text('vessel: ~')
; $input.attr('placeholder', 'ship-name')
; if(ship) {
; $input.val(ship)
; }
; }
;
; ident = function() {
; seal.who = $input.val()
;
; if( (seal.who.length != 13) &&
; (seal.who.length != 6) &&
; (seal.who.length != 3) )
; {
; $output.text('not a ship name - try again.');
; return false;
; }
;
; if(seal.who !== host) {
; var foreign = {oth: host, ses: session};
; var all = $.extend({}, seal, foreign);
;
; console.log('redirect')
; window.location="http://"+seal.who+".urbit.org/gul"
; + $.params(all);
; return false;
; }
;
; $output.text($prompt.text() + " " + seal.who)
; $input.val('')
; $input.attr('placeholder', 'ronber-bacnub-hanmev-labnyd')
; $prompt.text('secret: ~')
;
; return true;
; }
;
; login = function() {
; seal.pas = $input.val()
;
; output = $output.html()
; console.log($output.html())
; $output.html(output.replace('sorry. please try again.<br>',''))
;
; $.post(form, seal, function(data,xhr,status) {
; console.log(data);
; if(data.ok == true) {
; document.location = data.next;
; } else {
; $output.prepend('sorry. please try again.<br>')
; }
; })
; }
;
; steps = [ident,login]
; step = 0
; start(seal.who)
; if(seal.who) {
; ident()
; step++
; }
;
; $input.on('keydown', function(e) {
; if(e.keyCode == 17) {
; ctrl = true
; return;
; }
;
; if(e.keyCode == 68 &&
; ctrl == true &&
; step == 1) {
; $output.text('')
; step = 0
; start(null)
; return;
; }
;
; if(e.keyCode == 13) {
; if(steps[step]() && step < steps.length-1)
; step++
; return;
; }
; });
;
; $input.on('keyup', function(e) {
; if(e.keyCode == 17) {
; ctrl = false
; }
; });
; })
==
;script:'''
var seal = {
who: goal,
url: burl,
pas: null
}
var hist = []
var hind = 0
$(
function() {
$input = $('#input .line')
$prompt = $('#input .prompt')
$prompt.addClass('prefix')
$output = $('#output')
$input.focus()
$('body').click(function() { $input.focus() })
ctrl = false;
start = function(ship) {
$prompt.text('vessel: ~')
$input.attr('placeholder', 'ship-name')
if(ship) {
$input.val(ship)
}
}
ident = function() {
seal.who = $input.val()
if( (seal.who.length != 13) &&
(seal.who.length != 6) &&
(seal.who.length != 3) )
{
$output.text('not a ship name - try again.');
return false;
}
if(seal.who !== host) {
var foreign = {oth: host, ses: session};
var all = $.extend({}, seal, foreign);
console.log('redirect')
window.location="http://"+seal.who+".urbit.org/gul"
+ $.params(all);
return false;
}
$output.text($prompt.text() + " " + seal.who)
$input.val('')
$input.attr('placeholder', 'ronber-bacnub-hanmev-labnyd')
$prompt.text('secret: ~')
return true;
}
login = function() {
seal.pas = $input.val()
output = $output.html()
console.log($output.html())
$output.html(output.replace('sorry. please try again.<br>',''))
$.post(form, seal, function(data,xhr,status) {
console.log(data);
if(data.ok == true) {
document.location = data.next;
} else {
$output.prepend('sorry. please try again.<br>')
}
})
}
steps = [ident,login]
step = 0
start(seal.who)
if(seal.who) {
ident()
step++
}
$input.on('keydown', function(e) {
if(e.keyCode == 17) {
ctrl = true
return;
}
if(e.keyCode == 68 &&
ctrl == true &&
step == 1) {
$output.text('')
step = 0
start(null)
return;
}
if(e.keyCode == 13) {
if(steps[step]() && step < steps.length-1)
step++
return;
}
});
$input.on('keyup', function(e) {
if(e.keyCode == 17) {
ctrl = false
}
});
})
'''
=+ ^= cof
;=
; var host = '{(trip (rsh 3 1 (scot %p our)))}';
@ -1043,32 +1018,32 @@
;head
;title: urbit login
;script(type "text/javascript", src jqu);
;style
; body {
; margin: 60px 120px;
; font: normal 12px "Menlo" monospace;
; background-color: #000;
; color: #fff;
; }
;
; #output {
;
; }
;
; #input .prompt {
; display: inline-block;
; margin-right: 12px;
; }
;
; #input .line {
; outline: none;
; width: 80%;
; border: 0;
; background-color: transparent;
; color: #fff;
; font: normal 12px "Menlo" monospace;
; }
==
;style:'''
body {
margin: 60px 120px;
font: normal 12px "Menlo" monospace;
background-color: #000;
color: #fff;
}
#output {
}
#input .prompt {
display: inline-block;
margin-right: 12px;
}
#input .line {
outline: none;
width: 80%;
border: 0;
background-color: transparent;
color: #fff;
font: normal 12px "Menlo" monospace;
}
'''
==
=+ ^= bod ^- manx
;body
@ -1150,30 +1125,21 @@
?> ?=(%way pez.u.pup)
$(yov t.yov, q.rey (~(put by q.rey) i.yov u.pup(pez noz)))
::
++ duti :: heartbeat script
;script:'''
window.urb.seqn_h = 0
window.urb.heartbeat = function() {
this.poll({
type:"heb",
ship:this.ship,
dely:30000,
seqn:function() {
return window.urb.seqn_h
},
incs:function() {
window.urb.seqn_h = window.urb.seqn_h+1
}
},function() {
console.log('heartbeat.')
})
}
window.urb.heartbeat()
++ duty |= [our=ship nap=@ud you=ship orx=oryx] :: interface script
^- cord
%^ cat 3
%- crip
=- "window.urb = {(pojo (jobe -))}\0a"
:~
[%ship (jape |1:<our>)]
[%port (jone nap)]
[%auto %b %&]
[%user (jape |1:<you>)]
[%oryx %s orx]
==
'''
::
++ duty
;script:'''
window.urb.seqn_u = 0
window.urb.seqn_h = 0
window.urb.dely = 0
window.urb.puls = 0
window.urb.cabs = {}
@ -1312,6 +1278,23 @@
}
window.urb.poll(param)
}
window.urb.heartbeat = function() {
this.poll({
type:"heb",
ship:this.ship,
dely:30000,
seqn:function() {
return window.urb.seqn_h
},
incs:function() {
window.urb.seqn_h = window.urb.seqn_h+1
}
},function() {
console.log('heartbeat.')
})
}
window.urb.heartbeat()
'''
::
++ fape :: dispatch %ape
@ -1365,6 +1348,18 @@
(need (eccu orx.ced moh))
==
::
++ fapp
|= [fur=(unit term) you=@p paw=path]
?> ?=([~ %js] fur)
?> ?=(?([%hart ~] [@ %hart ~]) paw)
:- ~
:* %app
you
?~ t.paw
~
(some (slav %ui (cat 3 '0i' i.paw)))
==
::
++ faps :: dispatch %aps
|= [fur=(unit term) you=@p paw=path moh=moth]
^- (unit seam)
@ -1612,6 +1607,7 @@
%r :: app response
%s :: app subscribe
%h :: app heartbeat
%p :: app script by port
%n :: now
%u :: app unsubscribe
%z :: app version
@ -1642,6 +1638,7 @@
%l (fool r.pul)
%g (fapg p.q.pul yun paw)
%e (fape p.q.pul yun paw)
%p (fapp p.q.pul yun paw)
==
::
%p
@ -1727,7 +1724,6 @@
*seam
`pest`[%raw pul moh]
~
~
==
==
::
@ -1798,6 +1794,10 @@
:: ~& [%wink-apm +.som.pip]
:- [~ pip(pez %way)]
(yokm num +.som.pip)
::
%app :: script by port
:: ~& [%wink-app +.som.pip]
(yokp num +.som.pip)
::
%aps :: subscribe
:: ~& [%wink-aps +.som.pip]
@ -1913,7 +1913,7 @@
:+ 500
~[content-type/'text/html']
[~ (tact (xmlt | mad ~))]
& [%fin (lofe vaz.pip mog (lopo q.p.p.pez.pip))]
& [%fin (lofa mog (lopo q.p.p.pez.pip))]
==
==
::
@ -1969,6 +1969,17 @@
?~ yon (bust 204 num)
abet:(post:u.yon cnt num hap jon)
::
++ yokp :: script by port
|= [num=@ud you=ship nup=(unit ,@ud)]
^- [(unit pimp) _+>]
?~ nup
$(nup [~ num], +> ~(harp yo num you *sink))
=+ yon=(yolk u.nup)
?~ yon
=+ err=(bust 204 num)
[`(need (~(get by q.rey.err) num)) err]
[- abet:+]:(hark:u.yon num)
::
++ yoks :: subscribe
|= [num=@ud you=ship nap=@ud hap=hasp pax=path]
=+ yon=(yolk nap)
@ -2012,7 +2023,7 @@
(pass(hen hen.q.i.wuh) `p.i.wuh `note`[%g %nuke hap.q.i.wuh you])
==
::
++ beat
++ beat
|= [cnt=@ud num=@ud jon=json]
^- [(unit pimp) _+>]
?. =(cnt num.bet.siq)
@ -2022,6 +2033,18 @@
=. +>.$ (hear:(yule %bet) ~ %& %json !>((joba %a-ok %b %&)))
[`(need (~(get by q.rey) num)) +>.$]
::
++ hark
|= num=@ud
^- [(unit pimp) _+>]
=. +>.$ abet:(busk:(yule %nil) num _@ ~ %& %js !>((duty our nap you orx.ced)))
[`(need (~(get by q.rey) num)) +>.$]
::
++ harp
%_ abet
tim.bet.siq now
num.sub.siq 1
==
::
++ hoop :: request path
|= can=(unit ,@ud)
^- path
@ -2117,22 +2140,17 @@
==
=+ pip=u.pup
=+ ^= sip
?. =(%apg -.som.pip) sip.pip
[duty duti sip.pip]
?. ?=(%apg -.som.pip) sip.pip
=* his q.som.pip
=+ mef=?:(=(our his) "gop" "gip/{|1:<his>}")
[;script(src "/{mef}/{(pojo (jone nap))}/hart.js"); sip.pip]
?~ huq +>.$(..yo (bust 404 num))
%= +>.$
q.rey
%+ ~(put by q.rey) num
^- pimp
=+ quo=|=(a=cord :(weld "\"" (trip a) "\""))
%= pip
pez %new
vaz :~ [%ship (quo (rsh 3 1 (scot %p our)))]
[%port (trip (rsh 3 2 (scot %ui nap)))]
[%auto "true"]
[%oryx (quo orx.ced)]
[%user (quo (rsh 3 1 (scot %p you)))]
==
sip sip
som ^- seam
:+ %sil