import { SubscriptionSyncResult } from '../actions/subscriptions' type SubscriptionContext = { subscriptionConfigs: Map subscriptions: Map syncRequired: number error?: Error } export function hasPendingSync({ context }: { context: { syncRequired: number } }) { return context.syncRequired > 0 } export function updateSubscriptionTarget( context: { subscriptionConfigs: Map; syncRequired: number }, key: string, config?: Config, ) { const subscriptionConfigs = new Map(context.subscriptionConfigs) if (config === undefined) subscriptionConfigs.delete(key) else subscriptionConfigs.set(key, config) return { subscriptionConfigs, syncRequired: context.syncRequired + 1 } } export function beginSubscriptionSync() { // One reconciliation covers the whole target snapshot. Changes arriving during // an asynchronous sync increment this marker and trigger another pass. return { syncRequired: 0, error: undefined } } export function finishSubscriptionSync( context: { syncRequired: number }, result: SubscriptionSyncResult, ) { return { ...result, // Error states stop automatic retries; CONNECT must still retry failed work. syncRequired: result.error ? Math.max(context.syncRequired, 1) : context.syncRequired, } } export function detachSubscriptions( context: SubscriptionContext, ) { return { subscriptions: new Map(), syncRequired: context.subscriptionConfigs.size > 0 ? Math.max(context.syncRequired, 1) : context.syncRequired, } }