-
Notifications
You must be signed in to change notification settings - Fork 0
/
sources.gr
207 lines (196 loc) · 5.3 KB
/
sources.gr
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
import * from "./types"
import List from "list"
import Array from "array"
import Option from "option"
import { debug, logError } from "./packages/debug"
import { systemErrors } from "./packages/syserror"
import {
Fd,
FdPath,
FilePath,
PathOpenOptions,
readFile as readFileFromDisk,
openFdCallback,
} from "./packages/file"
import File from "sys/file"
let logFromList = debug("hessian:fromList", None)
let logFromArray = debug("hessian:fromArray", None)
let logReadFile = debug("hessian:readFile", None)
record PullState<a> {
mut looping: Bool,
mut pulled: Bool,
mut ended: Bool,
}
export record ReadFileOptions {
file: FilePath,
rwFlags: List<File.FdFlag>,
size: Number,
}
export let fromList = (values: List<a>) => (sink: Sink<b>) => {
let state = { looping: false, ended: false, pulled: false }
let mut current = values
sink(
Start(signal => match ((signal, state.looping)) {
(Pull, true) => {
logFromList("Talkback.Pull looping: true")
state.pulled = true
},
(Close, _) => {
logFromList("Talkback.Close")
state.ended = true
sink(End)
},
(Error(e), _) => {
logFromList("Talkback.Error" ++ toString(e))
state.ended = true
sink(End)
},
(Pull, false) => {
logFromList("Talkback.Pull looping: false")
state.pulled = true
state.looping = true
while (state.pulled && !state.ended) {
match (current) {
[head, ...tail] => {
logFromList("Sink(Push)")
state.pulled = false
current = tail
sink(Push(head))
},
[] => {
logFromList("End of list")
state.ended = true
sink(End)
},
}
}
state.looping = false
},
})
)
}
export let fromArray = (values: Array<a>) => (sink: Sink<b>) => {
let state = { looping: false, ended: false, pulled: false }
let mut current = 0
sink(
Start(signal => match ((signal, state.looping)) {
(Pull, true) => {
logFromArray("Talkback.Pull looping: true")
state.pulled = true
},
(Close, _) => {
logFromArray("Talkback.Close")
state.ended = true
sink(End)
},
(Error(e), _) => {
logFromList("Talkback.Error: " ++ toString(e))
state.ended = true
sink(End)
},
(Pull, false) => {
logFromArray("Talkback.Pull looping: false")
state.pulled = true
state.looping = true
while (state.pulled && !state.ended) {
match (current < Array.length(values)) {
true => {
logFromArray("Sink(Push)")
state.pulled = false
let value = values[current]
current += 1
sink(Push(value))
},
false => {
logFromArray("End of array")
state.ended = true
sink(End)
},
}
}
state.looping = false
},
})
)
}
export let readFile = (options: ReadFileOptions) => (sink: Sink<b>) => {
let state = { looping: false, ended: false, pulled: false }
let { file, rwFlags, size } = options
let mut fd = None
let mut openError = ""
let openFileOptions = {
lookupFlag: [],
openFlags: [],
rights: [File.FdRead, File.FdSeek, File.FdTell],
inheriting: [],
rwFlags,
}: PathOpenOptions
let open = openResult =>
match (openResult) {
Ok((f, path)) => {
fd = Some(f)
},
Err(e) => {
openError = toString(systemErrors(e, ""))
},
}
openFdCallback(file, openFileOptions, open)
sink(
Start(signal => match ((signal, state.looping)) {
(Pull, true) => {
logReadFile("Talkback.Pull looping: true")
state.pulled = true
},
(Close, _) => {
logReadFile("Talkback.Close")
state.ended = true
sink(End)
},
(Error(e), _) => {
logFromList("Talkback.Error: " ++ toString(e))
state.ended = true
sink(End)
},
(Pull, false) => {
logReadFile("Talkback.Pull looping: false")
state.pulled = true
state.looping = true
while (state.pulled && !state.ended) {
match (fd) {
None => {
logReadFile(openError)
logError(openError)
state.ended = true
state.pulled = false
sink(End)
},
Some(f) =>
match (readFileFromDisk(Fd(f), rwFlags, size)) {
Ok((_fd, (data, bytesRead))) => {
match (bytesRead) {
0 => {
logReadFile("End of file.")
state.ended = true
state.pulled = false
sink(End)
},
_ => {
logReadFile("Sink(Push)")
state.pulled = false
sink(Push(data))
},
}
},
Err(e) => {
logReadFile("End of file?")
state.ended = true
sink(End)
},
},
}
}
state.looping = false
},
})
)
}