@@ -103,7 +103,7 @@ pub const XMLHttpRequest = struct {
103103 ctx : ? Client.Ctx = null ,
104104
105105 method : std.http.Method ,
106- state : u16 ,
106+ state : State ,
107107 url : ? []const u8 ,
108108 uri : std.Uri ,
109109 // request headers
@@ -150,11 +150,13 @@ pub const XMLHttpRequest = struct {
150150 pub const prototype = * XMLHttpRequestEventTarget ;
151151 pub const mem_guarantied = true ;
152152
153- pub const UNSENT : u16 = 0 ;
154- pub const OPENED : u16 = 1 ;
155- pub const HEADERS_RECEIVED : u16 = 2 ;
156- pub const LOADING : u16 = 3 ;
157- pub const DONE : u16 = 4 ;
153+ const State = enum (u16 ) {
154+ unsent = 0 ,
155+ opened = 1 ,
156+ headers_received = 2 ,
157+ loading = 3 ,
158+ done = 4 ,
159+ };
158160
159161 // https://xhr.spec.whatwg.org/#response-type
160162 const ResponseType = enum {
@@ -297,7 +299,7 @@ pub const XMLHttpRequest = struct {
297299 .method = undefined ,
298300 .url = null ,
299301 .uri = undefined ,
300- .state = UNSENT ,
302+ .state = .unsent ,
301303 .cli = userctx .httpClient ,
302304 };
303305 }
@@ -347,7 +349,7 @@ pub const XMLHttpRequest = struct {
347349 }
348350
349351 pub fn get_readyState (self : * XMLHttpRequest ) u16 {
350- return self .state ;
352+ return @intFromEnum ( self .state ) ;
351353 }
352354
353355 pub fn get_timeout (_ : * XMLHttpRequest ) u32 {
@@ -367,7 +369,7 @@ pub const XMLHttpRequest = struct {
367369 }
368370
369371 pub fn set_withCredentials (self : * XMLHttpRequest , withCredentials : bool ) ! void {
370- if (self .state != OPENED and self .state != UNSENT ) return DOMError .InvalidState ;
372+ if (self .state != .opened and self .state != .unsent ) return DOMError .InvalidState ;
371373 if (self .send_flag ) return DOMError .InvalidState ;
372374
373375 self .withCredentials = withCredentials ;
@@ -401,7 +403,7 @@ pub const XMLHttpRequest = struct {
401403 log .debug ("open url ({s})" , .{self .url .? });
402404 self .sync = if (asyn ) | b | ! b else false ;
403405
404- self .state = OPENED ;
406+ self .state = .opened ;
405407 self .dispatchEvt ("readystatechange" );
406408 }
407409
@@ -477,14 +479,14 @@ pub const XMLHttpRequest = struct {
477479 }
478480
479481 pub fn _setRequestHeader (self : * XMLHttpRequest , name : []const u8 , value : []const u8 ) ! void {
480- if (self .state != OPENED ) return DOMError .InvalidState ;
482+ if (self .state != .opened ) return DOMError .InvalidState ;
481483 if (self .send_flag ) return DOMError .InvalidState ;
482484 return try self .headers .append (name , value );
483485 }
484486
485487 // TODO body can be either a XMLHttpRequestBodyInit or a document
486488 pub fn _send (self : * XMLHttpRequest , alloc : std.mem.Allocator , body : ? []const u8 ) ! void {
487- if (self .state != OPENED ) return DOMError .InvalidState ;
489+ if (self .state != .opened ) return DOMError .InvalidState ;
488490 if (self .send_flag ) return DOMError .InvalidState ;
489491
490492 // The body argument provides the request body, if any, and is ignored
@@ -554,7 +556,7 @@ pub const XMLHttpRequest = struct {
554556
555557 // TODO handle override mime type
556558
557- self .state = HEADERS_RECEIVED ;
559+ self .state = .headers_received ;
558560 self .dispatchEvt ("readystatechange" );
559561
560562 self .response_status = @intFromEnum (self .req .? .response .status );
@@ -592,7 +594,7 @@ pub const XMLHttpRequest = struct {
592594 if (prev_dispatch != null and now .since (prev_dispatch .? ) < min_delay ) continue ;
593595 defer prev_dispatch = now ;
594596
595- self .state = LOADING ;
597+ self .state = .loading ;
596598 self .dispatchEvt ("readystatechange" );
597599
598600 // dispatch a progress event progress.
@@ -604,7 +606,7 @@ pub const XMLHttpRequest = struct {
604606 self .response_bytes = buf .items ;
605607 self .send_flag = false ;
606608
607- self .state = DONE ;
609+ self .state = .done ;
608610 self .dispatchEvt ("readystatechange" );
609611
610612 // dispatch a progress event load.
@@ -666,7 +668,7 @@ pub const XMLHttpRequest = struct {
666668 self .priv_state = .done ;
667669
668670 self .err = err ;
669- self .state = DONE ;
671+ self .state = .done ;
670672 self .send_flag = false ;
671673 self .dispatchEvt ("readystatechange" );
672674 self .dispatchProgressEvent ("error" , .{});
@@ -697,7 +699,7 @@ pub const XMLHttpRequest = struct {
697699 }
698700
699701 pub fn set_responseType (self : * XMLHttpRequest , rtype : []const u8 ) ! void {
700- if (self .state == LOADING or self .state == DONE ) return DOMError .InvalidState ;
702+ if (self .state == .loading or self .state == .done ) return DOMError .InvalidState ;
701703
702704 if (std .mem .eql (u8 , rtype , "" )) {
703705 self .response_type = .Empty ;
@@ -735,7 +737,7 @@ pub const XMLHttpRequest = struct {
735737 return DOMError .InvalidState ;
736738 }
737739
738- if (self .state != DONE ) return null ;
740+ if (self .state != .done ) return null ;
739741
740742 // fastpath if response is previously parsed.
741743 if (self .response_obj ) | obj | {
@@ -761,7 +763,7 @@ pub const XMLHttpRequest = struct {
761763 // https://xhr.spec.whatwg.org/#the-response-attribute
762764 pub fn get_response (self : * XMLHttpRequest , alloc : std.mem.Allocator ) ! ? Response {
763765 if (self .response_type == .Empty or self .response_type == .Text ) {
764- if (self .state == LOADING or self .state == DONE ) {
766+ if (self .state == .loading or self .state == .done ) {
765767 return .{ .Text = try self .get_responseText () };
766768 }
767769 return .{ .Text = "" };
0 commit comments