urbit/app/modulo.hoon

220 lines
4.9 KiB
Plaintext
Raw Normal View History

2019-03-12 03:05:24 +03:00
/- *modulo
/+ *server
2019-03-13 03:47:57 +03:00
/= index
2019-03-12 03:05:24 +03:00
/^ octs
/; as-octs:mimes:html
2019-03-13 03:47:57 +03:00
/: /===/app/modulo/index /html/
2019-03-12 03:05:24 +03:00
/= modulo-js
/^ octs
/; as-octs:mimes:html
/: /===/app/modulo/script /js/
=, format
|%
:: +move: output effect
::
+$ move [bone card]
:: +card: output effect payload
::
+$ card
$% [%connect wire [(unit @t) (list @t)] term]
[%disconnect wire [(unit @t) (list @t)]]
[%http-response =http-event:http]
[%poke wire dock poke]
[%diff %json json]
==
+$ poke
$% [%modulo-bind app=term]
[%modulo-unbind app=term]
==
::
+$ state
$% $: %0
session=(map term @t)
order=(list term)
cur=(unit [term @])
==
==
::
2019-03-13 03:47:57 +03:00
++ session-as-json
|= [cur=(unit [term @]) session=(map term @t) order=(list term)]
^- json
?~ cur
*json
%- pairs:enjs
:~ [%app %s -.u.cur]
[%url %s (~(got by session) -.u.cur)]
:- %list
:- %a
%+ turn order
|= [a=term]
[%s a]
==
::
2019-03-12 03:05:24 +03:00
--
::
|_ [bow=bowl:gall sta=state]
::
++ this .
::
++ prep
|= old=(unit *)
^- (quip move _this)
?~ old
:_ this
[ost.bow [%connect / [~ /] %modulo]]~
[~ this(sta *state)]
2019-03-12 03:05:24 +03:00
:: alerts us that we were bound. we need this because the vane calls back.
::
++ bound
|= [wir=wire success=? binding=binding:http-server]
[~ this]
::
2019-03-13 03:47:57 +03:00
++ peer-applist
|= [pax=path]
^- (quip move _this)
:_ this
[ost.bow %diff %json (session-as-json cur.sta session.sta order.sta)]~
2019-03-12 03:05:24 +03:00
::
++ session-js
^- octs
?~ cur.sta
*octs
%- as-octs:mimes:html
%- crip
;: weld
(trip 'window.onload = function() {')
2019-03-13 03:47:57 +03:00
"window.ship = '{(trip (rsh 3 1 (crip <our.bow>)))}';"
2019-03-12 03:05:24 +03:00
" window.state = "
2019-03-13 03:47:57 +03:00
(en-json:html (session-as-json cur.sta session.sta order.sta))
2019-03-12 03:05:24 +03:00
(trip '}();')
2019-03-13 03:47:57 +03:00
%- trip
'''
document.onkeydown = (event) => {
if (!event.metaKey || event.keyCode !== 75) { return; }
window.parent.postMessage("commandPalette", "*");
};
'''
2019-03-12 03:05:24 +03:00
==
:: +poke-handle-http-request: received on a new connection established
::
++ poke-handle-http-request
%- (require-authorization ost.bow move this)
|= =inbound-request:http-server
^- (quip move _this)
::
=+ request-line=(parse-request-line url.request.inbound-request)
=/ name=@t
=+ back-path=(flop site.request-line)
?~ back-path
!!
2019-03-12 03:05:24 +03:00
i.back-path
::
?: =(name 'session')
:_ this
:~ ^- move
:- ost.bow
:* %http-response
[%start [200 ['content-type' 'application/javascript']~] [~ session-js] %.y]
==
==
?: =(name 'script')
:_ this
:~ ^- move
:- ost.bow
:* %http-response
[%start [200 ['content-type' 'application/javascript']~] [~ modulo-js] %.y]
==
==
::
:_ this
:~ ^- move
:- ost.bow
:* %http-response
2019-03-13 03:47:57 +03:00
[%start [200 ['content-type' 'text/html']~] [~ index] %.y]
2019-03-12 03:05:24 +03:00
==
==
:: +poke-handle-http-cancel: received when a connection was killed
::
++ poke-handle-http-cancel
|= =inbound-request:http-server
^- (quip move _this)
:: the only long lived connections we keep state about are the stream ones.
::
[~ this]
::
++ poke-modulo-bind
|= bin=term
^- (quip move _this)
=/ url (crip "~{(scow %tas bin)}")
?: (~(has by session.sta) bin)
[~ this]
:- [`move`[ost.bow %connect / [~ /[url]] bin] ~]
%= this
session.sta
(~(put by session.sta) bin url)
::
order.sta
(weld order.sta ~[bin])
2019-03-13 03:47:57 +03:00
::
cur.sta
?~ cur.sta `[bin 0]
cur.sta
2019-03-12 03:05:24 +03:00
==
::
++ poke-modulo-unbind
|= bin=term
^- (quip move _this)
=/ url (crip "~{(scow %tas bin)}")
?. (~(has by session.sta) bin)
[~ this]
=/ ind (need (find ~[bin] order.sta))
=/ neworder (oust [ind 1] order.sta)
:- [`move`[ost.bow %disconnect / [~ /(crip "~{(scow %tas bin)}")]] ~]
%= this
session.sta (~(del by session.sta) bin)
order.sta neworder
cur.sta
::
?: =(1 (lent order.sta))
~
?: (lth ind +:(need cur.sta))
`[-:(need cur.sta) (dec +:(need cur.sta))]
?: =(ind +:(need cur.sta))
`[(snag 0 neworder) 0]
cur.sta
==
::
++ poke-modulo-command
|= com=command
^- (quip move _this)
=/ length (lent order.sta)
?~ cur.sta
[~ this]
?: =(length 1)
[~ this]
=/ new-cur=(unit [term @])
?- -.com
%forward
2019-03-13 03:47:57 +03:00
?: =((dec length) +.u.cur.sta)
2019-03-12 03:05:24 +03:00
`[(snag 0 order.sta) 0]
2019-03-13 03:47:57 +03:00
=/ ind +(+.u.cur.sta)
2019-03-12 03:05:24 +03:00
`[(snag ind order.sta) ind]
%back
?: =(0 +.u.cur.sta)
=/ ind (dec length)
`[(snag ind order.sta) ind]
2019-03-13 03:47:57 +03:00
=/ ind (dec +.u.cur.sta)
2019-03-12 03:05:24 +03:00
`[(snag ind order.sta) ind]
2019-03-13 03:47:57 +03:00
%go
=/ ind (find [app.com]~ order.sta)
?~ ind
cur.sta
`[app.com u.ind]
2019-03-12 03:05:24 +03:00
==
:_ this(cur.sta new-cur)
2019-03-13 03:47:57 +03:00
%+ turn (prey:pubsub:userlib /applist bow)
2019-03-12 03:05:24 +03:00
|= [=bone ^]
2019-03-13 03:47:57 +03:00
[bone %diff %json (session-as-json new-cur session.sta order.sta)]
2019-03-12 03:05:24 +03:00
::
--