@@ -1,10 +1,11 @@
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−-}
1+module Ur.Run exposing
2+ ( Model
3+ , Msg
4+ , Program
5+ , application
6+ , document
7+ , element
8+ )
89
910import Browser exposing (Document, UrlRequest)
1011import Browser.Navigation as Nav
@@ -14,15 +15,15 @@ import Html exposing (Html)
1415import Json.Decode as JD
1516import Task
1617import Time
18+import Ur exposing (Agent, Path)
1719import Ur.Cmd
1820import Ur.Cmd.Internal
1921import Ur.Constructor as C
2022import Ur.Deconstructor as D
21−import Ur.NounDiff exposing (Patch, deconstructPatch)
23+import Ur.Phonemic exposing (Ship)
2224import Ur.Requests exposing (..)
2325import Ur.Sub
2426import Ur.Sub.Internal
25−import Ur.Types exposing (..)
2627import Ur.Uw
2728import Url exposing (Url)
2829
@@ -31,9 +32,8 @@ type alias SubDict msg =
3132 Dict
3233 -- (ship, agent, path)
3334 ( String, String, List String )
34− { deconstructor : D.Deconstructor msg
35+ { deconstructor : D.Deconstructor (msg -> msg) msg
3536 , number : Int
36− , sink : Bool
3737 }
3838
3939
@@ -41,8 +41,6 @@ type alias Flags =
4141 { uid : String }
4242
4343
44−{-| An Urbit wrapper around your application model.
45−-}
4644type alias Model app msg =
4745 { subscriptions : SubDict msg
4846 , subscriptionIntMapping : Dict Int ( String, String, List String )
@@ -51,12 +49,9 @@ type alias Model app msg =
5149 , eventId : Int
5250 , flags : Flags
5351 , requestsToRetry : List UrbitRequest
54− , sinks : Dict Int Noun
5552 }
5653
5754
58−{-| An Urbit wrapper around your application messages.
59−-}
6055type Msg msg
6156 = AppMsg msg
6257 | EventSourceMsg JD.Value
@@ -67,14 +62,10 @@ type Msg msg
6762 | RetryRequests
6863
6964
70−{-| A wrapper around your application `Program`.
71−-}
7265type alias Program model msg =
7366 Platform.Program Flags (Model model msg) (Msg msg)
7467
7568
76−{-| The same as `Browser.element` but with urbit stuff added around it.
77−-}
7869element :
7970 { init : ( model, Ur.Cmd.Cmd msg )
8071 , view : model -> Html msg
@@ -95,8 +86,6 @@ element inp =
9586 }
9687
9788
98−{-| The same as `Browser.element` but with urbit stuff added around it.
99−-}
10089document :
10190 { init : ( model, Ur.Cmd.Cmd msg )
10291 , view : model -> Document msg
@@ -120,8 +109,6 @@ document inp =
120109 }
121110
122111
123−{-| The same as `Browser.element` but with urbit stuff added around it.
124−-}
125112application :
126113 { init : Url -> Nav.Key -> ( model, Ur.Cmd.Cmd msg )
127114 , view : model -> Document msg
@@ -174,11 +161,12 @@ update inp msg model =
174161 ( appModel, appCmds ) =
175162 inp.update msg_ model.app
176163
164+ -- { subscriptions, eventId, subscriptionRequests, subscriptionIntMapping } =
177165 subsResult =
178166 processUrSubs
179167 model.eventId
180168 model.subscriptions
181− (inp.urbitSubscriptions appModel |> (\(Ur.Sub.Internal.Sub x) -> x))
169+ (inp.urbitSubscriptions model.app |> (\(Ur.Sub.Internal.Sub x) -> x))
182170
183171 ( eventId_, cmds, urReqs ) =
184172 processCmd subsResult.eventId appCmds
@@ -211,10 +199,12 @@ update inp msg model =
211199 Ok string ->
212200 case
213201 D.runBytes
214− (D.cell D.int (D.cell D.cord D.tar))
202+ (D.cell D.int (D.cell D.cord D.tar)
203+ |> D.map (\a b c -> ( a, b, c ))
204+ )
215205 (Ur.Uw.decode string)
216206 of
217− Just ( messageId, ( messageType, rest ) ) ->
207+ Just ( messageId, messageType, rest ) ->
218208 let
219209 ( eventId, ackReqs ) =
220210 tag model.eventId [ Ack messageId ]
@@ -244,51 +234,14 @@ update inp msg model =
244234 Dict.get messageId model.subscriptionIntMapping
245235 |> Maybe.andThen (\key -> Dict.get key model.subscriptions)
246236 of
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 )
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 )
292245
293246 -- Got a fact for a subscription we do not hold
294247 Nothing ->
@@ -313,11 +266,11 @@ update inp msg model =
313266 NeedsActivation ->
314267 let
315268 ( eventId, reqs ) =
316− [ Poke { ship = "", agent = "hood", mark = "helm-hi", noun = C.cord "Opening airlock!" } ]
269+ [ Poke { ship = "~zod", agent = "hood", mark = "helm-hi", noun = C.cord "Opening airlock!" } ]
317270 |> tag model.eventId
318271 in
319272 ( { model | eventId = eventId }
320− , send { url = url, requests = reqs, success = OpenConnection, error = Noop }
273+ , send { url = url, requests = reqs, success = OpenConnection, error = NeedsActivation }
321274 )
322275
323276 Noop ->
@@ -344,19 +297,6 @@ update inp msg model =
344297 )
345298
346299
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−
360300processCmd : EventId -> Ur.Cmd.Cmd msg -> ( EventId, Cmd (Msg msg), List ( EventId, UrbitRequest ) )
361301processCmd eventId urCmds =
362302 let
@@ -386,10 +326,10 @@ pureCmd msg =
386326
387327processUrSubs :
388328 EventId
389− -> Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId, sink : Bool }
390− -> Dict ( Ship, Agent, Path ) { deconstructor : d, sink : Bool }
329+ -> Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId }
330+ -> Dict ( Ship, Agent, Path ) d
391331 ->
392− { subscriptions : Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId, sink : Bool }
332+ { subscriptions : Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId }
393333 , eventId : EventId
394334 , subscriptionRequests : List ( EventId, UrbitRequest )
395335 , subscriptionIntMapping : Dict EventId ( Ship, Agent, Path )
@@ -398,7 +338,7 @@ processUrSubs eventId existingSubscriptions urbitSubs_ =
398338 let
399339 urbitSubs =
400340 urbitSubs_
401− |> Dict.map (\_ { deconstructor, sink } -> { deconstructor = deconstructor, sink = sink })
341+ |> Dict.map (\_ deconstructor -> { deconstructor = deconstructor })
402342
403343 ( eventId_, newSubscriptionActions ) =
404344 Dict.diff urbitSubs existingSubscriptions
@@ -421,11 +361,10 @@ processUrSubs eventId existingSubscriptions urbitSubs_ =
421361 newSubscriptions =
422362 Dict.merge
423363 (\_ _ x -> x)
424− (\key number { deconstructor, sink } ->
364+ (\key number { deconstructor } ->
425365 Dict.insert key
426366 { deconstructor = deconstructor
427367 , number = number
428− , sink = sink
429368 }
430369 )
431370 (\_ _ x -> x)
@@ -468,7 +407,6 @@ init inp ( app, appCmds ) flags =
468407 inp.urbitUrl app ++ "/~/channel/" ++ flags.uid
469408 in
470409 ( { subscriptions = subsResult.subscriptions
471− , sinks = Dict.empty
472410 , subscriptionIntMapping = subsResult.subscriptionIntMapping
473411 , app = app
474412 , connected = False
@@ -491,7 +429,7 @@ init inp ( app, appCmds ) flags =
491429
492430subscriptions :
493431 { a | subscriptions : b -> Sub msg, onEventSourceMsg : (JD.Value -> Msg c) -> Sub (Msg msg) }
494− -> { d | app : b, requestsToRetry : List e, connected : Bool }
432+ -> { d | app : b, requestsToRetry : List e }
495433 -> Sub (Msg msg)
496434subscriptions inp model =
497435 Sub.batch
@@ -502,9 +440,4 @@ subscriptions inp model =
502440
503441 else
504442 Time.every 1000 (always RetryRequests)
505− , if not model.connected then
506− Time.every 10000 (always NeedsActivation)
507−
508− else
509− Sub.none
510443 ]