import { finalize, tap } from "rxjs";
/** An operator that claims the latest event with the database */
export function claimLatest(claims) {
    return (source) => {
        let latest = undefined;
        return source.pipe(tap((event) => {
            // remove old claim
            if (latest)
                claims.removeClaim(latest, source);
            // claim new event
            if (event)
                claims.claim(event, source);
            // update state
            latest = event;
        }), finalize(() => {
            // remove latest claim
            if (latest)
                claims.removeClaim(latest, source);
        }));
    };
}
