I am a Dev
I am a Dev
Explore posts from servers
TtRPC
Created by I am a Dev on 10/14/2023 in #❓-help
Hello, is there any way to create a base router with common procedures? like an interface
I'm using electron-trpc, so basically the router is like a store, so I can use all the node logic in the main process, but subscribe to the state in the renderer. And because I have more than 30 store like this, want to create common hooks and interfaces
8 replies
TtRPC
Created by I am a Dev on 10/14/2023 in #❓-help
Hello, is there any way to create a base router with common procedures? like an interface
But i want to only pass the router as a argument, because i want to return more properties that only the state
8 replies
TtRPC
Created by I am a Dev on 10/14/2023 in #❓-help
Hello, is there any way to create a base router with common procedures? like an interface
I have something like this:
import { useEffect, useState } from 'react';
import type { Unsubscribable } from '@trpc/server/observable';
import type {} from '@trpc/client/src/';

interface UseRouterStateOptions<TState> {
stateSubscribe: (
input: undefined,
opts: { onData: (state: TState) => void },
) => Unsubscribable;
}

export function useRouterState<TState>(
opts: UseRouterStateOptions<TState>,
): TState | null {
const [state, setState] = useState<TState | null>(null);

useEffect(() => {
const stateSubscription = opts.stateSubscribe(undefined, {
onData: (data) => {
setState(data);
},
});

return () => {
stateSubscription.unsubscribe();
};
}, []);

return state;
}
import { useEffect, useState } from 'react';
import type { Unsubscribable } from '@trpc/server/observable';
import type {} from '@trpc/client/src/';

interface UseRouterStateOptions<TState> {
stateSubscribe: (
input: undefined,
opts: { onData: (state: TState) => void },
) => Unsubscribable;
}

export function useRouterState<TState>(
opts: UseRouterStateOptions<TState>,
): TState | null {
const [state, setState] = useState<TState | null>(null);

useEffect(() => {
const stateSubscription = opts.stateSubscribe(undefined, {
onData: (data) => {
setState(data);
},
});

return () => {
stateSubscription.unsubscribe();
};
}, []);

return state;
}
And in the widget I should use this:
const state = useRouterState({
stateSubscribe:
tRPCClient.router1.router2.state.subscribe,
});
const state = useRouterState({
stateSubscribe:
tRPCClient.router1.router2.state.subscribe,
});
8 replies
TtRPC
Created by I am a Dev on 9/30/2023 in #❓-help
How to return a stream? like open ai api
Thanks, I will check it. Meantime, how can I make each subscription return a different value for each request? for example, each chat completion request must be different for each user, and only each user can listen the response. Thanks
5 replies