Skip to content

Commit df034cd

Browse files
CatoLeanTruetschelgalvesribeiro
authored andcommitted
Removed cast to IJSInProcessRuntime (#17)
1 parent bf752bb commit df034cd

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Blazor.Extensions.SignalR/HubConnection.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public HubConnection(HttpConnectionOptions options)
2828
{
2929
this.Options = options;
3030
this.InternalConnectionId = Guid.NewGuid().ToString();
31-
((IJSInProcessRuntime)JSRuntime.Current).Invoke<object>(CREATE_CONNECTION_METHOD,
31+
JSRuntime.Current.InvokeSync<object>(CREATE_CONNECTION_METHOD,
3232
this.InternalConnectionId,
3333
new DotNetObjectRef(this.Options));
3434
}
@@ -163,7 +163,7 @@ internal void RegisterHandle(string methodName, HubMethodCallback callback)
163163
};
164164
}
165165

166-
((IJSInProcessRuntime)JSRuntime.Current).Invoke<object>(ON_METHOD, this.InternalConnectionId, new DotNetObjectRef(callback));
166+
JSRuntime.Current.InvokeSync<object>(ON_METHOD, this.InternalConnectionId, new DotNetObjectRef(callback));
167167
}
168168

169169
internal void RemoveHandle(string methodName, string callbackId)
@@ -172,7 +172,7 @@ internal void RemoveHandle(string methodName, string callbackId)
172172
{
173173
if (callbacks.TryGetValue(callbackId, out var callback))
174174
{
175-
((IJSInProcessRuntime)JSRuntime.Current).Invoke<object>(OFF_METHOD, this.InternalConnectionId, methodName, callbackId);
175+
JSRuntime.Current.InvokeSync<object>(OFF_METHOD, this.InternalConnectionId, methodName, callbackId);
176176
//HubConnectionManager.Off(this.InternalConnectionId, handle.Item1);
177177
callbacks.Remove(callbackId);
178178

@@ -187,7 +187,7 @@ internal void RemoveHandle(string methodName, string callbackId)
187187
public void OnClose(Func<Exception, Task> callback)
188188
{
189189
this._closeCallback = new HubCloseCallback(callback);
190-
((IJSInProcessRuntime)JSRuntime.Current).Invoke<object>(ON_CLOSE_METHOD,
190+
JSRuntime.Current.InvokeSync<object>(ON_CLOSE_METHOD,
191191
this.InternalConnectionId,
192192
new DotNetObjectRef(this._closeCallback));
193193
}
@@ -198,6 +198,6 @@ public Task InvokeAsync(string methodName, params object[] args) =>
198198
public Task<TResult> InvokeAsync<TResult>(string methodName, params object[] args) =>
199199
JSRuntime.Current.InvokeAsync<TResult>(INVOKE_WITH_RESULT_ASYNC_METHOD, this.InternalConnectionId, methodName, args);
200200

201-
public void Dispose() => ((IJSInProcessRuntime)JSRuntime.Current).Invoke<object>(REMOVE_CONNECTION_METHOD, this.InternalConnectionId);
201+
public void Dispose() => JSRuntime.Current.InvokeSync<object>(REMOVE_CONNECTION_METHOD, this.InternalConnectionId);
202202
}
203203
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using Microsoft.JSInterop;
3+
4+
namespace Blazor.Extensions
5+
{
6+
internal static class JSRuntimeExtensions
7+
{
8+
public static T InvokeSync<T>(this IJSRuntime jsRuntime, string identifier, params object[] args)
9+
{
10+
if (jsRuntime == null)
11+
throw new ArgumentNullException(nameof(jsRuntime));
12+
13+
if (jsRuntime is IJSInProcessRuntime inProcessJsRuntime)
14+
{
15+
return inProcessJsRuntime.Invoke<T>(identifier, args);
16+
}
17+
18+
return jsRuntime.InvokeAsync<T>(identifier, args).ConfigureAwait(false).GetAwaiter().GetResult();
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)