Browse Source

feature: obey CacheMode on BaseSocketClient

feature/rest-on-socket
Christopher F 6 years ago
parent
commit
6a562f5533
2 changed files with 70 additions and 2 deletions
  1. +68
    -1
      src/Discord.Net.WebSocket/BaseSocketClient.cs
  2. +2
    -1
      src/Discord.Net.WebSocket/DiscordSocketClient.cs

+ 68
- 1
src/Discord.Net.WebSocket/BaseSocketClient.cs View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.API; using Discord.API;
using Discord.Rest; using Discord.Rest;
@@ -46,7 +48,56 @@ namespace Discord.WebSocket
public abstract Task SetStatusAsync(UserStatus status); public abstract Task SetStatusAsync(UserStatus status);
public abstract Task SetGameAsync(string name, string streamUrl = null, ActivityType type = ActivityType.Playing); public abstract Task SetGameAsync(string name, string streamUrl = null, ActivityType type = ActivityType.Playing);
public abstract Task SetActivityAsync(IActivity activity); public abstract Task SetActivityAsync(IActivity activity);
public abstract Task DownloadUsersAsync(IEnumerable<IGuild> guilds);
public abstract Task DownloadUsersAsync(IEnumerable<IGuild> guilds);

public Task<IChannel> GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
{
Func<Task<IChannel>> restAction = async () =>
await ClientHelper.GetChannelAsync(this, id, options).ConfigureAwait(false) as IChannel;
Func<IChannel> cacheAction = () => GetChannel(id);

return CacheHelper(mode, restAction, cacheAction);
}
public Task<IReadOnlyCollection<IPrivateChannel>> GetPrivateChannelsAsync(CacheMode mode, RequestOptions options)
{
Func<Task<IReadOnlyCollection<IPrivateChannel>>> restAction = async () =>
{
var col = (await ClientHelper.GetPrivateChannelsAsync(this, options).ConfigureAwait(false)).OfType<IPrivateChannel>();
return col.ToReadOnlyCollection(col.Count);
};
Func<IReadOnlyCollection<IPrivateChannel>> cacheAction = () => PrivateChannels;

return CacheHelper(mode, restAction, cacheAction);
}

public Task<IGuild> GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
{
Func<Task<IGuild>> restAction = async () =>
await ClientHelper.GetGuildAsync(this, id, options).ConfigureAwait(false) as IGuild;
Func<IGuild> cacheAction = () => GetGuild(id);

return CacheHelper(mode, restAction, cacheAction);
}
public Task<IReadOnlyCollection<IGuild>> GetGuildsAsync(CacheMode mode, RequestOptions options)
{
Func<Task<IReadOnlyCollection<IGuild>>> restAction = async () =>
{
var col = (await ClientHelper.GetGuildsAsync(this, options)).OfType<IGuild>();
return col.ToReadOnlyCollection(col.Count);
};
Func<IReadOnlyCollection<IGuild>> cacheAction = () => Guilds;

return CacheHelper(mode, restAction, cacheAction);
}

public Task<IUser> GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
{
Func<Task<IUser>> restAction = async () =>
await ClientHelper.GetUserAsync(this, id, options).ConfigureAwait(false) as IUser;
Func<IUser> cacheAction = () => GetUser(id);

return CacheHelper(mode, restAction, cacheAction);
}


/// <inheritdoc /> /// <inheritdoc />
public Task<RestGuild> CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null, RequestOptions options = null) public Task<RestGuild> CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null, RequestOptions options = null)
@@ -90,5 +141,21 @@ namespace Discord.WebSocket
=> Task.FromResult<IVoiceRegion>(GetVoiceRegion(id)); => Task.FromResult<IVoiceRegion>(GetVoiceRegion(id));
Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options) Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options)
=> Task.FromResult<IReadOnlyCollection<IVoiceRegion>>(VoiceRegions); => Task.FromResult<IReadOnlyCollection<IVoiceRegion>>(VoiceRegions);

public async Task<T> CacheHelper<T>(CacheMode mode, Func<Task<T>> restAction, Func<T> cacheAction)
where T : class
{
switch (mode)
{
case CacheMode.CacheOnly:
return cacheAction();
case CacheMode.AllowDownload:
return cacheAction() ?? await restAction().ConfigureAwait(false);
case CacheMode.ForceDownload:
return await restAction().ConfigureAwait(false);
default:
throw new InvalidOperationException("Unhandled CacheMode");
}
}
} }
} }

+ 2
- 1
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -1806,6 +1806,7 @@ namespace Discord.WebSocket
internal int GetAudioId() => _nextAudioId++; internal int GetAudioId() => _nextAudioId++;


//IDiscordClient //IDiscordClient
/*
async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options) async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
=> await GetApplicationInfoAsync().ConfigureAwait(false); => await GetApplicationInfoAsync().ConfigureAwait(false);


@@ -1839,7 +1840,7 @@ namespace Discord.WebSocket
Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options) Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options)
=> Task.FromResult<IReadOnlyCollection<IVoiceRegion>>(VoiceRegions); => Task.FromResult<IReadOnlyCollection<IVoiceRegion>>(VoiceRegions);
Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
=> Task.FromResult<IVoiceRegion>(GetVoiceRegion(id));
=> Task.FromResult<IVoiceRegion>(GetVoiceRegion(id));*/


async Task IDiscordClient.StartAsync() async Task IDiscordClient.StartAsync()
=> await StartAsync().ConfigureAwait(false); => await StartAsync().ConfigureAwait(false);


Loading…
Cancel
Save