This repository was archived by the owner on Aug 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPlatform.elm
120 lines (82 loc) · 2.99 KB
/
Platform.elm
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
module Platform exposing
( Program, worker
, Task, ProcessId
, Router, sendToApp, sendToSelf
)
{-|
# Programs
@docs Program, worker
# Platform Internals
## Tasks and Processes
@docs Task, ProcessId
## Effect Manager Helpers
An extremely tiny portion of library authors should ever write effect managers.
Fundamentally, Elm needs maybe 10 of them total. I get that people are smart,
curious, etc. but that is not a substitute for a legitimate reason to make an
effect manager. Do you have an *organic need* this fills? Or are you just
curious? Public discussions of your explorations should be framed accordingly.
@docs Router, sendToApp, sendToSelf
-}
import Basics exposing (Never)
import Elm.Kernel.Platform
import Elm.Kernel.Scheduler
import Platform.Cmd exposing (Cmd)
import Platform.Sub exposing (Sub)
-- PROGRAMS
{-| A `Program` describes an Elm program! How does it react to input? Does it
show anything on screen? Etc.
-}
type Program flags model msg = Program
{-| Create a [headless][] program with no user interface.
This is great if you want to use Elm as the “brain” for something
else. For example, you could send messages out ports to modify the DOM, but do
all the complex logic in Elm.
[headless]: https://en.wikipedia.org/wiki/Headless_software
Initializing a headless program from JavaScript looks like this:
```javascript
var app = Elm.MyThing.worker();
```
If _do_ want to control the user interface in Elm, the [`Browser`][browser]
module has a few ways to create that kind of `Program` instead!
[headless]: https://en.wikipedia.org/wiki/Headless_software
[browser]: /packages/elm-lang/browser/latest/Browser
-}
worker
: { init : flags -> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, subscriptions : model -> Sub msg
}
-> Program flags model msg
worker =
Elm.Kernel.Platform.worker
-- TASKS and PROCESSES
{-| Head over to the documentation for the [`Task`](Task) module for more
information on this. It is only defined here because it is a platform
primitive.
-}
type Task err ok = Task
{-| Head over to the documentation for the [`Process`](Process) module for
information on this. It is only defined here because it is a platform
primitive.
-}
type ProcessId = ProcessId
-- EFFECT MANAGER INTERNALS
{-| An effect manager has access to a “router” that routes messages between
the main app and your individual effect manager.
-}
type Router appMsg selfMsg =
Router
{-| Send the router a message for the main loop of your app. This message will
be handled by the overall `update` function, just like events from `Html`.
-}
sendToApp : Router msg a -> msg -> Task x ()
sendToApp =
Elm.Kernel.Platform.sendToApp
{-| Send the router a message for your effect manager. This message will
be routed to the `onSelfMsg` function, where you can update the state of your
effect manager as necessary.
As an example, the effect manager for web sockets
-}
sendToSelf : Router a msg -> msg -> Task x ()
sendToSelf =
Elm.Kernel.Platform.sendToSelf