-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhttprequest_emscripten.wx
100 lines (56 loc) · 1.5 KB
/
httprequest_emscripten.wx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Namespace httprequest
#Import "<emscripten>"
Using emscripten..
Class HttpRequest Extends HttpRequestBase
Method New()
End
Protected
Method OnSend( text:String ) Override
Local attr:emscripten_fetch_attr_t
emscripten_fetch_attr_init( Varptr attr )
_req.ToCString( attr.requestMethod,4 )
attr.attributes=EMSCRIPTEN_FETCH_LOAD_TO_MEMORY
attr.onsuccess=FetchSuccess
attr.onerror=FetchError
SetReadyState( ReadyState.Loading )
_fetch=emscripten_fetch( Varptr attr,_url )
_sending.Add( Self )
End
Method OnCancel() Override
If Not _fetch Return
Close()
SetReadyState( ReadyState.Error )
End
Private
Field _fetch:emscripten_fetch_t Ptr
Global _sending:=New Stack<HttpRequest>
Method Close()
If Not _fetch Return
_sending.Remove( Self )
emscripten_fetch_close( _fetch )
_fetch=Null
End
Method Success()
_response=String.FromCString( _fetch->data,_fetch->numBytes )
_status=_fetch->status
SetReadyState( ReadyState.Done )
Close()
End
Method Error()
_status=_fetch->status
SetReadyState( ReadyState.Error )
Close()
End
Function FindRequest:HttpRequest( fetch:emscripten_fetch_t Ptr )
For Local request:=Eachin _sending
If request._fetch=fetch Return request
Next
Return Null
End
Function FetchSuccess( fetch:emscripten_fetch_t Ptr )
FindRequest( fetch )?.Success()
End
Function FetchError( fetch:emscripten_fetch_t Ptr )
FindRequest( fetch )?.Error()
End
End