mirror of https://github.com/ErsatzTV/ErsatzTV.git
9 changed files with 128 additions and 18 deletions
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
using System; |
||||
|
||||
namespace ErsatzTV.Core.Interfaces.Locking |
||||
{ |
||||
public interface IEntityLocker |
||||
{ |
||||
public event EventHandler OnMediaSourceChanged; |
||||
public bool LockMediaSource(int mediaSourceId); |
||||
public bool UnlockMediaSource(int mediaSourceId); |
||||
public bool IsMediaSourceLocked(int mediaSourceId); |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
using System; |
||||
using System.Collections.Concurrent; |
||||
using ErsatzTV.Core.Interfaces.Locking; |
||||
|
||||
namespace ErsatzTV.Infrastructure.Locking |
||||
{ |
||||
public class EntityLocker : IEntityLocker |
||||
{ |
||||
private readonly ConcurrentDictionary<int, byte> _lockedMediaSources; |
||||
|
||||
public EntityLocker() => _lockedMediaSources = new ConcurrentDictionary<int, byte>(); |
||||
|
||||
public event EventHandler OnMediaSourceChanged; |
||||
|
||||
public bool LockMediaSource(int mediaSourceId) |
||||
{ |
||||
if (_lockedMediaSources.TryAdd(mediaSourceId, 0)) |
||||
{ |
||||
OnMediaSourceChanged?.Invoke(this, EventArgs.Empty); |
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
public bool UnlockMediaSource(int mediaSourceId) |
||||
{ |
||||
if (_lockedMediaSources.TryRemove(mediaSourceId, out byte _)) |
||||
{ |
||||
OnMediaSourceChanged?.Invoke(this, EventArgs.Empty); |
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
public bool IsMediaSourceLocked(int mediaSourceId) => |
||||
_lockedMediaSources.ContainsKey(mediaSourceId); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue