-- "NetOp.transaction" Parent Script (v.1) -- @version 1.1 -- @Example Usage -- -- on mouseUp me -- urlStr = "http://www.lingoworkshop.com/" -- netOp = script("NetOp.transaction").new(urlStr) -- netOp.AddListener(me) -- netOp.Start() -- end -- -- on NetTransactionComplete (me, sender, args) -- put "Net Transaction Complete: " & args -- end -- -- on NetTransactionStatusUpdate(me, sender, args) -- put "Net Transaction Status: " & args -- end -- @Notes -- Works as a self-contained 'daemon' (the timer instances create -- keep the object alive. When the transaction is complete, the -- timer is forgotten. If you have not kept another reference to the -- instance, it will be destroyed automatically) -- @Dependencies -- script("NetOp") property ancestor property NetID property URL, FORMDATA on new (me, netAddress, data) URL = netAddress FORMDATA = data ancestor = script("NetOp").rawNew() callAncestor(#new, me) return me end on Start (me) if voidP(FORMDATA) then NetID = getNetText(URL ) else NetID = postNetText(URL , FORMDATA) aTimerObj = timeout().new(me.string, 100, #Timer_CheckProgress, me) return NetID end on Destroy (me) netAbort(NetID) NetID = VOID callAncestor(#Destroy, me) end on Timer_CheckProgress (me, aTimer) if voidP(NetID) then -- we have aborted the operation atimer.forget() return 0 end if finished = netDone(NetID) if finished then errorNum = netError(NetID) theText = netTextResult(NetID) if stringP(errorNum) then errorNum =0 aTimer.forget() me.GenerateEventMessage(#NetTransactionComplete, [#Text: theText, #Error: errorNum]) else -- check the current status status = getStreamStatus(NetID) currentState = status.state -- before sending the status, work out the portion downloaded case (currentState) of "InProgress": if status.bytesSoFar > 0 then if status.bytesTotal > 0 then f = MIN(1.0, float(status.bytesSoFar)/status.bytesTotal ) else f = 0.50 else f = 0 end if "Complete": f = 1.00 otherwise f = 0 end case -- add the fractionDone as a property to the statusList status.addProp( #fractiondone, f ) -- inform the callback object of the current state and the % transferred me.GenerateEventMessage(#NetTransactionStatusUpdate, status) end if end