本文整理汇总了C#中Subject.Do方法的典型用法代码示例。如果您正苦于以下问题:C# Subject.Do方法的具体用法?C# Subject.Do怎么用?C# Subject.Do使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Subject
的用法示例。
在下文中一共展示了Subject.Do方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FlatenningObservableOfObservables
private static void FlatenningObservableOfObservables()
{
Demo.DisplayHeader("The SelectMany operator - flattening observables of message to one stream of messages");
var roomsSubject = new Subject<ChatRoom>();
IObservable<ChatRoom> rooms = roomsSubject.AsObservable();
rooms
.Log("Rooms")
.SelectMany(r => r.Messages)
.Select(m => new ChatMessageViewModel(m))
.Subscribe(vm => AddToDashboard(vm));
//
// This is how the same example would look without SelectMany
//
//rooms
// .Log("Rooms")
// .Select(r => r.Messages)
// .Select(messages => messages.Select(m => new ChatMessageViewModel(m)))
// .Subscribe(roomMessages => roomMessages.Subscribe(m => AddToDashboard(m)));
var room1 = new Subject<ChatMessage>();
roomsSubject.OnNext(new ChatRoom {Id = "Room1", Messages = room1.Do(m => m.Room = "Room1")});
room1.OnNext(new ChatMessage {Content = "First Message", Sender = "1"});
room1.OnNext(new ChatMessage {Content = "Second Message", Sender = "1"});
var room2 = new Subject<ChatMessage>();
roomsSubject.OnNext(new ChatRoom {Id = "Room2", Messages = room2.Do(m => m.Room = "Room2")});
room2.OnNext(new ChatMessage {Content = "Hello World", Sender = "2"});
room1.OnNext(new ChatMessage {Content = "Another Message", Sender = "1"});
}
示例2: EventsBeforeYouSubscribedDoNotCount
public void EventsBeforeYouSubscribedDoNotCount()
{
var sum = 0;
var numbers = new Subject<int>();
var observable = numbers.Do(n => sum += n);
numbers.OnNext(1);
numbers.OnNext(2);
observable.Subscribe();
numbers.OnNext(3);
numbers.OnNext(4);
Assert.AreEqual(___, sum);
}
示例3: EventsAfterYouUnsubscribDoNotCount
public void EventsAfterYouUnsubscribDoNotCount()
{
var sum = 0;
var numbers = new Subject<int>();
var observable = numbers.Do(n => sum += n);
var subscription = observable.Subscribe();
numbers.OnNext(1);
numbers.OnNext(2);
subscription.Dispose();
numbers.OnNext(3);
numbers.OnNext(4);
Assert.AreEqual(___, sum);
}
示例4: EventsWhileSubscribing
public void EventsWhileSubscribing()
{
var recieved = new List<string>();
var words = new Subject<string>();
var observable = words.Do(recieved.Add);
words.OnNext("Peter");
words.OnNext("said");
var subscription = observable.Subscribe();
words.OnNext("you");
words.OnNext("look");
words.OnNext("pretty");
subscription.Dispose();
words.OnNext("ugly");
Assert.AreEqual(___, String.Join(" ", recieved));
}
示例5: TestScheduler
public void 複数のObservableを監視するには()
{
var scheduler = new TestScheduler();
var subject1 = scheduler.CreateHotObservable(OnNext(100, 1), OnNext(200, 3));
var subject2 = scheduler.CreateHotObservable(OnNext(100, 2), OnNext(200, 4));
var aggSubject = new Subject<int>();
var recorder = scheduler.CreateObserver<int>();
aggSubject.Do(Console.WriteLine).Subscribe(recorder);
subject1.Subscribe(aggSubject);
scheduler.AdvanceBy(100);
subject2.Subscribe(aggSubject);
scheduler.AdvanceBy(200);
}
示例6: BitmapBackgroundLoader
static BitmapBackgroundLoader()
{
_numprocessing = 0;
_requestob = new Subject<BitmapRequest>();
_requestob
.ObserveOn(Scheduler.ThreadPool)
.Do(x =>
{
while (_numprocessing >= MAX_SIMULTANEOUS_LOADING_COUNT)
{
Thread.Sleep(SLEEP_TIME);
}
})
.Do(x => ++_numprocessing)
.Do(x => Thread.Sleep(SLEEP_TIME))
.Do(x => Debug.WriteLine("_requestob: Received request - {0}", x.IsoPath))
.Subscribe(GenerateStreamAndPush);
_streamob = new Subject<BitmapRequest>();
_streamob
.Do(x => Thread.Sleep(SLEEP_TIME))
.Do(x => Debug.WriteLine("_streamob: Received request - {0}", x.IsoPath))
.ObserveOnDispatcher()
.Subscribe(GenerateBackgroundBitmapLoadingAndPush);
_requestdoneob = new Subject<BitmapRequest>();
_requestdoneob
.ObserveOn(Scheduler.ThreadPool)
.Do(x => Thread.Sleep(SLEEP_TIME))
.Do(x => Debug.WriteLine("_requestdoneob: Received request - {0}", x.IsoPath))
.Do(x => --_numprocessing)
.Subscribe(NotifyJobDone);
}
示例7: ConnectAsync
public async Task ConnectAsync()
{
// if we've already connected, don't do it again.
if (IsConnected)
return;
await await _cluster.Scheduler.Ask(async () =>
{
await _sync.WaitAsync();
try
{
if (IsConnected)
return;
_log.Debug("Connecting producer {1} for topic {0}", Topic, _id);
EtwTrace.Log.ProducerStarting(Topic, _id);
_sendMessagesSubject = new Subject<Message>();
if (_cluster.State != Cluster.ClusterState.Connected)
{
_log.Debug("Connecting cluster");
await _cluster.ConnectAsync();
_log.Debug("Connected cluster");
}
// Recovery: subscribe to partition offline/online events
_partitionStateSubsctiption = _cluster.PartitionStateChanges.
Where(p => p.Topic == Configuration.Topic).
Synchronize(_allPartitionQueues).
Subscribe(p =>
{
PartitionQueueInfo queue;
if (!_allPartitionQueues.TryGetValue(p.PartitionId, out queue))
{
queue = new PartitionQueueInfo(Configuration.SendBuffersInitialSize) { Partition = p.PartitionId };
_allPartitionQueues.Add(p.PartitionId, queue);
_queueSizeEvents.OnNext(new QueueResizeInfo{PartitionId = p.PartitionId, Size = queue.Queue.Size, Capacity = queue.Queue.Capacity});
}
_log.Info("Detected change in topic/partition '{0}'/{1}/{2} IsOnline {3}->{4}",
Configuration.Topic,
p.PartitionId,
p.ErrorCode,
queue.IsOnline,
p.ErrorCode.IsSuccess());
queue.IsOnline = p.ErrorCode.IsSuccess();
_queueEventWaitHandler.Set();
});
// get all of the partitions for this topic. Allows the MessagePartitioner to select a partition.
var topicPartitions = await _cluster.GetOrFetchMetaForTopicAsync(Configuration.Topic);
_log.Debug("Producer found {0} partitions for '{1}'", topicPartitions.Length, Configuration.Topic);
_sendMessagesSubject.
Do(msg => msg.PartitionId = Configuration.Partitioner.GetMessagePartition(msg, topicPartitions).Id).
Buffer(Configuration.BatchFlushTime, Configuration.BatchFlushSize).
Where(b => b.Count > 0).
Select(msgs => msgs.GroupBy(msg => msg.PartitionId)).
ObserveOn(_cluster.Scheduler).
Subscribe(partitionGroups =>
{
foreach (var batch in partitionGroups)
{
// partition queue might be created if metadata broadcast fired already
PartitionQueueInfo queue;
if (!_allPartitionQueues.TryGetValue(batch.Key, out queue))
{
queue = new PartitionQueueInfo(Configuration.SendBuffersInitialSize) { Partition = batch.Key };
_allPartitionQueues.Add(batch.Key, queue);
queue.IsOnline = topicPartitions.First(p => p.Id == batch.Key).ErrorCode.IsSuccess();
_log.Debug("{0} added new partition queue", this);
_queueSizeEvents.OnNext(new QueueResizeInfo { PartitionId = queue.Partition, Size = queue.Queue.Size, Capacity = queue.Queue.Capacity });
}
// now queue them up
var batchAr = batch.ToArray();
// make sure we have space.
if (queue.Queue.Size + batchAr.Length > queue.Queue.Capacity)
{
// try to increase the capacity.
if (Configuration.AutoGrowSendBuffers)
{
var growBy = Math.Max(queue.Queue.Capacity/2 + 1, 2 * batchAr.Length);
_log.Warn("Capacity of send buffer with size {3} not large enough to accept {2} new messages. Increasing capacity from {0} to {1}", queue.Queue.Capacity, queue.Queue.Capacity+growBy, batchAr.Length, queue.Queue.Size);
queue.Queue.Capacity += growBy;
_queueSizeEvents.OnNext(new QueueResizeInfo { PartitionId = queue.Partition, Size = queue.Queue.Size, Capacity = queue.Queue.Capacity });
}
else
{
// we're full and not allowed to grow. Throw the batch back to the caller, and continue on.
var msg = string.Format("Send Buffer Full for partition {0}. ",
Cluster.PartitionStateChanges.Where(
ps => ps.Topic == Configuration.Topic && ps.PartitionId == queue.Partition)
.Take(1)
.Wait());
_log.Error(msg);
//.........这里部分代码省略.........