Skip to content

Commit

Permalink
interactive example
Browse files Browse the repository at this point in the history
  • Loading branch information
wangcheng committed Nov 22, 2024
1 parent 8caf521 commit 11695f9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
64 changes: 64 additions & 0 deletions packages/example-taro/src/TestPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { View, Text, Button } from '@tarojs/components';
import { useLoad } from '@tarojs/taro';
import { Client } from '@connectrpc/connect';
import { ElizaService } from '@buf/connectrpc_eliza.bufbuild_es/connectrpc/eliza/v1/eliza_pb';
import { useState } from 'react';

interface Props {
client: Client<typeof ElizaService>;
}

export default function TestPage({ client }: Props) {
const [isLoading, setIsLoading] = useState(false);
const [messages, setMessages] = useState<{ sentence: string; ts: number }[]>(
[],
);
const appendMessage = (sentence: string) => {
setMessages((prev) => [...prev, { sentence, ts: Date.now() }]);
};

const sayHi = async () => {
setMessages([]);
setIsLoading(true);
client
.say({
sentence: 'I feel happy.',
})
.then((res) => {
setIsLoading(false);
console.log('[connect.say]', res);
appendMessage(res.sentence);
});
};

const introduce = async () => {
setMessages([]);
setIsLoading(true);
const res = client.introduce({ name: 'Joseph' });
for await (const chunk of res) {
console.log('[connect.introduce]', chunk);
appendMessage(chunk.sentence);
}
setIsLoading(false);
};

return (
<View>
<View>
<Button disabled={isLoading} onClick={introduce}>
Introduce
</Button>
<Button disabled={isLoading} onClick={sayHi}>
Say Hi
</Button>
</View>
<View>
{messages.map(({ sentence, ts }) => (
<View key={ts}>
<Text>{sentence}</Text>
</View>
))}
</View>
</View>
);
}
15 changes: 14 additions & 1 deletion packages/example-taro/src/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
export default defineAppConfig({
pages: ['pages/index/index'],
pages: ['pages/grpc-web/index', 'pages/connect/index'],
window: {
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
navigationBarTitleText: 'WeChat',
navigationBarTextStyle: 'black',
},
tabBar: {
position: 'top',
list: [
{
pagePath: 'pages/grpc-web/index',
text: 'GRPC Web',
},
{
pagePath: 'pages/connect/index',
text: 'Connect',
},
],
},
});
20 changes: 20 additions & 0 deletions packages/example-taro/src/pages/connect/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { request, getSystemInfoSync } from '@tarojs/taro';
import { createClient } from '@connectrpc/connect';
import { createConnectTransport } from 'connect-miniprogram/src';
import { ElizaService } from '@buf/connectrpc_eliza.bufbuild_es/connectrpc/eliza/v1/eliza_pb';
import TestPage from '../../TestPage';

const isDevTool = getSystemInfoSync().platform === 'devtools';
const baseUrl = 'https://demo.connectrpc.com';

const grpcTransport = createConnectTransport({
baseUrl,
request,
isDevTool,
});

const grpcClient = createClient(ElizaService, grpcTransport);

export default function Index() {
return <TestPage client={grpcClient} />;
}
20 changes: 20 additions & 0 deletions packages/example-taro/src/pages/grpc-web/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { request, getSystemInfoSync } from '@tarojs/taro';
import { createClient } from '@connectrpc/connect';
import { createGrpcWebTransport } from 'connect-miniprogram/src';
import { ElizaService } from '@buf/connectrpc_eliza.bufbuild_es/connectrpc/eliza/v1/eliza_pb';
import TestPage from '../../TestPage';

const isDevTool = getSystemInfoSync().platform === 'devtools';
const baseUrl = 'https://demo.connectrpc.com';

const grpcTransport = createGrpcWebTransport({
baseUrl,
request,
isDevTool,
});

const grpcClient = createClient(ElizaService, grpcTransport);

export default function Index() {
return <TestPage client={grpcClient} />;
}

0 comments on commit 11695f9

Please sign in to comment.