https://github.com/lesomnus/grpc-concierge
gRPC tranport for caching, deduplication, and managing life cycle
https://github.com/lesomnus/grpc-concierge
cache deduplication grpc grpc-typescript react
Last synced: about 1 month ago
JSON representation
gRPC tranport for caching, deduplication, and managing life cycle
- Host: GitHub
- URL: https://github.com/lesomnus/grpc-concierge
- Owner: lesomnus
- License: apache-2.0
- Created: 2024-01-22T16:12:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-17T17:04:17.000Z (almost 2 years ago)
- Last Synced: 2025-03-22T19:27:31.533Z (about 1 year ago)
- Topics: cache, deduplication, grpc, grpc-typescript, react
- Language: TypeScript
- Homepage:
- Size: 164 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gcpc-concierge
gRPC transport on top of [`timostamm/protobuf-ts`](https://github.com/timostamm/protobuf-ts) that helps you to cache, deduplicate, and abort the request.
## Usage
> Currently only unary call is supported.
### Cache and Deduplication
```ts
import { GrpcWebFetchTransport } from '@protobuf-ts/grpcweb-transport'
import { Cacher, Hitchhiker } from 'grpc-concierge'
import { IEchoClient } from './pb/echo.client'
const transport = new GrpcWebFetchTransport({
baseUrl: 'https://example.com',
interceptors: [
new Cacher(({ byKey })=>({
// Cached value will be returned if `msg.value` is same.
hello: byKey(msg => msg.value)
})),
new Hitchhiker(({ byKey }) => ({
// Request that has the same `msg.value` are processed as a single request.
hello: byKey(msg => msg.value)
})),
],
})
```
### React Hook
- Re-rendering the component when the dependent RPC succeeds.
- Abort RPCs used when the component unmounted.
```tsx
import { createServiceContext } from 'grpc-concierge/react'
import { EchoClient } from './pb/echo.client'
const [ctx, useService] = createServiceContext({
echo: EchoClient
}, {
// `echo.hello` depends on `echo.hola`.
echo: {
hello: {
echo: ['hola']
}
}
})
// This component rerendered whenever `svc.echo.hola` succeeds.
function EchoComponent(){
const svc = useService()
svc.echo.hello()
return <>...>
}
```
You can see more examples at test.
- [`core/Cacher`](/src/cacher.test.ts) - caches the responses.
- [`core/Hitchhiker`](/src/hitchhiker.test.ts) - deduplicates the requests.
- [`react/createServiceContext`](/src/react/ctx.test.tsx) - manages client life cycle.