private void HandlePut(ServerEvent serverEvent) { lock (CacheLock) { var token = Cache.SelectToken(serverEvent.ChildKey, false); if (token != null && token.Parent == null) { var newChildren = serverEvent.Data.Children().Except(Cache.Children()); foreach (var child in newChildren) { ChildAdded?.Invoke(child.GetType() == typeof(JValue) ? child : child.First); } var removedChildren = Cache.Children().Except(serverEvent.Data.Children()); foreach (var child in removedChildren) { ChildRemoved?.Invoke(child.First); } Cache = serverEvent.Data as JObject ?? new JObject(); } else { if (token == null) { Cache[serverEvent.Path] = serverEvent.Data as JToken; ChildAdded?.Invoke(Cache[serverEvent.Path]); } else { if (serverEvent.Data.ToObject <object>() == null) { var removedChild = Cache.SelectToken(serverEvent.Path, false); Cache.SelectToken(serverEvent.Path, false)?.Parent?.Remove(); ChildRemoved?.Invoke(removedChild); } else { var subChildPath = string.Join(".", serverEvent.Path.Split('.').Skip(1)); token[subChildPath] = serverEvent.Data as JToken; ChildChanged?.Invoke(Cache.SelectToken(serverEvent.ChildKey, false)); } } } ValueChanged?.Invoke(Cache); } }
/// <summary> /// Called when [child changed]. /// </summary> /// <param name="propertyName">Name of the property.</param> protected virtual void OnChildChanged([CallerMemberName] string propertyName = null) { ChildChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }