forked from wolfadex/elm-open-api-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.elm
More file actions
440 lines (321 loc) · 11.7 KB
/
Copy pathTask.elm
File metadata and controls
440 lines (321 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
module Effect.Task exposing
( Task, perform, attempt
, andThen, succeed, fail, sequence
, map, map2, map3, map4, map5
, onError, mapError
)
{-| Tasks make it easy to describe asynchronous operations that may fail, like
HTTP requests or writing to a database.
# Tasks
@docs Task, perform, attempt
# Chains
@docs andThen, succeed, fail, sequence
# Maps
@docs map, map2, map3, map4, map5
# Errors
@docs onError, mapError
-}
import Effect.Internal exposing (Command(..), HttpBody(..), Task(..))
{-| Here are some common tasks:
- [`now : Task x Posix`][now]
- [`focus : String -> Task Error ()`][focus]
- [`sleep : Float -> Task x ()`][sleep]
[now]: /packages/elm/time/latest/Time#now
[focus]: /packages/elm/browser/latest/Browser-Dom#focus
[sleep]: /packages/elm/core/latest/Process#sleep
In each case we have a `Task` that will resolve successfully with an `a` value
or unsuccessfully with an `x` value. So `Browser.Dom.focus` we may fail with an
`Error` if the given ID does not exist. Whereas `Time.now` never fails so
I cannot be more specific than `x`. No such value will ever exist! Instead it
always succeeds with the current POSIX time.
More generally a task is a _description_ of what you need to do. Like a todo
list. Or like a grocery list. Or like GitHub issues. So saying "the task is
to tell me the current POSIX time" does not complete the task! You need
[`perform`](#perform) tasks or [`attempt`](#attempt) tasks.
-}
type alias Task restriction x a =
Effect.Internal.Task restriction x a
{-| Like I was saying in the [`Task`](#Task) documentation, just having a
`Task` does not mean it is done. We must command Elm to `perform` the task:
-- elm install elm/time
import Task
import Time
type Msg
= Click
| Search String
| NewTime Time.Posix
getNewTime : Cmd Msg
getNewTime =
Task.perform NewTime Time.now
If you have worked through [`guide.elm-lang.org`][guide] (highly recommended!)
you will recognize `Cmd` from the section on The Elm Architecture. So we have
changed a task like "make delicious lasagna" into a command like "Hey Elm, make
delicious lasagna and give it to my `update` function as a `Msg` value."
[guide]: https://guide.elm-lang.org/
-}
perform : (a -> msg) -> Task restriction Never a -> Command restriction toMsg msg
perform f task =
task
|> map f
|> mapError never
|> Task
{-| This is very similar to [`perform`](#perform) except it can handle failures!
So we could _attempt_ to focus on a certain DOM node like this:
-- elm install elm/browser
import Browser.Dom
import Task
type Msg
= Click
| Search String
| Focus (Result Browser.DomError ())
focus : Cmd Msg
focus =
Task.attempt Focus (Browser.Dom.focus "my-app-search-box")
So the task is "focus on this DOM node" and we are turning it into the command
"Hey Elm, attempt to focus on this DOM node and give me a `Msg` about whether
you succeeded or failed."
**Note:** Definitely work through [`guide.elm-lang.org`][guide] to get a
feeling for how commands fit into The Elm Architecture.
[guide]: https://guide.elm-lang.org/
-}
attempt : (Result x a -> msg) -> Task restriction x a -> Command restriction toMsg msg
attempt f task =
task
|> map (Ok >> f)
|> mapError (Err >> f)
|> Task
{-| Chain together a task and a callback. The first task will run, and if it is
successful, you give the result to the callback resulting in another task. This
task then gets run. We could use this to make a task that resolves an hour from
now:
-- elm install elm/time
import Process
import Time
timeInOneHour : Task x Time.Posix
timeInOneHour =
Process.sleep (60 * 60 * 1000)
|> andThen (\_ -> Time.now)
First the process sleeps for an hour **and then** it tells us what time it is.
-}
andThen : (a -> Task restriction x b) -> Task restriction x a -> Task restriction x b
andThen =
Effect.Internal.andThen
{-| A task that succeeds immediately when run. It is usually used with
[`andThen`](#andThen). You can use it like `map` if you want:
import Time
-- elm install elm/time
timeInMillis : Task x Int
timeInMillis =
Time.now
|> andThen (\t -> succeed (Time.posixToMillis t))
-}
succeed : a -> Task restriction x a
succeed =
Succeed
{-| A task that fails immediately when run. Like with `succeed`, this can be
used with `andThen` to check on the outcome of another task.
type Error
= NotFound
notFound : Task Error a
notFound =
fail NotFound
-}
fail : x -> Task restriction x a
fail =
Fail
{-| Transform a task. Maybe you want to use [`elm/time`][time] to figure
out what time it will be in one hour:
import Task exposing (Task)
import Time
-- elm install elm/time
timeInOneHour : Task x Time.Posix
timeInOneHour =
Task.map addAnHour Time.now
addAnHour : Time.Posix -> Time.Posix
addAnHour time =
Time.millisToPosix (Time.posixToMillis time + 60 * 60 * 1000)
[time]: /packages/elm/time/latest/
-}
map : (a -> b) -> Task restriction x a -> Task restriction x b
map =
Effect.Internal.taskMap
{-| Put the results of two tasks together. For example, if we wanted to know
the current month, we could use [`elm/time`][time] to ask:
import Task exposing (Task)
import Time
-- elm install elm/time
getMonth : Task x Int
getMonth =
Task.map2 Time.toMonth Time.here Time.now
**Note:** Say we were doing HTTP requests instead. `map2` does each task in
order, so it would try the first request and only continue after it succeeds.
If it fails, the whole thing fails!
[time]: /packages/elm/time/latest/
-}
map2 : (a -> b -> result) -> Task restriction x a -> Task restriction x b -> Task restriction x result
map2 func taskA taskB =
taskA
|> andThen
(\a ->
taskB
|> andThen (\b -> succeed (func a b))
)
{-| -}
map3 : (a -> b -> c -> result) -> Task restriction x a -> Task restriction x b -> Task restriction x c -> Task restriction x result
map3 func taskA taskB taskC =
taskA
|> andThen
(\a ->
taskB
|> andThen
(\b ->
taskC
|> andThen (\c -> succeed (func a b c))
)
)
{-| -}
map4 :
(a -> b -> c -> d -> result)
-> Task restriction x a
-> Task restriction x b
-> Task restriction x c
-> Task restriction x d
-> Task restriction x result
map4 func taskA taskB taskC taskD =
taskA
|> andThen
(\a ->
taskB
|> andThen
(\b ->
taskC
|> andThen
(\c ->
taskD
|> andThen (\d -> succeed (func a b c d))
)
)
)
{-| -}
map5 :
(a -> b -> c -> d -> e -> result)
-> Task restriction x a
-> Task restriction x b
-> Task restriction x c
-> Task restriction x d
-> Task restriction x e
-> Task restriction x result
map5 func taskA taskB taskC taskD taskE =
taskA
|> andThen
(\a ->
taskB
|> andThen
(\b ->
taskC
|> andThen
(\c ->
taskD
|> andThen
(\d ->
taskE
|> andThen (\e -> succeed (func a b c d e))
)
)
)
)
{-| Transform the error value. This can be useful if you need a bunch of error
types to match up.
type Error
= Http Http.Error
| WebGL WebGL.Error
getResources : Task Error Resource
getResources =
sequence
[ mapError Http serverTask
, mapError WebGL textureTask
]
-}
mapError : (x -> y) -> Task restriction x a -> Task restriction y a
mapError =
Effect.Internal.taskMapError
{-| Start with a list of tasks, and turn them into a single task that returns a
list. The tasks will be run in order one-by-one and if any task fails the whole
sequence fails.
sequence [ succeed 1, succeed 2 ] == succeed [ 1, 2 ]
-}
sequence : List (Task restriction x a) -> Task restriction x (List a)
sequence tasks =
List.foldr (map2 (::)) (succeed []) tasks
{-| Recover from a failure in a task. If the given task fails, we use the
callback to recover.
fail "file not found"
|> onError (\msg -> succeed 42)
-- succeed 42
succeed 9
|> onError (\msg -> succeed 42)
-- succeed 9
-}
onError : (x -> Task restriction y a) -> Task restriction x a -> Task restriction y a
onError f task =
case task of
Succeed a ->
Succeed a
Fail x ->
f x
HttpStringTask request ->
HttpStringTask
{ method = request.method
, url = request.url
, body = request.body
, headers = request.headers
, onRequestComplete = request.onRequestComplete >> onError f
, timeout = request.timeout
, isRisky = request.isRisky
}
HttpBytesTask request ->
HttpBytesTask
{ method = request.method
, url = request.url
, body = request.body
, headers = request.headers
, onRequestComplete = request.onRequestComplete >> onError f
, timeout = request.timeout
, isRisky = request.isRisky
}
SleepTask delay onResult ->
SleepTask delay (onResult >> onError f)
TimeNow gotTime ->
TimeNow (gotTime >> onError f)
TimeHere gotTimeZone ->
TimeHere (gotTimeZone >> onError f)
TimeGetZoneName gotTimeZoneName ->
TimeGetZoneName (gotTimeZoneName >> onError f)
SetViewport x y function ->
SetViewport x y (function >> onError f)
GetViewport function ->
GetViewport (function >> onError f)
GetElement string function ->
GetElement string (function >> onError f)
Focus string function ->
Focus string (function >> onError f)
Blur string function ->
Blur string (function >> onError f)
GetViewportOf string function ->
GetViewportOf string (function >> onError f)
SetViewportOf string x y function ->
SetViewportOf string x y (function >> onError f)
FileToString file function ->
FileToString file (function >> onError f)
FileToBytes file function ->
FileToBytes file (function >> onError f)
FileToUrl file function ->
FileToUrl file (function >> onError f)
LoadTexture loadTextureOptions string function ->
LoadTexture loadTextureOptions string (function >> onError f)
RequestXrStart options function ->
RequestXrStart options (function >> onError f)
RenderXrFrame entities function ->
RenderXrFrame entities (function >> onError f)
EndXrSession function ->
EndXrSession (function >> onError f)