예제 #1
0
        public Task <AVUser> GetAsync(CancellationToken cancellationToken)
        {
            AVUser cachedCurrent;

            lock (mutex) {
                cachedCurrent = CurrentUser;
            }

            if (cachedCurrent != null)
            {
                return(Task <AVUser> .FromResult(cachedCurrent));
            }

            return(taskQueue.Enqueue(toAwait => {
                return toAwait.ContinueWith(_ => {
                    return storageController.LoadAsync().OnSuccess(t => {
                        object temp;
                        t.Result.TryGetValue("CurrentUser", out temp);
                        var userDataString = temp as string;
                        AVUser user = null;
                        if (userDataString != null)
                        {
                            var userData = Json.Parse(userDataString) as IDictionary <string, object>;
                            var state = AVObjectCoder.Instance.Decode(userData, AVDecoder.Instance);
                            user = AVObject.FromState <AVUser>(state, "_User");
                        }

                        CurrentUser = user;
                        return user;
                    });
                }).Unwrap();
            }, cancellationToken));
        }
예제 #2
0
        /// <summary>
        /// Retrieves at most one AVObject that satisfies this query.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A single AVObject that satisfies this query, or else null.</returns>
        public Task <T> FirstOrDefaultAsync(CancellationToken cancellationToken)
        {
            EnsureNotInstallationQuery();
            return(QueryController.FirstAsync <T>(this, AVUser.CurrentUser, cancellationToken).OnSuccess(t =>
            {
                IObjectState state = t.Result;

                return state == null ? default(T) : AVObject.FromState <T>(state, ClassName);
            }));
        }
예제 #3
0
        /// <summary>
        /// Retrieves a list of AVObjects that satisfy this query from LeanCloud.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The list of AVObjects that match this query.</returns>
        public Task <IEnumerable <T> > FindAsync(CancellationToken cancellationToken)
        {
            EnsureNotInstallationQuery();
            return(QueryController.FindAsync <T>(this, AVUser.CurrentUser, cancellationToken).OnSuccess(t =>
            {
                IEnumerable <IObjectState> states = t.Result;

                return (from state in states
                        select AVObject.FromState <T>(state, ClassName));
            }));
        }
예제 #4
0
파일: AVQuery.cs 프로젝트: longyunzhou/NFC
 /// <summary>
 /// Retrieves at most one AVObject that satisfies this query.
 /// </summary>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>A single AVObject that satisfies this query, or else null.</returns>
 public override Task <T> FirstOrDefaultAsync(CancellationToken cancellationToken)
 {
     return(AVUser.GetCurrentUserAsync().OnSuccess(t =>
     {
         return QueryController.FirstAsync <T>(this, t.Result, cancellationToken);
     }).Unwrap().OnSuccess(t =>
     {
         IObjectState state = t.Result;
         return state == null ? default(T) : AVObject.FromState <T>(state, ClassName);
     }));
 }
예제 #5
0
        public void Emit(string scope, IDictionary <string, object> payloadMap)
        {
            var objectState   = AVObjectCoder.Instance.Decode(payloadMap, AVDecoder.Instance);
            var payloadObject = AVObject.FromState <T>(objectState, Query.GetClassName <T>());
            var args          = new AVLiveQueryEventArgs <T>()
            {
                Scope   = scope,
                Payload = payloadObject
            };

            OnLiveQueryReceived.Invoke(this, args);
        }
예제 #6
0
파일: AVQuery.cs 프로젝트: longyunzhou/NFC
 /// <summary>
 /// Retrieves a list of AVObjects that satisfy this query from LeanCloud.
 /// </summary>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The list of AVObjects that match this query.</returns>
 public override Task <IEnumerable <T> > FindAsync(CancellationToken cancellationToken)
 {
     return(AVUser.GetCurrentUserAsync().OnSuccess(t =>
     {
         return QueryController.FindAsync <T>(this, t.Result, cancellationToken);
     }).Unwrap().OnSuccess(t =>
     {
         IEnumerable <IObjectState> states = t.Result;
         return (from state in states
                 select AVObject.FromState <T>(state, ClassName));
     }));
 }
예제 #7
0
        protected virtual object DecodeAVObject(IDictionary <string, object> dict)
        {
            var className = dict["className"] as string;

            if (className == "_File")
            {
                return(DecodeAVFile(dict));
            }
            var state = AVObjectCoder.Instance.Decode(dict, this);

            return(AVObject.FromState <AVObject>(state, dict["className"] as string));
        }
예제 #8
0
        internal static Task <string> UpgradeToRevocableSessionAsync(string sessionToken, CancellationToken cancellationToken)
        {
            if (sessionToken == null || SessionController.IsRevocableSessionToken(sessionToken))
            {
                return(Task <string> .FromResult(sessionToken));
            }

            return(SessionController.UpgradeToRevocableSessionAsync(sessionToken, cancellationToken).OnSuccess(t =>
            {
                AVSession session = AVObject.FromState <AVSession>(t.Result, "_Session");
                return session.SessionToken;
            }));
        }
예제 #9
0
 public override ISlarkMessage CreateMessage(string message)
 {
     if (Json.Parse(message) is IDictionary <string, object> result)
     {
         if (result.ContainsKey("results") && result.ContainsKey("si"))
         {
             return(result["results"] is Dictionary <string, object> results
                 ? new TMJsonResponse(message, results, result["si"].ToString())
                     : new TMJsonResponse(message, result["si"].ToString()));
         }
         var state = AVObjectCoder.Instance.Decode(result, AVDecoder.Instance);
         return(AVObject.FromState <TMJsonRequest>(state, result["className"] as string));
     }
     return(base.CreateMessage(message));
 }
예제 #10
0
        /// <summary>
        /// 执行 CQL 查询
        /// </summary>
        /// <param name="cql">CQL 语句</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>返回符合条件的对象集合</returns>
        public static Task <IEnumerable <T> > DoCloudQueryAsync(string cql, CancellationToken cancellationToken)
        {
            var command = new AVCommand(string.Format("/cloudQuery?cql={0}", Uri.EscapeDataString(cql)),
                                        method: "GET",
                                        sessionToken: AVUser.CurrentSessionToken,
                                        data: null);

            return(AVClient.AVCommandRunner.RunCommandAsync(command, cancellationToken: cancellationToken).OnSuccess(t => {
                var items = t.Result.Item2["results"] as IList <object>;
                var className = t.Result.Item2["className"].ToString();

                IEnumerable <IObjectState> states = (from item in items
                                                     select AVObjectCoder.Instance.Decode(item as IDictionary <string, object>, AVDecoder.Instance));

                return (from state in states
                        select AVObject.FromState <T>(state, className));
            }));
        }
예제 #11
0
파일: AVQuery.cs 프로젝트: longyunzhou/NFC
        internal static Task <IEnumerable <T> > rebuildObjectFromCloudQueryResult(string queryString)
        {
            var command = new AVCommand(queryString,
                                        method: "GET",
                                        sessionToken: AVUser.CurrentSessionToken,
                                        data: null);

            return(AVPlugins.Instance.CommandRunner.RunCommandAsync(command, cancellationToken: CancellationToken.None).OnSuccess(t =>
            {
                var items = t.Result.Item2["results"] as IList <object>;
                var className = t.Result.Item2["className"].ToString();

                IEnumerable <IObjectState> states = (from item in items
                                                     select AVObjectCoder.Instance.Decode(item as IDictionary <string, object>, AVDecoder.Instance));

                return (from state in states
                        select AVObject.FromState <T>(state, className));
            }));
        }
예제 #12
0
        public StorageRpcContext CreateContext(RpcConnection sender, string message)
        {
            var context = new StorageRpcContext();

            context.Connection = sender;
            context.Message    = message;
            if (Json.Parse(message) is IDictionary <string, object> result)
            {
                if (result.ContainsKey("results") && result.ContainsKey("si"))
                {
                    var responseState = AVObjectCoder.Instance.Decode(result, AVDecoder.Instance);
                    context.Response = AVObject.FromState <StorageResponse>(responseState, result["className"] as string);
                }
                else
                {
                    var state = AVObjectCoder.Instance.Decode(result, AVDecoder.Instance);
                    context.Request = AVObject.FromState <StorageRequest>(state, result["className"] as string);
                }
            }
            return(context);
        }
예제 #13
0
        /// <summary>
        /// Gets the current <see cref="AVSession"/> object related to the current user.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token</param>
        public static Task <AVSession> GetCurrentSessionAsync(CancellationToken cancellationToken)
        {
            return(AVUser.GetCurrentUserAsync().OnSuccess(t1 =>
            {
                AVUser user = t1.Result;
                if (user == null)
                {
                    return Task <AVSession> .FromResult((AVSession)null);
                }

                string sessionToken = user.SessionToken;
                if (sessionToken == null)
                {
                    return Task <AVSession> .FromResult((AVSession)null);
                }

                return SessionController.GetSessionAsync(sessionToken, cancellationToken).OnSuccess(t =>
                {
                    AVSession session = AVObject.FromState <AVSession>(t.Result, "_Session");
                    return session;
                });
            }).Unwrap());
        }
예제 #14
0
 public static T FromState <T>(IObjectState state, string defaultClassName) where T : AVObject
 {
     return(AVObject.FromState <T>(state, defaultClassName));
 }