Skip to content

Commit

Permalink
Fix initialization issue by adding GnokeyProvider (#138)
Browse files Browse the repository at this point in the history
This pull request fixes an issue with multiple initializations and adds
a new component called GnokeyProvider. The GnokeyProvider component
allows for the initialization and management of Gnokey.

---------

Signed-off-by: D4ryl00 <[email protected]>
Signed-off-by: Iuri Pereira <[email protected]>
Co-authored-by: D4ryl00 <[email protected]>
  • Loading branch information
iuricmp and D4ryl00 authored May 13, 2024
1 parent 550beef commit 7a95b89
Show file tree
Hide file tree
Showing 5 changed files with 525 additions and 15 deletions.
4 changes: 2 additions & 2 deletions expo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ before continuing.
You can run one of the following command:

```shell
make pack
make npm.pack
```

or

```shell
make publish
make npm.publish
```
30 changes: 23 additions & 7 deletions expo/example/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import * as Gnonative from '@gnolang/gnonative';
import { GnokeyProvider, useGnokeyContext } from '@gnolang/gnonative';
import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';

const config = {
remote: 'https://gno.berty.io',
chain_id: 'dev',
};

export default function App() {
const gno = Gnonative.useGno();
return (
<GnokeyProvider config={config}>
<InnerApp />
</GnokeyProvider>
);
}

const InnerApp = () => {
const gno = useGnokeyContext();
const [greeting, setGreeting] = useState('');

useEffect(() => {
const greeting = async () => {
(async () => {
try {
const accounts = await gno.listKeyInfo();
console.log(accounts);

setGreeting(await gno.hello('Gno'));

for await (const res of await gno.helloStream('Gno')) {
Expand All @@ -17,16 +33,16 @@ export default function App() {
} catch (error) {
console.log(error);
}
};
greeting();
})();
}, []);

return (
<View style={styles.container}>
<Text>Hey {greeting}</Text>
<Text>Gnonative App</Text>
<Text>{greeting}</Text>
</View>
);
}
};

const styles = StyleSheet.create({
container: {
Expand Down
20 changes: 14 additions & 6 deletions expo/src/hooks/use-gno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,18 @@ export interface GnoResponse {
helloStream: (name: string) => Promise<AsyncIterable<HelloStreamResponse>>;
}

enum Status {
Stopped,
Starting,
Started,
}

let clientInstance: PromiseClient<typeof GnoNativeService> | undefined = undefined;
let bridgeInstance: boolean = false;
let bridgeStatus: Status = Status.Stopped;

export const useGno = (): GnoResponse => {
const getClient = async () => {
if (!bridgeInstance) {
if (bridgeStatus === Status.Stopped) {
await initBridge();
}

Expand All @@ -127,20 +133,22 @@ export const useGno = (): GnoResponse => {
};

const closeBridge = async () => {
if (bridgeInstance) {
if (bridgeStatus !== Status.Stopped) {
console.log('Closing bridge...');
await GoBridge.closeBridge();
console.log('Bridge closed.');
bridgeInstance = false;
bridgeStatus = Status.Stopped;
clientInstance = undefined;
}
};

const initBridge = async () => {
if (!bridgeInstance) {
if (bridgeStatus === Status.Stopped) {
console.log('Initializing bridge...');
bridgeStatus = Status.Starting;
await GoBridge.initBridge();
console.log('Bridge initialized.');
bridgeInstance = true;
bridgeStatus = Status.Started;
}
};

Expand Down
1 change: 1 addition & 0 deletions expo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export function addChangeListener(listener: (event: ChangeEventPayload) => void)

export { ChangeEventPayload, GnonativeView, GnonativeViewProps };
export { useGno } from './hooks/use-gno';
export * from './provider/gnokey-provider';
export * from '@buf/gnolang_gnonative.bufbuild_es/gnonativetypes_pb';
Loading

0 comments on commit 7a95b89

Please sign in to comment.