-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
285 additions
and
13 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
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,35 @@ | ||
|
||
using System; | ||
using Sheller.Models; | ||
|
||
namespace Sheller.Implementations | ||
{ | ||
/// <summary> | ||
/// The default result type for executables. | ||
/// </summary> | ||
public class CommandEvent : ICommandEvent | ||
{ | ||
/// <summary> | ||
/// StreamType property. | ||
/// </summary> | ||
/// <value>The stream type of the event.</value> | ||
public CommandEventType Type { get; private set; } | ||
|
||
/// <summary> | ||
/// Data property. | ||
/// </summary> | ||
/// <value>The string data of the event.</value> | ||
public string Data { get; private set; } | ||
|
||
/// <summary> | ||
/// The CommandEvent constructor. | ||
/// </summary> | ||
/// <param name="type"></param> | ||
/// <param name="data"></param> | ||
public CommandEvent(CommandEventType type, string data) | ||
{ | ||
Type = type; | ||
Data = data; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Sheller.Models; | ||
|
||
namespace Sheller.Implementations | ||
{ | ||
/// <summary> | ||
/// Observable for command events. | ||
/// </summary> | ||
public class ObservableCommandEvent : IObservable<ICommandEvent> | ||
{ | ||
internal List<IObserver<ICommandEvent>> Observers { get; private set; } = new List<IObserver<ICommandEvent>>(); | ||
|
||
/// <summary> | ||
/// The Subscribe method. | ||
/// </summary> | ||
/// <param name="observer">The observer.</param> | ||
/// <returns>An <see cref="IDisposable"/>.</returns> | ||
public IDisposable Subscribe(IObserver<ICommandEvent> observer) | ||
{ | ||
if(!Observers.Contains(observer)) | ||
Observers.Add(observer); | ||
return new Unsubscriber(Observers, observer); | ||
} | ||
|
||
internal void FireEvent(ICommandEvent commandEvent) | ||
{ | ||
foreach(var observer in Observers) | ||
observer.OnNext(commandEvent); | ||
} | ||
|
||
internal void FireError(Exception e) | ||
{ | ||
foreach(var observer in Observers) | ||
observer.OnError(e); | ||
} | ||
|
||
internal void FireCompleted() | ||
{ | ||
foreach (var observer in Observers.ToArray()) | ||
if (Observers.Contains(observer)) | ||
observer.OnCompleted(); | ||
|
||
Observers.Clear(); | ||
} | ||
|
||
internal static ObservableCommandEvent Merge(ObservableCommandEvent ob1, ObservableCommandEvent ob2) | ||
{ | ||
var newObservable = new ObservableCommandEvent(); | ||
|
||
ob1.Observers.ForEach(o => newObservable.Subscribe(o)); | ||
ob2.Observers.ForEach(o => newObservable.Subscribe(o)); | ||
|
||
return newObservable; | ||
} | ||
|
||
private class Unsubscriber : IDisposable | ||
{ | ||
private List<IObserver<ICommandEvent>> _observers; | ||
private IObserver<ICommandEvent> _observer; | ||
|
||
public Unsubscriber(List<IObserver<ICommandEvent>> observers, IObserver<ICommandEvent> observer) | ||
{ | ||
this._observers = observers; | ||
this._observer = observer; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_observer != null && _observers.Contains(_observer)) | ||
_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
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,37 @@ | ||
using System; | ||
|
||
namespace Sheller.Models | ||
{ | ||
/// <summary> | ||
/// The default result interface for executables. | ||
/// </summary> | ||
public interface ICommandEvent | ||
{ | ||
/// <summary> | ||
/// StreamType property. | ||
/// </summary> | ||
/// <value>The stream type of the event.</value> | ||
CommandEventType Type { get; } | ||
|
||
/// <summary> | ||
/// Data property. | ||
/// </summary> | ||
/// <value>The string data of the event.</value> | ||
string Data { get; } | ||
} | ||
|
||
/// <summary> | ||
/// The type (stdout, stderr) of the event data. | ||
/// </summary> | ||
public enum CommandEventType | ||
{ | ||
/// <summary> | ||
/// The Standard Output type. | ||
/// </summary> | ||
StandardOutput, | ||
/// <summary> | ||
/// The Standard Error type. | ||
/// </summary> | ||
StandardError | ||
} | ||
} |
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.