Skip to content

Commit

Permalink
#264 - Refactor dependencies between ApiHubService and SignalRListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Mar 20, 2020
1 parent 587c472 commit d2ba4fd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
18 changes: 12 additions & 6 deletions src/Money.UI.Blazor/Services/ApiHubService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.JSInterop;
using Microsoft.Extensions.Options;
using Neptuo;
using Neptuo.Events;
using Neptuo.Exceptions;
Expand All @@ -18,33 +18,39 @@ public class ApiHubService
private readonly BrowserEventDispatcher events;
private readonly BrowserExceptionHandler exceptions;
private readonly Navigator navigator;
private readonly ApiClientConfiguration apiConfiguration;
private readonly TokenContainer token;
private readonly ILog log;
private HubConnection connection;

public bool IsStarted { get; private set; }

public ApiHubService(BrowserEventDispatcher events, BrowserExceptionHandler exceptions, Navigator navigator, ILogFactory logFactory)
public ApiHubService(BrowserEventDispatcher events, BrowserExceptionHandler exceptions, Navigator navigator, IOptions<ApiClientConfiguration> apiConfiguration, TokenContainer token, ILogFactory logFactory)
{
Ensure.NotNull(events, "events");
Ensure.NotNull(exceptions, "exceptions");
Ensure.NotNull(navigator, "navigator");
Ensure.NotNull(apiConfiguration, "apiConfiguration");
Ensure.NotNull(token, "token");
Ensure.NotNull(logFactory, "logFactory");
this.events = events;
this.exceptions = exceptions;
this.navigator = navigator;
this.apiConfiguration = apiConfiguration.Value;
this.token = token;
this.log = logFactory.Scope("ApiHub");
}

public async Task StartAsync(string url, string token)
public async Task StartAsync()
{
await StopAsync();

log.Debug($"Connecting with token '{token}'.");
log.Debug($"Connecting with token '{token.Value}'.");

url = $"{url}?access_token={token}";
string url = $"{apiConfiguration.ApiUrl}api?access_token={token.Value}";

connection = new HubConnectionBuilder()
.WithUrl(url, o => o.AccessTokenProvider = () => Task.FromResult(token))
.WithUrl(url, o => o.AccessTokenProvider = () => Task.FromResult(token.Value))
.Build();

connection.On<string>("RaiseEvent", payload =>
Expand Down
13 changes: 3 additions & 10 deletions src/Money.UI.Blazor/Services/SignalRListener.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Options;
using Money.Events;
using Money.Events;
using Neptuo;
using Neptuo.Events.Handlers;
using System;
Expand All @@ -14,21 +13,15 @@ namespace Money.Services
public class SignalRListener : IEventHandler<UserSignedIn>, IEventHandler<UserSignedOut>
{
private readonly ApiHubService apiHub;
private readonly ApiClientConfiguration apiConfiguration;
private readonly TokenContainer token;

public SignalRListener(ApiHubService apiHub, IOptions<ApiClientConfiguration> apiConfiguration, TokenContainer token)
public SignalRListener(ApiHubService apiHub)
{
Ensure.NotNull(apiHub, "apiHub");
Ensure.NotNull(apiConfiguration, "apiConfiguration");
Ensure.NotNull(token, "token");
this.apiHub = apiHub;
this.apiConfiguration = apiConfiguration.Value;
this.token = token;
}

Task IEventHandler<UserSignedIn>.HandleAsync(UserSignedIn payload)
=> apiHub.StartAsync(apiConfiguration.ApiUrl.ToString() + "api", token.Value);
=> apiHub.StartAsync();

Task IEventHandler<UserSignedOut>.HandleAsync(UserSignedOut payload)
=> apiHub.StopAsync();
Expand Down

0 comments on commit d2ba4fd

Please sign in to comment.