import type { StateForSync } from "@syn/zome-client" ; import type { EntryHashB64 } from "@holochain-open-dev/core-types" ; import { merge } from "lodash-es" ; import { selectLatestCommittedContentHash , selectSessionWorkspace , } from "../../../state/selectors" ; import { orderCommits , applyCommits , applyChangeBundle } from "../../utils" ; import type { SynWorkspace } from "../../workspace" ; export function handleSyncResponse < CONTENT , DELTA > ( workspace , sessionHash , stateForSync ) { workspace . store . update ( ( state ) => { const sessionWorkspace = selectSessionWorkspace ( state , sessionHash ) ; const latestCommittedContentHash = selectLatestCommittedContentHash ( state , sessionHash ) ; const missedCommitHashes = orderCommits ( latestCommittedContentHash , stateForSync . missedCommits ) ; for ( const missedCommitHash of missedCommitHashes ) { state . commits [ missedCommitHash ] = stateForSync . missedCommits [ missedCommitHash ] ; } sessionWorkspace . commitHashes = [ ... sessionWorkspace . commitHashes , ... missedCommitHashes , ] ; const uncommittedChanges = sessionWorkspace . uncommittedChanges ; const lastSeenIndex = uncommittedChanges . atSessionIndex + uncommittedChanges . deltas . length ; if ( lastSeenIndex !== stateForSync . uncommittedChanges . atSessionIndex ) throw new Error ( `I requested changes from session index ${ lastSeenIndex } and received changes from ${ stateForSync . uncommittedChanges . atSessionIndex }` ) ; uncommittedChanges . deltas = [ ... uncommittedChanges . deltas , ... stateForSync . uncommittedChanges . deltas , ] ; uncommittedChanges . authors = merge ( uncommittedChanges . authors , stateForSync . uncommittedChanges . authors ) ; const commitArray = missedCommitHashes . map ( ( missedCommitHash ) => stateForSync . missedCommits [ missedCommitHash ] ) ; let currentContent = applyCommits ( sessionWorkspace . currentContent , workspace . applyDeltaFn , commitArray ) ; sessionWorkspace . currentContent = applyChangeBundle ( currentContent , workspace . applyDeltaFn , stateForSync . uncommittedChanges ) ; return state ; } ) ; }