// The HandleChangeAsync method is called for every observed Realm file // whenever it has changes. It is called with a change event which contains // a version of the Realm from before and after the change, as well as // collections of all objects which were added, deleted, or modified in this change public override async Task HandleChangeAsync(IChangeDetails details) { Console.WriteLine("I'm listening!!!!!!!"); //TODO: add this code //if (details.TryGetValue("Coupon", out var changeSetDetails) && // changeSetDetails.Insertions.Length > 0) //{ // // Extract the user ID from the virtual path, assuming that we're using // // a filter which only subscribes us to updates of user-scoped Realms. // var userId = details.RealmPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[0]; // using (var realm = details.GetRealmForWriting()) // { // foreach (var coupon in changeSetDetails.Insertions.Select(c => c.CurrentObject)) // { // var isValid = await CouponVerifier.VerifyAsync(coupon, userId); // // Create a ThreadSafeReference of the coupon. While both // // details.CurrentRealm and details.GetRealmForWriting() are open // // on the same thread, they are at different versions, so you need // // to pass the Coupon between them either via ThreadSafeReference // // or by its PrimaryKey. // var writeableCoupon = realm.ResolveReference(ThreadSafeReference.Create(coupon)); // // It may be null if the coupon was deleted by the time we get here // if (writeableCoupon != null) // { // realm.Write(() => writeableCoupon.IsValid = isValid); // } // } // } //} }
public override Task HandleChangeAsync(IChangeDetails details) { try { if (details.Changes.TryGetValue("Task", out var changeSetDetails)) { //if (changeSetDetails.Insertions.Any()) //{ // var newTasks = (from m in changeSetDetails.Insertions // let obj = m.CurrentObject // let text = (string)obj.Title // select new TaskItem { // Id = (string)obj.Id, // Reference = ThreadSafeReference.Create(obj), // Text = text, // Realm = details.GetRealmForWriting() // }) // .ToList(); // newTasks.ForEach(t => newTaskSubject.OnNext(t)); //} if (changeSetDetails.Modifications.Any()) { var modifiedTasks = (from m in changeSetDetails.Modifications //where m.CurrentObject != null && m.PreviousObject != null //group m by m.CurrentObject.Id into g //let obj = g.Last().CurrentObject //let text = (string)obj.Title let obj = m.CurrentObject let text = (string)obj.Title select new TaskItem { Id = (string)obj.Id, Reference = ThreadSafeReference.Create(obj), Text = text, Realm = details.GetRealmForWriting() }) .ToList(); modifiedTasks.ForEach(t => modifiedTaskSubject.OnNext(t)); } } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } return(Task.CompletedTask); }
/// <summary> /// A method invoked by the <see cref="INotifier"/> when a Realm has changed and /// <see cref="ShouldHandle"/> has returned <c>true</c>. /// </summary> /// <param name="details"> /// An instance of <see cref="IChangeDetails"/>, containing detailed information /// about the changes that have occurred in the Realm. /// </param> /// <returns> /// An awaitable task that, upon completion, signifies that the changes have been processed. /// </returns> /// <remarks> /// Handlers will be invoked sequentially in the order in which they have been supplied /// in the <see cref="NotifierConfiguration.Handlers"/>. /// <para/> /// This method will be invoked sequentially for Realms with the same path and in parallel /// for different Realms. This means that if the processing takes a lot of time, it will /// build up a queue of changes for that Realm path but will not affect notifications from /// other Realms. /// </remarks> public abstract Task HandleChangeAsync(IChangeDetails details);
public override async Task HandleChangeAsync(IChangeDetails details) { if (details.Changes.TryGetValue("Ticket", out var changeSetDetails) && changeSetDetails.Insertions.Length > 0) { // A new thread has been started - run sentiment analysis. try { var tickets = changeSetDetails.Insertions .Select(i => i.CurrentObject) .Select(o => (string)(o.Title + Environment.NewLine + o.Description)) .ToArray(); if (tickets.Length == 0) { return; } Console.WriteLine($"Requesting sentiment score for {tickets.Length} objects..."); var sentimentRequest = tickets.Select((text, index) => new SentimentDocument { Id = index.ToString(), Text = text, Language = "en" }) .Cast <IDocument>() .ToList(); var sentimentResponse = await _sentimentClient.GetSentimentAsync(new SentimentRequest { Documents = sentimentRequest }); foreach (var error in sentimentResponse.Errors) { Console.WriteLine("Error from sentiment API: " + error.Message); } Console.WriteLine($"Requesting key phrases for {tickets.Length} objects..."); var keyPhraseRequest = tickets.Select((text, index) => new KeyPhraseDocument { Id = index.ToString(), Text = text, Language = "en" }) .Cast <IDocument>() .ToList(); var keyPhraseResponse = await _keyPhraseClient.GetKeyPhrasesAsync(new KeyPhraseRequest { Documents = keyPhraseRequest }); foreach (var error in keyPhraseResponse.Errors) { Console.WriteLine("Error from KeyPhrase API: " + error.Message); } var keyPhraseDictionary = keyPhraseResponse.Documents.ToDictionary(d => d.Id, d => d.KeyPhrases); var toUpdate = sentimentResponse.Documents .Select(doc => { var obj = changeSetDetails.Insertions[int.Parse(doc.Id)].CurrentObject; if (!keyPhraseDictionary.TryGetValue(doc.Id, out var keyPhrases) || keyPhrases == null) { keyPhrases = new List <string> { "Unknown" }; } Console.WriteLine("------------------"); Console.WriteLine($"Analyzed: {obj.Title}"); Console.WriteLine($"Score: {doc.Score}"); Console.WriteLine($"KeyPhrases: {string.Join(", ", keyPhrases)}"); Console.WriteLine("------------------"); return(new { Score = doc.Score, Reference = ThreadSafeReference.Create(obj), KeyPhrases = keyPhrases }); }) .ToArray(); using (var realm = details.GetRealmForWriting()) { var resolved = toUpdate.Select(t => new { Score = t.Score, Object = realm.ResolveReference(t.Reference), KeyPhrases = t.KeyPhrases }) .ToArray(); realm.Write(() => { foreach (var item in resolved) { item.Object.Score = item.Score; item.Object.Tags = string.Join(" ", item.KeyPhrases); } }); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } }
public Task HandleChangeAsync(IChangeDetails details) => Task.CompletedTask;
public Task HandleChangeAsync(IChangeDetails details) => _handleChanges(details);