-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncGameModelInstanceList.cs
More file actions
58 lines (51 loc) · 2.18 KB
/
AsyncGameModelInstanceList.cs
File metadata and controls
58 lines (51 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Raid.Toolkit.Extensibility;
namespace Raid.Toolkit.Community.Extensibility.Utilities
{
public class AsyncGameModelInstanceList<T, U> : AsyncObservableCollection<T>, IDisposable where T : GameModelInstance<U> where U : ModelScopeBase
{
private readonly IServiceProvider ServiceProvider;
private readonly IGameInstanceManager InstanceManager;
private bool IsDisposed;
public IList<T> Instances => Items;
public AsyncGameModelInstanceList(IServiceProvider serviceProvider, IGameInstanceManager instanceManager)
: base(instanceManager.Instances.Select(instance => ActivatorUtilities.CreateInstance<T>(serviceProvider, instance)).ToList())
{
ServiceProvider = serviceProvider;
InstanceManager = instanceManager;
instanceManager.OnRemoved += InstanceManager_OnRemoved;
instanceManager.OnAdded += InstanceManager_OnAdded;
}
private void InstanceManager_OnRemoved(object sender, IGameInstanceManager.GameInstancesUpdatedEventArgs e)
{
T[] itemsToRemove = Items.Where(item => item.GameInstance.Id == e.Instance.Id).ToArray();
foreach (var itemToRemove in itemsToRemove)
_ = Remove(itemToRemove);
}
private void InstanceManager_OnAdded(object sender, IGameInstanceManager.GameInstancesUpdatedEventArgs e)
{
Add(ActivatorUtilities.CreateInstance<T>(ServiceProvider, e.Instance));
}
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
InstanceManager.OnRemoved -= InstanceManager_OnRemoved;
InstanceManager.OnAdded -= InstanceManager_OnAdded;
}
IsDisposed = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}