Example #1
0
        void IDocumentInternal.HandleFetch(ShareDBException ex, int version, string type, JToken ops)
        {
            if (!_inflightFetches.TryDequeue(out TaskCompletionSource <bool> tcs))
            {
                return;
            }

            if (ex != null)
            {
                tcs.SetException(ex);
            }
            else
            {
                IOTType otType = null;
                if (type != null && !OTTypes.TryGetType(type, out otType))
                {
                    tcs.SetException(new ShareDBException($"Unregistered type {type}.", 4008));
                }
                if (Type == null)
                {
                    Type = otType;
                }

                if (otType != null && Type != otType)
                {
                    tcs.SetException(new ShareDBException("The document type does not match the retrieved data type.",
                                                          4101));
                }
                else
                {
                    Version = version;
                    if (ops != null)
                    {
                        Data     = (T)Type.Deserialize(ops);
                        IsLoaded = true;
                        tcs.SetResult(true);
                    }
                    else
                    {
                        tcs.SetResult(false);
                    }
                }
            }
        }
        public Document <T> Get <T>(string collection, string id)
        {
            if (!_collections.TryGetValue(collection, out Dictionary <string, IDocumentInternal> docs))
            {
                docs = new Dictionary <string, IDocumentInternal>();
                _collections[collection] = docs;
            }

            if (!docs.TryGetValue(id, out IDocumentInternal doc))
            {
                if (!OTTypes.TryGetType(typeof(T), out IOTType type))
                {
                    type = null;
                }
                doc      = new Document <T>(this, collection, id, type);
                docs[id] = doc;
            }
            return((Document <T>)doc);
        }