1module Alien exposing (Event, Tag(..), broadcast, hostDecoder, report, tagDecoder, tagFromJson, tagFromString, tagToJson, tagToString, trigger)
2
3{-| 👽 Aliens.
4
5This involves the incoming and outgoing data.
6Including the communication between the different Elm apps/workers.
7
8-}
9
10import Enum exposing (Enum)
11import Json.Decode
12import Json.Encode
13
14
15
16-- 🌳
17
18
19type alias Event =
20 { tag : String
21 , data : Json.Encode.Value
22 , error : Maybe String
23 }
24
25
26type Tag
27 = EnclosedData
28 | SearchTracks
29 | SecretKey
30 | SyncLocal
31 | SyncMethod
32 -----------------------------------------
33 -- from UI
34 -----------------------------------------
35 | DownloadTracks
36 | ProcessSources
37 | RefreshedAccessToken
38 | RemoveEncryptionKey
39 | RemoveTracksBySourceId
40 | RemoveTracksFromCache
41 | SaveEnclosedUserData
42 | SaveFavourites
43 | SavePlaylists
44 | SaveProgress
45 | SaveSettings
46 | SaveSources
47 | SaveTracks
48 | SetSyncMethod
49 | StopProcessing
50 | StoreTracksInCache
51 | SyncHypaethralData
52 | SyncTrackTags
53 | ToCache
54 | UnsetSyncMethod
55 | UpdateEncryptionKey
56 -----------------------------------------
57 -- to UI
58 -----------------------------------------
59 | AddTracks
60 | FinishedProcessingSource
61 | FinishedProcessingSources
62 | FinishedSyncing
63 | GotCachedCover
64 | HideLoadingScreen
65 | LoadEnclosedUserData
66 | LoadHypaethralUserData
67 | ReloadTracks
68 | RemoveTracksByPath
69 | ReportError
70 | ReportProcessingError
71 | ReportProcessingProgress
72 | StartedSyncing
73 | UpdateSourceData
74
75
76enum : Enum Tag
77enum =
78 Enum.create
79 [ ( "ENCLOSED_DATA", EnclosedData )
80 , ( "SEARCH_TRACKS", SearchTracks )
81 , ( "SECRET_KEY", SecretKey )
82 , ( "SYNC_LOCAL", SyncLocal )
83 , ( "SYNC_METHOD", SyncMethod )
84
85 -----------------------------------------
86 -- From UI
87 -----------------------------------------
88 , ( "DOWNLOAD_TRACKS", DownloadTracks )
89 , ( "PROCESS_SOURCES", ProcessSources )
90 , ( "REFRESHED_ACCESS_TOKEN", RefreshedAccessToken )
91 , ( "REMOVE_ENCRYPTION_KEY", RemoveEncryptionKey )
92 , ( "REMOVE_TRACKS_BY_SOURCE_ID", RemoveTracksBySourceId )
93 , ( "REMOVE_TRACKS_FROM_CACHE", RemoveTracksFromCache )
94 , ( "SAVE_ENCLOSED_USER_DATA", SaveEnclosedUserData )
95 , ( "SAVE_FAVOURITES", SaveFavourites )
96 , ( "SAVE_PLAYLISTS", SavePlaylists )
97 , ( "SAVE_PROGRESS", SaveProgress )
98 , ( "SAVE_SETTINGS", SaveSettings )
99 , ( "SAVE_SOURCES", SaveSources )
100 , ( "SAVE_TRACKS", SaveTracks )
101 , ( "SET_SYNC_METHOD", SetSyncMethod )
102 , ( "STOP_PROCESSING", StopProcessing )
103 , ( "STORE_TRACKS_IN_CACHE", StoreTracksInCache )
104 , ( "SYNC_HYPAETHRAL_DATA", SyncHypaethralData )
105 , ( "SYNC_TRACK_TAGS", SyncTrackTags )
106 , ( "TO_CACHE", ToCache )
107 , ( "UNSET_SYNC_METHOD", UnsetSyncMethod )
108 , ( "UPDATE_ENCRYPTION_KEY", UpdateEncryptionKey )
109
110 -----------------------------------------
111 -- To UI
112 -----------------------------------------
113 , ( "ADD_TRACKS", AddTracks )
114 , ( "FINISHED_PROCESSING_SOURCE", FinishedProcessingSource )
115 , ( "FINISHED_PROCESSING_SOURCES", FinishedProcessingSources )
116 , ( "GOT_CACHED_COVER", GotCachedCover )
117 , ( "HIDE_LOADING_SCREEN", HideLoadingScreen )
118 , ( "LOAD_ENCLOSED_USER_DATA", LoadEnclosedUserData )
119 , ( "LOAD_HYPAETHRAL_USER_DATA", LoadHypaethralUserData )
120 , ( "RELOAD_TRACKS", ReloadTracks )
121 , ( "REMOVE_TRACKS_BY_PATH", RemoveTracksByPath )
122 , ( "REPORT_ERROR", ReportError )
123 , ( "REPORT_PROCESSING_ERROR", ReportProcessingError )
124 , ( "REPORT_PROCESSING_PROGRESS", ReportProcessingProgress )
125 , ( "STARTED_SYNCING", StartedSyncing )
126 , ( "UPDATE_SOURCE_DATA", UpdateSourceData )
127 ]
128
129
130
131-- 🔱
132
133
134broadcast : Tag -> Json.Encode.Value -> Event
135broadcast tag data =
136 { tag = tagToString tag
137 , data = data
138 , error = Nothing
139 }
140
141
142report : Tag -> String -> Event
143report tag error =
144 { tag = tagToString tag
145 , data = Json.Encode.null
146 , error = Just error
147 }
148
149
150trigger : Tag -> Event
151trigger tag =
152 { tag = tagToString tag
153 , data = Json.Encode.null
154 , error = Nothing
155 }
156
157
158tagDecoder : Json.Decode.Decoder Tag
159tagDecoder =
160 enum.decoder
161
162
163tagToJson : Tag -> Json.Encode.Value
164tagToJson =
165 enum.encode
166
167
168tagToString : Tag -> String
169tagToString =
170 enum.toString
171
172
173tagFromJson : Json.Decode.Value -> Result Json.Decode.Error Tag
174tagFromJson =
175 Json.Decode.decodeValue enum.decoder
176
177
178tagFromString : String -> Maybe Tag
179tagFromString =
180 enum.fromString
181
182
183
184-- ⚗️
185
186
187{-| Decoder for an alien event inside another alient event.
188-}
189hostDecoder : Json.Decode.Decoder Event
190hostDecoder =
191 Json.Decode.map3
192 (\tag data error ->
193 { tag = tag
194 , data = data
195 , error = error
196 }
197 )
198 (Json.Decode.field "tag" Json.Decode.string)
199 (Json.Decode.field "data" Json.Decode.value)
200 (Json.Decode.field "error" <| Json.Decode.maybe Json.Decode.string)