batch zap requests (#1424)

This commit is contained in:
Keyan 2024-09-24 09:38:48 -05:00 committed by GitHub
parent b82641d1bd
commit 76218dccac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import { usePaidMutation } from './use-paid-mutation'
import { ACT_MUTATION } from '@/fragments/paidAction' import { ACT_MUTATION } from '@/fragments/paidAction'
import { meAnonSats } from '@/lib/apollo' import { meAnonSats } from '@/lib/apollo'
import { BoostItemInput } from './adv-post-form' import { BoostItemInput } from './adv-post-form'
import { useWallet } from '../wallets'
const defaultTips = [100, 1000, 10_000, 100_000] const defaultTips = [100, 1000, 10_000, 100_000]
@ -250,12 +251,12 @@ export function useAct ({ query = ACT_MUTATION, ...options } = {}) {
} }
export function useZap () { export function useZap () {
const wallet = useWallet()
const act = useAct() const act = useAct()
const { me } = useMe()
const strike = useLightning() const strike = useLightning()
const toaster = useToast() const toaster = useToast()
return useCallback(async ({ item, abortSignal }) => { return useCallback(async ({ item, me, abortSignal }) => {
const meSats = (item?.meSats || 0) const meSats = (item?.meSats || 0)
// add current sats to next tip since idempotent zaps use desired total zap not difference // add current sats to next tip since idempotent zaps use desired total zap not difference
@ -267,7 +268,8 @@ export function useZap () {
try { try {
await abortSignal.pause({ me, amount: sats }) await abortSignal.pause({ me, amount: sats })
strike() strike()
const { error } = await act({ variables, optimisticResponse }) // batch zaps if wallet is enabled or using fee credits so they can be executed serially in a single request
const { error } = await act({ variables, optimisticResponse, context: { batch: !!wallet || me?.privates?.sats > sats } })
if (error) throw error if (error) throw error
} catch (error) { } catch (error) {
if (error instanceof ActCanceledError) { if (error instanceof ActCanceledError) {
@ -277,7 +279,7 @@ export function useZap () {
const reason = error?.message || error?.toString?.() const reason = error?.message || error?.toString?.()
toaster.danger(reason) toaster.danger(reason)
} }
}, [act, me?.id, strike]) }, [act, toaster, strike, !!wallet])
} }
export class ActCanceledError extends Error { export class ActCanceledError extends Error {

View File

@ -1,4 +1,5 @@
import { ApolloClient, InMemoryCache, HttpLink, makeVar } from '@apollo/client' import { ApolloClient, InMemoryCache, HttpLink, makeVar, split } from '@apollo/client'
import { BatchHttpLink } from '@apollo/client/link/batch-http'
import { decodeCursor, LIMIT } from './cursor' import { decodeCursor, LIMIT } from './cursor'
import { SSR } from './constants' import { SSR } from './constants'
@ -28,8 +29,15 @@ export default function getApolloClient () {
export const meAnonSats = {} export const meAnonSats = {}
function getClient (uri) { function getClient (uri) {
const link = split(
// batch zaps if wallet is enabled so they can be executed serially in a single request
operation => operation.operationName === 'act' && operation.variables.act === 'TIP' && operation.getContext().batch,
new BatchHttpLink({ uri, batchInterval: 1000, batchDebounce: true, batchMax: 0, batchKey: op => op.variables.id }),
new HttpLink({ uri })
)
return new ApolloClient({ return new ApolloClient({
link: new HttpLink({ uri }), link,
ssrMode: SSR, ssrMode: SSR,
connectToDevTools: process.env.NODE_ENV !== 'production', connectToDevTools: process.env.NODE_ENV !== 'production',
cache: new InMemoryCache({ cache: new InMemoryCache({

View File

@ -16,6 +16,7 @@ const apolloServer = new ApolloServer({
typeDefs, typeDefs,
resolvers, resolvers,
introspection: true, introspection: true,
allowBatchedHttpRequests: true,
plugins: [{ plugins: [{
requestDidStart (initialRequestContext) { requestDidStart (initialRequestContext) {
return { return {