-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Froussios/Examples-2.0
Added source files for examples
- Loading branch information
Showing
94 changed files
with
5,451 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
Examples/Examples/Chapter1/KeyTypes/AsyncSubjectExample.cs
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,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Subjects; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IntroToRx.Examples | ||
{ | ||
class AsyncSubjectExample | ||
{ | ||
void Example1(string[] args) | ||
{ | ||
var subject = new AsyncSubject<string>(); | ||
subject.OnNext("a"); | ||
WriteSequenceToConsole(subject); | ||
subject.OnNext("b"); | ||
subject.OnNext("c"); | ||
Console.ReadKey(); | ||
} | ||
|
||
void Example2(string[] args) | ||
{ | ||
var subject = new AsyncSubject<string>(); | ||
subject.OnNext("a"); | ||
WriteSequenceToConsole(subject); | ||
subject.OnNext("b"); | ||
subject.OnNext("c"); | ||
subject.OnCompleted(); | ||
Console.ReadKey(); | ||
|
||
//c | ||
} | ||
|
||
//Takes an IObservable<string> as its parameter. | ||
//Subject<string> implements this interface. | ||
static void WriteSequenceToConsole(IObservable<string> sequence) | ||
{ | ||
//The next two lines are equivalent. | ||
//sequence.Subscribe(value=>Console.WriteLine(value)); | ||
sequence.Subscribe(Console.WriteLine); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Examples/Examples/Chapter1/KeyTypes/BehaviorSubjectExample.cs
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,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Subjects; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IntroToRx.Examples | ||
{ | ||
class BehaviorSubjectExample | ||
{ | ||
public void BehaviorSubjectExample1() | ||
{ | ||
//Need to provide a default value. | ||
var subject = new BehaviorSubject<string>("a"); | ||
subject.Subscribe(Console.WriteLine); | ||
|
||
//a | ||
} | ||
|
||
public void BehaviorSubjectExample2() | ||
{ | ||
var subject = new BehaviorSubject<string>("a"); | ||
subject.OnNext("b"); | ||
subject.Subscribe(Console.WriteLine); | ||
|
||
//b | ||
} | ||
|
||
public void BehaviorSubjectExample3() | ||
{ | ||
var subject = new BehaviorSubject<string>("a"); | ||
subject.OnNext("b"); | ||
subject.Subscribe(Console.WriteLine); | ||
subject.OnNext("c"); | ||
subject.OnNext("d"); | ||
|
||
//b | ||
//c | ||
//d | ||
} | ||
|
||
public void BehaviorSubjectCompletedExample() | ||
{ | ||
var subject = new BehaviorSubject<string>("a"); | ||
subject.OnNext("b"); | ||
subject.OnNext("c"); | ||
subject.OnCompleted(); | ||
subject.Subscribe(Console.WriteLine); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Examples/Examples/Chapter1/KeyTypes/ImplicitContractsExample.cs
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,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Subjects; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IntroToRx.Examples | ||
{ | ||
class ImplicitContractsExample | ||
{ | ||
public void SubjectInvalidUsageExample() | ||
{ | ||
var subject = new Subject<string>(); | ||
subject.Subscribe(Console.WriteLine); | ||
subject.OnNext("a"); | ||
subject.OnNext("b"); | ||
subject.OnCompleted(); | ||
subject.OnNext("c"); | ||
|
||
//a | ||
//b | ||
} | ||
} | ||
} |
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,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Disposables; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IntroToRx.Examples | ||
{ | ||
class ObserverExample | ||
{ | ||
public class MyConsoleObserver<T> : IObserver<T> | ||
{ | ||
public void OnNext(T value) | ||
{ | ||
Console.WriteLine("Received value {0}", value); | ||
} | ||
public void OnError(Exception error) | ||
{ | ||
Console.WriteLine("Sequence faulted with {0}", error); | ||
} | ||
public void OnCompleted() | ||
{ | ||
Console.WriteLine("Sequence terminated"); | ||
} | ||
} | ||
|
||
public class MySequenceOfNumbers : IObservable<int> | ||
{ | ||
public IDisposable Subscribe(IObserver<int> observer) | ||
{ | ||
observer.OnNext(1); | ||
observer.OnNext(2); | ||
observer.OnNext(3); | ||
observer.OnCompleted(); | ||
return Disposable.Empty; | ||
} | ||
} | ||
|
||
public void Run() | ||
{ | ||
var numbers = new MySequenceOfNumbers(); | ||
var observer = new MyConsoleObserver<int>(); | ||
numbers.Subscribe(observer); | ||
|
||
//Received value 1 | ||
//Received value 2 | ||
//Received value 3 | ||
//Sequence terminated | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
Examples/Examples/Chapter1/KeyTypes/ReplaySubjectExample.cs
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,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Subjects; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace IntroToRx.Examples | ||
{ | ||
class ReplaySubjectExample | ||
{ | ||
public void NoReplay(string[] args) | ||
{ | ||
var subject = new Subject<string>(); | ||
subject.OnNext("a"); | ||
WriteSequenceToConsole(subject); | ||
subject.OnNext("b"); | ||
subject.OnNext("c"); | ||
Console.ReadKey(); | ||
|
||
//b | ||
//c | ||
} | ||
|
||
public void Replay(string[] args) | ||
{ | ||
var subject = new ReplaySubject<string>(); | ||
subject.OnNext("a"); | ||
WriteSequenceToConsole(subject); | ||
subject.OnNext("b"); | ||
subject.OnNext("c"); | ||
|
||
//a | ||
//b | ||
//c | ||
} | ||
|
||
public void ReplaySubjectBufferExample() | ||
{ | ||
var bufferSize = 2; | ||
var subject = new ReplaySubject<string>(bufferSize); | ||
subject.OnNext("a"); | ||
subject.OnNext("b"); | ||
subject.OnNext("c"); | ||
subject.Subscribe(Console.WriteLine); | ||
subject.OnNext("d"); | ||
|
||
//b | ||
//c | ||
//d | ||
} | ||
|
||
public void ReplaySubjectWindowExample() | ||
{ | ||
var window = TimeSpan.FromMilliseconds(150); | ||
var subject = new ReplaySubject<string>(window); | ||
subject.OnNext("w"); | ||
Thread.Sleep(TimeSpan.FromMilliseconds(100)); | ||
subject.OnNext("x"); | ||
Thread.Sleep(TimeSpan.FromMilliseconds(100)); | ||
subject.OnNext("y"); | ||
subject.Subscribe(Console.WriteLine); | ||
subject.OnNext("z"); | ||
|
||
//x | ||
//y | ||
//z | ||
} | ||
|
||
//Takes an IObservable<string> as its parameter. | ||
//Subject<string> implements this interface. | ||
static void WriteSequenceToConsole(IObservable<string> sequence) | ||
{ | ||
//The next two lines are equivalent. | ||
//sequence.Subscribe(value=>Console.WriteLine(value)); | ||
sequence.Subscribe(Console.WriteLine); | ||
} | ||
} | ||
} |
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,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Subjects; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IntroToRx.Examples | ||
{ | ||
class SubjectExample | ||
{ | ||
public void Run() | ||
{ | ||
var subject = new Subject<string>(); | ||
WriteSequenceToConsole(subject); | ||
subject.OnNext("a"); | ||
subject.OnNext("b"); | ||
subject.OnNext("c"); | ||
Console.ReadKey(); | ||
} | ||
|
||
//Takes an IObservable<string> as its parameter. | ||
//Subject<string> implements this interface. | ||
static void WriteSequenceToConsole(IObservable<string> sequence) | ||
{ | ||
//The next two lines are equivalent. | ||
//sequence.Subscribe(value=>Console.WriteLine(value)); | ||
sequence.Subscribe(Console.WriteLine); | ||
} | ||
} | ||
} |
Oops, something went wrong.