1919import com .wyjson .router .exception .RouterException ;
2020import com .wyjson .router .utils .TextUtils ;
2121
22+ import java .util .Map ;
23+
2224public class EventCenter {
2325
2426 public static <T > void registerEvent (LifecycleOwner owner , Class <T > type , boolean isForever , @ NonNull Observer <T > observer ) {
@@ -39,7 +41,7 @@ public static <T> void registerEvent(LifecycleOwner owner, Class<T> type, boolea
3941 return ;
4042 }
4143
42- String key = path + "$" + type .getCanonicalName ();
44+ String key = path + "$" + type .getCanonicalName () + "$" + owner ;
4345 MutableLiveData <T > liveData ;
4446 if (Warehouse .events .containsKey (key )) {
4547 liveData = Warehouse .events .get (key );
@@ -70,7 +72,7 @@ public static <T> void unRegisterEvent(LifecycleOwner owner, Class<T> type, Obse
7072 return ;
7173 }
7274
73- String key = path + "$" + type .getCanonicalName ();
75+ String key = path + "$" + type .getCanonicalName () + "$" + owner ;
7476 if (Warehouse .events .containsKey (key )) {
7577 MutableLiveData <T > liveData = Warehouse .events .get (key );
7678 if (liveData != null ) {
@@ -133,18 +135,24 @@ public static <T> void postEvent(String path, T value) {
133135 throw new RouterException ("value cannot be empty!" );
134136 }
135137 String key = path + "$" + value .getClass ().getCanonicalName ();
136- if (Warehouse .events .containsKey (key )) {
137- MutableLiveData <T > liveData = Warehouse .events .get (key );
138- if (liveData != null ) {
139- if (Looper .myLooper () == Looper .getMainLooper ()) {
140- liveData .setValue (value );
138+ boolean isFound = false ;
139+ for (Map .Entry <String , MutableLiveData > entry : Warehouse .events .entrySet ()) {
140+ if (entry .getKey ().startsWith (key )) {
141+ isFound = true ;
142+ MutableLiveData <T > liveData = entry .getValue ();
143+ if (liveData != null ) {
144+ GoRouter .logger .info (null , "[postEvent] send path[" + entry .getKey () + "] value[" + value + "]" );
145+ if (Looper .myLooper () == Looper .getMainLooper ()) {
146+ liveData .setValue (value );
147+ } else {
148+ liveData .postValue (value );
149+ }
141150 } else {
142- liveData . postValue ( value );
151+ GoRouter . logger . error ( null , "[postEvent] LiveData is empty??" );
143152 }
144- } else {
145- GoRouter .logger .error (null , "[postEvent] LiveData is empty??" );
146153 }
147- } else {
154+ }
155+ if (!isFound ) {
148156 GoRouter .logger .warning (null , "[postEvent] No observer was found for this event" );
149157 }
150158 }
0 commit comments