From 873f740886a128248f8a34b18bc836326c75350f Mon Sep 17 00:00:00 2001 From: Emir Rahman Date: Mon, 15 Nov 2021 12:46:14 +0330 Subject: [PATCH 1/3] id added to subscriber now when we use eventbus in list and we need to send public and private events for specefic event, id is useful. --- .../src/org/greenrobot/eventbus/EventBus.java | 49 ++++++++-- .../org/greenrobot/eventbus/Subscription.java | 4 +- .../eventbus/EventBusPostByIdTest.java | 94 +++++++++++++++++++ 3 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 EventBusTest/src/org/greenrobot/eventbus/EventBusPostByIdTest.java diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index aa6c5981..b7f8d1ab 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -15,6 +15,8 @@ */ package org.greenrobot.eventbus; +import android.util.Log; + import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; @@ -139,19 +141,23 @@ public EventBus() { * ThreadMode} and priority. */ public void register(Object subscriber) { + register(subscriber, null); + } + + public void register(Object subscriber, Object id) { Class subscriberClass = subscriber.getClass(); List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass); synchronized (this) { for (SubscriberMethod subscriberMethod : subscriberMethods) { - subscribe(subscriber, subscriberMethod); + subscribe(id, subscriber, subscriberMethod); } } } // Must be called in synchronized block - private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) { + private void subscribe(Object id, Object subscriber, SubscriberMethod subscriberMethod) { Class eventType = subscriberMethod.eventType; - Subscription newSubscription = new Subscription(subscriber, subscriberMethod); + Subscription newSubscription = new Subscription(id, subscriber, subscriberMethod); CopyOnWriteArrayList subscriptions = subscriptionsByEventType.get(eventType); if (subscriptions == null) { subscriptions = new CopyOnWriteArrayList<>(); @@ -253,6 +259,10 @@ public synchronized void unregister(Object subscriber) { /** Posts the given event to the event bus. */ public void post(Object event) { + post(event, null); + } + + public void post(Object event, Object id) { PostingThreadState postingState = currentPostingThreadState.get(); List eventQueue = postingState.eventQueue; eventQueue.add(event); @@ -265,7 +275,7 @@ public void post(Object event) { } try { while (!eventQueue.isEmpty()) { - postSingleEvent(eventQueue.remove(0), postingState); + postSingleEvent(id, eventQueue.remove(0), postingState); } } finally { postingState.isPosting = false; @@ -302,11 +312,15 @@ public void cancelEventDelivery(Object event) { * event of an event's type is kept in memory for future access by subscribers using {@link Subscribe#sticky()}. */ public void postSticky(Object event) { + postSticky(event, null); + } + + public void postSticky(Object event, Object id) { synchronized (stickyEvents) { stickyEvents.put(event.getClass(), event); } // Should be posted after it is putted, in case the subscriber wants to remove immediately - post(event); + post(event, id); } /** @@ -376,7 +390,7 @@ public boolean hasSubscriberForEvent(Class eventClass) { return false; } - private void postSingleEvent(Object event, PostingThreadState postingState) throws Error { + private void postSingleEvent(Object id, Object event, PostingThreadState postingState) throws Error { Class eventClass = event.getClass(); boolean subscriptionFound = false; if (eventInheritance) { @@ -384,10 +398,10 @@ private void postSingleEvent(Object event, PostingThreadState postingState) thro int countTypes = eventTypes.size(); for (int h = 0; h < countTypes; h++) { Class clazz = eventTypes.get(h); - subscriptionFound |= postSingleEventForEventType(event, postingState, clazz); + subscriptionFound |= postSingleEventForEventType(id, event, postingState, clazz); } } else { - subscriptionFound = postSingleEventForEventType(event, postingState, eventClass); + subscriptionFound = postSingleEventForEventType(id, event, postingState, eventClass); } if (!subscriptionFound) { if (logNoSubscriberMessages) { @@ -400,12 +414,13 @@ private void postSingleEvent(Object event, PostingThreadState postingState) thro } } - private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class eventClass) { + private boolean postSingleEventForEventType(Object id, Object event, PostingThreadState postingState, Class eventClass) { CopyOnWriteArrayList subscriptions; synchronized (this) { subscriptions = subscriptionsByEventType.get(eventClass); } if (subscriptions != null && !subscriptions.isEmpty()) { + subscriptions = clearSubscriptionsById(subscriptions, id); for (Subscription subscription : subscriptions) { postingState.event = event; postingState.subscription = subscription; @@ -427,6 +442,22 @@ private boolean postSingleEventForEventType(Object event, PostingThreadState pos return false; } + private CopyOnWriteArrayList clearSubscriptionsById(CopyOnWriteArrayList subscriptions, Object id) { + + Log.e(TAG, "clearSubscriptionsById: "+ subscriptions.size() ); + if (id == null){ + return subscriptions; + } else { + CopyOnWriteArrayList result = new CopyOnWriteArrayList<>(); + for (Subscription s : subscriptions) { + if (s.id == id) { + result.add(s); + } + } + return result; + } + } + private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) { switch (subscription.subscriberMethod.threadMode) { case POSTING: diff --git a/EventBus/src/org/greenrobot/eventbus/Subscription.java b/EventBus/src/org/greenrobot/eventbus/Subscription.java index cc0de1e3..0c94c1ef 100644 --- a/EventBus/src/org/greenrobot/eventbus/Subscription.java +++ b/EventBus/src/org/greenrobot/eventbus/Subscription.java @@ -16,6 +16,7 @@ package org.greenrobot.eventbus; final class Subscription { + final Object id; final Object subscriber; final SubscriberMethod subscriberMethod; /** @@ -24,7 +25,8 @@ final class Subscription { */ volatile boolean active; - Subscription(Object subscriber, SubscriberMethod subscriberMethod) { + Subscription(Object id, Object subscriber, SubscriberMethod subscriberMethod) { + this.id = id; this.subscriber = subscriber; this.subscriberMethod = subscriberMethod; active = true; diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusPostByIdTest.java b/EventBusTest/src/org/greenrobot/eventbus/EventBusPostByIdTest.java new file mode 100644 index 00000000..adec6dff --- /dev/null +++ b/EventBusTest/src/org/greenrobot/eventbus/EventBusPostByIdTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.greenrobot.eventbus; + +import static org.junit.Assert.assertEquals; + +import android.util.Log; + +import org.junit.Test; + +/** + * @author Emir Rahman Muhammadzadeh + */ +public class EventBusPostByIdTest extends AbstractAndroidEventBusTest { + @Test + public void testPostById() throws InterruptedException { + + Object obj1 = new Object() { + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEvent(String event) { + assertEquals("hello First Object", event); + } + }; + + Object obj2 = new Object() { + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEvent(String event) { + assertEquals("hello Second Object", event); + } + }; + + Object obj3 = new Object() { + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEvent(String event) { + assertEquals("hello Third Object", event); + } + }; + + eventBus.register(obj1, 1); + eventBus.register(obj2, 2); + eventBus.register(obj3, 3); + + + eventBus.post("hello First Object", 1); + eventBus.post("hello Second Object", 2); + eventBus.post("hello Third Object", 3); + } + + @Test + public void testPostPublic() throws InterruptedException { + + Object obj1 = new Object() { + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEvent(String event) { + assertEquals("hello everybody", event); + } + }; + + Object obj2 = new Object() { + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEvent(String event) { + assertEquals("hello everybody", event); + } + }; + + Object obj3 = new Object() { + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEvent(String event) { + assertEquals("hello everybody", event); + } + }; + + eventBus.register(obj1, 1); + eventBus.register(obj2, 2); + eventBus.register(obj3, 3); + + eventBus.post("hello everybody"); + + } + +} From b20145b7cb7075d2e8ff5eb9a441a8da6debe0e2 Mon Sep 17 00:00:00 2001 From: Emir Rahman Date: Mon, 15 Nov 2021 12:58:24 +0330 Subject: [PATCH 2/3] log removed --- EventBus/src/org/greenrobot/eventbus/EventBus.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index b7f8d1ab..90b7a652 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -443,8 +443,6 @@ private boolean postSingleEventForEventType(Object id, Object event, PostingThre } private CopyOnWriteArrayList clearSubscriptionsById(CopyOnWriteArrayList subscriptions, Object id) { - - Log.e(TAG, "clearSubscriptionsById: "+ subscriptions.size() ); if (id == null){ return subscriptions; } else { From 56b509d4fe45e99824b85d100856ba7fab5b369f Mon Sep 17 00:00:00 2001 From: Emir Rahman Date: Mon, 15 Nov 2021 12:59:16 +0330 Subject: [PATCH 3/3] log removed --- .../src/org/greenrobot/eventbus/EventBusPostByIdTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusPostByIdTest.java b/EventBusTest/src/org/greenrobot/eventbus/EventBusPostByIdTest.java index adec6dff..66b58f2a 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusPostByIdTest.java +++ b/EventBusTest/src/org/greenrobot/eventbus/EventBusPostByIdTest.java @@ -17,8 +17,6 @@ import static org.junit.Assert.assertEquals; -import android.util.Log; - import org.junit.Test; /**