(.module: [lux #* [abstract [monad (#+ do)]] [control [io (#+ IO)] ["." try (#+ Try)] [concurrency ["." promise (#+ Promise)] ["." stm]]] [data [binary (#+ Binary)]] [world [net [uri (#+ URI)]]]]) (signature: #export (Repository !) (: (-> URI (! (Try Binary))) download) (: (-> URI Binary (! (Try Any))) upload)) (def: #export (async repository) (-> (Repository IO) (Repository Promise)) (structure (def: (download uri) (promise.future (\ repository download uri))) (def: (upload uri content) (promise.future (\ repository upload uri content))) )) (signature: #export (Simulation s) (: (-> URI s (Try [s Binary])) on_download) (: (-> URI Binary s (Try s)) on_upload)) (def: #export (mock simulation init) (All [s] (-> (Simulation s) s (Repository Promise))) (let [state (stm.var init)] (structure (def: (download uri) (stm.commit (do {! stm.monad} [|state| (stm.read state)] (case (\ simulation on_download uri |state|) (#try.Success [|state| output]) (do ! [_ (stm.write |state| state)] (wrap (#try.Success output))) (#try.Failure error) (wrap (#try.Failure error)))))) (def: (upload uri content) (stm.commit (do {! stm.monad} [|state| (stm.read state)] (case (\ simulation on_upload uri content |state|) (#try.Success |state|) (do ! [_ (stm.write |state| state)] (wrap (#try.Success []))) (#try.Failure error) (wrap (#try.Failure error)))))) )))