|
2 | 2 |
|
3 | 3 | module Neo (main) where |
4 | 4 |
|
5 | | -import Command qualified |
| 5 | +import Action qualified |
| 6 | +import Array qualified |
6 | 7 | import Core |
7 | 8 | import File qualified |
8 | | -import Platform qualified |
9 | 9 | import Result qualified |
| 10 | +import Service qualified |
10 | 11 | import ToText (Show) |
| 12 | +import Time qualified |
11 | 13 | import Yaml qualified |
12 | 14 |
|
| 15 | + |
13 | 16 | type Model = |
14 | 17 | Record |
15 | 18 | '[ "project" := Maybe ProjectDefinition, |
16 | 19 | "path" := Maybe Path, |
| 20 | + "count" := Int, |
17 | 21 | "status" := Text |
18 | 22 | ] |
19 | 23 |
|
| 24 | + |
20 | 25 | type ProjectDefinition = |
21 | 26 | Record |
22 | 27 | '[ "name" := Text, |
23 | 28 | "version" := Version |
24 | 29 | ] |
25 | 30 |
|
26 | | -data Message |
| 31 | + |
| 32 | +data Event |
27 | 33 | = ProjectFileRead Text |
28 | 34 | | ProjectFileAccessErrored File.Error |
29 | 35 | | ProjectFileParsed ProjectDefinition |
30 | 36 | | BuildStarted |
| 37 | + | Tick |
31 | 38 | | BuildFailed FailureReason |
32 | 39 | deriving (Show) |
33 | 40 |
|
| 41 | + |
34 | 42 | data FailureReason |
35 | 43 | = ProjectFileParseError Text |
36 | 44 | deriving (Show) |
37 | 45 |
|
38 | | -init :: (Model, Command Message) |
| 46 | + |
| 47 | +init :: (Model, Action Event) |
39 | 48 | init = do |
40 | 49 | let emptyModel = |
41 | 50 | ANON |
42 | 51 | { project = Nothing, |
43 | 52 | path = Nothing, |
| 53 | + count = 0, |
44 | 54 | status = "Starting up" |
45 | 55 | } |
46 | | - let command = |
| 56 | + let action = |
47 | 57 | File.readText |
48 | 58 | ANON |
49 | 59 | { path = [path|project.yaml|], |
50 | 60 | onSuccess = ProjectFileRead, |
51 | 61 | onError = ProjectFileAccessErrored |
52 | 62 | } |
53 | | - (emptyModel, command) |
| 63 | + (emptyModel, action) |
| 64 | + |
54 | 65 |
|
55 | | -update :: Message -> Model -> (Model, Command Message) |
56 | | -update message model = |
57 | | - case message of |
| 66 | +update :: Event -> Model -> (Model, Action Event) |
| 67 | +update event model = |
| 68 | + case event of |
58 | 69 | ProjectFileRead fileContent -> do |
59 | 70 | let parsedContent = Yaml.parse fileContent |
60 | 71 | let newModel = model {status = "Parsing project file"} |
61 | 72 | case parsedContent of |
62 | 73 | Result.Ok projectDefinition -> |
63 | | - (newModel, Command.continueWith (ProjectFileParsed projectDefinition)) |
| 74 | + (newModel, Action.continueWith (ProjectFileParsed projectDefinition)) |
64 | 75 | Result.Err _ -> do |
65 | 76 | let error = ProjectFileParseError fileContent |
66 | | - (newModel, Command.continueWith (BuildFailed error)) |
| 77 | + (newModel, Action.continueWith (BuildFailed error)) |
67 | 78 | ProjectFileAccessErrored _ -> |
68 | | - (model {status = "File Access Errored"}, Command.none) |
| 79 | + (model {status = "File Access Errored"}, Action.none) |
69 | 80 | ProjectFileParsed projectDefinition -> |
70 | | - (model {project = Just projectDefinition}, Command.none) |
| 81 | + (model {project = Just projectDefinition}, Action.none) |
71 | 82 | BuildStarted -> |
72 | | - (model {status = "Build Started!"}, Command.none) |
| 83 | + (model {status = "Build Started!"}, Action.none) |
73 | 84 | BuildFailed _ -> |
74 | | - (model {status = "Build Failed!"}, Command.none) |
| 85 | + (model {status = "Build Failed!"}, Action.none) |
| 86 | + Tick -> |
| 87 | + ( model |
| 88 | + { count = model.count + 1, |
| 89 | + status = "Count: " ++ toText model.count |
| 90 | + }, |
| 91 | + Action.none |
| 92 | + ) |
| 93 | + |
75 | 94 |
|
76 | 95 | view :: Model -> Text |
77 | 96 | view m = |
78 | 97 | case m.project of |
79 | 98 | Just project -> |
80 | | - toText project |
| 99 | + m.status ++ "\n\n" ++ toText project |
81 | 100 | Nothing -> |
82 | 101 | m.status |
83 | 102 |
|
| 103 | + |
84 | 104 | main :: IO () |
85 | | -main = Platform.init (ANON {init = init, view = view, update = update}) |
| 105 | +main = |
| 106 | + Service.init |
| 107 | + ( ANON |
| 108 | + { init = (init), |
| 109 | + view = (view), |
| 110 | + triggers = |
| 111 | + Array.fromLinkedList |
| 112 | + [ Time.triggerEveryMilliseconds 1000 (\_ -> Tick) |
| 113 | + ], |
| 114 | + update = (update) |
| 115 | + } |
| 116 | + ) |
0 commit comments