Skip to content

Commit

Permalink
Use ConcurrentDictionary to track properties and events subscriptions (
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroservienti authored Dec 13, 2020
1 parent c897bd6 commit ed6b411
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/ServiceComposer.AspNetCore/DynamicViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
Expand All @@ -13,9 +14,9 @@ class DynamicViewModel : DynamicObject, IPublishCompositionEvents, ICompositionE
readonly string _requestId;
readonly RouteData _routeData;
readonly HttpRequest _httpRequest;
readonly IDictionary<Type, List<EventHandler<object>>> _subscriptions = new Dictionary<Type, List<EventHandler<object>>>();
readonly IDictionary<Type, List<CompositionEventHandler<object>>> _compositionEventsSubscriptions = new Dictionary<Type, List<CompositionEventHandler<object>>>();
readonly IDictionary<string, object> _properties = new Dictionary<string, object>();
readonly ConcurrentDictionary<Type, List<EventHandler<object>>> _subscriptions = new ConcurrentDictionary<Type, List<EventHandler<object>>>();
readonly ConcurrentDictionary<Type, List<CompositionEventHandler<object>>> _compositionEventsSubscriptions = new ConcurrentDictionary<Type, List<CompositionEventHandler<object>>>();
readonly ConcurrentDictionary<string, object> _properties = new ConcurrentDictionary<string, object>();

public DynamicViewModel(string requestId, RouteData routeData, HttpRequest httpRequest)
{
Expand All @@ -31,7 +32,7 @@ public void Subscribe<TEvent>(EventHandler<TEvent> handler)
if (!_subscriptions.TryGetValue(typeof(TEvent), out var handlers))
{
handlers = new List<EventHandler<object>>();
_subscriptions.Add(typeof(TEvent), handlers);
_subscriptions.TryAdd(typeof(TEvent), handlers);
}

handlers.Add((requestId, pageViewModel, @event, routeData, query) => handler(requestId, pageViewModel, (TEvent) @event, routeData, query));
Expand All @@ -42,7 +43,7 @@ public void Subscribe<TEvent>(CompositionEventHandler<TEvent> handler)
if (!_compositionEventsSubscriptions.TryGetValue(typeof(TEvent), out var handlers))
{
handlers = new List<CompositionEventHandler<object>>();
_compositionEventsSubscriptions.Add(typeof(TEvent), handlers);
_compositionEventsSubscriptions.TryAdd(typeof(TEvent), handlers);
}

handlers.Add((@event, request) => handler((TEvent) @event, request));
Expand All @@ -52,7 +53,7 @@ public void Subscribe<TEvent>(CompositionEventHandler<TEvent> handler)

public override bool TrySetMember(SetMemberBinder binder, object value)
{
_properties[binder.Name] = value;
_properties.AddOrUpdate(binder.Name, value, (key, existingValue) => value);
return true;
}

Expand Down

0 comments on commit ed6b411

Please sign in to comment.