import type { Dictionary , EntryHashB64 , HeaderHashB64 , } from "@holochain-open-dev/core-types" ; import type { ChangeBundle , Commit } from "@syn/zome-client" ; import type { ApplyDeltaFn } from "../apply-delta" ; export function orderCommits ( initialContentHash , commits ) { let byPreviousContentHash = { } ; for ( const [ hash , commit ] of Object . entries ( commits ) ) { byPreviousContentHash [ commit . previousContentHash ] = hash ; } let orderedCommits = [ ] ; let contentHash = initialContentHash ; while ( Object . keys ( byPreviousContentHash ) . length > 0 ) { if ( ! byPreviousContentHash [ contentHash ] ) throw new Error ( "We have a corrupted chain of commits" ) ; orderedCommits . push ( byPreviousContentHash [ contentHash ] as string ) ; byPreviousContentHash [ contentHash ] = undefined ; delete byPreviousContentHash [ contentHash ] ; } return orderedCommits ; } export function applyCommits < CONTENT , DELTA > ( initialContent , applyDeltaFn , commits ) { let content = initialContent ; for ( const commit of commits ) { content = applyChangeBundle ( content , applyDeltaFn , commit . changes ) ; } return content ; } export function applyChangeBundle < CONTENT , DELTA > ( initialContent , applyDeltaFn , changeBundle ) { let content = initialContent ; for ( const delta of changeBundle . deltas ) { content = applyDeltaFn ( content , delta ) ; } return content ; }