-
Notifications
You must be signed in to change notification settings - Fork 2
/
Typing.elm
169 lines (108 loc) · 4.45 KB
/
Typing.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
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
module TUpdate where
import Graphics.Element
import Graphics.Input.Field exposing (..)
import Graphics.Element exposing (..)
import Signal exposing (..)
import String exposing (..)
import Json.Encode as Json
import Dict
import Char exposing (..)
import Keyboard
import Debug
import Window
import SocketIO exposing (..)
import Task exposing (..)
import Editor exposing (..)
import Checks exposing (..)
import DraftTests exposing (..)
import ConvertJson exposing (jsonToWUpdates, wUpdatesToJson, stringUpdateToJson, jsonToTUpdate, tUpdatesToJson)
import Model exposing (..)
import Constants exposing (..)
import Woot exposing (grabNext
, wToString
, canIntegrate)
import Graph exposing (generateInsert
, generateDelete
, integrateRemoteInsert
, integrateRemoteDelete)
-- - - - - - - U T I L I T I E S - - - - -
throwOutNoTUpdates : TUpdate -> Bool
throwOutNoTUpdates tUp =
case tUp of
NoTUpdate -> False
_ -> True
throwOutNoUpdatesAndCaret : WUpdate -> Bool
throwOutNoUpdatesAndCaret wUp =
case wUp of
NoUpdate -> False
--Caret n -> False
_ -> True
editToWUpdate : Edit -> WUpdate
editToWUpdate e =
case e of
W wUpd -> wUpd
T tUpd -> NoUpdate
editToTUpdate : Edit -> TUpdate
editToTUpdate e =
case e of
W wUpd -> NoTUpdate
T tUpd -> tUpd
handleTUpdate : TUpdate -> Edit
handleTUpdate tUpdate = T tUpdate
handleServerUpdate : WUpdate -> Edit
handleServerUpdate wUpdate = W wUpdate
---- - - - - - - - N E T W O R K I N G - - - - - -
sockConnected : Signal.Mailbox Bool
sockConnected = Signal.mailbox False
incoming : Signal.Mailbox String
incoming = Signal.mailbox "null"
port incomingPeer : Signal String
port windowLocPort : String
socket = io windowLocPort defaultOptions
port incomingPort : Task x ()
port incomingPort = socket `andThen` SocketIO.on "serverWUpdates" incoming.address
port tUpdatePort: Signal String
tUpdate = Signal.map jsonToTUpdate tUpdatePort
-- - - - - - - - - O U T G O I N G - - - - - - - - -
port initializePort : Task x ()
port initializePort = socket `andThen` SocketIO.emit "example" "whaddup"
-- - - - - - S E N D - S E R V E R - U P D A T E S - - - - - - -
port sendUpdatesPort : Signal (Task x ())
port sendUpdatesPort = Signal.map (\i -> socket `andThen` SocketIO.emit "localEdits" i) localUpdatesAsJsonToSend
port sendUpdatesPortPeer : Signal String
port sendUpdatesPortPeer = localUpdatesAsJsonToSend
serverUpdates = Signal.map (\u -> jsonToWUpdates u) incomingPeer
localUpdatesAsJsonToSend : Signal String
localUpdatesAsJsonToSend = Signal.map (\updates -> wUpdatesToJson updates) cleanedUpdatesToSend
cleanedUpdatesToSend : Signal (List WUpdate)
cleanedUpdatesToSend = Signal.map (\ (model, edits) -> List.map editToWUpdate edits) modelFold
-- - - - - - - S E N D - L O C A L - D O C - U P D A T E S - - - - - - - -
port docUpdatesPort : Signal String
port docUpdatesPort = Signal.map (\updates -> tUpdatesToJson updates) docUpdatesToSend
docUpdatesToSend : Signal (List TUpdate)
docUpdatesToSend = Signal.map (\ (model, edits) -> List.map editToTUpdate edits) modelFold
-- - - - - - - P O S S I B L Y - U N N E C E S S A R Y - U P D A T E S - - - -
port sendNewStringUpdatesPort : Signal String
port sendNewStringUpdatesPort = sendNewString
sendNewString : Signal String
sendNewString = Signal.map (\(mod, upd) -> stringUpdateToJson mod.doc.str) modelFold
-- - - - - - P R O C E S S - I N C O M I N G - - - - -
serverUpdateToEdit : Signal (List Edit)
serverUpdateToEdit = Signal.map (List.map handleServerUpdate) serverUpdates
tUpdateToEdit : Signal (List Edit)
tUpdateToEdit = Signal.map (\t -> [handleTUpdate t]) tUpdate
edits : Signal (List Edit)
edits = Signal.merge tUpdateToEdit serverUpdateToEdit
modelFold : Signal (Model, List Edit)
modelFold = Signal.foldp (\ e (m,w) -> processEdits e m) (emptyModel, []) edits
-- - - - - - - - - V I E W - - - - - - - - - - - -
tester =
let
local = makeEmptySite 1
remote = makeEmptySite 2
(localModel, lEdits) = insertString "hey" 0 local
(remoteModel, rEdits) = processEdits lEdits remote
in
(remoteModel, localModel)
main = Graphics.Element.empty
-- Signal.map3 (\ (m, w) t s -> show ("WSTRING: " ++ wToString m.wString ++ ".typing.."++ toString t ++ ".. server updates .. " ++ toString s ++ " DEBUG : " ++ m.debug)) modelFold tUpdate serverUpdates