Skip to content
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
28 changes: 12 additions & 16 deletions android/src/main/java/com/reactnativegrpc/GrpcModule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.reactnativegrpc;
import android.util.Log;
import android.widget.Toast;

import android.util.Base64;
import android.util.Log;
import android.widget.Toast;
Expand Down Expand Up @@ -42,11 +41,10 @@ public class GrpcModule extends ReactContextBaseJavaModule {
private boolean keepAliveEnabled = false;
private Integer keepAliveTime;
private Integer keepAliveTimeout;
private boolean isUiLogEnabled = false;

private ManagedChannel managedChannel = null;

private boolean isUiLogEnabled = false;

public GrpcModule(ReactApplicationContext context) {
this.context = context;
}
Expand All @@ -70,34 +68,29 @@ public void getIsInsecure(final Promise promise) {
@ReactMethod
public void setHost(String host) {
this.host = host;
this.handleOptionsChanged();
}

@ReactMethod
public void setInsecure(boolean insecure) {
this.isInsecure = insecure;
this.handleOptionsChanged();
}

@ReactMethod
public void setCompression(Boolean enable, String compressorName, String limit) {
public void setCompression(Boolean enable, String compressorName) {
this.withCompression = enable;
this.compressorName = compressorName;
this.handleOptionsChanged();
}

@ReactMethod
public void setResponseSizeLimit(int limit) {
this.responseSizeLimit = limit;
this.handleOptionsChanged();
}

@ReactMethod
public void setKeepalive(boolean enabled, int time, int timeout) {
public void setKeepAlive(boolean enabled, int time, int timeout) {
this.keepAliveEnabled = enabled;
this.keepAliveTime = time;
this.keepAliveTimeout = timeout;
this.handleOptionsChanged();
}

@ReactMethod
Expand Down Expand Up @@ -333,13 +326,12 @@ private static String normalizePath(String path) {
if (path.startsWith("/")) {
path = path.substring(1);
}

return path;
}

private void handleOptionsChanged() {
@ReactMethod
public void initGrpcChannel() {
if (this.managedChannel != null) {
this.managedChannel.resetConnectBackoff();
this.managedChannel.shutdown();
}
this.managedChannel = createManagedChannel();
Expand All @@ -360,7 +352,7 @@ private ManagedChannel createManagedChannel() {
channelBuilder = channelBuilder
.keepAliveWithoutCalls(true)
.keepAliveTime(keepAliveTime, TimeUnit.SECONDS)
.keepAliveTimeout(keepAliveTime, TimeUnit.SECONDS);
.keepAliveTimeout(keepAliveTimeout, TimeUnit.SECONDS);
}

managedChannel = channelBuilder.build();
Expand All @@ -369,7 +361,11 @@ private ManagedChannel createManagedChannel() {

@ReactMethod
public void resetConnection(final String message){
handleOptionsChanged();
if(null == managedChannel) return;

this.managedChannel.resetConnectBackoff();

this.initGrpcChannel();

showToast("resetConnection "+message);
}
Expand Down
117 changes: 46 additions & 71 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbortController, AbortSignal } from 'abort-controller';
import { fromByteArray, toByteArray } from 'base64-js';
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import {NativeEventEmitter, NativeModules, Platform} from 'react-native';
import { GrpcError } from './errors';
import {
GrpcServerStreamingCall,
Expand All @@ -14,18 +14,14 @@ type GrpcRequestObject = {
};

type GrpcType = {

getHost: () => Promise<string>;
getIsInsecure: () => Promise<boolean>;
setHost(host: string): void;
setInsecure(insecure: boolean): void;
setCompression(enable: boolean, compressorName: string, limit?: string): void;
setKeepalive(enabled: boolean, time: number, timeout: number): void;
setCompression(enable: boolean, compressorName: string): void;
setResponseSizeLimit(limitInBytes: number): void;
resetConnection(message: string): void;
setKeepAliveTime(keepAliveTime: number): void;
onConnectionStateChange(): void;
setUiLogEnabled(enable: boolean): void;
enterIdle(): void;
initGrpcChannel(): void;
unaryCall(
id: number,
path: string,
Expand All @@ -46,6 +42,11 @@ type GrpcType = {
requestHeaders?: GrpcMetadata
): Promise<void>;
finishClientStreaming(id: number): Promise<void>;
resetConnection(message: string):void;
setKeepAlive(enable : boolean,keepAliveTime: number,keepAliveTimeOut: number):void;
onConnectionStateChange():void;
setUiLogEnabled(enable:boolean):void;
enterIdle():void;
};

type GrpcEventType = 'response' | 'error' | 'headers' | 'trailers';
Expand All @@ -56,20 +57,20 @@ type GrpcEventPayload =
type: 'response';
payload: string;
} | {
type: 'error';
error: string;
code?: number;
trailers?: GrpcMetadata;
} | {
type: 'headers';
payload: GrpcMetadata;
} | {
type: 'trailers';
payload: GrpcMetadata;
} | {
type: 'status';
payload: number;
};
type: 'error';
error: string;
code?: number;
trailers?: GrpcMetadata;
} | {
type: 'headers';
payload: GrpcMetadata;
} | {
type: 'trailers';
payload: GrpcMetadata;
} | {
type: 'status';
payload: number;
};

type GrpcEvent = {
id: number;
Expand Down Expand Up @@ -145,13 +146,18 @@ function handleGrpcEvent(event: GrpcEvent) {
case 'trailers':
deferred.trailers?.resolve(event.payload);
deferred.data?.notifyComplete();

delete deferredMap[event.id];
break;
case 'error':
const error = new GrpcError(event.error, event.code, event.trailers);

deferred.headers?.reject(error);
deferred.trailers?.reject(error);
deferred.response?.reject(error);
deferred.data?.noitfyError(error);

delete deferredMap[event.id];
break;
}
}
Expand Down Expand Up @@ -180,72 +186,41 @@ export class GrpcClient {
setInsecure(insecure: boolean): void {
Grpc.setInsecure(insecure);
}

/**
* setCompression - only for Android.
* @param enable
* @param compressorName
* @param limit
*/
setCompression(
enable: boolean,
compressorName: string,
limit?: number
): void {
if (!this.isAndroid()) {
return;
}
Grpc.setCompression(enable, compressorName, limit?.toString());
}
setKeepalive(enabled: boolean, time: number, timeout: number): void {
Grpc.setKeepalive(enabled, time, timeout);
setCompression(enable: boolean, compressorName: string): void {
Grpc.setCompression(enable, compressorName);
}
setResponseSizeLimit(limitInBytes: number): void {
Grpc.setResponseSizeLimit(limitInBytes);
}

/**
* resetConnection - only for Android
* @param message - to debug where we are calling 'resetConnection'
* */
initGrpcChannel(){
Grpc.initGrpcChannel();
};

setKeepAlive(enable : boolean,keepAliveTime: number,keepAliveTimeOut: number): void {
Grpc.setKeepAlive(enable,keepAliveTime,keepAliveTimeOut);
}

resetConnection(message: string): void {
if (!this.isAndroid()) {
return;
}
if(!this.isAndroid()) return;
Grpc.resetConnection(message);
}
/**
* setUiLogEnabled - only for Android
* @param enable - to debug where we are calling current grpc connection status
* in ui
* */
setUiLogEnabled(enable: boolean): void {
if (!this.isAndroid()) {
return;
}
if(!this.isAndroid()) return;
Grpc.setUiLogEnabled(enable);
}
/**
* onConnectionStateChange - only for Android
* grpc connection different state
* * */

onConnectionStateChange(): void {
if (!this.isAndroid()) {
return;
}
if(!this.isAndroid()) return;
Grpc.onConnectionStateChange();
}
/**
* enterIdle - only for Android
* set grpc connection state to idle
* * */

enterIdle(): void {
if (!this.isAndroid()) {
return;
}
if(!this.isAndroid()) return;
Grpc.enterIdle();
}


unaryCall(
method: string,
data: Uint8Array,
Expand Down Expand Up @@ -340,7 +315,7 @@ export class GrpcClient {
return call;
}

private isAndroid(): Boolean {
private isAndroid() : Boolean {
return Platform.OS === 'android';
}
}
Expand Down