Beispiel #1
0
        public IObservable <DealRowViewModel> GetDealUpdates(Guid dealId)
        {
            //Subscribe to the EventStream looking for the DealId
            var streamName = StreamNames.DealStreamName(dealId);

            return(_eventStoreClient.GetEvents(streamName)
                   .Scan <ResolvedEvent, DealRowViewModel>(null, AccumlateDealEvents));
        }
        protected override async Task ProcessJsonAsync(string json, CancellationToken token = default)
        {
            try
            {
                //string streamName;
                //IJsonStreamObserver[] subscribers;

                // NOTE: Avoid locking... allowing for eventual consistency of subscribers.
                //lock (_sync)
                //{
                var jObject = JObject.Parse(json);

                // Get stream name.
                var streamName = jObject["stream"]?.Value <string>();
                if (streamName != null)
                {
                    // Get JSON data.
                    var data = jObject["data"]?.ToString(Formatting.None);
                    if (data == null)
                    {
                        Logger?.LogError($"{nameof(BinanceWebSocketStream)}: No JSON 'data' in message: \"{json}\"  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                        return;     // ignore.
                    }

                    json = data;
                }

                if (streamName == null)
                {
                    // Get stream name.
                    streamName = StreamNames.FirstOrDefault();
                    if (streamName == null)
                    {
                        Logger?.LogError($"{nameof(BinanceWebSocketStream)}: No subscribed streams.  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                        return;     // ignore.
                    }
                }

                if (!Subscribers.TryGetValue(streamName, out var observers))
                {
                    return;     // ignore.
                }
                // Get subscribers.
                var subscribers = observers?.ToArray();
                //}

                await NotifyListenersAsync(subscribers, streamName, json, token)
                .ConfigureAwait(false);
            }
            catch (OperationCanceledException) { /* ignored */ }
            catch (Exception e)
            {
                if (!token.IsCancellationRequested)
                {
                    Logger?.LogError(e, $"{nameof(BinanceWebSocketStream)}: Failed processing JSON message.  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                }
            }
        }
        private async Task ProcessNext()
        {
            //TODO: Allow cancellation
            var cmd = await _messageBusClient.Dequeue <CreateDealCommand>();

            //Run slow just for fun
            await Task.Delay(TimeSpan.FromSeconds(2));

            //Create the DealCreatedEvent in the new Deal-GUID stream.

            var streamName = StreamNames.DealStreamName(cmd.DealId);
            var eventId    = Guid.NewGuid(); //Is this correct? Or should it be cmd.DealId? Or something else?
            var payload    = cmd.ToJson();
            await _eventStoreClient.SaveEvent(streamName, ExpectedVersion.NoStream, eventId, "DealCreated", payload);

            //We will use Projections to merge them together.
        }
Beispiel #4
0
        public async Task <int> GetDealUpdatesHead(Guid dealId)
        {
            var streamName = StreamNames.DealStreamName(dealId);

            return(await _eventStoreClient.GetHeadVersion(streamName));
        }