Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions Runtime/Scripts/TokenSource/TokenSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using LiveKit.Internal.Threading;
using Newtonsoft.Json;

namespace LiveKit
Expand All @@ -21,7 +22,7 @@ public interface ITokenSource
/// </summary>
public interface ITokenSourceFixed : ITokenSource
{
public Task<ConnectionDetails> FetchConnectionDetails();
public TaskYieldInstruction<ConnectionDetails> FetchConnectionDetails();
}

/// <summary>
Expand All @@ -30,7 +31,7 @@ public interface ITokenSourceFixed : ITokenSource
/// </summary>
public interface ITokenSourceConfigurable : ITokenSource
{
public Task<ConnectionDetails> FetchConnectionDetails(TokenSourceFetchOptions options);
public TaskYieldInstruction<ConnectionDetails> FetchConnectionDetails(TokenSourceFetchOptions options);
}

/// <summary>
Expand All @@ -48,10 +49,10 @@ public TokenSourceLiteral(string serverUrl, string participantToken)
_participantToken = participantToken;
}

public Task<ConnectionDetails> FetchConnectionDetails()
public TaskYieldInstruction<ConnectionDetails> FetchConnectionDetails()
{
var result = new ConnectionDetails { ServerUrl = _serverUrl, ParticipantToken = _participantToken };
return Task.FromResult(result);
return new TaskYieldInstruction<ConnectionDetails>(Task.FromResult(result));
}
}

Expand All @@ -70,9 +71,22 @@ public TokenSourceCustom(CustomTokenFunction customTokenFunction)
_customTokenFunction = customTokenFunction;
}

public Task<ConnectionDetails> FetchConnectionDetails()
public TaskYieldInstruction<ConnectionDetails> FetchConnectionDetails()
{
return _customTokenFunction();
// Route a synchronous throw (or a null return) from the user's function through the
// instruction's IsError/Exception, so callers never have to guard the call itself.
Task<ConnectionDetails> task;
try
{
task = _customTokenFunction()
?? Task.FromException<ConnectionDetails>(
new InvalidOperationException("Custom token function returned a null task"));
}
catch (Exception e)
{
task = Task.FromException<ConnectionDetails>(e);
}
return new TaskYieldInstruction<ConnectionDetails>(task);
}
}

Expand All @@ -94,7 +108,14 @@ public TokenSourceEndpoint(string endpointUrl, IEnumerable<StringPair> headers)
_headers = headers;
}

public async Task<ConnectionDetails> FetchConnectionDetails(TokenSourceFetchOptions options)
public TaskYieldInstruction<ConnectionDetails> FetchConnectionDetails(TokenSourceFetchOptions options)
{
// Async methods can't return the (non-awaitable) instruction directly, so the actual
// request lives in the helper below; the returned task carries any synchronous throw.
return new TaskYieldInstruction<ConnectionDetails>(FetchConnectionDetailsAsync(options));
}

private async Task<ConnectionDetails> FetchConnectionDetailsAsync(TokenSourceFetchOptions options)
{
var requestBody = BuildRequest(options);
var jsonBody = JsonConvert.SerializeObject(requestBody);
Expand Down
9 changes: 2 additions & 7 deletions Runtime/Scripts/TokenSource/TokenSourceComponent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using LiveKit.Internal;

Expand Down Expand Up @@ -60,23 +59,19 @@ public void Awake()
/// </summary>
public TaskYieldInstruction<ConnectionDetails> FetchConnectionDetails(TokenSourceFetchOptions options)
{
Task<ConnectionDetails> task;
switch (_tokenSource)
{
case ITokenSourceConfigurable configurableSource:
task = configurableSource.FetchConnectionDetails(Coalesce(_config, options));
break;
return configurableSource.FetchConnectionDetails(Coalesce(_config, options));

case ITokenSourceFixed fixedSource:
if (options != null)
Utils.Warning("TokenSourceComponent uses a fixed config, so fetch options are ignored.");
task = fixedSource.FetchConnectionDetails();
break;
return fixedSource.FetchConnectionDetails();

default:
throw new InvalidOperationException("Unknown token source type");
}
return new TaskYieldInstruction<ConnectionDetails>(task);
}

private static TokenSourceFetchOptions Coalesce(TokenSourceComponentConfig config, TokenSourceFetchOptions? options)
Expand Down
Loading