grpc-concierge

0.1.2 • Public • Published

gcpc-concierge

gRPC transport on top of timostamm/protobuf-ts that helps you to cache, deduplicate, and abort the request.

Usage

Currently only unary call is supported.

Cache and Deduplication

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<IEchoClient>(({ byKey })=>({
			// Cached value will be returned if `msg.value` is same.
			hello: byKey(msg => msg.value)
		})),
		new Hitchhiker<IEchoClient>(({ 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.
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.

Readme

Keywords

none

Package Sidebar

Install

npm i grpc-concierge

Weekly Downloads

121

Version

0.1.2

License

Apache-2.0

Unpacked Size

79.2 kB

Total Files

54

Last publish

Collaborators

  • lesomnus