-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Broadcasting and Observing for log messages
- Loading branch information
1 parent
285116e
commit 62c11b0
Showing
53 changed files
with
482 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SpotifyGPX by Simon Field | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SpotifyGPX.Broadcasting; | ||
|
||
public class Broadcaster | ||
{ | ||
public List<IObserver<string>> Observers { get; set; } | ||
|
||
public string Type { get; set; } | ||
|
||
public Broadcaster() | ||
{ | ||
Type = "INFO"; | ||
Observers = new(); | ||
} | ||
|
||
public void Broadcast(string message) | ||
{ | ||
foreach (var observer in Observers) | ||
{ | ||
observer.OnNext($"[{Type}] {message}"); | ||
} | ||
} | ||
|
||
public void BroadcastError(Exception error) | ||
{ | ||
foreach (var observer in Observers) | ||
{ | ||
observer.OnError(error); | ||
} | ||
} | ||
|
||
public void BroadcastCompletion() | ||
{ | ||
foreach (var observer in Observers) | ||
{ | ||
observer.OnCompleted(); | ||
} | ||
} | ||
|
||
public IDisposable Subscribe(IObserver<string> observer) | ||
{ | ||
if (!Observers.Contains(observer)) | ||
{ | ||
Observers.Add(observer); | ||
} | ||
|
||
IDisposable? additional = observer as IDisposable ?? null; | ||
|
||
return new Unsubscriber(observer, Observers, additional); | ||
} | ||
|
||
private class Unsubscriber : IDisposable | ||
{ | ||
private IObserver<string> _observer; | ||
private List<IObserver<string>> _observers; | ||
private IDisposable? _additional; | ||
|
||
public Unsubscriber(IObserver<string> observer, List<IObserver<string>> observers, IDisposable? additional) | ||
{ | ||
_observer = observer; | ||
_observers = observers; | ||
_additional = additional; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_observer != null && _observers.Contains(_observer)) | ||
{ | ||
_additional?.Dispose(); | ||
_observers.Remove(_observer); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SpotifyGPX by Simon Field | ||
|
||
using System; | ||
|
||
namespace SpotifyGPX.Broadcasting; | ||
|
||
public abstract class BroadcasterBase : IObservable<string> | ||
{ | ||
public Broadcaster BCaster { get; } | ||
|
||
protected BroadcasterBase(Broadcaster bCaster) | ||
{ | ||
BCaster = bCaster; | ||
BCaster.Type = BroadcasterPrefix; | ||
} | ||
|
||
protected abstract string BroadcasterPrefix { get; } | ||
|
||
public IDisposable Subscribe(IObserver<string> observer) | ||
{ | ||
return BCaster.Subscribe(observer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.