11effect module Browser.Events where { subscription = MySub } exposing
2- ( onAnimationFrame, onAnimationFrameDelta
3- , onKeyPress, onKeyDown, onKeyUp
4- , onClick, onMouseMove, onMouseDown, onMouseUp
5- , onResize, onVisibilityChange, Visibility (..)
6- )
2+ ( onAnimationFrame, onAnimationFrameDelta
3+ , onKeyPress, onKeyDown, onKeyUp
4+ , onClick, onMouseMove, onMouseDown, onMouseUp
5+ , onResize, onVisibilityChange, Visibility (..)
6+ )
77
88{- | In JavaScript, information about the root of an HTML document is held in
99the `document` and `window` objects. This module lets you create event
@@ -61,7 +61,7 @@ sync up with the browsers natural refresh rate. This hooks into JavaScript's
6161-}
6262onAnimationFrame : (Time .Posix -> msg ) -> Sub msg
6363onAnimationFrame =
64- AM . onAnimationFrame
64+ AM . onAnimationFrame
6565
6666
6767{- | Just like `onAnimationFrame`, except message is the time in milliseconds
@@ -70,7 +70,7 @@ since the previous frame. So you should get a sequence of values all around
7070-}
7171onAnimationFrameDelta : (Float -> msg ) -> Sub msg
7272onAnimationFrameDelta =
73- AM . onAnimationFrameDelta
73+ AM . onAnimationFrameDelta
7474
7575
7676
@@ -88,7 +88,7 @@ It is more complicated than it should be.
8888-}
8989onKeyPress : Decode .Decoder msg -> Sub msg
9090onKeyPress =
91- on Document " keypress"
91+ on Document " keypress"
9292
9393
9494{- | Subscribe to get codes whenever a key goes down. This can be useful for
@@ -103,7 +103,7 @@ It is more complicated than it should be.
103103-}
104104onKeyDown : Decode .Decoder msg -> Sub msg
105105onKeyDown =
106- on Document " keydown"
106+ on Document " keydown"
107107
108108
109109{- | Subscribe to get codes whenever a key goes up. Often used in combination
@@ -112,7 +112,7 @@ to down and never come back up.
112112-}
113113onKeyUp : Decode .Decoder msg -> Sub msg
114114onKeyUp =
115- on Document " keyup"
115+ on Document " keyup"
116116
117117
118118
@@ -127,21 +127,21 @@ if someone clicked out of it:
127127 import Json.Decode as D
128128
129129 type Msg
130- = ClickOut
130+ = ClickOut
131131
132132 subscriptions : Model -> Sub Msg
133133 subscriptions model =
134- case model.dropDown of
135- Closed _ ->
136- Sub.none
134+ case model.dropDown of
135+ Closed _ ->
136+ Sub.none
137137
138- Open _ ->
139- Events.onClick (D.succeed ClickOut)
138+ Open _ ->
139+ Events.onClick (D.succeed ClickOut)
140140
141141-}
142142onClick : Decode .Decoder msg -> Sub msg
143143onClick =
144- on Document " click"
144+ on Document " click"
145145
146146
147147{- | Subscribe to mouse moves anywhere on screen.
@@ -158,14 +158,14 @@ subscribe when absolutely necessary.
158158-}
159159onMouseMove : Decode .Decoder msg -> Sub msg
160160onMouseMove =
161- on Document " mousemove"
161+ on Document " mousemove"
162162
163163
164164{- | Subscribe to get mouse information whenever the mouse button goes down.
165165-}
166166onMouseDown : Decode .Decoder msg -> Sub msg
167167onMouseDown =
168- on Document " mousedown"
168+ on Document " mousedown"
169169
170170
171171{- | Subscribe to get mouse information whenever the mouse button goes up.
@@ -174,7 +174,7 @@ to be sure keys do not appear to down and never come back up.
174174-}
175175onMouseUp : Decode .Decoder msg -> Sub msg
176176onMouseUp =
177- on Document " mouseup"
177+ on Document " mouseup"
178178
179179
180180
@@ -193,11 +193,11 @@ this](TODO).
193193-}
194194onResize : (Int -> Int -> msg ) -> Sub msg
195195onResize func =
196- on Window " resize" <|
197- Decode . field " target" <|
198- Decode . map2 func
199- ( Decode . field " innerWidth" Decode . int)
200- ( Decode . field " innerHeight" Decode . int)
196+ on Window " resize" <|
197+ Decode . field " target" <|
198+ Decode . map2 func
199+ ( Decode . field " innerWidth" Decode . int)
200+ ( Decode . field " innerHeight" Decode . int)
201201
202202
203203{- | Subscribe to any visibility changes, like if the user switches to a
@@ -213,116 +213,106 @@ different tab or window. When the user looks away, you may want to:
213213-}
214214onVisibilityChange : (Visibility -> msg ) -> Sub msg
215215onVisibilityChange func =
216- let
217- info =
218- Elm . Kernel . Browser . visibilityInfo ()
219- in
220- on Document info. change <|
221- Decode . map ( withHidden func) <|
222- Decode . field " target" <|
223- Decode . field info. hidden Decode . bool
216+ let
217+ info = Elm . Kernel . Browser . visibilityInfo ()
218+ in
219+ on Document info. change <|
220+ Decode . map ( withHidden func) <|
221+ Decode . field " target" <|
222+ Decode . field info. hidden Decode . bool
224223
225224
226225withHidden : (Visibility -> msg ) -> Bool -> msg
227226withHidden func isHidden =
228- func
229- ( if isHidden then
230- Hidden
231-
232- else
233- Visible
234- )
227+ func ( if isHidden then Hidden else Visible )
235228
236229
237230{- | Value describing whether the page is hidden or visible.
238231-}
239232type Visibility
240- = Visible
241- | Hidden
233+ = Visible
234+ | Hidden
242235
243236
244237
245238-- SUBSCRIPTIONS
246239
247240
248241type Node
249- = Document
250- | Window
242+ = Document
243+ | Window
251244
252245
253246on : Node -> String -> Decode .Decoder msg -> Sub msg
254247on node name decoder =
255- subscription ( MySub node name decoder)
248+ subscription ( MySub node name decoder)
256249
257250
258251type MySub msg
259- = MySub Node String ( Decode . Decoder msg)
252+ = MySub Node String ( Decode . Decoder msg)
260253
261254
262255subMap : (a -> b ) -> MySub a -> MySub b
263256subMap func ( MySub node name decoder) =
264- MySub node name ( Decode . map func decoder)
257+ MySub node name ( Decode . map func decoder)
265258
266259
267260
268261-- EFFECT MANAGER
269262
270263
271264type alias State msg =
272- { subs : List ( String , MySub msg )
273- , pids : Dict . Dict String Process . Id
274- }
265+ { subs : List ( String , MySub msg )
266+ , pids : Dict . Dict String Process . Id
267+ }
275268
276269
277270init : Task Never (State msg )
278271init =
279- Task . succeed ( State [] Dict . empty)
272+ Task . succeed ( State [] Dict . empty)
280273
281274
282275type alias Event =
283- { key : String
284- , event : Decode . Value
285- }
276+ { key : String
277+ , event : Decode . Value
278+ }
286279
287280
288281onSelfMsg : Platform .Router msg Event -> Event -> State msg -> Task Never (State msg )
289282onSelfMsg router { key, event } state =
290- let
291- toMessage ( subKey, MySub node name decoder ) =
292- if subKey == key then
293- Elm . Kernel . Browser . decodeEvent decoder event
294-
295- else
296- Nothing
283+ let
284+ toMessage ( subKey, MySub node name decoder ) =
285+ if subKey == key then
286+ Elm . Kernel . Browser . decodeEvent decoder event
287+ else
288+ Nothing
297289
298- messages =
299- List . filterMap toMessage state. subs
300- in
301- Task . sequence ( List . map ( Platform . sendToApp router) messages)
302- |> Task . andThen ( \ _ -> Task . succeed state)
290+ messages = List . filterMap toMessage state. subs
291+ in
292+ Task . sequence ( List . map ( Platform . sendToApp router) messages)
293+ |> Task . andThen ( \ _ -> Task . succeed state)
303294
304295
305296onEffects : Platform .Router msg Event -> List (MySub msg ) -> State msg -> Task Never (State msg )
306297onEffects router subs state =
307- let
308- newSubs =
309- List . map addKey subs
298+ let
299+ newSubs = List . map addKey subs
310300
311- stepLeft _ pid ( deads, lives, news ) =
312- ( pid :: deads, lives, news )
301+ stepLeft _ pid ( deads, lives, news) =
302+ ( pid :: deads, lives, news)
313303
314- stepBoth key pid _ ( deads, lives, news ) =
315- ( deads, Dict . insert key pid lives, news )
304+ stepBoth key pid _ ( deads, lives, news) =
305+ ( deads, Dict . insert key pid lives, news)
316306
317- stepRight key sub ( deads, lives, news ) =
318- ( deads, lives, spawn router key sub :: news )
307+ stepRight key sub ( deads, lives, news) =
308+ ( deads, lives, spawn router key sub :: news)
319309
320- ( deadPids, livePids, makeNewPids ) =
321- Dict . merge stepLeft stepBoth stepRight state. pids ( Dict . fromList newSubs) ( [], Dict . empty, [] )
322- in
323- Task . sequence ( List . map Process . kill deadPids)
324- |> Task . andThen ( \ _ -> Task . sequence makeNewPids)
325- |> Task . andThen ( \ pids -> Task . succeed ( State newSubs ( Dict . union livePids ( Dict . fromList pids))))
310+ ( deadPids, livePids, makeNewPids) =
311+ Dict . merge stepLeft stepBoth stepRight state. pids ( Dict . fromList newSubs) ( [], Dict . empty, [] )
312+ in
313+ Task . sequence ( List . map Process . kill deadPids)
314+ |> Task . andThen ( \ _ -> Task . sequence makeNewPids)
315+ |> Task . andThen ( \ pids -> Task . succeed ( State newSubs ( Dict . union livePids ( Dict . fromList pids))))
326316
327317
328318
@@ -331,17 +321,14 @@ onEffects router subs state =
331321
332322addKey : MySub msg -> ( String , MySub msg )
333323addKey (( MySub node name _) as sub) =
334- ( nodeToKey node ++ name, sub )
324+ ( nodeToKey node ++ name, sub)
335325
336326
337327nodeToKey : Node -> String
338328nodeToKey node =
339- case node of
340- Document ->
341- " d_"
342-
343- Window ->
344- " w_"
329+ case node of
330+ Document -> " d_"
331+ Window -> " w_"
345332
346333
347334
@@ -350,15 +337,12 @@ nodeToKey node =
350337
351338spawn : Platform .Router msg Event -> String -> MySub msg -> Task Never ( String , Process .Id )
352339spawn router key ( MySub node name _) =
353- let
354- actualNode =
355- case node of
356- Document ->
357- Elm . Kernel . Browser . doc
358-
359- Window ->
360- Elm . Kernel . Browser . window
361- in
362- Task . map ( \ value -> ( key, value )) <|
363- Elm . Kernel . Browser . on actualNode name <|
364- \ event -> Platform . sendToSelf router ( Event key event)
340+ let
341+ actualNode =
342+ case node of
343+ Document -> Elm . Kernel . Browser . doc
344+ Window -> Elm . Kernel . Browser . window
345+ in
346+ Task . map ( \ value -> ( key, value )) <|
347+ Elm . Kernel . Browser . on actualNode name <|
348+ \ event -> Platform . sendToSelf router ( Event key event)
0 commit comments