From a7bfeae4450d7d4aecc7499f894b90eea40480f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 5 Nov 2014 21:01:44 +0100 Subject: [PATCH] Only send events after the new Sone has been stored in the database. --- src/main/java/net/pterodactylus/sone/core/Core.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 13076b8..97182c2 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -25,6 +25,7 @@ import static java.lang.String.format; import static java.util.logging.Level.WARNING; import static java.util.logging.Logger.getLogger; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -864,6 +865,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone)); return; } + final List events = new ArrayList(); SoneChangeDetector soneChangeDetector = new SoneChangeDetector(storedSone.get()); soneChangeDetector.onNewPosts(new PostProcessor() { @Override @@ -871,14 +873,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (post.getTime() < getSoneFollowingTime(sone)) { post.setKnown(true); } else if (!post.isKnown()) { - eventBus.post(new NewPostFoundEvent(post)); + events.add(new NewPostFoundEvent(post)); } } }); soneChangeDetector.onRemovedPosts(new PostProcessor() { @Override public void processPost(Post post) { - eventBus.post(new PostRemovedEvent(post)); + events.add(new PostRemovedEvent(post)); } }); soneChangeDetector.onNewPostReplies(new PostReplyProcessor() { @@ -887,18 +889,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (postReply.getTime() < getSoneFollowingTime(sone)) { postReply.setKnown(true); } else if (!postReply.isKnown()) { - eventBus.post(new NewPostReplyFoundEvent(postReply)); + events.add(new NewPostReplyFoundEvent(postReply)); } } }); soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() { @Override public void processPostReply(PostReply postReply) { - eventBus.post(new PostReplyRemovedEvent(postReply)); + events.add(new PostReplyRemovedEvent(postReply)); } }); soneChangeDetector.detectChanges(sone); database.storeSone(sone); + for (Object event : events) { + eventBus.post(event); + } sone.setOptions(storedSone.get().getOptions()); sone.setKnown(storedSone.get().isKnown()); sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); -- 2.7.4