1
1
using Microsoft . JSInterop ;
2
2
using System ;
3
3
using System . Collections . Generic ;
4
+ using System . Text . Json . Serialization ;
4
5
using System . Threading . Tasks ;
5
6
6
7
namespace Blazor . Extensions
@@ -20,63 +21,63 @@ public class HubConnection : IDisposable
20
21
internal HttpConnectionOptions Options { get ; }
21
22
internal string InternalConnectionId { get ; }
22
23
23
- private Dictionary < string , Dictionary < string , HubMethodCallback > > _callbacks = new Dictionary < string , Dictionary < string , HubMethodCallback > > ( ) ;
24
+ private Dictionary < string , Dictionary < string , HubMethodCallback > > callbacks = new Dictionary < string , Dictionary < string , HubMethodCallback > > ( ) ;
24
25
25
- private HubCloseCallback _closeCallback ;
26
- private IJSRuntime _runtime ;
26
+ private HubCloseCallback closeCallback ;
27
+ private IJSRuntime runtime ;
27
28
28
29
public HubConnection ( IJSRuntime runtime , HttpConnectionOptions options )
29
30
{
30
- this . _runtime = runtime ;
31
+ this . runtime = runtime ;
31
32
this . Options = options ;
32
33
this . InternalConnectionId = Guid . NewGuid ( ) . ToString ( ) ;
33
34
runtime . InvokeSync < object > ( CREATE_CONNECTION_METHOD ,
34
35
this . InternalConnectionId ,
35
- new DotNetObjectRef ( this . Options ) ) ;
36
+ DotNetObjectRef . Create ( this . Options ) ) ;
36
37
}
37
38
38
39
39
- public Task StartAsync ( ) => this . _runtime . InvokeAsync < object > ( START_CONNECTION_METHOD , this . InternalConnectionId ) ;
40
- public Task StopAsync ( ) => this . _runtime . InvokeAsync < object > ( STOP_CONNECTION_METHOD , this . InternalConnectionId ) ;
40
+ public Task StartAsync ( ) => this . runtime . InvokeAsync < object > ( START_CONNECTION_METHOD , this . InternalConnectionId ) ;
41
+ public Task StopAsync ( ) => this . runtime . InvokeAsync < object > ( STOP_CONNECTION_METHOD , this . InternalConnectionId ) ;
41
42
42
43
public IDisposable On < TResult1 > ( string methodName , Func < TResult1 , Task > handler )
43
- => On < TResult1 , object , object , object , object , object , object , object , object , object > ( methodName ,
44
+ => this . On < TResult1 , object , object , object , object , object , object , object , object , object > ( methodName ,
44
45
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 ) ) ;
45
46
46
47
public IDisposable On < TResult1 , TResult2 > ( string methodName , Func < TResult1 , TResult2 , Task > handler )
47
- => On < TResult1 , TResult2 , object , object , object , object , object , object , object , object > ( methodName ,
48
+ => this . On < TResult1 , TResult2 , object , object , object , object , object , object , object , object > ( methodName ,
48
49
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 , t2 ) ) ;
49
50
50
51
public IDisposable On < TResult1 , TResult2 , TResult3 > ( string methodName , Func < TResult1 , TResult2 , TResult3 , Task > handler )
51
- => On < TResult1 , TResult2 , TResult3 , object , object , object , object , object , object , object > ( methodName ,
52
+ => this . On < TResult1 , TResult2 , TResult3 , object , object , object , object , object , object , object > ( methodName ,
52
53
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 , t2 , t3 ) ) ;
53
54
54
55
public IDisposable On < TResult1 , TResult2 , TResult3 , TResult4 > ( string methodName , Func < TResult1 , TResult2 , TResult3 , TResult4 , Task > handler )
55
- => On < TResult1 , TResult2 , TResult3 , TResult4 , object , object , object , object , object , object > ( methodName ,
56
+ => this . On < TResult1 , TResult2 , TResult3 , TResult4 , object , object , object , object , object , object > ( methodName ,
56
57
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 , t2 , t3 , t4 ) ) ;
57
58
58
59
public IDisposable On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 > ( string methodName , Func < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , Task > handler )
59
- => On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , object , object , object , object , object > ( methodName ,
60
+ => this . On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , object , object , object , object , object > ( methodName ,
60
61
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 , t2 , t3 , t4 , t5 ) ) ;
61
62
62
63
public IDisposable On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 > ( string methodName ,
63
64
Func < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , Task > handler )
64
- => On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , object , object , object , object > ( methodName ,
65
+ => this . On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , object , object , object , object > ( methodName ,
65
66
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 , t2 , t3 , t4 , t5 , t6 ) ) ;
66
67
67
68
public IDisposable On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 > ( string methodName ,
68
69
Func < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , Task > handler )
69
- => On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , object , object , object > ( methodName ,
70
+ => this . On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , object , object , object > ( methodName ,
70
71
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 , t2 , t3 , t4 , t5 , t6 , t7 ) ) ;
71
72
72
73
public IDisposable On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 > ( string methodName ,
73
74
Func < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 , Task > handler )
74
- => On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 , object , object > ( methodName ,
75
+ => this . On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 , object , object > ( methodName ,
75
76
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 ) ) ;
76
77
77
78
public IDisposable On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 , TResult9 > ( string methodName ,
78
79
Func < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 , TResult9 , Task > handler )
79
- => On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 , TResult9 , object > ( methodName ,
80
+ => this . On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 , TResult9 , object > ( methodName ,
80
81
( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) => handler ( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 ) ) ;
81
82
82
83
public IDisposable On < TResult1 , TResult2 , TResult3 , TResult4 , TResult5 , TResult6 , TResult7 , TResult8 , TResult9 , TResult10 > ( string methodName ,
@@ -103,103 +104,103 @@ public IDisposable On<TResult1, TResult2, TResult3, TResult4, TResult5, TResult6
103
104
104
105
if ( payloads . Length > 0 )
105
106
{
106
- t1 = Json . Deserialize < TResult1 > ( payloads [ 0 ] ) ;
107
+ t1 = JsonSerializer . Parse < TResult1 > ( payloads [ 0 ] ) ;
107
108
}
108
109
if ( payloads . Length > 1 )
109
110
{
110
- t2 = Json . Deserialize < TResult2 > ( payloads [ 1 ] ) ;
111
+ t2 = JsonSerializer . Parse < TResult2 > ( payloads [ 1 ] ) ;
111
112
}
112
113
if ( payloads . Length > 2 )
113
114
{
114
- t3 = Json . Deserialize < TResult3 > ( payloads [ 2 ] ) ;
115
+ t3 = JsonSerializer . Parse < TResult3 > ( payloads [ 2 ] ) ;
115
116
}
116
117
if ( payloads . Length > 3 )
117
118
{
118
- t4 = Json . Deserialize < TResult4 > ( payloads [ 3 ] ) ;
119
+ t4 = JsonSerializer . Parse < TResult4 > ( payloads [ 3 ] ) ;
119
120
}
120
121
if ( payloads . Length > 4 )
121
122
{
122
- t5 = Json . Deserialize < TResult5 > ( payloads [ 4 ] ) ;
123
+ t5 = JsonSerializer . Parse < TResult5 > ( payloads [ 4 ] ) ;
123
124
}
124
125
if ( payloads . Length > 5 )
125
126
{
126
- t6 = Json . Deserialize < TResult6 > ( payloads [ 5 ] ) ;
127
+ t6 = JsonSerializer . Parse < TResult6 > ( payloads [ 5 ] ) ;
127
128
}
128
129
if ( payloads . Length > 6 )
129
130
{
130
- t7 = Json . Deserialize < TResult7 > ( payloads [ 6 ] ) ;
131
+ t7 = JsonSerializer . Parse < TResult7 > ( payloads [ 6 ] ) ;
131
132
}
132
133
if ( payloads . Length > 7 )
133
134
{
134
- t8 = Json . Deserialize < TResult8 > ( payloads [ 7 ] ) ;
135
+ t8 = JsonSerializer . Parse < TResult8 > ( payloads [ 7 ] ) ;
135
136
}
136
137
if ( payloads . Length > 8 )
137
138
{
138
- t9 = Json . Deserialize < TResult9 > ( payloads [ 8 ] ) ;
139
+ t9 = JsonSerializer . Parse < TResult9 > ( payloads [ 8 ] ) ;
139
140
}
140
141
if ( payloads . Length > 9 )
141
142
{
142
- t10 = Json . Deserialize < TResult10 > ( payloads [ 9 ] ) ;
143
+ t10 = JsonSerializer . Parse < TResult10 > ( payloads [ 9 ] ) ;
143
144
}
144
145
145
146
return handler ( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 ) ;
146
147
}
147
148
) ;
148
149
149
- RegisterHandle ( methodName , callback ) ;
150
+ this . RegisterHandle ( methodName , callback ) ;
150
151
151
152
return callback ;
152
153
}
153
154
154
155
internal void RegisterHandle ( string methodName , HubMethodCallback callback )
155
156
{
156
- if ( this . _callbacks . TryGetValue ( methodName , out var methodHandlers ) )
157
+ if ( this . callbacks . TryGetValue ( methodName , out var methodHandlers ) )
157
158
{
158
159
methodHandlers [ callback . Id ] = callback ;
159
160
}
160
161
else
161
162
{
162
- this . _callbacks [ methodName ] = new Dictionary < string , HubMethodCallback >
163
+ this . callbacks [ methodName ] = new Dictionary < string , HubMethodCallback >
163
164
{
164
165
{ callback . Id , callback }
165
166
} ;
166
167
}
167
168
168
- this . _runtime . InvokeSync < object > ( ON_METHOD , this . InternalConnectionId , new DotNetObjectRef ( callback ) ) ;
169
+ this . runtime . InvokeSync < object > ( ON_METHOD , this . InternalConnectionId , DotNetObjectRef . Create ( callback ) ) ;
169
170
}
170
171
171
172
internal void RemoveHandle ( string methodName , string callbackId )
172
173
{
173
- if ( this . _callbacks . TryGetValue ( methodName , out var callbacks ) )
174
+ if ( this . callbacks . TryGetValue ( methodName , out var callbacks ) )
174
175
{
175
176
if ( callbacks . TryGetValue ( callbackId , out var callback ) )
176
177
{
177
- this . _runtime . InvokeSync < object > ( OFF_METHOD , this . InternalConnectionId , methodName , callbackId ) ;
178
+ this . runtime . InvokeSync < object > ( OFF_METHOD , this . InternalConnectionId , methodName , callbackId ) ;
178
179
//HubConnectionManager.Off(this.InternalConnectionId, handle.Item1);
179
180
callbacks . Remove ( callbackId ) ;
180
181
181
182
if ( callbacks . Count == 0 )
182
183
{
183
- this . _callbacks . Remove ( methodName ) ;
184
+ this . callbacks . Remove ( methodName ) ;
184
185
}
185
186
}
186
187
}
187
188
}
188
189
189
190
public void OnClose ( Func < Exception , Task > callback )
190
191
{
191
- this . _closeCallback = new HubCloseCallback ( callback ) ;
192
- this . _runtime . InvokeSync < object > ( ON_CLOSE_METHOD ,
192
+ this . closeCallback = new HubCloseCallback ( callback ) ;
193
+ this . runtime . InvokeSync < object > ( ON_CLOSE_METHOD ,
193
194
this . InternalConnectionId ,
194
- new DotNetObjectRef ( this . _closeCallback ) ) ;
195
+ DotNetObjectRef . Create ( this . closeCallback ) ) ;
195
196
}
196
197
197
198
public Task InvokeAsync ( string methodName , params object [ ] args ) =>
198
- this . _runtime . InvokeAsync < object > ( INVOKE_ASYNC_METHOD , this . InternalConnectionId , methodName , args ) ;
199
+ this . runtime . InvokeAsync < object > ( INVOKE_ASYNC_METHOD , this . InternalConnectionId , methodName , args ) ;
199
200
200
201
public Task < TResult > InvokeAsync < TResult > ( string methodName , params object [ ] args ) =>
201
- this . _runtime . InvokeAsync < TResult > ( INVOKE_WITH_RESULT_ASYNC_METHOD , this . InternalConnectionId , methodName , args ) ;
202
+ this . runtime . InvokeAsync < TResult > ( INVOKE_WITH_RESULT_ASYNC_METHOD , this . InternalConnectionId , methodName , args ) ;
202
203
203
- public void Dispose ( ) => this . _runtime . InvokeSync < object > ( REMOVE_CONNECTION_METHOD , this . InternalConnectionId ) ;
204
+ public void Dispose ( ) => this . runtime . InvokeSync < object > ( REMOVE_CONNECTION_METHOD , this . InternalConnectionId ) ;
204
205
}
205
206
}
0 commit comments