@@ -1,6 +1,6 @@
1module Ur.Run exposing (element, document, application, Program, Model, Msg)
1module Ur.Run exposing (element, document, application, Program, Model, Msg)
3−{-| This module contains functions to run your application with Urbit integration.
3+{-|
5@docs element, document, application, Program, Model, Msg
5@docs element, document, application, Program, Model, Msg
@@ -18,7 +18,6 @@ import Ur.Cmd
18import Ur.Cmd.Internal
18import Ur.Cmd.Internal
19import Ur.Constructor as C
19import Ur.Constructor as C
20import Ur.Deconstructor as D
20import Ur.Deconstructor as D
21−import Ur.NounDiff exposing (Patch, deconstructPatch)
22import Ur.Requests exposing (..)
21import Ur.Requests exposing (..)
23import Ur.Sub
22import Ur.Sub
24import Ur.Sub.Internal
23import Ur.Sub.Internal
@@ -31,9 +30,8 @@ type alias SubDict msg =
32 -- (ship, agent, path)
31 -- (ship, agent, path)
33 ( String, String, List String )
32 ( String, String, List String )
34− { deconstructor : D.Deconstructor msg
33+ { deconstructor : D.Deconstructor (msg -> msg) msg
35 , number : Int
34 , number : Int
@@ -51,7 +49,6 @@ type alias Model app msg =
51 , eventId : Int
49 , eventId : Int
52 , flags : Flags
50 , flags : Flags
53 , requestsToRetry : List UrbitRequest
51 , requestsToRetry : List UrbitRequest
54− , sinks : Dict Int Noun
@@ -67,7 +64,7 @@ type Msg msg
67 | RetryRequests
64 | RetryRequests
70−{-| A wrapper around your application `Program`.
67+{-| A wrapper around your application `Program`
72type alias Program model msg =
69type alias Program model msg =
73 Platform.Program Flags (Model model msg) (Msg msg)
70 Platform.Program Flags (Model model msg) (Msg msg)
@@ -174,11 +171,12 @@ update inp msg model =
174 ( appModel, appCmds ) =
171 ( appModel, appCmds ) =
175 inp.update msg_ model.app
172 inp.update msg_ model.app
174+ -- { subscriptions, eventId, subscriptionRequests, subscriptionIntMapping } =
177 subsResult =
175 subsResult =
178 processUrSubs
176 processUrSubs
179 model.eventId
177 model.eventId
180 model.subscriptions
178 model.subscriptions
181− (inp.urbitSubscriptions appModel |> (\(Ur.Sub.Internal.Sub x) -> x))
179+ (inp.urbitSubscriptions model.app |> (\(Ur.Sub.Internal.Sub x) -> x))
183 ( eventId_, cmds, urReqs ) =
181 ( eventId_, cmds, urReqs ) =
184 processCmd subsResult.eventId appCmds
182 processCmd subsResult.eventId appCmds
@@ -211,10 +209,12 @@ update inp msg model =
211 Ok string ->
209 Ok string ->
213 D.runBytes
211 D.runBytes
214− (D.cell D.int (D.cell D.cord D.tar))
212+ (D.cell D.int (D.cell D.cord D.tar)
213+ |> D.map (\a b c -> ( a, b, c ))
215 (Ur.Uw.decode string)
215 (Ur.Uw.decode string)
217− Just ( messageId, ( messageType, rest ) ) ->
217+ Just ( messageId, messageType, rest ) ->
219 ( eventId, ackReqs ) =
219 ( eventId, ackReqs ) =
220 tag model.eventId [ Ack messageId ]
220 tag model.eventId [ Ack messageId ]
@@ -244,51 +244,14 @@ update inp msg model =
244 Dict.get messageId model.subscriptionIntMapping
244 Dict.get messageId model.subscriptionIntMapping
245 |> Maybe.andThen (\key -> Dict.get key model.subscriptions)
245 |> Maybe.andThen (\key -> Dict.get key model.subscriptions)
247− Just { deconstructor, sink } ->
247+ Just { deconstructor } ->
249− case D.run (D.cell D.ignore (D.cell D.ignore deconstructSink) |> D.map (\( (), ( (), s ) ) -> s)) rest of
248+ case D.run (D.cell D.ignore (D.cell D.ignore deconstructor)) 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 ->
249+ Just subMsg ->
254− pureCmd (AppMsg subMsg)
261− Just (Drain patch) ->
262− case Dict.get messageId model.sinks of
266− Ur.NounDiff.apply patch oldNoun
268− ( { model | sinks = Dict.insert messageId newNoun model.sinks }
269− , case D.run deconstructor newNoun of
271− pureCmd (AppMsg subMsg)
250+ ( model_, pureCmd (AppMsg subMsg) )
285− case D.run (D.cell D.ignore (D.cell D.ignore deconstructor) |> D.map (\( (), ( (), m ) ) -> m)) rest of
287− ( model_, pureCmd (AppMsg subMsg) )
289− -- Got gargbage
252+ -- Got gargbage
290− Nothing ->
253+ Nothing ->
291− ( model_, ackCmd )
254+ ( model_, ackCmd )
293 -- Got a fact for a subscription we do not hold
256 -- Got a fact for a subscription we do not hold
294 Nothing ->
257 Nothing ->
@@ -313,11 +276,11 @@ update inp msg model =
313 NeedsActivation ->
276 NeedsActivation ->
315 ( eventId, reqs ) =
278 ( eventId, reqs ) =
316− [ Poke { ship = "", agent = "hood", mark = "helm-hi", noun = C.cord "Opening airlock!" } ]
279+ [ Poke { ship = "~zod", agent = "hood", mark = "helm-hi", noun = C.cord "Opening airlock!" } ]
317 |> tag model.eventId
280 |> tag model.eventId
319 ( { model | eventId = eventId }
282 ( { model | eventId = eventId }
320− , send { url = url, requests = reqs, success = OpenConnection, error = Noop }
283+ , send { url = url, requests = reqs, success = OpenConnection, error = NeedsActivation }
@@ -344,19 +307,6 @@ update inp msg model =
352−deconstructSink : D.Deconstructor Sink
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)
360processCmd : EventId -> Ur.Cmd.Cmd msg -> ( EventId, Cmd (Msg msg), List ( EventId, UrbitRequest ) )
310processCmd : EventId -> Ur.Cmd.Cmd msg -> ( EventId, Cmd (Msg msg), List ( EventId, UrbitRequest ) )
361processCmd eventId urCmds =
311processCmd eventId urCmds =
@@ -386,10 +336,10 @@ pureCmd msg =
387processUrSubs :
337processUrSubs :
389− -> Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId, sink : Bool }
339+ -> Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId }
390− -> Dict ( Ship, Agent, Path ) { deconstructor : d, sink : Bool }
340+ -> Dict ( Ship, Agent, Path ) d
392− { subscriptions : Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId, sink : Bool }
342+ { subscriptions : Dict ( Ship, Agent, Path ) { deconstructor : d, number : EventId }
393 , eventId : EventId
343 , eventId : EventId
394 , subscriptionRequests : List ( EventId, UrbitRequest )
344 , subscriptionRequests : List ( EventId, UrbitRequest )
395 , subscriptionIntMapping : Dict EventId ( Ship, Agent, Path )
345 , subscriptionIntMapping : Dict EventId ( Ship, Agent, Path )
@@ -398,7 +348,7 @@ processUrSubs eventId existingSubscriptions urbitSubs_ =
399 urbitSubs =
349 urbitSubs =
400 urbitSubs_
350 urbitSubs_
401− |> Dict.map (\_ { deconstructor, sink } -> { deconstructor = deconstructor, sink = sink })
351+ |> Dict.map (\_ deconstructor -> { deconstructor = deconstructor })
403 ( eventId_, newSubscriptionActions ) =
353 ( eventId_, newSubscriptionActions ) =
404 Dict.diff urbitSubs existingSubscriptions
354 Dict.diff urbitSubs existingSubscriptions
@@ -421,11 +371,10 @@ processUrSubs eventId existingSubscriptions urbitSubs_ =
421 newSubscriptions =
371 newSubscriptions =
422 Dict.merge
372 Dict.merge
423 (\_ _ x -> x)
373 (\_ _ x -> x)
424− (\key number { deconstructor, sink } ->
374+ (\key number { deconstructor } ->
425 Dict.insert key
375 Dict.insert key
426 { deconstructor = deconstructor
376 { deconstructor = deconstructor
427 , number = number
377 , number = number
431 (\_ _ x -> x)
380 (\_ _ x -> x)
@@ -468,7 +417,6 @@ init inp ( app, appCmds ) flags =
468 inp.urbitUrl app ++ "/~/channel/" ++ flags.uid
417 inp.urbitUrl app ++ "/~/channel/" ++ flags.uid
470 ( { subscriptions = subsResult.subscriptions
419 ( { subscriptions = subsResult.subscriptions
471− , sinks = Dict.empty
472 , subscriptionIntMapping = subsResult.subscriptionIntMapping
420 , subscriptionIntMapping = subsResult.subscriptionIntMapping
473 , app = app
421 , app = app
474 , connected = False
422 , connected = False
@@ -491,7 +439,7 @@ init inp ( app, appCmds ) flags =
492subscriptions :
440subscriptions :
493 { a | subscriptions : b -> Sub msg, onEventSourceMsg : (JD.Value -> Msg c) -> Sub (Msg msg) }
441 { a | subscriptions : b -> Sub msg, onEventSourceMsg : (JD.Value -> Msg c) -> Sub (Msg msg) }
494− -> { d | app : b, requestsToRetry : List e, connected : Bool }
442+ -> { d | app : b, requestsToRetry : List e }
495 -> Sub (Msg msg)
443 -> Sub (Msg msg)
496subscriptions inp model =
444subscriptions inp model =
497 Sub.batch
445 Sub.batch
@@ -502,9 +450,4 @@ subscriptions inp model =
504 Time.every 1000 (always RetryRequests)
452 Time.every 1000 (always RetryRequests)
505− , if not model.connected then
506− Time.every 10000 (always NeedsActivation)