Esempio n. 1
0
        private void HandleOnSnapshotJsCallback(string docId, string docJson)
        {
            Type docType = _docIdVsDocType[docId];

            if (docType == null)
            {
                Logger.LogError($"No registered type found for doc id {docId}");
                return;
            }

            DocumentUpdateCallback callback = _docIdVsSnapshotCallback[docId];

            if (callback == null)
            {
                Logger.LogError($"No registered callback for dod id {docId}");
                return;
            }

            try
            {
                var document = (IFirestoreDocument)JsonSerializer.Deserialize(docJson, docType,
                                                                              new JsonSerializerOptions {
                    PropertyNameCaseInsensitive = true
                });
                callback.Invoke(document);
            }
            catch (Exception e)
            {
                Logger.LogError($"Failed to deserialize updated document:");
                Logger.LogError(docJson);
                Logger.LogError(e.Message);
            }
        }
Esempio n. 2
0
        public async Task <FirestoreOperationResult <T> > SubscribeForDocumentUpdates <T>(
            string collection, string docId, DocumentUpdateCallback callback) where T : IFirestoreDocument
        {
            await Init();

            if (_docIdVsDocType.ContainsKey(docId))
            {
                Logger.LogInformation($"doc id {docId} already subscribed for updated.");
                return(new FirestoreOperationResult <T>()
                {
                    Success = true
                });
            }

            string operationResult = string.Empty;

            _docIdVsDocType.Add(docId, typeof(T));
            _docIdVsSnapshotCallback.Add(docId, callback);

            var module = await firestoreModuleTask.Value;

            try
            {
                operationResult =
                    await module.InvokeAsync <string>("onDocumentSnapshot",
                                                      collection, docId, "BlazorUtils.Firebase", "OnSnapshotJsCallback");
            }
            catch (Exception e)
            {
                Logger.LogError("Failed to subscribe for document updates");
                Logger.LogError(e.Message);
            }

            return(ConvertJsonToResult <T>(operationResult));
        }