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