A music player that connects to your cloud/distributed storage.
1module Brain.Common.State exposing (..)
2
3import Alien
4import Brain.Ports as Ports
5import Brain.Types exposing (..)
6import Json.Decode as Json
7import Return.Ext as Return
8import Task
9import TaskPort
10import TaskPort.Extra as TaskPort
11
12
13
14-- 🛠
15
16
17attemptPortTask : (a -> Msg) -> Task.Task TaskPort.Error a -> Cmd Msg
18attemptPortTask mapFn =
19 Task.attempt (reportPortErrorToUI mapFn)
20
21
22attemptTask : (a -> Msg) -> Task.Task String a -> Cmd Msg
23attemptTask mapFn =
24 Task.attempt (reportErrorToUI mapFn)
25
26
27
28-- GIVE
29
30
31giveUI : Alien.Tag -> Json.Value -> Manager
32giveUI tag data =
33 data
34 |> giveUICmd tag
35 |> Return.communicate
36
37
38giveUICmd : Alien.Tag -> Json.Value -> Cmd Msg
39giveUICmd tag data =
40 data
41 |> Alien.broadcast tag
42 |> Ports.toUI
43
44
45giveUICmdMsg : Alien.Tag -> Json.Value -> Msg
46giveUICmdMsg tag data =
47 data
48 |> giveUICmd tag
49 |> Cmd
50
51
52
53-- NUDGE
54
55
56nudgeUI : Alien.Tag -> Manager
57nudgeUI =
58 nudgeUICmd >> Return.communicate
59
60
61nudgeUICmd : Alien.Tag -> Cmd Msg
62nudgeUICmd tag =
63 tag
64 |> Alien.trigger
65 |> Ports.toUI
66
67
68nudgeUICmdMsg : Alien.Tag -> Msg
69nudgeUICmdMsg =
70 nudgeUICmd >> Cmd
71
72
73
74-- REPORT
75
76
77reportErrorToUI : (a -> Msg) -> Result String a -> Msg
78reportErrorToUI mapFn result =
79 case result of
80 Ok value ->
81 mapFn value
82
83 Err error ->
84 reportUICmdMsg Alien.ReportError error
85
86
87reportPortErrorToUI : (a -> Msg) -> Result TaskPort.Error a -> Msg
88reportPortErrorToUI mapFn =
89 Result.mapError TaskPort.errorToStringCustom >> reportErrorToUI mapFn
90
91
92reportUI : Alien.Tag -> String -> Manager
93reportUI tag error =
94 error
95 |> reportUICmd tag
96 |> Return.communicate
97
98
99reportUICmd : Alien.Tag -> String -> Cmd Msg
100reportUICmd tag error =
101 error
102 |> Alien.report tag
103 |> Ports.toUI
104
105
106reportUICmdMsg : Alien.Tag -> String -> Msg
107reportUICmdMsg tag error =
108 error
109 |> reportUICmd tag
110 |> Cmd