on master help sign in
Refactor
fe9c4e6iko
example/src/Main.elm+1
@@ -20,6 +20,7 @@ import Widget.Icon as Icon
20import Widget.Material as Material
20import Widget.Material as Material
21
21
22
22
23+main : Ur.Run.Program Model Msg
23main =
24main =
24 Ur.Run.application
25 Ur.Run.application
25 { init =
26 { init =
src/Ur/Run.elm+75−57
@@ -1,4 +1,4 @@
1−module Ur.Run exposing (Model, Msg, application)
1+module Ur.Run exposing (Model, Msg, Program, application)
2
2
3import Browser exposing (Document, UrlRequest)
3import Browser exposing (Document, UrlRequest)
4import Browser.Navigation as Nav
4import Browser.Navigation as Nav
@@ -55,6 +55,10 @@ type Msg msg
55 | RetryRequests
55 | RetryRequests
56
56
57
57
58+type alias Program model msg =
59+ Platform.Program Flags (Model model msg) (Msg msg)
60+
61+
58application :
62application :
59 { init : Url -> Nav.Key -> ( model, Ur.Cmd.Cmd msg )
63 { init : Url -> Nav.Key -> ( model, Ur.Cmd.Cmd msg )
60 , view : model -> Document msg
64 , view : model -> Document msg
@@ -67,66 +71,20 @@ application :
67 , onEventSourceMsg : (JD.Value -> Msg msg) -> Sub (Msg msg)
71 , onEventSourceMsg : (JD.Value -> Msg msg) -> Sub (Msg msg)
68 , urbitUrl : model -> String
72 , urbitUrl : model -> String
69 }
73 }
70− -> Program Flags (Model model msg) (Msg msg)
74+ -> Program model msg
71application inp =
75application inp =
72 let
76 let
73− { init, view, onUrlRequest, onUrlChange, onEventSourceMsg } =
77+ { view, onUrlRequest, onUrlChange } =
74 inp
78 inp
75 in
79 in
76 Browser.application
80 Browser.application
77− { init =
81+ { init = \flags url key -> init inp (inp.init url key) flags
78− \flags u key ->
79− let
80− ( app, appCmds ) =
81− init u key
82−
83− { subscriptions, eventId, subscriptionRequests, subscriptionIntMapping } =
84− processUrSubs
85− 0
86− Dict.empty
87− (inp.urbitSubscriptions app |> (\(Ur.Sub.Internal.Sub x) -> x))
88−
89− ( eventId_, cmds, urReqs ) =
90− processCmd eventId appCmds
91−
92− url =
93− inp.urbitUrl app ++ "/~/channel/" ++ flags.uid
94− in
95− ( { subscriptions = subscriptions
96− , subscriptionIntMapping = subscriptionIntMapping
97− , app = app
98− , connected = False
99− , eventId = eventId_
100− , flags = flags
101− , requestsToRetry = []
102− }
103− , [ cmds
104− , pureCmd NeedsActivation
105− , send
106− { requests = urReqs ++ subscriptionRequests
107− , url = url
108− , error = Noop
109− , success = Noop
110− }
111− ]
112− |> Cmd.batch
113− )
114 , view =
82 , view =
115 \model ->
83 \model ->
116 view model.app
84 view model.app
117 |> (\{ body, title } -> { title = title, body = body |> List.map (Html.map AppMsg) })
85 |> (\{ body, title } -> { title = title, body = body |> List.map (Html.map AppMsg) })
118 , update = update inp
86 , update = update inp
119− , subscriptions =
87+ , subscriptions = subscriptions inp
120− \model ->
121− Sub.batch
122− [ inp.subscriptions model.app |> Sub.map AppMsg
123− , onEventSourceMsg EventSourceMsg
124− , if List.isEmpty model.requestsToRetry then
125− Sub.none
126−
127− else
128− Time.every 1000 (always RetryRequests)
129− ]
130 , onUrlRequest = \req -> onUrlRequest req |> AppMsg
88 , onUrlRequest = \req -> onUrlRequest req |> AppMsg
131 , onUrlChange = \url -> onUrlChange url |> AppMsg
89 , onUrlChange = \url -> onUrlChange url |> AppMsg
132 }
90 }
@@ -153,20 +111,21 @@ update inp msg model =
153 ( appModel, appCmds ) =
111 ( appModel, appCmds ) =
154 inp.update msg_ model.app
112 inp.update msg_ model.app
155
113
156− { subscriptions, eventId, subscriptionRequests, subscriptionIntMapping } =
114+ -- { subscriptions, eventId, subscriptionRequests, subscriptionIntMapping } =
115+ subsResult =
157 processUrSubs
116 processUrSubs
158 model.eventId
117 model.eventId
159 model.subscriptions
118 model.subscriptions
160 (inp.urbitSubscriptions model.app |> (\(Ur.Sub.Internal.Sub x) -> x))
119 (inp.urbitSubscriptions model.app |> (\(Ur.Sub.Internal.Sub x) -> x))
161
120
162 ( eventId_, cmds, urReqs ) =
121 ( eventId_, cmds, urReqs ) =
163− processCmd eventId appCmds
122+ processCmd subsResult.eventId appCmds
164 in
123 in
165 ( { model
124 ( { model
166 | app = appModel
125 | app = appModel
167 , eventId = eventId_
126 , eventId = eventId_
168− , subscriptions = subscriptions
127+ , subscriptions = subsResult.subscriptions
169− , subscriptionIntMapping = model.subscriptionIntMapping |> Dict.union subscriptionIntMapping
128+ , subscriptionIntMapping = model.subscriptionIntMapping |> Dict.union subsResult.subscriptionIntMapping
170 }
129 }
171 , Cmd.batch
130 , Cmd.batch
172 [ cmds
131 [ cmds
@@ -177,9 +136,9 @@ update inp msg model =
177 , requests = urReqs
136 , requests = urReqs
178 }
137 }
179 , send
138 , send
180− { requests = subscriptionRequests
139+ { requests = subsResult.subscriptionRequests
181 , url = url
140 , url = url
182− , error = subscriptionRequests |> List.map (\( _, x ) -> x) |> FailedRequest
141+ , error = subsResult.subscriptionRequests |> List.map (\( _, x ) -> x) |> FailedRequest
183 , success = Noop
142 , success = Noop
184 }
143 }
185 ]
144 ]
@@ -373,3 +332,62 @@ processUrSubs eventId existingSubscriptions urbitSubs_ =
373 , subscriptionRequests =
332 , subscriptionRequests =
374 removedSubscriptionActions ++ (newSubscriptionActions |> List.map (\( id, ( req, _ ) ) -> ( id, req )))
333 removedSubscriptionActions ++ (newSubscriptionActions |> List.map (\( id, ( req, _ ) ) -> ( id, req )))
375 }
334 }
335+
336+
337+init :
338+ { a | urbitSubscriptions : model -> Ur.Sub.Sub msg, urbitUrl : model -> String }
339+ -> ( model, Ur.Cmd.Cmd msg )
340+ -> Flags
341+ ->
342+ ( Model model msg
343+ , Cmd (Msg msg)
344+ )
345+init inp ( app, appCmds ) flags =
346+ let
347+ subsResult =
348+ processUrSubs
349+ 0
350+ Dict.empty
351+ (inp.urbitSubscriptions app |> (\(Ur.Sub.Internal.Sub x) -> x))
352+
353+ ( eventId_, cmds, urReqs ) =
354+ processCmd subsResult.eventId appCmds
355+
356+ url =
357+ inp.urbitUrl app ++ "/~/channel/" ++ flags.uid
358+ in
359+ ( { subscriptions = subsResult.subscriptions
360+ , subscriptionIntMapping = subsResult.subscriptionIntMapping
361+ , app = app
362+ , connected = False
363+ , eventId = eventId_
364+ , flags = flags
365+ , requestsToRetry = []
366+ }
367+ , [ cmds
368+ , pureCmd NeedsActivation
369+ , send
370+ { requests = urReqs ++ subsResult.subscriptionRequests
371+ , url = url
372+ , error = Noop
373+ , success = Noop
374+ }
375+ ]
376+ |> Cmd.batch
377+ )
378+
379+
380+subscriptions :
381+ { a | subscriptions : b -> Sub msg, onEventSourceMsg : (JD.Value -> Msg c) -> Sub (Msg msg) }
382+ -> { d | app : b, requestsToRetry : List e }
383+ -> Sub (Msg msg)
384+subscriptions inp model =
385+ Sub.batch
386+ [ inp.subscriptions model.app |> Sub.map AppMsg
387+ , inp.onEventSourceMsg EventSourceMsg
388+ , if List.isEmpty model.requestsToRetry then
389+ Sub.none
390+
391+ else
392+ Time.every 1000 (always RetryRequests)
393+ ]