fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
6107ffc
v2.1.4
1local downloader ={} 2 3local REQUESTS ={} 4 5local log =require ( "log" ) 6 7local THREAD =love . thread . newThread ( "modules/downloader/thread.lua" ) 8THREAD : start () 9 10-- these two channels are used to send requests and progress updates back and forth 11local requests_channel =love . thread . getChannel ( "dl-requests" ) 12local progress_channel =love . thread . getChannel ( "dl-progress" ) 13 14function downloader . request ( request , callback ) 15for id =1 , # REQUESTS + 1 do 16-- find a nil table entry 17if REQUESTS [ id ] == nil then 18-- save our callback with ID as the key 19REQUESTS [ id ] =callback 20-- push our request to the web-requests thread channel 21requests_channel : push ( { id =id , request =request } ) 22break 23end 24end 25end 26 27function downloader . download ( url , filename , callback ) 28local request ={ 29url =url , 30filename =filename 31} 32downloader . request ( request , callback ) 33end 34 35-- poll the progress channel for updates 36function downloader . update () 37while progress_channel : peek () do 38-- got response from channel 39local pop =progress_channel : pop () 40local id =pop . id 41local callback =REQUESTS [ id ] 42if pop . event and pop . event . code then 43-- we got a status code, delete the callback 44REQUESTS [ id ] =nil 45end 46local succ , err =xpcall ( callback , debug . traceback , pop . event ) 47if not succ then 48-- calling our callback resulted in an error 49log . error ( "[DOWNLOADER] callback error: %s" , err ) 50end 51end 52end 53 54function downloader . close () 55requests_channel : push ( false ) 56end 57 58return downloader