From 304437b834e8c87687f68333ae67a13ee1377a73 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Wed, 12 Jun 2019 21:47:51 +0200 Subject: Adjust Codestyle --- dsa/FireBase/Offline/ConcurrentOfflineDatabase.cs | 51 +++----- dsa/FireBase/Offline/DatabaseExtensions.cs | 39 +++---- dsa/FireBase/Offline/ISetHandler.cs | 6 +- dsa/FireBase/Offline/InitialPullStrategy.cs | 6 +- .../Offline/Internals/MemberAccessVisitor.cs | 24 ++-- dsa/FireBase/Offline/OfflineCacheAdapter.cs | 72 ++++-------- dsa/FireBase/Offline/OfflineDatabase.cs | 51 +++----- dsa/FireBase/Offline/OfflineEntry.cs | 18 +-- dsa/FireBase/Offline/RealtimeDatabase.cs | 129 +++++++-------------- dsa/FireBase/Offline/SetHandler.cs | 12 +- dsa/FireBase/Offline/StreamingOptions.cs | 6 +- dsa/FireBase/Offline/SyncOptions.cs | 6 +- 12 files changed, 140 insertions(+), 280 deletions(-) (limited to 'dsa/FireBase/Offline') diff --git a/dsa/FireBase/Offline/ConcurrentOfflineDatabase.cs b/dsa/FireBase/Offline/ConcurrentOfflineDatabase.cs index 1a9e607..724115f 100644 --- a/dsa/FireBase/Offline/ConcurrentOfflineDatabase.cs +++ b/dsa/FireBase/Offline/ConcurrentOfflineDatabase.cs @@ -6,13 +6,11 @@ using System.IO; using System.Linq; using LiteDB; -namespace Firebase.Database.Offline -{ +namespace Firebase.Database.Offline { /// /// The offline database. /// - public class ConcurrentOfflineDatabase : IDictionary - { + public class ConcurrentOfflineDatabase : IDictionary { private readonly ConcurrentDictionary ccache; private readonly LiteRepository db; @@ -21,8 +19,7 @@ namespace Firebase.Database.Offline /// /// The item type which is used to determine the database file name. /// Custom string which will get appended to the file name. - public ConcurrentOfflineDatabase(Type itemType, string filenameModifier) - { + public ConcurrentOfflineDatabase(Type itemType, string filenameModifier) { var fullName = GetFileName(itemType.ToString()); if (fullName.Length > 100) fullName = fullName.Substring(0, 100); @@ -78,12 +75,10 @@ namespace Firebase.Database.Offline /// /// The key of the element to get or set. /// The element with the specified key. - public OfflineEntry this[string key] - { + public OfflineEntry this[string key] { get => ccache[key]; - set - { + set { ccache.AddOrUpdate(key, value, (k, existing) => value); db.Upsert(value); } @@ -93,13 +88,11 @@ namespace Firebase.Database.Offline /// Returns an enumerator that iterates through the collection. /// /// An enumerator that can be used to iterate through the collection. - public IEnumerator> GetEnumerator() - { + public IEnumerator> GetEnumerator() { return ccache.GetEnumerator(); } - IEnumerator IEnumerable.GetEnumerator() - { + IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } @@ -107,16 +100,14 @@ namespace Firebase.Database.Offline /// Adds an item to the . /// /// The object to add to the . - public void Add(KeyValuePair item) - { + public void Add(KeyValuePair item) { Add(item.Key, item.Value); } /// /// Removes all items from the . /// - public void Clear() - { + public void Clear() { ccache.Clear(); db.Delete(LiteDB.Query.All()); } @@ -129,8 +120,7 @@ namespace Firebase.Database.Offline /// True if is found in the ; /// otherwise, false. /// - public bool Contains(KeyValuePair item) - { + public bool Contains(KeyValuePair item) { return ContainsKey(item.Key); } @@ -144,8 +134,7 @@ namespace Firebase.Database.Offline /// zero-based indexing. /// /// The zero-based index in at which copying begins. - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { + public void CopyTo(KeyValuePair[] array, int arrayIndex) { ccache.ToList().CopyTo(array, arrayIndex); } @@ -159,8 +148,7 @@ namespace Firebase.Database.Offline /// ; otherwise, false. This method also returns false if /// is not found in the original . /// - public bool Remove(KeyValuePair item) - { + public bool Remove(KeyValuePair item) { return Remove(item.Key); } @@ -173,8 +161,7 @@ namespace Firebase.Database.Offline /// True if the contains an element with the key; /// otherwise, false. /// - public bool ContainsKey(string key) - { + public bool ContainsKey(string key) { return ccache.ContainsKey(key); } @@ -183,8 +170,7 @@ namespace Firebase.Database.Offline /// /// The object to use as the key of the element to add. /// The object to use as the value of the element to add. - public void Add(string key, OfflineEntry value) - { + public void Add(string key, OfflineEntry value) { ccache.AddOrUpdate(key, value, (k, existing) => value); db.Upsert(value); } @@ -197,8 +183,7 @@ namespace Firebase.Database.Offline /// True if the element is successfully removed; otherwise, false. This method also returns false if /// was not found in the original . /// - public bool Remove(string key) - { + public bool Remove(string key) { ccache.TryRemove(key, out _); return db.Delete(key); } @@ -216,13 +201,11 @@ namespace Firebase.Database.Offline /// True if the object that implements contains an /// element with the specified key; otherwise, false. /// - public bool TryGetValue(string key, out OfflineEntry value) - { + public bool TryGetValue(string key, out OfflineEntry value) { return ccache.TryGetValue(key, out value); } - private string GetFileName(string fileName) - { + private string GetFileName(string fileName) { var invalidChars = new[] {'`', '[', ',', '='}; foreach (var c in invalidChars.Concat(Path.GetInvalidFileNameChars()).Distinct()) fileName = fileName.Replace(c, '_'); diff --git a/dsa/FireBase/Offline/DatabaseExtensions.cs b/dsa/FireBase/Offline/DatabaseExtensions.cs index e7c4074..75624f1 100644 --- a/dsa/FireBase/Offline/DatabaseExtensions.cs +++ b/dsa/FireBase/Offline/DatabaseExtensions.cs @@ -4,10 +4,8 @@ using System.Linq.Expressions; using System.Reflection; using Firebase.Database.Query; -namespace Firebase.Database.Offline -{ - public static class DatabaseExtensions - { +namespace Firebase.Database.Offline { + public static class DatabaseExtensions { /// /// Create new instances of the . /// @@ -24,8 +22,7 @@ namespace Firebase.Database.Offline public static RealtimeDatabase AsRealtimeDatabase(this ChildQuery query, string filenameModifier = "", string elementRoot = "", StreamingOptions streamingOptions = StreamingOptions.LatestOnly, InitialPullStrategy initialPullStrategy = InitialPullStrategy.MissingOnly, bool pushChanges = true) - where T : class - { + where T : class { return new RealtimeDatabase(query, elementRoot, query.Client.Options.OfflineDatabaseFactory, filenameModifier, streamingOptions, initialPullStrategy, pushChanges); } @@ -49,8 +46,7 @@ namespace Firebase.Database.Offline StreamingOptions streamingOptions = StreamingOptions.LatestOnly, InitialPullStrategy initialPullStrategy = InitialPullStrategy.MissingOnly, bool pushChanges = true) where T : class - where TSetHandler : ISetHandler, new() - { + where TSetHandler : ISetHandler, new() { return new RealtimeDatabase(query, elementRoot, query.Client.Options.OfflineDatabaseFactory, filenameModifier, streamingOptions, initialPullStrategy, pushChanges, Activator.CreateInstance()); @@ -68,8 +64,7 @@ namespace Firebase.Database.Offline /// public static void Patch(this RealtimeDatabase db, string key, T obj, bool syncOnline = true, int priority = 1) - where T : class - { + where T : class { db.Set(key, obj, syncOnline ? SyncOptions.Patch : SyncOptions.None, priority); } @@ -85,8 +80,7 @@ namespace Firebase.Database.Offline /// public static void Put(this RealtimeDatabase db, string key, T obj, bool syncOnline = true, int priority = 1) - where T : class - { + where T : class { db.Set(key, obj, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); } @@ -101,8 +95,7 @@ namespace Firebase.Database.Offline /// /// The generated key for this object. public static string Post(this RealtimeDatabase db, T obj, bool syncOnline = true, int priority = 1) - where T : class - { + where T : class { var key = FirebaseKeyGenerator.Next(); db.Set(key, obj, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); @@ -120,8 +113,7 @@ namespace Firebase.Database.Offline /// priority. /// public static void Delete(this RealtimeDatabase db, string key, bool syncOnline = true, int priority = 1) - where T : class - { + where T : class { db.Set(key, null, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); } @@ -143,8 +135,7 @@ namespace Firebase.Database.Offline public static void Put(this RealtimeDatabase db, string key, Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) - where T : class - { + where T : class { db.Set(key, propertyExpression, value, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); } @@ -166,8 +157,7 @@ namespace Firebase.Database.Offline public static void Patch(this RealtimeDatabase db, string key, Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) - where T : class - { + where T : class { db.Set(key, propertyExpression, value, syncOnline ? SyncOptions.Patch : SyncOptions.None, priority); } @@ -189,8 +179,7 @@ namespace Firebase.Database.Offline public static void Delete(this RealtimeDatabase db, string key, Expression> propertyExpression, bool syncOnline = true, int priority = 1) where T : class - where TProperty : class - { + where TProperty : class { db.Set(key, propertyExpression, null, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); } @@ -215,8 +204,7 @@ namespace Firebase.Database.Offline Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) where T : class - where TSelector : IDictionary - { + where TSelector : IDictionary { var nextKey = FirebaseKeyGenerator.Next(); var expression = Expression.Lambda>( Expression.Call(propertyExpression.Body, @@ -245,8 +233,7 @@ namespace Firebase.Database.Offline public static void Delete(this RealtimeDatabase db, string key, Expression>> propertyExpression, string dictionaryKey, bool syncOnline = true, int priority = 1) - where T : class - { + where T : class { var expression = Expression.Lambda>( Expression.Call(propertyExpression.Body, typeof(IDictionary).GetRuntimeMethod("get_Item", new[] {typeof(string)}), diff --git a/dsa/FireBase/Offline/ISetHandler.cs b/dsa/FireBase/Offline/ISetHandler.cs index c04bd41..699d33a 100644 --- a/dsa/FireBase/Offline/ISetHandler.cs +++ b/dsa/FireBase/Offline/ISetHandler.cs @@ -1,10 +1,8 @@ using System.Threading.Tasks; using Firebase.Database.Query; -namespace Firebase.Database.Offline -{ - public interface ISetHandler - { +namespace Firebase.Database.Offline { + public interface ISetHandler { Task SetAsync(ChildQuery query, string key, OfflineEntry entry); } } \ No newline at end of file diff --git a/dsa/FireBase/Offline/InitialPullStrategy.cs b/dsa/FireBase/Offline/InitialPullStrategy.cs index ca2bebf..292c716 100644 --- a/dsa/FireBase/Offline/InitialPullStrategy.cs +++ b/dsa/FireBase/Offline/InitialPullStrategy.cs @@ -1,10 +1,8 @@ -namespace Firebase.Database.Offline -{ +namespace Firebase.Database.Offline { /// /// Specifies the strategy for initial pull of server data. /// - public enum InitialPullStrategy - { + public enum InitialPullStrategy { /// /// Don't pull anything. /// diff --git a/dsa/FireBase/Offline/Internals/MemberAccessVisitor.cs b/dsa/FireBase/Offline/Internals/MemberAccessVisitor.cs index 89a77da..fe888ac 100644 --- a/dsa/FireBase/Offline/Internals/MemberAccessVisitor.cs +++ b/dsa/FireBase/Offline/Internals/MemberAccessVisitor.cs @@ -3,37 +3,29 @@ using System.Linq.Expressions; using System.Reflection; using Newtonsoft.Json; -namespace Firebase.Database.Offline.Internals -{ - public class MemberAccessVisitor : ExpressionVisitor - { +namespace Firebase.Database.Offline.Internals { + public class MemberAccessVisitor : ExpressionVisitor { private readonly IList propertyNames = new List(); private bool wasDictionaryAccess; public IEnumerable PropertyNames => propertyNames; - public override Expression Visit(Expression expr) - { - if (expr?.NodeType == ExpressionType.MemberAccess) - { - if (wasDictionaryAccess) - { + public override Expression Visit(Expression expr) { + if (expr?.NodeType == ExpressionType.MemberAccess) { + if (wasDictionaryAccess) { wasDictionaryAccess = false; } - else - { + else { var memberExpr = (MemberExpression) expr; var jsonAttr = memberExpr.Member.GetCustomAttribute(); propertyNames.Add(jsonAttr?.PropertyName ?? memberExpr.Member.Name); } } - else if (expr?.NodeType == ExpressionType.Call) - { + else if (expr?.NodeType == ExpressionType.Call) { var callExpr = (MethodCallExpression) expr; - if (callExpr.Method.Name == "get_Item" && callExpr.Arguments.Count == 1) - { + if (callExpr.Method.Name == "get_Item" && callExpr.Arguments.Count == 1) { var e = Expression.Lambda(callExpr.Arguments[0]).Compile(); propertyNames.Add(e.DynamicInvoke().ToString()); wasDictionaryAccess = callExpr.Arguments[0].NodeType == ExpressionType.MemberAccess; diff --git a/dsa/FireBase/Offline/OfflineCacheAdapter.cs b/dsa/FireBase/Offline/OfflineCacheAdapter.cs index 3153d1b..9881c78 100644 --- a/dsa/FireBase/Offline/OfflineCacheAdapter.cs +++ b/dsa/FireBase/Offline/OfflineCacheAdapter.cs @@ -3,19 +3,15 @@ using System.Collections; using System.Collections.Generic; using System.Linq; -namespace Firebase.Database.Offline -{ - internal class OfflineCacheAdapter : IDictionary, IDictionary - { +namespace Firebase.Database.Offline { + internal class OfflineCacheAdapter : IDictionary, IDictionary { private readonly IDictionary database; - public OfflineCacheAdapter(IDictionary database) - { + public OfflineCacheAdapter(IDictionary database) { this.database = database; } - public void CopyTo(Array array, int index) - { + public void CopyTo(Array array, int index) { throw new NotImplementedException(); } @@ -23,12 +19,10 @@ namespace Firebase.Database.Offline public object SyncRoot { get; } - object IDictionary.this[object key] - { + object IDictionary.this[object key] { get => database[key.ToString()].Deserialize(); - set - { + set { var keyString = key.ToString(); if (database.ContainsKey(keyString)) database[keyString] = new OfflineEntry(keyString, value, database[keyString].Priority, @@ -42,25 +36,21 @@ namespace Firebase.Database.Offline ICollection IDictionary.Keys { get; } - public bool Contains(object key) - { + public bool Contains(object key) { return ContainsKey(key.ToString()); } - IDictionaryEnumerator IDictionary.GetEnumerator() - { + IDictionaryEnumerator IDictionary.GetEnumerator() { throw new NotImplementedException(); } - public void Remove(object key) - { + public void Remove(object key) { Remove(key.ToString()); } public bool IsFixedSize => false; - public void Add(object key, object value) - { + public void Add(object key, object value) { Add(key.ToString(), (T) value); } @@ -72,12 +62,10 @@ namespace Firebase.Database.Offline public ICollection Values => database.Values.Select(o => o.Deserialize()).ToList(); - public T this[string key] - { + public T this[string key] { get => database[key].Deserialize(); - set - { + set { if (database.ContainsKey(key)) database[key] = new OfflineEntry(key, value, database[key].Priority, database[key].SyncOptions); else @@ -85,62 +73,50 @@ namespace Firebase.Database.Offline } } - public IEnumerator> GetEnumerator() - { + public IEnumerator> GetEnumerator() { return database.Select(d => new KeyValuePair(d.Key, d.Value.Deserialize())).GetEnumerator(); } - IEnumerator IEnumerable.GetEnumerator() - { + IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } - public void Add(KeyValuePair item) - { + public void Add(KeyValuePair item) { Add(item.Key, item.Value); } - public void Clear() - { + public void Clear() { database.Clear(); } - public bool Contains(KeyValuePair item) - { + public bool Contains(KeyValuePair item) { return ContainsKey(item.Key); } - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { + public void CopyTo(KeyValuePair[] array, int arrayIndex) { throw new NotImplementedException(); } - public bool Remove(KeyValuePair item) - { + public bool Remove(KeyValuePair item) { return database.Remove(item.Key); } - public void Add(string key, T value) - { + public void Add(string key, T value) { database.Add(key, new OfflineEntry(key, value, 1, SyncOptions.None)); } - public bool ContainsKey(string key) - { + public bool ContainsKey(string key) { return database.ContainsKey(key); } - public bool Remove(string key) - { + public bool Remove(string key) { return database.Remove(key); } - public bool TryGetValue(string key, out T value) - { + public bool TryGetValue(string key, out T value) { OfflineEntry val; - if (database.TryGetValue(key, out val)) - { + if (database.TryGetValue(key, out val)) { value = val.Deserialize(); return true; } diff --git a/dsa/FireBase/Offline/OfflineDatabase.cs b/dsa/FireBase/Offline/OfflineDatabase.cs index be0380b..5820dc6 100644 --- a/dsa/FireBase/Offline/OfflineDatabase.cs +++ b/dsa/FireBase/Offline/OfflineDatabase.cs @@ -5,13 +5,11 @@ using System.IO; using System.Linq; using LiteDB; -namespace Firebase.Database.Offline -{ +namespace Firebase.Database.Offline { /// /// The offline database. /// - public class OfflineDatabase : IDictionary - { + public class OfflineDatabase : IDictionary { private readonly IDictionary cache; private readonly LiteRepository db; @@ -20,8 +18,7 @@ namespace Firebase.Database.Offline /// /// The item type which is used to determine the database file name. /// Custom string which will get appended to the file name. - public OfflineDatabase(Type itemType, string filenameModifier) - { + public OfflineDatabase(Type itemType, string filenameModifier) { var fullName = GetFileName(itemType.ToString()); if (fullName.Length > 100) fullName = fullName.Substring(0, 100); @@ -73,12 +70,10 @@ namespace Firebase.Database.Offline /// /// The key of the element to get or set. /// The element with the specified key. - public OfflineEntry this[string key] - { + public OfflineEntry this[string key] { get => cache[key]; - set - { + set { cache[key] = value; db.Upsert(value); } @@ -88,13 +83,11 @@ namespace Firebase.Database.Offline /// Returns an enumerator that iterates through the collection. /// /// An enumerator that can be used to iterate through the collection. - public IEnumerator> GetEnumerator() - { + public IEnumerator> GetEnumerator() { return cache.GetEnumerator(); } - IEnumerator IEnumerable.GetEnumerator() - { + IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } @@ -102,16 +95,14 @@ namespace Firebase.Database.Offline /// Adds an item to the . /// /// The object to add to the . - public void Add(KeyValuePair item) - { + public void Add(KeyValuePair item) { Add(item.Key, item.Value); } /// /// Removes all items from the . /// - public void Clear() - { + public void Clear() { cache.Clear(); db.Delete(LiteDB.Query.All()); } @@ -124,8 +115,7 @@ namespace Firebase.Database.Offline /// True if is found in the ; /// otherwise, false. /// - public bool Contains(KeyValuePair item) - { + public bool Contains(KeyValuePair item) { return ContainsKey(item.Key); } @@ -139,8 +129,7 @@ namespace Firebase.Database.Offline /// zero-based indexing. /// /// The zero-based index in at which copying begins. - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { + public void CopyTo(KeyValuePair[] array, int arrayIndex) { cache.CopyTo(array, arrayIndex); } @@ -154,8 +143,7 @@ namespace Firebase.Database.Offline /// ; otherwise, false. This method also returns false if /// is not found in the original . /// - public bool Remove(KeyValuePair item) - { + public bool Remove(KeyValuePair item) { return Remove(item.Key); } @@ -168,8 +156,7 @@ namespace Firebase.Database.Offline /// True if the contains an element with the key; /// otherwise, false. /// - public bool ContainsKey(string key) - { + public bool ContainsKey(string key) { return cache.ContainsKey(key); } @@ -178,8 +165,7 @@ namespace Firebase.Database.Offline /// /// The object to use as the key of the element to add. /// The object to use as the value of the element to add. - public void Add(string key, OfflineEntry value) - { + public void Add(string key, OfflineEntry value) { cache.Add(key, value); db.Insert(value); } @@ -192,8 +178,7 @@ namespace Firebase.Database.Offline /// True if the element is successfully removed; otherwise, false. This method also returns false if /// was not found in the original . /// - public bool Remove(string key) - { + public bool Remove(string key) { cache.Remove(key); return db.Delete(key); } @@ -211,13 +196,11 @@ namespace Firebase.Database.Offline /// True if the object that implements contains an /// element with the specified key; otherwise, false. /// - public bool TryGetValue(string key, out OfflineEntry value) - { + public bool TryGetValue(string key, out OfflineEntry value) { return cache.TryGetValue(key, out value); } - private string GetFileName(string fileName) - { + private string GetFileName(string fileName) { var invalidChars = new[] {'`', '[', ',', '='}; foreach (var c in invalidChars.Concat(Path.GetInvalidFileNameChars()).Distinct()) fileName = fileName.Replace(c, '_'); diff --git a/dsa/FireBase/Offline/OfflineEntry.cs b/dsa/FireBase/Offline/OfflineEntry.cs index 9feffa3..c959510 100644 --- a/dsa/FireBase/Offline/OfflineEntry.cs +++ b/dsa/FireBase/Offline/OfflineEntry.cs @@ -1,13 +1,11 @@ using System; using Newtonsoft.Json; -namespace Firebase.Database.Offline -{ +namespace Firebase.Database.Offline { /// /// Represents an object stored in offline storage. /// - public class OfflineEntry - { + public class OfflineEntry { private object dataInstance; /// @@ -21,8 +19,7 @@ namespace Firebase.Database.Offline /// /// The sync options. public OfflineEntry(string key, object obj, string data, int priority, SyncOptions syncOptions, - bool isPartial = false) - { + bool isPartial = false) { Key = key; Priority = priority; Data = data; @@ -44,15 +41,13 @@ namespace Firebase.Database.Offline /// /// The sync options. public OfflineEntry(string key, object obj, int priority, SyncOptions syncOptions, bool isPartial = false) - : this(key, obj, JsonConvert.SerializeObject(obj), priority, syncOptions, isPartial) - { + : this(key, obj, JsonConvert.SerializeObject(obj), priority, syncOptions, isPartial) { } /// /// Initializes a new instance of the class. /// - public OfflineEntry() - { + public OfflineEntry() { } /// @@ -91,8 +86,7 @@ namespace Firebase.Database.Offline /// /// Type of object to deserialize into. /// Instance of . - public T Deserialize() - { + public T Deserialize() { return (T) (dataInstance ?? (dataInstance = JsonConvert.DeserializeObject(Data))); } } diff --git a/dsa/FireBase/Offline/RealtimeDatabase.cs b/dsa/FireBase/Offline/RealtimeDatabase.cs index 973db46..e66a1f1 100644 --- a/dsa/FireBase/Offline/RealtimeDatabase.cs +++ b/dsa/FireBase/Offline/RealtimeDatabase.cs @@ -16,14 +16,12 @@ using Firebase.Database.Query; using Firebase.Database.Streaming; using Newtonsoft.Json; -namespace Firebase.Database.Offline -{ +namespace Firebase.Database.Offline { /// /// The real-time Database which synchronizes online and offline data. /// /// Type of entities. - public class RealtimeDatabase : IDisposable where T : class - { + public class RealtimeDatabase : IDisposable where T : class { private readonly ChildQuery childQuery; private readonly string elementRoot; private readonly FirebaseCache firebaseCache; @@ -55,8 +53,7 @@ namespace Firebase.Database.Offline public RealtimeDatabase(ChildQuery childQuery, string elementRoot, Func> offlineDatabaseFactory, string filenameModifier, StreamingOptions streamingOptions, InitialPullStrategy initialPullStrategy, bool pushChanges, - ISetHandler setHandler = null) - { + ISetHandler setHandler = null) { this.childQuery = childQuery; this.elementRoot = elementRoot; this.streamingOptions = streamingOptions; @@ -80,8 +77,7 @@ namespace Firebase.Database.Offline public ISetHandler PutHandler { private get; set; } - public void Dispose() - { + public void Dispose() { subject.OnCompleted(); firebaseSubscription?.Dispose(); } @@ -102,19 +98,16 @@ namespace Firebase.Database.Offline /// The priority. Objects with higher priority will be synced first. Higher number indicates higher /// priority. /// - public void Set(string key, T obj, SyncOptions syncOptions, int priority = 1) - { + public void Set(string key, T obj, SyncOptions syncOptions, int priority = 1) { SetAndRaise(key, new OfflineEntry(key, obj, priority, syncOptions)); } public void Set(string key, Expression> propertyExpression, object value, - SyncOptions syncOptions, int priority = 1) - { + SyncOptions syncOptions, int priority = 1) { var fullKey = GenerateFullKey(key, propertyExpression, syncOptions); var serializedObject = JsonConvert.SerializeObject(value).Trim('"', '\\'); - if (fullKey.Item3) - { + if (fullKey.Item3) { if (typeof(TProperty) != typeof(string) || value == null) // don't escape non-string primitives and null; serializedObject = $"{{ \"{fullKey.Item2}\" : {serializedObject} }}"; @@ -142,8 +135,7 @@ namespace Firebase.Database.Offline /// The priority. Objects with higher priority will be synced first. Higher number indicates higher /// priority. /// - public void Pull(string key, int priority = 1) - { + public void Pull(string key, int priority = 1) { if (!Database.ContainsKey(key)) Database[key] = new OfflineEntry(key, null, priority, SyncOptions.Pull); else if (Database[key].SyncOptions == SyncOptions.None) @@ -154,8 +146,7 @@ namespace Firebase.Database.Offline /// /// Fetches everything from the remote database. /// - public async Task PullAsync() - { + public async Task PullAsync() { var existingEntries = await childQuery .OnceAsync() .ToObservable() @@ -166,8 +157,7 @@ namespace Firebase.Database.Offline .OK) // OK implies the request couldn't complete due to network error. .Select(e => ResetDatabaseFromInitial(e, false)) .SelectMany(e => e) - .Do(e => - { + .Do(e => { Database[e.Key] = new OfflineEntry(e.Key, e.Object, 1, SyncOptions.None); subject.OnNext(new FirebaseEvent(e.Key, e.Object, FirebaseEventType.InsertOrUpdate, FirebaseEventSource.OnlinePull)); @@ -175,8 +165,7 @@ namespace Firebase.Database.Offline .ToList(); // Remove items not stored online - foreach (var item in Database.Keys.Except(existingEntries.Select(f => f.Key)).ToList()) - { + foreach (var item in Database.Keys.Except(existingEntries.Select(f => f.Key)).ToList()) { Database.Remove(item); subject.OnNext(new FirebaseEvent(item, null, FirebaseEventType.Delete, FirebaseEventSource.OnlinePull)); @@ -186,8 +175,7 @@ namespace Firebase.Database.Offline /// /// Retrieves all offline items currently stored in local database. /// - public IEnumerable> Once() - { + public IEnumerable> Once() { return Database .Where(kvp => !string.IsNullOrEmpty(kvp.Value.Data) && kvp.Value.Data != "null" && !kvp.Value.IsPartial) .Select(kvp => new FirebaseObject(kvp.Key, kvp.Value.Deserialize())) @@ -198,17 +186,14 @@ namespace Firebase.Database.Offline /// Starts observing the real-time Database. Events will be fired both when change is done locally and remotely. /// /// Stream of . - public IObservable> AsObservable() - { - if (!isSyncRunning) - { + public IObservable> AsObservable() { + if (!isSyncRunning) { isSyncRunning = true; Task.Factory.StartNew(SynchronizeThread, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } - if (observable == null) - { + if (observable == null) { var initialData = Observable.Return(FirebaseEvent.Empty(FirebaseEventSource.Offline)); if (Database.TryGetValue(elementRoot, out var oe)) initialData = Observable.Return(oe) @@ -251,8 +236,7 @@ namespace Firebase.Database.Offline } private IReadOnlyCollection> ResetDatabaseFromInitial( - IReadOnlyCollection> collection, bool onlyWhenInitialEverything = true) - { + IReadOnlyCollection> collection, bool onlyWhenInitialEverything = true) { if (onlyWhenInitialEverything && initialPullStrategy != InitialPullStrategy.Everything) return collection; // items which are in local db, but not in the online collection @@ -264,8 +248,7 @@ namespace Firebase.Database.Offline return collection.Concat(extra).ToList(); } - private void SetObjectFromInitialPull(FirebaseObject e) - { + private void SetObjectFromInitialPull(FirebaseObject e) { // set object with no sync only if it doesn't exist yet // and the InitialPullStrategy != Everything // this attempts to deal with scenario when you are offline, have local changes and go online @@ -276,11 +259,9 @@ namespace Firebase.Database.Offline Database[e.Key] = new OfflineEntry(e.Key, e.Object, 1, SyncOptions.None); } - private IObservable>> GetInitialPullObservable() - { + private IObservable>> GetInitialPullObservable() { FirebaseQuery query; - switch (initialPullStrategy) - { + switch (initialPullStrategy) { case InitialPullStrategy.MissingOnly: query = childQuery.OrderByKey().StartAt(() => GetLatestKey()); break; @@ -301,12 +282,10 @@ namespace Firebase.Database.Offline .Select(e => new[] {new FirebaseObject(elementRoot, e)})); } - private IDisposable InitializeStreamingSubscription(IObserver> observer) - { + private IDisposable InitializeStreamingSubscription(IObserver> observer) { var completeDisposable = Disposable.Create(() => isSyncRunning = false); - switch (streamingOptions) - { + switch (streamingOptions) { case StreamingOptions.LatestOnly: // stream since the latest key var queryLatest = childQuery.OrderByKey().StartAt(() => GetLatestKey()); @@ -328,8 +307,7 @@ namespace Firebase.Database.Offline } private void SetAndRaise(string key, OfflineEntry obj, - FirebaseEventSource eventSource = FirebaseEventSource.Offline) - { + FirebaseEventSource eventSource = FirebaseEventSource.Offline) { Database[key] = obj; subject.OnNext(new FirebaseEvent(key, obj?.Deserialize(), string.IsNullOrEmpty(obj?.Data) || obj?.Data == "null" @@ -337,12 +315,9 @@ namespace Firebase.Database.Offline : FirebaseEventType.InsertOrUpdate, eventSource)); } - private async void SynchronizeThread() - { - while (isSyncRunning) - { - try - { + private async void SynchronizeThread() { + while (isSyncRunning) { + try { var validEntries = Database.Where(e => e.Value != null); await PullEntriesAsync(validEntries.Where(kvp => kvp.Value.SyncOptions == SyncOptions.Pull)); @@ -350,8 +325,7 @@ namespace Firebase.Database.Offline await PushEntriesAsync(validEntries.Where(kvp => kvp.Value.SyncOptions == SyncOptions.Put || kvp.Value.SyncOptions == SyncOptions.Patch)); } - catch (Exception ex) - { + catch (Exception ex) { SyncExceptionThrown?.Invoke(this, new ExceptionEventArgs(ex)); } @@ -359,8 +333,7 @@ namespace Firebase.Database.Offline } } - private string GetLatestKey() - { + private string GetLatestKey() { var key = Database.OrderBy(o => o.Key, StringComparer.Ordinal).LastOrDefault().Key ?? string.Empty; if (!string.IsNullOrWhiteSpace(key)) @@ -369,60 +342,50 @@ namespace Firebase.Database.Offline return key; } - private async Task PushEntriesAsync(IEnumerable> pushEntries) - { + private async Task PushEntriesAsync(IEnumerable> pushEntries) { var groups = pushEntries.GroupBy(pair => pair.Value.Priority).OrderByDescending(kvp => kvp.Key).ToList(); - foreach (var group in groups) - { + foreach (var group in groups) { var tasks = group.OrderBy(kvp => kvp.Value.IsPartial).Select(kvp => kvp.Value.IsPartial ? ResetSyncAfterPush(PutHandler.SetAsync(childQuery, kvp.Key, kvp.Value), kvp.Key) : ResetSyncAfterPush(PutHandler.SetAsync(childQuery, kvp.Key, kvp.Value), kvp.Key, kvp.Value.Deserialize())); - try - { + try { await Task.WhenAll(tasks).WithAggregateException(); } - catch (Exception ex) - { + catch (Exception ex) { SyncExceptionThrown?.Invoke(this, new ExceptionEventArgs(ex)); } } } - private async Task PullEntriesAsync(IEnumerable> pullEntries) - { + private async Task PullEntriesAsync(IEnumerable> pullEntries) { var taskGroups = pullEntries.GroupBy(pair => pair.Value.Priority).OrderByDescending(kvp => kvp.Key); - foreach (var group in taskGroups) - { + foreach (var group in taskGroups) { var tasks = group.Select(pair => ResetAfterPull( childQuery.Child(pair.Key == elementRoot ? string.Empty : pair.Key).OnceSingleAsync(), pair.Key, pair.Value)); - try - { + try { await Task.WhenAll(tasks).WithAggregateException(); } - catch (Exception ex) - { + catch (Exception ex) { SyncExceptionThrown?.Invoke(this, new ExceptionEventArgs(ex)); } } } - private async Task ResetAfterPull(Task task, string key, OfflineEntry entry) - { + private async Task ResetAfterPull(Task task, string key, OfflineEntry entry) { await task; SetAndRaise(key, new OfflineEntry(key, task.Result, entry.Priority, SyncOptions.None), FirebaseEventSource.OnlinePull); } - private async Task ResetSyncAfterPush(Task task, string key, T obj) - { + private async Task ResetSyncAfterPush(Task task, string key, T obj) { await ResetSyncAfterPush(task, key); if (streamingOptions == StreamingOptions.None) @@ -431,35 +394,29 @@ namespace Firebase.Database.Offline FirebaseEventSource.OnlinePush)); } - private async Task ResetSyncAfterPush(Task task, string key) - { + private async Task ResetSyncAfterPush(Task task, string key) { await task; ResetSyncOptions(key); } - private void ResetSyncOptions(string key) - { + private void ResetSyncOptions(string key) { var item = Database[key]; - if (item.IsPartial) - { + if (item.IsPartial) { Database.Remove(key); } - else - { + else { item.SyncOptions = SyncOptions.None; Database[key] = item; } } - private void StreamingExceptionThrown(object sender, ExceptionEventArgs e) - { + private void StreamingExceptionThrown(object sender, ExceptionEventArgs e) { SyncExceptionThrown?.Invoke(this, new ExceptionEventArgs(e.Exception)); } private Tuple GenerateFullKey(string key, - Expression> propertyGetter, SyncOptions syncOptions) - { + Expression> propertyGetter, SyncOptions syncOptions) { var visitor = new MemberAccessVisitor(); visitor.Visit(propertyGetter); var propertyType = typeof(TProperty).GetTypeInfo(); diff --git a/dsa/FireBase/Offline/SetHandler.cs b/dsa/FireBase/Offline/SetHandler.cs index 6314c3c..c683fdd 100644 --- a/dsa/FireBase/Offline/SetHandler.cs +++ b/dsa/FireBase/Offline/SetHandler.cs @@ -1,14 +1,10 @@ using System.Threading.Tasks; using Firebase.Database.Query; -namespace Firebase.Database.Offline -{ - public class SetHandler : ISetHandler - { - public virtual async Task SetAsync(ChildQuery query, string key, OfflineEntry entry) - { - using (var child = query.Child(key)) - { +namespace Firebase.Database.Offline { + public class SetHandler : ISetHandler { + public virtual async Task SetAsync(ChildQuery query, string key, OfflineEntry entry) { + using (var child = query.Child(key)) { if (entry.SyncOptions == SyncOptions.Put) await child.PutAsync(entry.Data); else diff --git a/dsa/FireBase/Offline/StreamingOptions.cs b/dsa/FireBase/Offline/StreamingOptions.cs index a420cbb..205118d 100644 --- a/dsa/FireBase/Offline/StreamingOptions.cs +++ b/dsa/FireBase/Offline/StreamingOptions.cs @@ -1,7 +1,5 @@ -namespace Firebase.Database.Offline -{ - public enum StreamingOptions - { +namespace Firebase.Database.Offline { + public enum StreamingOptions { /// /// No realtime streaming. /// diff --git a/dsa/FireBase/Offline/SyncOptions.cs b/dsa/FireBase/Offline/SyncOptions.cs index ca68d0a..3b6e9c8 100644 --- a/dsa/FireBase/Offline/SyncOptions.cs +++ b/dsa/FireBase/Offline/SyncOptions.cs @@ -1,10 +1,8 @@ -namespace Firebase.Database.Offline -{ +namespace Firebase.Database.Offline { /// /// Specifies type of sync requested for given data. /// - public enum SyncOptions - { + public enum SyncOptions { /// /// No sync needed for given data. /// -- cgit v1.2.3-54-g00ecf