Beispiel #1
0
    public async Task <LCUser> GetUser(string id)
    {
        if (users.TryGetValue(id, out LCUser user))
        {
            return(user);
        }
        if (fetchUserTasks.TryGetValue(id, out Task <LCUser> runningTask))
        {
            return(await runningTask);
        }
        Task <LCUser> task = LCUser.GetQuery().Get(id);

        fetchUserTasks.Add(id, task);
        user = await task;
        fetchUserTasks.Remove(id);
        users[id] = user;
        return(user);
    }
Beispiel #2
0
        static void Main(string[] args)
        {
            WriteLine("Hello World!");

            SingleThreadSynchronizationContext.Run(async() => {
                LCLogger.LogDelegate += Print;
                LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz",
                                         "NUKmuRbdAhg1vrb2wexYo1jo",
                                         "https://ikggdre2.lc-cn-n1-shared.com");

                await LCUser.Login("hello", "world");
                LCQuery <LCUser> userQuery = LCUser.GetQuery();
                userQuery.WhereEqualTo("username", "hello");
                LCLiveQuery userLiveQuery = await userQuery.Subscribe();
                userLiveQuery.OnLogin     = (user) => {
                    WriteLine($"login: {user.Username}");
                };

                LCQuery <LCObject> query = new LCQuery <LCObject>("Account");
                query.WhereGreaterThan("balance", 100);
                LCLiveQuery liveQuery = await query.Subscribe();
                liveQuery.OnCreate    = (obj) => {
                    WriteLine($"create: {obj}");
                };
                liveQuery.OnUpdate = (obj, keys) => {
                    WriteLine($"update: {obj}");
                    WriteLine(keys.Count);
                };
                liveQuery.OnDelete = (objId) => {
                    WriteLine($"delete: {objId}");
                };
                liveQuery.OnEnter = (obj, keys) => {
                    WriteLine($"enter: {obj}");
                    WriteLine(keys.Count);
                };
                liveQuery.OnLeave = (obj, keys) => {
                    WriteLine($"leave: {obj}");
                    WriteLine(keys.Count);
                };
            });
        }
Beispiel #3
0
        public async Task Login()
        {
            TaskCompletionSource <object> tcs = new TaskCompletionSource <object>();

            await LCUser.Login("hello", "world");

            LCQuery <LCUser> userQuery = LCUser.GetQuery();

            userQuery.WhereEqualTo("username", "hello");
            LCLiveQuery userLiveQuery = await userQuery.Subscribe();

            userLiveQuery.OnLogin = (user) => {
                WriteLine($"login: {user}");
                tcs.SetResult(null);
            };

            // 模拟 REST API
            string             url     = "https://ikggdre2.lc-cn-n1-shared.com/1.1/login";
            HttpRequestMessage request = new HttpRequestMessage {
                RequestUri = new Uri(url),
                Method     = HttpMethod.Post
            };

            request.Headers.Add("X-LC-Id", "ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz");
            request.Headers.Add("X-LC-Key", "NUKmuRbdAhg1vrb2wexYo1jo");
            string content = JsonConvert.SerializeObject(new Dictionary <string, object> {
                { "username", "hello" },
                { "password", "world" }
            });
            StringContent requestContent = new StringContent(content);

            requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            request.Content = requestContent;
            HttpClient client = new HttpClient();
            await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

            await tcs.Task;
        }