Exemple #1
0
        /// <summary>
        /// Allows you to request an entity (from the store) and get a callback when load succeeds or fails.
        /// If the entity is not initially loaded, sends the usual notifications and then listens for updates
        /// </summary>
        public static Request <ResolveMultipleResultDTO <DataType> > ResolveAll(
            IEnumerable <string> keys,
            HasEntities <DataType> store,
            Action <Request <ResolveMultipleResultDTO <DataType> > > callback = null)
        {
            var promise = new Promise <ResolveMultipleResultDTO <DataType> >((resolve, reject, cancel, attach) =>
            {
                var all = new JoinRequests();
                foreach (var k in keys)
                {
                    all.Add(new DataRequest(NewResolveRequest(k), store));
                }
                all.Execute(result =>
                {
                    var allResult = result as JoinRequests;
                    using (var resultRequests = ListPool <Request> .Get())
                        using (var resultItems = ListPool <ResolveResultDTO <DataType> > .Get())
                        {
                            allResult.GetResults(resultRequests);
                            foreach (var rr in resultRequests)
                            {
                                var key    = (rr as DataRequest).loadKey;
                                var reqDTO = (rr as DataRequest).requestDTO;
                                if (rr.hasError)
                                {
                                    resultItems.Add(
                                        ResolveResultDTO <DataType> .ResolveError(
                                            reqDTO, rr.error
                                            )
                                        );
                                    continue;
                                }
                                Entity <DataType> entity;
                                if (!store.GetEntity(key, out entity))
                                {
                                    resultItems.Add(
                                        ResolveResultDTO <DataType> .ResolveError(
                                            reqDTO,
                                            "enity not in store after resolve"
                                            )
                                        );
                                    continue;
                                }
                                var status = entity.status;
                                resultItems.Add(ResolveResultDTO <DataType> .ResolveSucceeded(
                                                    reqDTO, entity.id, entity.data, status.maxAgeSecs, status.timestamp
                                                    ));
                            }
                            resolve(new ResolveMultipleResultDTO <DataType>
                            {
                                entities = resultItems.ToArray()
                            });
                        }
                });
            });

            promise.Execute(callback);
            return(promise);
        }
Exemple #2
0
        /// <summary>
        /// A convenience extension/wrapper around HasEntities&lt;DataType>&gt::GetData
        /// that also issues a resolve request under specifiable conditions,
        /// e.g. if the entity is missing or if it is expired
        /// </summary>
        /// <returns><c>true</c>, if data was gotten, <c>false</c> otherwise.</returns>
        /// <param name="entities">Entities.</param>
        /// <param name="key">Key.</param>
        /// <param name="d">D.</param>
        /// <param name="requestResolvePolicy">Request resolve policy.</param>
        /// <typeparam name="DataType">The 1st type parameter.</typeparam>
        public static bool GetData <DataType>(
            this HasEntities <DataType> entities,
            string key,
            out DataType d,
            RequestResolvePolicyFlags requestResolvePolicy
            )
        {
            Entity <DataType> entity;

            if (!entities.GetEntity(key, out entity) || !entity.status.hasResolved)
            {
                d = default(DataType);

                // By default DO NOT try to resolve an entity that is in error status
                if (entity.status.HasError() && !requestResolvePolicy.HasFlag(RequestResolvePolicyFlags.IF_HAS_ERROR))
                {
                    return(false);
                }

                if (requestResolvePolicy.HasFlag(RequestResolvePolicyFlags.IF_MISSING))
                {
                    Entity <DataType> .RequestResolve(new ResolveRequestDTO
                    {
                        key = key
                    });
                }
                return(false);
            }

            d = entity.data;

#if UNITY_EDITOR || DEBUG_UNSTRIP
            if (Entity <DataType> .DEBUG)
            {
                Debug.Log("[" + Time.frameCount + "][" + typeof(Entity <DataType>).Name
                          + "] key=" + key
                          + " - found with timestamp=" + entity.status.timestamp
                          + ", and maxAgeSecs=" + entity.status.maxAgeSecs
                          + ", isExpired=" + entity.status.IsExpiredAt(DateTimeOffset.Now)
                          + ", isResolveInProgress=" + entity.status.isResolveInProgress);
            }
#endif
            if (entity.status.HasError() && !requestResolvePolicy.HasFlag(RequestResolvePolicyFlags.IF_HAS_ERROR))
            {
                // By default DO NOT try to resolve an entity that is in error status
                return(true);
            }

            if (requestResolvePolicy.HasFlag(RequestResolvePolicyFlags.IF_EXPIRED))
            {
                var status = entity.status;
                if (status.IsExpiredAt(DateTimeOffset.Now) && !status.isResolveInProgress)
                {
                    Entity <DataType> .RequestResolve(key);
                }
            }

            return(true);
        }