Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
- fix bug: HandlerPipe, remove weak reference of 'Dispatcher'
- fix bug: MessengerPipe, remove weak reference of 'Dispatcher'
  • Loading branch information
jrfeng committed Jun 28, 2020
1 parent c6aca3a commit 6010a87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
16 changes: 8 additions & 8 deletions pipe/src/main/java/channel/helper/pipe/HandlerPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class HandlerPipe extends Handler implements Emitter {
private static final String TAG = "HandlerPipe";
private final WeakReference<Dispatcher> mDispatcherWeakReference;
private final Dispatcher mDispatcher;

/**
* Use Main Looper
Expand All @@ -27,7 +27,12 @@ public HandlerPipe(Dispatcher dispatcher) {

public HandlerPipe(Looper looper, Dispatcher dispatcher) {
super(looper);
mDispatcherWeakReference = new WeakReference<>(dispatcher);

if (dispatcher == null) {
throw new IllegalArgumentException("param 'dispatcher' is not null.");
}

mDispatcher = dispatcher;
}

@Override
Expand All @@ -39,12 +44,7 @@ public void emit(Map<String, Object> data) {

@Override
public void handleMessage(@NonNull Message msg) {
Dispatcher dispatcher = mDispatcherWeakReference.get();
if (dispatcher == null) {
return;
}

dispatcher.dispatch(getData(msg));
mDispatcher.dispatch(getData(msg));
}

@SuppressWarnings("unchecked cast")
Expand Down
30 changes: 8 additions & 22 deletions pipe/src/main/java/channel/helper/pipe/MessengerPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ public class MessengerPipe extends Handler implements Emitter {
private static final String TAG = "MessengerPipe";

private Messenger mMessenger;

private WeakReference<Dispatcher> mDispatcherWeakReference;
private boolean mDispatcher;
private Dispatcher mDispatcher;

public MessengerPipe(IBinder binder) {
mDispatcher = false;
mMessenger = new Messenger(binder);
}

Expand All @@ -75,28 +72,22 @@ public MessengerPipe(Dispatcher dispatcher) {

public MessengerPipe(Looper looper, Dispatcher dispatcher) {
super(looper);
mDispatcher = true;

if (dispatcher == null) {
throw new IllegalArgumentException("param 'dispatcher' is not null.");
}

mMessenger = new Messenger(this);
mDispatcherWeakReference = new WeakReference<>(dispatcher);
mDispatcher = dispatcher;
}

@Override
public void handleMessage(@NonNull Message msg) {
Dispatcher dispatcher = mDispatcherWeakReference.get();
if (dispatcher == null) {
return;
}

dispatcher.dispatch(getData(msg));
mDispatcher.dispatch(getData(msg));
}

@Override
public void emit(Map<String, Object> data) {
if (mDispatcher) {
Log.e(TAG, "The current MessengerPipe can only be a dispatcher.");
return;
}

Message message = Message.obtain();
message.obj = new MapWrapper(data);

Expand All @@ -108,11 +99,6 @@ public void emit(Map<String, Object> data) {
}

private Map<String, Object> getData(Message dataWrapper) {
if (!mDispatcher) {
Log.e(TAG, "The current MessengerPipe can only be a emitter.");
return new HashMap<>();
}

if (dataWrapper.obj == null) {
Log.d(TAG, "dataWrapper is empty.");
return new HashMap<>();
Expand Down

0 comments on commit 6010a87

Please sign in to comment.