diff --git a/Common/Common/Json/LCJsonConverter.cs b/Common/Common/Json/LCJsonConverter.cs new file mode 100644 index 0000000..3e78108 --- /dev/null +++ b/Common/Common/Json/LCJsonConverter.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace LeanCloud.Common { + public class LCJsonConverter : JsonConverter { + public override bool CanConvert(Type objectType) { + return objectType == typeof(object); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { + serializer.Serialize(writer, value); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { + if (reader.TokenType == JsonToken.StartObject) { + var obj = new Dictionary(); + serializer.Populate(reader, obj); + return obj; + } + if (reader.TokenType == JsonToken.StartArray) { + var arr = new List(); + serializer.Populate(reader, arr); + return arr; + } + if (reader.TokenType == JsonToken.Integer) { + if ((long)reader.Value < int.MaxValue) { + return Convert.ToInt32(reader.Value); + } + } + + return serializer.Deserialize(reader); + } + + public readonly static LCJsonConverter Default = new LCJsonConverter(); + } +} diff --git a/LiveQuery/LiveQuery/Internal/LCLiveQueryHeartBeat.cs b/LiveQuery/LiveQuery/Internal/LCLiveQueryHeartBeat.cs new file mode 100644 index 0000000..a856d91 --- /dev/null +++ b/LiveQuery/LiveQuery/Internal/LCLiveQueryHeartBeat.cs @@ -0,0 +1,7 @@ +using System; +namespace LiveQuery.Internal { + public class LCLiveQueryHeartBeat { + public LCLiveQueryHeartBeat() { + } + } +} diff --git a/LiveQuery/LiveQuery/LCQueryExtension.cs b/LiveQuery/LiveQuery/LCQueryExtension.cs new file mode 100644 index 0000000..5877435 --- /dev/null +++ b/LiveQuery/LiveQuery/LCQueryExtension.cs @@ -0,0 +1,7 @@ +using System; +namespace LiveQuery { + public class LCQueryExtension { + public LCQueryExtension() { + } + } +} diff --git a/Sample/LiveQueryApp/LiveQueryApp.csproj b/Sample/LiveQueryApp/LiveQueryApp.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Sample/LiveQueryApp/LiveQueryApp.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Sample/LiveQueryApp/Program.cs b/Sample/LiveQueryApp/Program.cs new file mode 100644 index 0000000..2888d80 --- /dev/null +++ b/Sample/LiveQueryApp/Program.cs @@ -0,0 +1,9 @@ +using System; + +namespace LiveQueryApp { + class Program { + static void Main(string[] args) { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/Sample/LiveQueryApp/SingleThreadSynchronizationContext.cs b/Sample/LiveQueryApp/SingleThreadSynchronizationContext.cs new file mode 100644 index 0000000..fdfb2c9 --- /dev/null +++ b/Sample/LiveQueryApp/SingleThreadSynchronizationContext.cs @@ -0,0 +1,43 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using System.Collections.Concurrent; +using System.Collections.Generic; + +namespace RealtimeApp { + /// + /// 单线程环境,用于控制台应用 await 返回 + /// + public class SingleThreadSynchronizationContext : SynchronizationContext { + private readonly BlockingCollection> queue = new BlockingCollection>(); + + public override void Post(SendOrPostCallback d, object state) { + queue.Add(new KeyValuePair(d, state)); + } + + public void RunOnCurrentThread() { + while (queue.TryTake(out KeyValuePair workItem, Timeout.Infinite)) { + workItem.Key(workItem.Value); + } + } + + public void Complete() { + queue.CompleteAdding(); + } + + public static void Run(Func func) { + SynchronizationContext prevContext = Current; + try { + SingleThreadSynchronizationContext syncContext = new SingleThreadSynchronizationContext(); + SetSynchronizationContext(syncContext); + + Task t = func(); + syncContext.RunOnCurrentThread(); + + t.GetAwaiter().GetResult(); + } finally { + SetSynchronizationContext(prevContext); + } + } + } +}