Skip to content

Commit 16012c3

Browse files
committed
examples: update Downloader.par
1 parent a8c2471 commit 16012c3

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

examples/Downloader.par

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def DownloadFile = [(sourceUrl, dest)!] chan yield {
9090
file.write(bytes).try@clearResp
9191

9292
bytesSinceLastEvent->Nat.Add(Bytes.Length(bytes))
93-
Nat.Compare(Time.Now(!), Nat.Add(timeOfLastEvent, 100)).case {
93+
Time.Now(!)->Nat.Compare(timeOfLastEvent->Nat.Add(100)).case {
9494
.greater! => {
9595
yield.item((dest) .progressed bytesSinceLastEvent)
9696
let bytesSinceLastEvent = 0
@@ -120,34 +120,34 @@ def ShowDownloadProgress = [events, rootPath] chan exit {
120120

121121
catch e => {
122122
Debug.Log(Concat(*("Stdout error: ", e)))
123-
progress.close
123+
tracker.close
124124
exit!
125125
}
126126

127127
let stdout = Os.Stdout
128-
let progress = NewDownloadProgress
128+
let tracker = NewProgressTracker
129129

130130
events.begin.case {
131131
.item((destination) event) => {
132132
let name = getName(destination)
133133

134134
event.case {
135135
.started expected => {
136-
progress.file(name).started(expected)
136+
tracker.file(name).started(expected)
137137
stdout.write(Concat(*("\nStarted downloading ", name, "\n"))).try
138138
}
139139

140140
.progressed size => {
141-
progress.file(name).progressed(size)
142-
progress.stats[numFiles, sumDownloaded, sumExpected]
141+
tracker.file(name).progressed(size)
142+
tracker.stats[numFiles, sumDownloaded, sumExpected]
143143
stdout.write(Concat(*(
144144
"\r>> ", Int.ToString(numFiles), " file(s) in progress: ",
145145
Int.ToString(sumDownloaded), " B / ", Int.ToString(sumExpected), " B",
146146
))).try
147147
}
148148

149149
.finished! => {
150-
progress.file(name).ended[startTime, downloadedSize]
150+
tracker.file(name).ended[startTime, downloadedSize]
151151
let duration = Time.Now(!)->Int.Sub(startTime)
152152
let speed = downloadedSize->Int.Mul(1000)->Int.Div(duration)
153153
stdout.write(Concat(*(
@@ -159,7 +159,7 @@ def ShowDownloadProgress = [events, rootPath] chan exit {
159159
}
160160

161161
.failed msg => {
162-
progress.file(name).ended[_, _]
162+
tracker.file(name).ended[_, _]
163163
stdout.write(Concat(*("\nFailed to download ", name, ": ", msg, "\n"))).try
164164
}
165165
}
@@ -169,12 +169,12 @@ def ShowDownloadProgress = [events, rootPath] chan exit {
169169
.end! => {}
170170
}
171171

172-
progress.close
172+
tracker.close
173173
stdout.close
174174
exit!
175175
}
176176

177-
type DownloadProgress = iterative choice {
177+
type ProgressTracker = iterative choice {
178178
.close => !,
179179
.stats => (Nat, Int, Int) self,
180180
.file(String) => choice {
@@ -184,8 +184,7 @@ type DownloadProgress = iterative choice {
184184
}
185185
}
186186

187-
def NewDownloadProgress: DownloadProgress = do {
188-
// ("start time", "expected size", "downloaded size")
187+
def NewProgressTracker: ProgressTracker = do {
189188
let filesInProgress = Map.String(type (Nat, Nat, Nat)!)(*())
190189
let sumDownloaded: Int = 0
191190
let sumExpected: Int = 0
@@ -226,26 +225,13 @@ def NewDownloadProgress: DownloadProgress = do {
226225

227226
dec FanIn : [type a] [List<List<a>>] List<a>
228227
def FanIn = [type a] [lists] chan yield {
229-
let yield = Cell.Share(type dual List<a>)(yield, chan cell {
230-
lists.begin.case {
231-
.item(list) => {
232-
cell.split(chan cell {
233-
list.begin.case {
234-
.item(value) => {
235-
cell.take[yield]
236-
yield.item(value)
237-
cell.put(yield)
238-
list.loop
239-
}
240-
.end! => { cell.end! }
241-
}
242-
})
243-
lists.loop
244-
}
245-
.end! => { cell.end! }
246-
}
247-
})
248-
yield.end!
228+
Cell.Share(type dual List<a>)(yield, lists.begin.case {
229+
.end! => .end!,
230+
.item(list) lists => .split(list.begin.case {
231+
.end! => .end!,
232+
.item(value) list => .take[yield].put(yield.item(value)) list.loop,
233+
}) lists.loop,
234+
}).end!
249235
}
250236

251237
dec Map : [type a, b] [List<a>, box [a] b] List<b>

0 commit comments

Comments
 (0)