StreamUpload
Processes
Stream chunked uploads over a typed channel.
The browser (or any socket client) sends chunked Up values. The server validates each chunk as it arrives and broadcasts an error Down immediately when something fails. A final Up marks the stream complete; the server then commits and broadcasts a finished Down.
Wire this into a channel module the same way chat uses Channel.broadcast channel: keep Channel.define at the top level, and call handleIn from your own handleIn so the channel knot stays a function:
handleIn up state =
StreamUpload.handleIn
{ topicOf = \s -> s.topic
, broadcast = \topic down -> Channel.broadcast channel topic down
, asChunk = \msg -> asChunk msg
, isFinish = \msg -> isFinish msg
, onChunk = \rows s -> onChunk rows s
, onFinish = \s -> onFinish s
, failed = \s -> s.failed
, markFailed = \s -> { s | failed = True }
, error = \message -> Error message
, progress = \n -> Just (Accepted n)
, finished = \s -> Finished s.accepted
, accepted = \s -> s.accepted
}
up
statePass callbacks as lambdas (not bare multi-argument names). The BEAM backend stores top-level functions as multi-arity funs; lambdas stay curried and match how handleIn applies record fields.
chunk is whatever your Up carries: structured rows (List row), opaque Bytes, or another wire-safe payload. See examples/14_ws_books_upload for both structured and binary chunk paths.
Values
handleIn
handleIn : { topicOf : state -> topic , broadcast : topic -> down -> Cmd msg , asChunk : up -> Maybe chunk , isFinish : up -> Bool , onChunk : chunk -> state -> Result String state , onFinish : state -> Result String (state, Cmd msg) , failed : state -> Bool , markFailed : state -> state , error : String -> down , progress : Int -> Maybe down , finished : state -> down , accepted : state -> Int } -> up -> state -> (state, Cmd msg)Handle one up for a streaming upload.
asChunk and isFinish classify your concrete Up constructors. onChunk accumulates or validates a chunk; return Err to stop and warn the client right away. onFinish runs when the client signals completion (persist, swap tables, etc.). Pass broadcast with the usual Channel.broadcast channel knot.
Examples
StreamUpload.handleIn config up state