Improved load performance when in many servers
This commit is contained in:
@@ -415,7 +415,9 @@ namespace Discord
|
||||
|
||||
#region Servers
|
||||
private Server AddServer(ulong id)
|
||||
=> _servers.GetOrAdd(id, x => new Server(this, x));
|
||||
=> _servers.GetOrAdd(id, x => new Server(this, id));
|
||||
private Server AddServer(API.Client.ExtendedGuild model)
|
||||
=> _servers.GetOrAdd(model.Id, x => new Server(this, model));
|
||||
private Server RemoveServer(ulong id)
|
||||
{
|
||||
Server server;
|
||||
@@ -481,7 +483,7 @@ namespace Discord
|
||||
{
|
||||
if (model.Unavailable != true)
|
||||
{
|
||||
var server = AddServer(model.Id);
|
||||
var server = AddServer(model);
|
||||
server.Update(model);
|
||||
}
|
||||
}
|
||||
@@ -505,7 +507,7 @@ namespace Discord
|
||||
var data = e.Payload.ToObject<GuildCreateEvent>(_serializer);
|
||||
if (data.Unavailable != true)
|
||||
{
|
||||
var server = AddServer(data.Id);
|
||||
var server = AddServer(data);
|
||||
server.Update(data);
|
||||
|
||||
if (Config.LogEvents)
|
||||
|
||||
@@ -29,9 +29,9 @@ namespace Discord
|
||||
}
|
||||
}
|
||||
|
||||
private readonly ConcurrentDictionary<ulong, Role> _roles;
|
||||
private readonly ConcurrentDictionary<ulong, Member> _users;
|
||||
private readonly ConcurrentDictionary<ulong, Channel> _channels;
|
||||
private ConcurrentDictionary<ulong, Role> _roles;
|
||||
private ConcurrentDictionary<ulong, Member> _users;
|
||||
private ConcurrentDictionary<ulong, Channel> _channels;
|
||||
private ulong _ownerId;
|
||||
private ulong? _afkChannelId;
|
||||
|
||||
@@ -39,11 +39,6 @@ namespace Discord
|
||||
|
||||
/// <summary> Gets the unique identifier for this server. </summary>
|
||||
public ulong Id { get; }
|
||||
/// <summary> Gets the default channel for this server. </summary>
|
||||
public Channel DefaultChannel { get; }
|
||||
/// <summary> Gets the the role representing all users in a server. </summary>
|
||||
public Role EveryoneRole { get; }
|
||||
|
||||
/// <summary> Gets the name of this server. </summary>
|
||||
public string Name { get; private set; }
|
||||
/// <summary> Gets the voice region for this server. </summary>
|
||||
@@ -54,6 +49,10 @@ namespace Discord
|
||||
public int AFKTimeout { get; private set; }
|
||||
/// <summary> Gets the date and time you joined this server. </summary>
|
||||
public DateTime JoinedAt { get; private set; }
|
||||
/// <summary> Gets the default channel for this server. </summary>
|
||||
public Channel DefaultChannel { get; private set; }
|
||||
/// <summary> Gets the the role representing all users in a server. </summary>
|
||||
public Role EveryoneRole { get; private set; }
|
||||
|
||||
/// <summary> Gets the user that created this server. </summary>
|
||||
public User Owner => GetUser(_ownerId);
|
||||
@@ -76,17 +75,23 @@ namespace Discord
|
||||
public IEnumerable<User> Users => _users.Select(x => x.Value.User);
|
||||
/// <summary> Gets a collection of all roles in this server. </summary>
|
||||
public IEnumerable<Role> Roles => _roles.Select(x => x.Value);
|
||||
|
||||
|
||||
internal Server(DiscordClient client, ulong id)
|
||||
{
|
||||
Client = client;
|
||||
Id = id;
|
||||
|
||||
_channels = new ConcurrentDictionary<ulong, Channel>();
|
||||
_roles = new ConcurrentDictionary<ulong, Role>();
|
||||
_users = new ConcurrentDictionary<ulong, Member>();
|
||||
DefaultChannel = AddChannel(id);
|
||||
EveryoneRole = AddRole(id);
|
||||
|
||||
EveryoneRole = new Role(id, this);
|
||||
DefaultChannel = new Channel(client, id, this);
|
||||
}
|
||||
internal Server(DiscordClient client, ExtendedGuild model)
|
||||
{
|
||||
Client = client;
|
||||
Id = model.Id;
|
||||
Update(model);
|
||||
}
|
||||
|
||||
internal void Update(GuildReference model)
|
||||
@@ -118,28 +123,78 @@ namespace Discord
|
||||
}
|
||||
internal void Update(ExtendedGuild model)
|
||||
{
|
||||
Update(model as Guild);
|
||||
var everyone = EveryoneRole;
|
||||
var defaultChannel = DefaultChannel;
|
||||
|
||||
//Bulk create dictionary items
|
||||
if (model.Members != null)
|
||||
{
|
||||
_users = new ConcurrentDictionary<ulong, Member>(
|
||||
model.Members.Select(x => new KeyValuePair<ulong, Member>(x.User.Id, new Member(new User(Client, x.User.Id, this)))));
|
||||
}
|
||||
else
|
||||
_users = new ConcurrentDictionary<ulong, Member>();
|
||||
if (model.Channels != null)
|
||||
{
|
||||
_channels = new ConcurrentDictionary<ulong, Channel>(
|
||||
model.Channels.Select(x => new KeyValuePair<ulong, Channel>(x.Id, new Channel(Client, x.Id, this))));
|
||||
}
|
||||
else
|
||||
_channels = new ConcurrentDictionary<ulong, Channel>();
|
||||
if (model.Roles != null)
|
||||
{
|
||||
_roles = new ConcurrentDictionary<ulong, Role>(
|
||||
model.Roles.Select(x => new KeyValuePair<ulong, Role>(x.Id, new Role(x.Id, this))));
|
||||
}
|
||||
else
|
||||
_roles = new ConcurrentDictionary<ulong, Role>();
|
||||
|
||||
//Preserve old models
|
||||
if (everyone != null)
|
||||
_roles[Id] = everyone;
|
||||
else
|
||||
EveryoneRole = _roles[Id];
|
||||
if (defaultChannel != null)
|
||||
_channels[Id] = defaultChannel;
|
||||
else
|
||||
DefaultChannel = _channels[Id];
|
||||
|
||||
//Update models
|
||||
if (model.Channels != null)
|
||||
{
|
||||
foreach (var subModel in model.Channels)
|
||||
AddChannel(subModel.Id).Update(subModel);
|
||||
{
|
||||
var channel = _channels[subModel.Id];
|
||||
channel.Update(subModel);
|
||||
Client.AddChannel(channel);
|
||||
}
|
||||
}
|
||||
if (model.Members != null)
|
||||
{
|
||||
foreach (var subModel in model.Members)
|
||||
AddUser(subModel.User.Id).Update(subModel);
|
||||
_users[subModel.User.Id].User.Update(subModel);
|
||||
}
|
||||
if (model.VoiceStates != null)
|
||||
{
|
||||
foreach (var subModel in model.VoiceStates)
|
||||
GetUser(subModel.UserId)?.Update(subModel);
|
||||
_users[subModel.UserId].User.Update(subModel);
|
||||
}
|
||||
if (model.Presences != null)
|
||||
{
|
||||
foreach (var subModel in model.Presences)
|
||||
GetUser(subModel.User.Id)?.Update(subModel);
|
||||
{
|
||||
if (subModel.User.Username != null)
|
||||
_users[subModel.User.Id].User.Update(subModel);
|
||||
}
|
||||
}
|
||||
if (model.Roles != null)
|
||||
{
|
||||
foreach (var subModel in model.Roles)
|
||||
_roles[subModel.Id].Update(subModel);
|
||||
}
|
||||
|
||||
model.Roles = null; //Don't double-process roles
|
||||
Update(model as Guild);
|
||||
}
|
||||
|
||||
/// <summary> Edits this server, changing only non-null attributes. </summary>
|
||||
@@ -207,8 +262,7 @@ namespace Discord
|
||||
{
|
||||
var channel = new Channel(Client, id, this);
|
||||
Client.AddChannel(channel);
|
||||
channel = _channels.GetOrAdd(id, x => channel);
|
||||
return channel;
|
||||
return _channels.GetOrAdd(id, x => channel);
|
||||
}
|
||||
internal Channel RemoveChannel(ulong id)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user