How to break Dependency Cycles?
Problem
Solution
Alternative Solution
Main.elm
1
import User exposing (User)
2
​
3
type Model =
4
Maybe User
5
​
6
type Msg =
7
UserSpecific User.Msg
8
Login String String
9
​
10
update : Msg -> Model -> (Model,Cmd Msg)
11
update msg =
12
case msg of
13
UserSpecific userMsg ->
14
User.update userMsg
15
Login name pass ->
16
Debug.todo "login user"
Copied!
User.elm
1
import Main exposing (Msg)
2
​
3
type User =
4
..
5
​
6
type Msg =
7
..
8
9
updateUser : Msg -> User -> (Model,Cmd Msg)
10
​
11
viewUser : User -> Html Msg
Copied!
Main.elm
1
import User exposing (User)
2
​
3
type Model =
4
Maybe User
5
​
6
type Msg =
7
UserSpecific User.Msg
8
Login String String
9
​
10
update : Msg -> Model -> (Model,Cmd Msg)
11
update msg =
12
case msg of
13
UserSpecific userMsg ->
14
User.update Just UserSpecific userMsg
15
Login name pass ->
16
Debug.todo "login user"
Copied!
User.elm
1
type User =
2
..
3
​
4
type Msg =
5
..
6
7
updateUser : (User -> model) -> (Msg -> msg) -> Msg -> User -> (model,Cmd msg)
Copied!
Main.elm
1
import User exposing (User)
2
​
3
type Model =
4
Maybe User
5
​
6
type Msg =
7
UserSpecific User.Msg
8
Login String String
9
​
10
update : Msg -> Model -> (Model,Cmd Msg)
11
update msg =
12
case msg of
13
UserSpecific userMsg ->
14
let
15
(user,msg) = User.update userMsg
16
in
17
(Just User,UserSpecific msg)
18
Login name pass ->
19
Debug.todo "login user"
Copied!
User.elm
1
type User =
2
..
3
​
4
type Msg =
5
..
6
7
updateUser : Msg -> User -> (User,Cmd User.Msg)
Copied!

Question

How to break out of the dependency cycles?

Answer

  1. 1.
    Isolate the elements that both of the source modules need access to.
  2. 2.
    Move the shared elements into a new module.
  3. 3.
    Make the source module depend on the new Module.

Further Reading