App switcher

This commit is contained in:
Logan Allen 2019-03-11 17:05:24 -07:00 committed by Isaac Visintainer
parent 801f298eb9
commit 813249b199
13 changed files with 395 additions and 4 deletions

199
app/modulo.hoon Normal file
View File

@ -0,0 +1,199 @@
/- *modulo
/+ *server
/= test-page
/^ octs
/; as-octs:mimes:html
/: /===/app/modulo/index /&html&/!hymn/
/= 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 @])
==
==
::
--
::
|_ [bow=bowl:gall sta=state]
::
++ this .
::
++ prep
|= old=(unit *)
^- (quip move _this)
~& %prep
?~ old
:_ this
[ost.bow [%connect / [~ /] %modulo]]~
:_ this(sta *state)
:~ [ost.bow %poke /bind-subapp [our.bow %modulo] %modulo-bind %subapp]
[ost.bow %poke /bind-subapp1 [our.bow %modulo] %modulo-bind %subapp1]
==
:: alerts us that we were bound. we need this because the vane calls back.
::
++ bound
|= [wir=wire success=? binding=binding:http-server]
~& [%bound success]
[~ this]
::
++ session-as-json
^- json
?~ cur.sta
*json
%- pairs:enjs
:~ [%app %s -.u.cur.sta]
[%url %s (~(got by session.sta) u.cur.sta)]
==
::
++ session-js
^- octs
?~ cur.sta
*octs
%- as-octs:mimes:html
%- crip
;: weld
(trip 'window.onload = function() {')
" window.state = "
(en-json:html session-as-json)
(trip '}();')
==
:: +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)
~& [%request-line request-line]
=/ name=@t
=+ back-path=(flop site.request-line)
?~ back-path
'World'
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
[%start [200 ['content-type' 'text/html']~] [~ test-page] %.y]
==
==
:: +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)}")
~& [%poke-mod-bind 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])
==
::
++ poke-modulo-unbind
|= bin=term
^- (quip move _this)
~& [%poke-mod-unbind bin]
=/ 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)
~& [%poke-mod-com com]
=/ length (lent order.sta)
?~ cur.sta
[~ this]
?: =(length 1)
[~ this]
=/ new-cur=(unit [term @])
?- -.com
%forward
?: =(length +.u.cur.sta)
`[(snag 0 order.sta) 0]
=/ ind +(-.u.cur.sta)
`[(snag ind order.sta) ind]
%back
?: =(0 +.u.cur.sta)
=/ ind (dec length)
`[(snag ind order.sta) ind]
=/ ind (dec -.u.cur.sta)
`[(snag ind order.sta) ind]
==
:_ this(cur.sta new-cur)
%+ turn (prey:pubsub:userlib /sessions bow)
|= [=bone ^]
[bone %diff %json session-as-json]
::
--

9
app/modulo/index.hoon Normal file
View File

@ -0,0 +1,9 @@
^- manx
;html
;head
;script(type "application/javascript", src "/~modulo/session.js");
==
;body
;script(type "application/javascript", src "/~modulo/script.js");
==
==

6
app/modulo/script.js Normal file
View File

@ -0,0 +1,6 @@
let iframe = document.createElement('iframe');
iframe.setAttribute('src', window.state.url);
iframe.setAttribute('width', '100%;');
iframe.setAttribute('height', '100%;');
iframe.setAttribute('style', 'border-style: none !important;');
document.body.appendChild(iframe);

55
app/subapp.hoon Normal file
View File

@ -0,0 +1,55 @@
/+ *server
/= index
/^ octs
/; as-octs:mimes:html
/: /===/app/subapp/index /&html&/!hymn/
::
|%
:: +move: output effect
::
+$ move [bone card]
:: +card: output effect payload
::
+$ card
$% [%poke wire dock poke]
[%http-response =http-event:http]
==
+$ poke
$% [%modulo-bind app=term]
[%modulo-unbind app=term]
==
--
::
|_ [bol=bowl:gall sta=@t]
::
++ this .
::
++ poke-noun
|= asd=?(%bind %unbind)
^- (quip move _this)
:_ this
?: =(%bind asd)
[ost.bol %poke /subapp [our.bol %modulo] `poke`[%modulo-bind %subapp]]~
[ost.bol %poke /subapp [our.bol %modulo] `poke`[%modulo-unbind %subapp]]~
++ prep
|= old=(unit @t)
^- (quip move _this)
~& %prep
:- [ost.bol %poke /subapp [our.bol %modulo] [%modulo-bind %subapp]]~
?~ old
this
this(sta u.old)
::
++ poke-handle-http-request
%- (require-authorization ost.bol move this)
|= =inbound-request:http-server
^- (quip move _this)
:_ this
:~ ^- move
:- ost.bol
:* %http-response
[%start [200 ['content-type' 'text/html']~] [~ index] %.y]
==
==
--

6
app/subapp/index.hoon Normal file
View File

@ -0,0 +1,6 @@
^- manx
;html
;body
;h1: Subapp
==
==

55
app/subapp1.hoon Normal file
View File

@ -0,0 +1,55 @@
/+ *server
/= index
/^ octs
/; as-octs:mimes:html
/: /===/app/subapp1/index /&html&/!hymn/
::
|%
:: +move: output effect
::
+$ move [bone card]
:: +card: output effect payload
::
+$ card
$% [%poke wire dock poke]
[%http-response =http-event:http]
==
+$ poke
$% [%modulo-bind app=term]
[%modulo-unbind app=term]
==
--
::
|_ [bol=bowl:gall sta=@t]
::
++ this .
::
++ poke-noun
|= asd=?(%bind %unbind)
^- (quip move _this)
:_ this
?: =(%bind asd)
[ost.bol %poke /subapp1 [our.bol %modulo] `poke`[%modulo-bind %subapp1]]~
[ost.bol %poke /subapp1 [our.bol %modulo] `poke`[%modulo-unbind %subapp1]]~
++ prep
|= old=(unit @t)
^- (quip move _this)
~& %prep
:- [ost.bol %poke /subapp1 [our.bol %modulo] [%modulo-bind %subapp1]]~
?~ old
this
this(sta u.old)
::
++ poke-handle-http-request
%- (require-authorization ost.bol move this)
|= =inbound-request:http-server
^- (quip move _this)
:_ this
:~ ^- move
:- ost.bol
:* %http-response
[%start [200 ['content-type' 'text/html']~] [~ index] %.y]
==
==
--

6
app/subapp1/index.hoon Normal file
View File

@ -0,0 +1,6 @@
^- manx
;html
;body
;h1: Subapp1
==
==

29
lib/server.hoon Normal file
View File

@ -0,0 +1,29 @@
=, http-server
|%
::
:: +parse-request-line: take a cord and parse out a url
::
++ parse-request-line
|= url=@t
^- [[(unit @ta) site=(list @t)] args=(list [key=@t value=@t])]
(fall (rush url ;~(plug apat:de-purl:html yque:de-purl:html)) [[~ ~] ~])
::
:: +require-authorization: redirect to the login page when unauthenticated
::
++ require-authorization
|* [=bone move=mold this=*]
|= handler=$-(inbound-request:http-server (quip move _this))
|= =inbound-request:http-server
^- (quip move _this)
::
?: authenticated.inbound-request
(handler inbound-request)
::
:_ this
^- (list move)
=/ redirect=cord
%- crip
"/~/login?redirect={(trip url.request.inbound-request)}"
[bone [%http-response %start [307 ['location' redirect]~] ~ %.y]]~
::
--

6
mar/modulo/bind.hoon Normal file
View File

@ -0,0 +1,6 @@
|_ ter=term
++ grab
|%
++ noun term
--
--

7
mar/modulo/command.hoon Normal file
View File

@ -0,0 +1,7 @@
/- *modulo
|_ com=command
++ grab
|%
++ noun command
--
--

6
mar/modulo/unbind.hoon Normal file
View File

@ -0,0 +1,6 @@
|_ ter=term
++ grab
|%
++ noun term
--
--

6
sur/modulo.hoon Normal file
View File

@ -0,0 +1,6 @@
|%
+$ command
$% [%forward ~]
[%back ~]
==
--

View File

@ -1328,10 +1328,11 @@
$wind `%j
$wipe `%f
::
$request `%l
$serve `%r
$connect `%r
$rule `%r
$request `%l
$serve `%r
$connect `%r
$disconnect `%r
$rule `%r
==
--
--