コード例 #1
0
ファイル: Query.cs プロジェクト: zub786/google-cloud-dotnet
        /// <summary>
        /// Listen to this query for changes.
        /// </summary>
        /// <param name="callback">The callback to invoke each time the query results change. Must not be null.</param>
        /// <param name="cancellationToken">Optional cancellation token which may be used to cancel the listening operation.</param>
        /// <returns>A <see cref="FirestoreChangeListener"/> which may be used to monitor the listening operation and stop it gracefully.</returns>
        public FirestoreChangeListener Listen(Func <QuerySnapshot, CancellationToken, Task> callback, CancellationToken cancellationToken = default)
        {
            GaxPreconditions.CheckNotNull(callback, nameof(callback));
            var target = WatchStream.CreateTarget(this);
            var stream = new WatchStream(new WatchState(this, callback), target, Database, cancellationToken);

            return(FirestoreChangeListener.Start(stream));
        }
コード例 #2
0
        /// <summary>
        /// Watch this document for changes.
        /// </summary>
        /// <param name="callback">The callback to invoke each time the document changes. Must not be null.</param>
        /// <param name="cancellationToken">Optional cancellation token which may be used to cancel the listening operation.</param>
        /// <returns>A <see cref="FirestoreChangeListener"/> which may be used to monitor the listening operation and stop it gracefully.</returns>
        public FirestoreChangeListener Listen(Func <DocumentSnapshot, CancellationToken, Task> callback, CancellationToken cancellationToken = default)
        {
            GaxPreconditions.CheckNotNull(callback, nameof(callback));
            var target = WatchStream.CreateTarget(this);
            Func <QuerySnapshot, CancellationToken, Task> queryCallback = async(querySnapshot, localCancellationToken) =>
            {
                foreach (var doc in querySnapshot)
                {
                    if (doc.Reference.Equals(this))
                    {
                        await callback(doc, localCancellationToken).ConfigureAwait(false);

                        return;
                    }
                }
                // TODO: This will mean parsing the path back to a DocumentReference. Maybe we should accept "this".
                var missingDoc = DocumentSnapshot.ForMissingDocument(Database, Path, querySnapshot.ReadTime);
                await callback(missingDoc, cancellationToken).ConfigureAwait(false);
            };
            var stream = new WatchStream(new WatchState(Parent, queryCallback), target, Database, cancellationToken);

            return(FirestoreChangeListener.Start(stream));
        }
コード例 #3
0
        internal static SnapshotListener Start(WatchStream stream)
        {
            var task = stream.StartAsync();

            return(new SnapshotListener(stream, task));
        }
コード例 #4
0
 internal SnapshotListener(WatchStream stream, Task task)
 {
     _stream      = stream;
     ListenerTask = task;
 }
コード例 #5
0
        internal static FirestoreChangeListener Start(WatchStream stream)
        {
            var task = stream.StartAsync();

            return(new FirestoreChangeListener(stream, task));
        }
コード例 #6
0
 internal FirestoreChangeListener(WatchStream stream, Task task)
 {
     _stream      = stream;
     ListenerTask = task;
 }