Skip to content

Commit

Permalink
feat: add react-native streams support (#55)
Browse files Browse the repository at this point in the history
This PR adds the `stream` function to the React-Native gRPC transport.
Because the `fetch` API is not fully implemented in React-Native (see
issue [here](facebook/react-native#27741)), I
had to polyfill the missing `fetch` function (package
[here](https://github.com/react-native-community/fetch)) and replaced
some functions in the global namespace (see
[here](https://github.com/acostalima/react-native-polyfill-globals)).

On Android, text-streaming doesn't work out-of-the-box on the `debug`
variant. One solution is to comment the `NetworkFlipperPlugin` (see
[here](react-native-community/fetch#13 (comment))).
Because we don't use it for the moment, I commented that plugin.

I added a `HelloStream` API function to test the stream feature.
We can call it in Reac-Native by doing this:
```typescript
for await (const res of clientInstance.helloStream(new HelloStreamRequest({ name: 'd4ryl00' }))) {
  console.log(res.greeting);
}
```

Signed-off-by: D4ryl00 <[email protected]>
  • Loading branch information
D4ryl00 authored Oct 24, 2023
1 parent 29a8608 commit 99510ab
Show file tree
Hide file tree
Showing 26 changed files with 1,577 additions and 315 deletions.
8 changes: 4 additions & 4 deletions gen.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ceb2922aa6e25cc4b9256163c5a31443770af3a6 Makefile
3f8e7619fa38e476bb3d35923f6c2676cfe8809b buf.gen.yaml
8e0d611d49279bc49aa043c5f518326b50dfd76c service/gnomobiletypes/gnomobiletypes.go
50596a8d996f91253ef714fe24b618f05a6f179a service/gnomobiletypes/package.go
cc43128277f99518087578f22e7e56a3de0c0f1f service/rpc/gnomobiletypes.proto
5cf0c76d7a484f82c7c56834983dba63286916f7 service/rpc/rpc.proto
781263c443b6ffebe091128855ce2a8b594fd8ea service/gnomobiletypes/gnomobiletypes.go
c4488aa6a301b2865856719295d704c77ff45512 service/gnomobiletypes/package.go
646d974f3a99753ed94a3d8ba4affb2a0267d3f2 service/rpc/gnomobiletypes.proto
7b283a24b6c14a7e8907d5b82c95f6ffec4b0332 service/rpc/rpc.proto
9 changes: 7 additions & 2 deletions gnoboard/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import CustomRouter from '@gno/router/custom-router';
import 'fast-text-encoding';
import CustomRouter from "@gno/router/custom-router";
import "react-native-polyfill-globals/auto";

// Polyfill async.Iterator. For some reason, the Babel presets and plugins are not doing the trick.
// Code from here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-3.html#caveats
(Symbol as any).asyncIterator = Symbol.asyncIterator ||
Symbol.for("Symbol.asyncIterator");

function App() {
return <CustomRouter />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ public static void initializeFlipper(Context context, ReactInstanceManager react
client.addPlugin(new SharedPreferencesFlipperPlugin(context));
client.addPlugin(CrashReporterPlugin.getInstance());

NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin();
NetworkingModule.setCustomClientBuilder(
new NetworkingModule.CustomClientBuilder() {
@Override
public void apply(OkHttpClient.Builder builder) {
builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
}
});
client.addPlugin(networkFlipperPlugin);
// Need to comment this code to allow gRPC streams to work: https://github.com/react-native-community/fetch/issues/13#issuecomment-1703097655
// NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin();
// NetworkingModule.setCustomClientBuilder(
// new NetworkingModule.CustomClientBuilder() {
// @Override
// public void apply(OkHttpClient.Builder builder) {
// builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
// }
// });
// client.addPlugin(networkFlipperPlugin);
client.start();

// Fresco Plugin needs to ensure that ImagePipelineFactory is initialized
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit 99510ab

Please sign in to comment.