private void handleSpecificCallbacks(string basePath, ChangeSet set, Dictionary <string, List <Action <string, ChangeSet> > > callbacks, ChangeType desiredChange) { //for each callback someone asked me, i am going to filter the set to see what should i give that guy. foreach (var cb in callbacks.Where(kvp => kvp.Key.StartsWith(basePath) || basePath.StartsWith(kvp.Key))) { ChangeSet node; //those nodes to add. var key = cb.Key; if (cb.Key.Length >= basePath.Length) { key = cb.Key.Substring(basePath.Length); node = set; } else { //the key the asked for is shorter than my base path, this means, that i have some steps missing node = set; Queue <string> toBuild = new Queue <string>(basePath.Split('/').Reverse()); while (toBuild.Count > 0) { var childs = new Dictionary <string, ChangeSet>(); childs.Add(toBuild.Dequeue(), node); node = new ChangesetBranch(childs); } } node = node.Find(key); //really not sure about this. foreach (var child in node.Childs.Where(t => t.Value.Type == desiredChange)) { foreach (var action in cb.Value) { action(child.Key, child.Value); } } } }
private void mapSnapshotToObject(object obj, ChangeSet input) { string myType = input["type"].As <string>(); if (myType == "Array") { List <object> list = (List <object>)obj; ChangesetBranch received = null; int maxIndex = 0; if (input.Childs.ContainsKey("data")) { received = (ChangesetBranch)input["data"]; maxIndex = received.Childs.Keys.Select(int.Parse).Max(); } //todo: if i remove things from an array this wont update correctly. if (received == null) { return; } while (list.Count <= maxIndex) { list.Add(null); } //remove elements to ensure the length. //not anymore. list.RemoveRange(received.Childs.Count(), list.Count - received.Childs.Count()); foreach (var item in received.Childs) { int i = int.Parse(item.Key); string type = item.Value["type"].As <string>(); if (isPrimitiveTypeName(type)) { list[i] = item.Value["value"].As <object>(); } else if (type == "null") { list[i] = null; } else { //object or array. string otherKey = item.Value["value"].As <string>(); var other = Get(otherKey); if (other == null) { missingReferences.Add(new KeyValuePair <string, Action <object> >(otherKey, (otherObj) => { list[i] = otherObj; })); } else { list[i] = other; } } } } else { IDictionary <string, object> dictionary = ((ExpandibleObject)obj).asDictionary(); if (input.Childs.ContainsKey("data")) { ChangesetBranch dataBranch = (ChangesetBranch)input["data"]; foreach (var item in dataBranch.Childs) { string type = ""; if (item.Value.Childs.ContainsKey("type")) { type = item.Value["type"].As <string>(); } else { type = sanitizeTypeName(dictionary[item.Key].GetType()); } if (isPrimitiveTypeName(type)) { dictionary[item.Key] = item.Value["value"].As <object>(); } else if (type == "null") { dictionary[item.Key as string] = null; } else { //object or array. string otherKey = item.Value["value"].As <string>(); var other = Get(otherKey); if (other == null) { missingReferences.Add(new KeyValuePair <string, Action <object> >(otherKey, (otherObj) => { dictionary[item.Key] = otherObj; })); } else { dictionary[item.Key] = other; } } } } } }