@@ -1,11 +1,10 @@
1−module Ur.Run exposing
2− ( Model
3− , Msg
4− , Program
5− , application
6− , document
7− , element
8− )
1+module Ur.Run exposing (element, document, application, Program, Model, Msg)
2+
3+{-| This module contains functions to run your application with Urbit integration.
4+
5+@docs element, document, application, Program, Model, Msg
6+
7+-}
98
109import Browser exposing (Document, UrlRequest)
1110import Browser.Navigation as Nav
@@ -15,15 +14,15 @@ import Html exposing (Html)
1514import Json.Decode as JD
1615import Task
1716import Time
18−import Ur exposing (Agent, Path)
1917import Ur.Cmd
2018import Ur.Cmd.Internal
2119import Ur.Constructor as C
2220import Ur.Deconstructor as D
23−import Ur.Phonemic exposing (Ship)
21+import Ur.NounDiff exposing (Patch, deconstructPatch)
2422import Ur.Requests exposing (..)
2523import Ur.Sub
2624import Ur.Sub.Internal
25+import Ur.Types exposing (..)
2726import Ur.Uw
2827import Url exposing (Url)
2928
@@ -32,8 +31,9 @@ type alias SubDict msg =
3231 Dict
3332 -- (ship, agent, path)
3433 ( String, String, List String )
35− { deconstructor : D.Deconstructor (msg -> msg) msg
34+ { deconstructor : D.Deconstructor msg
3635 , number : Int
36+ , sink : Bool
3737 }
3838
3939
@@ -41,6 +41,8 @@ type alias Flags =
4141 { uid : String }
4242
4343
44+{-| An Urbit wrapper around your application model.
45+-}
4446type alias Model app msg =
4547 { subscriptions : SubDict msg
4648 , subscriptionIntMapping : Dict Int ( String, String, List String )
@@ -49,9 +51,12 @@ type alias Model app msg =
4951 , eventId : Int
5052 , flags : Flags
5153 , requestsToRetry : List UrbitRequest
54+ , sinks : Dict Int Noun
5255 }
5356
5457
58+{-| An Urbit wrapper around your application messages.
59+-}
5560type Msg msg
5661 = AppMsg msg
5762 | EventSourceMsg JD.Value
@@ -62,10 +67,14 @@ type Msg msg
6267 | RetryRequests
6368
6469
70+{-| A wrapper around your application `Program`.
71+-}
6572type alias Program model msg =
6673 Platform.Program Flags (Model model msg) (Msg msg)
6774
6875
76+{-| The same as `Browser.element` but with urbit stuff added around it.
77+-}
6978element :
7079 { init : ( model, Ur.Cmd.Cmd msg )
7180 , view : model -> Html msg
@@ -86,6 +95,8 @@ element inp =
8695 }
8796
8897
98+{-| The same as `Browser.element` but with urbit stuff added around it.
99+-}
89100document :
90101 { init : ( model, Ur.Cmd.Cmd msg )
91102 , view : model -> Document msg
@@ -109,6 +120,8 @@ document inp =
109120 }
110121
111122
123+{-| The same as `Browser.element` but with urbit stuff added around it.
124+-}
112125application :
113126 { init : Url -> Nav.Key -> ( model, Ur.Cmd.Cmd msg )
114127 , view : model -> Document msg
@@ -161,12 +174,11 @@ update inp msg model =
161174 ( appModel, appCmds ) =
162175 inp.update msg_ model.app
163176
164− -- { subscriptions, eventId, subscriptionRequests, subscriptionIntMapping } =
165177 subsResult =
166178 processUrSubs
167179 model.eventId
168180 model.subscriptions
169− (inp.urbitSubscriptions model.app |> (\(Ur.Sub.Internal.Sub x) -> x))
181+ (inp.urbitSubscriptions appModel |> (\(Ur.Sub.Internal.Sub x) -> x))
170182
171183 ( eventId_, cmds, urReqs ) =
172184 processCmd subsResult.eventId appCmds
@@ -199,12 +211,10 @@ update inp msg model =
199211 Ok string ->
200212 case
201213 D.runBytes
202− (D.cell D.int (D.cell D.cord D.tar)
203− |> D.map (\a b c -> ( a, b, c ))
204− )
214+ (D.cell D.int (D.cell D.cord D.tar))
205215 (Ur.Uw.decode string)
206216 of
207− Just ( messageId, messageType, rest ) ->
217+ Just ( messageId, ( messageType, rest ) ) ->
208218 let
209219 ( eventId, ackReqs ) =
210220 tag model.eventId [ Ack messageId ]
@@ -234,14 +244,51 @@ update inp msg model =
234244 Dict.get messageId model.subscriptionIntMapping
235245 |> Maybe.andThen (\key -> Dict.get key model.subscriptions)
236246 of
237− Just { deconstructor } ->
238− case D.run (D.cell D.ignore (D.cell D.ignore deconstructor)) rest of
239− Just subMsg ->
240− ( model_, pureCmd (AppMsg subMsg) )
241−
242− -- Got gargbage
243− Nothing ->
244− ( model_, ackCmd )
247+ Just { deconstructor, sink } ->
248+ if sink then
249+ case D.run (D.cell D.ignore (D.cell D.ignore deconstructSink) |> D.map (\( (), ( (), s ) ) -> s)) rest of
250+ Just (Flush noun) ->
251+ ( { model | sinks = Dict.insert messageId noun model.sinks }
252+ , case D.run deconstructor noun of
253+ Just subMsg ->
254+ pureCmd (AppMsg subMsg)
255+
256+ -- Got garbage
257+ Nothing ->
258+ Cmd.none
259+ )
260+
261+ Just (Drain patch) ->
262+ case Dict.get messageId model.sinks of
263+ Just oldNoun ->
264+ let
265+ newNoun =
266+ Ur.NounDiff.apply patch oldNoun
267+ in
268+ ( { model | sinks = Dict.insert messageId newNoun model.sinks }
269+ , case D.run deconstructor newNoun of
270+ Just subMsg ->
271+ pureCmd (AppMsg subMsg)
272+
273+ Nothing ->
274+ Cmd.none
275+ )
276+
277+ Nothing ->
278+ ( model, Cmd.none )
279+
280+ -- Got garbage
281+ Nothing ->
282+ ( model, Cmd.none )
283+
284+ else
285+ case D.run (D.cell D.ignore (D.cell D.ignore deconstructor) |> D.map (\( (), ( (), m ) ) -> m)) rest of
286+ Just subMsg ->
287+ ( model_, pureCmd (AppMsg subMsg) )
288+
289+ -- Got gargbage
290+ Nothing ->
291+ ( model_, ackCmd )
245292
246293 -- Got a fact for a subscription we do not hold
247294 Nothing ->
@@ -266,11 +313,11 @@ update inp msg model =
266313 NeedsActivation ->
267314 let
268315 ( eventId, reqs ) =
269− [ Poke { ship = "~zod", agent = "hood", mark = "helm-hi", noun = C.cord "Opening airlock!" } ]
316+ [ Poke { ship = "", agent = "hood", mark = "helm-hi", noun = C.cord "Opening airlock!" } ]
270317 |> tag model.eventId
271318 in
272319 ( { model | eventId = eventId }
273− , send { url = url, requests = reqs, success = OpenConnection, error = NeedsActivation }
320+ , send { url = url, requests = reqs, success = OpenConnection, error = Noop }
274321 )
275322
276323 Noop ->
@@ -297,6 +344,19 @@ update inp msg model =
297344 )
298345
299346
347+type Sink
348+ = Flush Noun
349+ | Drain Patch
350+
351+
352+deconstructSink : D.Deconstructor Sink
353+deconstructSink =
354+ D.oneOf
355+ [ D.cell (D.const D.cord "flush") D.tar |> D.map (\( (), n ) -> Flush n)
356+ , D.cell (D.const D.cord "drain") (D.lazy deconstructPatch) |> D.map (\( (), patch ) -> Drain patch)
357+ ]
358+
359+
300360processCmd : EventId -> Ur.Cmd.Cmd msg -> ( EventId, Cmd (Msg msg), List ( EventId, UrbitRequest ) )
301361processCmd eventId urCmds =
302362 let
@@ -326,10 +386,10 @@ pureCmd msg =
326386
327387processUrSubs :
328388 EventId
329− -> Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId }
330− -> Dict ( Ship, Agent, Path ) d
389+ -> Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId, sink : Bool }
390+ -> Dict ( Ship, Agent, Path ) { deconstructor : d, sink : Bool }
331391 ->
332− { subscriptions : Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId }
392+ { subscriptions : Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId, sink : Bool }
333393 , eventId : EventId
334394 , subscriptionRequests : List ( EventId, UrbitRequest )
335395 , subscriptionIntMapping : Dict EventId ( Ship, Agent, Path )
@@ -338,7 +398,7 @@ processUrSubs eventId existingSubscriptions urbitSubs_ =
338398 let
339399 urbitSubs =
340400 urbitSubs_
341− |> Dict.map (\_ deconstructor -> { deconstructor = deconstructor })
401+ |> Dict.map (\_ { deconstructor, sink } -> { deconstructor = deconstructor, sink = sink })
342402
343403 ( eventId_, newSubscriptionActions ) =
344404 Dict.diff urbitSubs existingSubscriptions
@@ -361,10 +421,11 @@ processUrSubs eventId existingSubscriptions urbitSubs_ =
361421 newSubscriptions =
362422 Dict.merge
363423 (\_ _ x -> x)
364− (\key number { deconstructor } ->
424+ (\key number { deconstructor, sink } ->
365425 Dict.insert key
366426 { deconstructor = deconstructor
367427 , number = number
428+ , sink = sink
368429 }
369430 )
370431 (\_ _ x -> x)
@@ -407,6 +468,7 @@ init inp ( app, appCmds ) flags =
407468 inp.urbitUrl app ++ "/~/channel/" ++ flags.uid
408469 in
409470 ( { subscriptions = subsResult.subscriptions
471+ , sinks = Dict.empty
410472 , subscriptionIntMapping = subsResult.subscriptionIntMapping
411473 , app = app
412474 , connected = False
@@ -429,7 +491,7 @@ init inp ( app, appCmds ) flags =
429491
430492subscriptions :
431493 { a | subscriptions : b -> Sub msg, onEventSourceMsg : (JD.Value -> Msg c) -> Sub (Msg msg) }
432− -> { d | app : b, requestsToRetry : List e }
494+ -> { d | app : b, requestsToRetry : List e, connected : Bool }
433495 -> Sub (Msg msg)
434496subscriptions inp model =
435497 Sub.batch
@@ -440,4 +502,9 @@ subscriptions inp model =
440502
441503 else
442504 Time.every 1000 (always RetryRequests)
505+ , if not model.connected then
506+ Time.every 10000 (always NeedsActivation)
507+
508+ else
509+ Sub.none
443510 ]