Skip to content

Commit

Permalink
FIx a but in the standard library that caused read/writeFile callback…
Browse files Browse the repository at this point in the history
…s to be called more than once
  • Loading branch information
thesephist committed Jul 7, 2019
1 parent b1519ae commit af0c5fa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion samples/io.ink
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ incrementalCopy := (src, dest, offset) => read(src, offset, BUFSIZE, evt => (
dataLength := len(evt.data)

` log progress `
log('copying --> ' + slice(decode(evt.data), 0, 50) + '...')
log('copying --> ' + slice(decode(evt.data), 0, 20) + '...')

` write the read bit, and recurse back to reading `
write(dest, offset, evt.data, evt => evt.type :: {
Expand Down
72 changes: 48 additions & 24 deletions samples/std.ink
Original file line number Diff line number Diff line change
Expand Up @@ -184,34 +184,52 @@ decode := data => reduce(data, (acc, cp) => acc + char(cp), '')
implementation can be more efficient here `
readFile := (path, callback) => (
BUFSIZE := 4096 ` bytes `
sent := [false]
(accumulate := (offset, acc) => read(path, offset, BUFSIZE, evt => (
evt.type :: {
'error' -> callback(())
'data' -> (
dataLen := len(evt.data)
dataLen = BUFSIZE :: {
true -> accumulate(offset + dataLen, acc + decode(evt.data))
false -> callback(acc + decode(evt.data))
}
)
}
sent.0 :: {false -> (
evt.type :: {
'error' -> (
sent.0 := true
callback(())
)
'data' -> (
dataLen := len(evt.data)
dataLen = BUFSIZE :: {
true -> accumulate(offset + dataLen, acc + decode(evt.data))
false -> (
sent.0 := true
callback(acc + decode(evt.data))
)
}
)
}
)}
)))(0, '')
)

` utility for reading an entire file without string conversion `
readRawFile := (path, callback) => (
BUFSIZE := 4096 ` bytes `
sent := [false]
(accumulate := (offset, acc) => read(path, offset, BUFSIZE, evt => (
evt.type :: {
'error' -> callback(())
'data' -> (
dataLen := len(evt.data)
dataLen = BUFSIZE :: {
true -> accumulate(offset + dataLen, append(acc, evt.data))
false -> callback(append(acc, evt.data))
}
)
}
sent.0 :: {false -> (
evt.type :: {
'error' -> (
sent.0 := true
callback(())
)
'data' -> (
dataLen := len(evt.data)
dataLen = BUFSIZE :: {
true -> accumulate(offset + dataLen, append(acc, evt.data))
false -> (
sent.0 := true
callback(append(acc, evt.data))
)
}
)
}
)}
)))(0, [])
)

Expand All @@ -222,8 +240,14 @@ writeFile := (path, data, callback) => writeRawFile(path, encode(data), callback

` analogue of readRawFile for writeFile `
writeRawFile := (path, data, callback) => (
write(path, 0, data, evt => evt.type :: {
'error' -> callback(())
'end' -> callback(true)
})
sent := [false]
write(path, 0, data, evt => (
sent.0 :: {false -> (
sent.0 := true
evt.type :: {
'error' -> callback(())
'end' -> callback(true)
}
)}
))
)

0 comments on commit af0c5fa

Please sign in to comment.