Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregister request handler #51

Merged
merged 4 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1196,4 +1196,94 @@ public void onResponse(ReadableMap response) {
waitForCountDownToFinishOrFail(countDownLatch);
}

@Test
public void testRequestHandlerRemoval() {
final CountDownLatch countDownLatch = new CountDownLatch(3);
final String requestName = "testRequestHandlerRemoval";

RequestHandlerHandle requestHandle = new RequestHandlerProcessor<>(requestName, None.class, None.class, new ElectrodeBridgeRequestHandler<None, None>() {
@Override
public void onRequest(@Nullable None payload, @NonNull ElectrodeBridgeResponseListener<None> responseListener) {
assertSame(None.NONE, payload);
responseListener.onSuccess(null);
countDownLatch.countDown();
}
}).execute();


new RequestProcessor<None, None>(requestName, null, None.class, new ElectrodeBridgeResponseListener<None>() {
@Override
public void onFailure(@NonNull FailureMessage failureMessage) {
fail();
}

@Override
public void onSuccess(@Nullable None responseData) {
assertSame(None.NONE, responseData);
countDownLatch.countDown();
}
}).execute();

requestHandle.unregister();

new RequestProcessor<None, None>(requestName, null, None.class, new ElectrodeBridgeResponseListener<None>() {
@Override
public void onFailure(@NonNull FailureMessage failureMessage) {
failureMessage.getMessage();
countDownLatch.countDown();
}

@Override
public void onSuccess(@Nullable None responseData) {
fail();
}
}).execute();

waitForCountDownToFinishOrFail(countDownLatch);
}

@Test
public void testRequestHandlerRemovalWithApi() {
final CountDownLatch countDownLatch = new CountDownLatch(3);
final int RESULT_AGE = 10;

RequestHandlerHandle handlerHandle = PersonApi.requests().registerGetAgeRequestHandler(new ElectrodeBridgeRequestHandler<String, Integer>() {
@Override
public void onRequest(@Nullable String payload, @NonNull ElectrodeBridgeResponseListener<Integer> responseListener) {
responseListener.onSuccess(RESULT_AGE);
countDownLatch.countDown();
}
});

PersonApi.requests().getAge("user", new ElectrodeBridgeResponseListener<Integer>() {
@Override
public void onSuccess(@Nullable Integer responseData) {
assertNotNull(responseData);
assertEquals(RESULT_AGE, responseData.intValue());
countDownLatch.countDown();
}

@Override
public void onFailure(@NonNull FailureMessage failureMessage) {
fail();
}
});

handlerHandle.unregister();

PersonApi.requests().getAge("user", new ElectrodeBridgeResponseListener<Integer>() {
@Override
public void onSuccess(@Nullable Integer responseData) {
fail();
}

@Override
public void onFailure(@NonNull FailureMessage failureMessage) {
assertNotNull(failureMessage);
countDownLatch.countDown();
}
});
waitForCountDownToFinishOrFail(countDownLatch);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.walmartlabs.electrode.reactnative.bridge.ElectrodeBridgeResponseListener;
import com.walmartlabs.electrode.reactnative.bridge.FailureMessage;
import com.walmartlabs.electrode.reactnative.bridge.None;
import com.walmartlabs.electrode.reactnative.bridge.RequestHandlerHandle;
import com.walmartlabs.electrode.reactnative.sample.model.Person;
import com.walmartlabs.electrode.reactnative.sample.model.Status;

Expand Down Expand Up @@ -102,22 +103,22 @@ public interface Requests {
*
* @param handler {@link ElectrodeBridgeRequestHandler}
*/
void registerGetPersonRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<None, Person> handler);
RequestHandlerHandle registerGetPersonRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<None, Person> handler);

/**
* Registers a handler that returns the user status when {@link #getStatus(Person, ElectrodeBridgeResponseListener)} is invoked through client(Native or JS side).
*
* @param handler {@link ElectrodeBridgeRequestHandler}
*/
void registerGetStatusRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<Person, Status> handler);
RequestHandlerHandle registerGetStatusRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<Person, Status> handler);

void registerGetAgeRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<String, Integer> handler);
RequestHandlerHandle registerGetAgeRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<String, Integer> handler);

void registerUpdatePersonRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<UpdatePersonRequestData, Person> handler);
RequestHandlerHandle registerUpdatePersonRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<UpdatePersonRequestData, Person> handler);

void registerFindPersonsByStatus(@NonNull final ElectrodeBridgeRequestHandler<List<Status>, List<Person>> handler);
RequestHandlerHandle registerFindPersonsByStatus(@NonNull final ElectrodeBridgeRequestHandler<List<Status>, List<Person>> handler);

void registerFindPersonsAgeByName(@NonNull final ElectrodeBridgeRequestHandler<List<String>, List<Integer>> handler);
RequestHandlerHandle registerFindPersonsAgeByName(@NonNull final ElectrodeBridgeRequestHandler<List<String>, List<Integer>> handler);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.walmartlabs.electrode.reactnative.bridge.ElectrodeBridgeRequestHandler;
import com.walmartlabs.electrode.reactnative.bridge.ElectrodeBridgeResponseListener;
import com.walmartlabs.electrode.reactnative.bridge.None;
import com.walmartlabs.electrode.reactnative.bridge.RequestHandlerHandle;
import com.walmartlabs.electrode.reactnative.bridge.RequestHandlerProcessor;
import com.walmartlabs.electrode.reactnative.bridge.RequestProcessor;
import com.walmartlabs.electrode.reactnative.sample.model.Person;
Expand All @@ -42,33 +43,33 @@ final class PersonRequests implements PersonApi.Requests {
}

@Override
public void registerGetPersonRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<None, Person> handler) {
new RequestHandlerProcessor<>(REQUEST_GET_PERSON, None.class, Person.class, handler).execute();
public RequestHandlerHandle registerGetPersonRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<None, Person> handler) {
return new RequestHandlerProcessor<>(REQUEST_GET_PERSON, None.class, Person.class, handler).execute();
}

@Override
public void registerGetStatusRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<Person, Status> handler) {
new RequestHandlerProcessor<>(REQUEST_GET_STATUS, Person.class, Status.class, handler).execute();
public RequestHandlerHandle registerGetStatusRequestHandler(@NonNull final ElectrodeBridgeRequestHandler<Person, Status> handler) {
return new RequestHandlerProcessor<>(REQUEST_GET_STATUS, Person.class, Status.class, handler).execute();
}

@Override
public void registerGetAgeRequestHandler(@NonNull ElectrodeBridgeRequestHandler<String, Integer> handler) {
new RequestHandlerProcessor<>(REQUEST_GET_AGE, String.class, Integer.class, handler).execute();
public RequestHandlerHandle registerGetAgeRequestHandler(@NonNull ElectrodeBridgeRequestHandler<String, Integer> handler) {
return new RequestHandlerProcessor<>(REQUEST_GET_AGE, String.class, Integer.class, handler).execute();
}

@Override
public void registerUpdatePersonRequestHandler(@NonNull ElectrodeBridgeRequestHandler<UpdatePersonRequestData, Person> handler) {
new RequestHandlerProcessor<>(REQUEST_POST_PERSON_UPDATE, UpdatePersonRequestData.class, Person.class, handler).execute();
public RequestHandlerHandle registerUpdatePersonRequestHandler(@NonNull ElectrodeBridgeRequestHandler<UpdatePersonRequestData, Person> handler) {
return new RequestHandlerProcessor<>(REQUEST_POST_PERSON_UPDATE, UpdatePersonRequestData.class, Person.class, handler).execute();
}

@Override
public void registerFindPersonsByStatus(@NonNull ElectrodeBridgeRequestHandler<List<Status>, List<Person>> handler) {
new RequestHandlerProcessor<>(REQUEST_FIND_PERSONS_BY_STATUS, (Class) Status.class, (Class) Person.class, handler).execute();
public RequestHandlerHandle registerFindPersonsByStatus(@NonNull ElectrodeBridgeRequestHandler<List<Status>, List<Person>> handler) {
return new RequestHandlerProcessor<>(REQUEST_FIND_PERSONS_BY_STATUS, (Class) Status.class, (Class) Person.class, handler).execute();
}

@Override
public void registerFindPersonsAgeByName(@NonNull ElectrodeBridgeRequestHandler<List<String>, List<Integer>> handler) {
new RequestHandlerProcessor<>(REQUEST_FIND_PERSONS_AGE_BY_NAME, (Class) String.class, (Class) Integer.class, handler).execute();
public RequestHandlerHandle registerFindPersonsAgeByName(@NonNull ElectrodeBridgeRequestHandler<List<String>, List<Integer>> handler) {
return new RequestHandlerProcessor<>(REQUEST_FIND_PERSONS_AGE_BY_NAME, (Class) String.class, (Class) Integer.class, handler).execute();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @param <T> Event payload type: Accepted types are Primitive wrappers and {@link Bridgeable}
*/

public class EventListenerProcessor<T> implements Processor {
public class EventListenerProcessor<T> {
private static final String TAG = EventListenerProcessor.class.getSimpleName();

private final String eventName;
Expand All @@ -44,7 +44,6 @@ public EventListenerProcessor(@NonNull String eventName, @NonNull final Class<T>
}

@SuppressWarnings("unchecked")
@Override
public UUID execute() {
ElectrodeBridgeEventListener<ElectrodeBridgeEvent> intermediateEventListener = new ElectrodeBridgeEventListener<ElectrodeBridgeEvent>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @param <T> eventPayload
*/

public class EventProcessor<T> implements Processor {
public class EventProcessor<T> {
private static final String TAG = EventProcessor.class.getSimpleName();

private final T eventPayload;
Expand All @@ -40,12 +40,11 @@ public EventProcessor(@NonNull String eventName, @Nullable T eventPayload) {
this.eventName = eventName;
}

@Override
public UUID execute() {
@SuppressWarnings("unchecked")
public void execute() {
Logger.d(TAG, "EventProcessor is emitting event(%s), with payload(%s)", eventName, eventPayload);
ElectrodeBridgeHolder.emitEvent(new ElectrodeBridgeEvent.Builder(eventName)
.withData(eventPayload)
.build());
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 WalmartLabs
* Copyright 2019 WalmartLabs

* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,16 @@

package com.walmartlabs.electrode.reactnative.bridge;

import java.util.UUID;

public interface Processor {
UUID execute();
/**
* Interface that is returned by the request handler processors when a request is being registered.
* <p>
* Use this handle to properly unregister the request handler when not in use.
*/
public interface RequestHandlerHandle {
/**
* Unregisters a request handler.
*
* @return
*/
boolean unregister();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
* @param <TReq>
* @param <TResp>
*/
public class RequestHandlerProcessor<TReq, TResp> implements Processor {
public class RequestHandlerProcessor<TReq, TResp> implements RequestHandlerHandle {
private final String TAG = RequestHandlerProcessor.class.getSimpleName();

private final String requestName;
private final Class<TReq> reqClazz;
private final Class<TResp> respClazz;
private final ElectrodeBridgeRequestHandler<TReq, TResp> handler;
private ElectrodeBridgeRequestHandler<TReq, TResp> handler;
private UUID id;
private ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object> intermediateRequestHandler;

public RequestHandlerProcessor(@NonNull String requestName, @NonNull Class<TReq> reqClazz, @NonNull Class<TResp> respClazz, @NonNull ElectrodeBridgeRequestHandler<TReq, TResp> handler) {
this.requestName = requestName;
Expand All @@ -48,9 +50,8 @@ public RequestHandlerProcessor(@NonNull String requestName, @NonNull Class<TReq>
}

@SuppressWarnings("unchecked")
@Override
public UUID execute() {
final ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object> intermediateRequestHandler = new ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object>() {
public RequestHandlerHandle execute() {
intermediateRequestHandler = new ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object>() {

@Override
public void onRequest(@Nullable ElectrodeBridgeRequest bridgeRequest, @NonNull final ElectrodeBridgeResponseListener<Object> responseListener) {
Expand Down Expand Up @@ -84,6 +85,21 @@ public void onSuccess(TResp obj) {
}
};

return ElectrodeBridgeHolder.registerRequestHandler(requestName, intermediateRequestHandler);
id = ElectrodeBridgeHolder.registerRequestHandler(requestName, intermediateRequestHandler);
return this;
}

@Override
public boolean unregister() {
Logger.d(TAG, "Removing registered request handler %s with id %s", handler, id);
ElectrodeBridgeRequestHandler removedHandler = ElectrodeBridgeHolder.unregisterRequestHandler(id);
if (handler != null && intermediateRequestHandler == removedHandler) {
intermediateRequestHandler = null;
handler = null;
return true;
} else {
Logger.w(TAG, "Not able to unregister a request handler. This is not normal as the request handle should have proper reference of the registered handler. ");
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import com.walmartlabs.electrode.reactnative.bridge.helpers.Logger;
import com.walmartlabs.electrode.reactnative.bridge.util.BridgeArguments;

import java.util.UUID;

/**
* This class takes care of processing a given request when {@link #execute()} is called on the instance.
*
* @param <TReq>
* @param <TResp>
*/
public class RequestProcessor<TReq, TResp> implements Processor {
public class RequestProcessor<TReq, TResp> {
private final String TAG = RequestProcessor.class.getSimpleName();

private final String requestName;
Expand All @@ -52,8 +50,7 @@ public RequestProcessor(@NonNull String requestName, @Nullable TReq requestPaylo
}

@SuppressWarnings("unchecked")
@Override
public UUID execute() {
public void execute() {
Logger.d(TAG, "Request processor started processing request(%s)", requestName);
ElectrodeBridgeRequest req = new ElectrodeBridgeRequest.Builder(requestName)
.withData(requestPayload)
Expand Down Expand Up @@ -82,6 +79,5 @@ public void onSuccess(@Nullable ElectrodeBridgeResponse bridgeResponse) {
responseListener.onSuccess(response);
}
});
return null;
}
}