X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F06_net%2F01_XNetXHR.js;h=fa2e49c94faabbbd70a65a7638190526cb2847b6;hb=4afbf1b687e035eaf2c6b8bbf14e3c443d843d4e;hp=11e5ba7a108de06c1da7eb79e05683d33357d036;hpb=7f26e99d39211b5749c4ad62a84855404c7390a3;p=pettanr%2FclientJs.git diff --git a/0.6.x/js/06_net/01_XNetXHR.js b/0.6.x/js/06_net/01_XNetXHR.js index 11e5ba7..fa2e49c 100644 --- a/0.6.x/js/06_net/01_XNetXHR.js +++ b/0.6.x/js/06_net/01_XNetXHR.js @@ -86,6 +86,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ _busy : false, _canceled : false, _percent : 0, + _timerID : 0, load : function( obj ){ var raw = this._rawObject, @@ -96,7 +97,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ password = obj[ 'password' ], headers = obj[ 'headers' ], postbody = obj[ 'postbody' ], - timeout = obj[ 'timeout' ], + timeout = obj[ 'timeout' ] || 20000, temp; if( obj[ 'type' ] ){ @@ -127,8 +128,30 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ }; }; }; - //if( raw.timeout !== undefined ) raw.timeout = timeout || -1; - raw.open( method, url, false ); + + raw.open( method, url, true ); + + if( raw.responseType !== undefined ){ + switch( this._type ){ + case '' : + case 'text' : + raw.responseType = 'text'; + break; + case 'json' : + case 'moz-json' : + break; + case 'document' : + case 'xml' : + case 'html' : + case 'htm' : + raw.responseType = 'document'; + break; + case 'blob' : + case 'arraybuffer' : + raw.responseType = this._type; + break; + }; + }; // http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_r_1.html // raw.overrideMimeType() @@ -138,6 +161,12 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ }; }; + if( raw.timeout !== undefined ){ + raw.timeout = timeout; //Firefox33 でエラー,,, + } else { + this._timerID = X.Timer.once( timeout, this, this.onTimeout ); + }; + // send 前にフラグを立てる,回線が早いと raw.send() 内で onload -> _busy = false ののち、 _busy = true するため。 this._busy = true; @@ -148,14 +177,33 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ }, cancel : function(){ - X.Net.XHR.CANCELABLE && this._rawObject.abort(); + /* X.Net.XHR.CANCELABLE && */ this._rawObject.abort && this._rawObject.abort(); this._canceled = true; + this.asyncDispatch( X.Event.CANCELED ); }, reset : function(){ + // XMLHttpRequest で順番にリソースを取得する + // http://note.chiebukuro.yahoo.co.jp/detail/n16248 + // TODO Opera 10.10 と Safari 4.1 はエラーが起きた XHR を再利用できないので毎回作る + + // + // domes.lingua.heliohost.org/dom-intro/load-save2.html + // 規定上は open() を呼び出すと XMLHttpRequest オブジェクトが未送信状態に戻りますが、 + // Opera 10.10、Safari 4.1 では、同一オリジン制限に違反した XMLHttpRequest オブジェクトは再度 open() しても未送信状態に戻りません。 + if( X_UA.Opera || X_UA.Webkit ){ + + }; + + // XMLHttpRequest の使い方 + // http://webos-goodies.jp/archives/50548720.html + // XMLHttpRequest オブジェクトを再利用する際も、 abort メソッドを呼び出す必要があるようです。 + this._rawObject.abort && this._rawObject.abort(); + this._method = this._type = ''; this._canceled = this._busy = false; - this._percent = 0; + this._timerID && X.Timer.remove( this._timerID ); + this._percent = this._timerID = 0; }, handleEvent : function( e ){ @@ -253,7 +301,6 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ break; case 'progress' : - // TODO X.Dom.Event でコピーしていないのでまだ動かない、、、 if( e.lengthComputable ){ this._percent = e.loaded / e.total; live && this.asyncDispatch( { type : X.Event.PROGRESS, percent : this._percent } ); @@ -265,18 +312,25 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ this._busy = false; live && this.asyncDispatch( { type : X.Event.ERROR, status : raw.status } ); break; - - //case 'abort' : - // this._busy = false; - // this.asyncDispatch( { type : X.Event.ERROR, status : raw.status } ); - // break; + case 'timeout' : // Gecko 12.0 https://developer.mozilla.org/ja/docs/XMLHttpRequest/Synchronous_and_Asynchronous_Requests this._busy = false; - live && this.asyncDispatch( { type : X.Event.ERROR, status : raw.status } ); + this.asyncDispatch( X.Event.TIMEOUT ); break; }; }, + onTimeout : function(){ + var raw = this._rawObject, + live = !X_NET_XHRWrapper._canceled || !this._busy; + + if( raw.readyState < 3 ){ + this._busy = false; + live && this.asyncDispatch( X.Event.TIMEOUT ); + }; + this._timerID = 0; + }, + onUploadProgress : X.Net.XHR.UL_PROGRESS && function( e ){ var raw = X_NET_XHRWrapper._rawObject.upload, live = !X_NET_XHRWrapper._canceled, @@ -287,25 +341,23 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){ ); // 同期リクエストでなければならない場合, unload, beforeunload時 - - // ie8 では timeout が有効, MSXML のバージョンは関係なさそう、、、 if( X_UA.IE8 ){ - X_NET_XHRWrapper.listen( [ 'readystatechange', 'error', 'abort', 'timeout' ] ); + X_NET_XHRWrapper.listen( [ 'readystatechange', 'error', 'timeout' ] ); //, 'abort' } else if( X_UA.IE7 ){ if( X_URL_IS_LOCAL ){ X_NET_XHRWrapper.listen( 'readystatechange' ); // ie7 ActiveX の場合、error は不可 } else { - X_NET_XHRWrapper.listen( [ 'readystatechange', 'error' ] ); // ie7 ActiveX の場合、error は不可 + X_NET_XHRWrapper.listen( [ 'readystatechange', 'error' ] ); }; } else if( X_Net_XHR_ACTIVE_X ){ // win ie5-6 X_NET_XHRWrapper.listen( 'readystatechange' ); } else if( X.Net.XHR.PROGRESS ){ - X_NET_XHRWrapper.listen( [ 'load', 'progress', 'error', 'abort', 'timeout' ] ); + X_NET_XHRWrapper.listen( [ 'load', 'progress', 'error', 'timeout' ] ); //, 'abort' } else { - X_NET_XHRWrapper.listen( [ 'load', 'readystatechange', 'error', 'abort', 'timeout' ] ); + X_NET_XHRWrapper.listen( [ 'load', 'readystatechange', 'error', 'timeout' ] ); //, 'abort' }; if( X.Net.XHR.UL_PROGRESS ){