{"version":3,"file":"useRerenderOnChange.mjs","names":[],"sources":["../../src/react/useRerenderOnChange.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport type { FragmentReference } from '../core/FragmentReference';\nimport type { WithEncounteredRecords } from '../core/read';\nimport type { ReaderAst } from '../core/reader';\nimport { subscribe } from '../core/subscribe';\nimport { useIsographEnvironment } from './IsographEnvironmentProvider';\n\n// TODO add unit tests for this. Add integration tests that test\n// behavior when the encounteredRecords underneath a fragment change.\nexport function useRerenderOnChange<\n  TReadFromStore extends { parameters: object; data: object },\n>(\n  encounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>,\n  fragmentReference: FragmentReference<any, any>,\n  setEncounteredDataAndRecords: (\n    data: WithEncounteredRecords<TReadFromStore>,\n  ) => void,\n  readerAst: ReaderAst<TReadFromStore>,\n) {\n  const environment = useIsographEnvironment();\n  useEffect(() => {\n    return subscribe(\n      environment,\n      encounteredDataAndRecords,\n      fragmentReference,\n      (newEncounteredDataAndRecords) => {\n        setEncounteredDataAndRecords(newEncounteredDataAndRecords);\n      },\n      readerAst,\n    );\n    // Note: this is an empty array on purpose:\n    // - the fragment reference is stable for the life of the component\n    // - ownership of encounteredDataAndRecords is transferred into the\n    //   environment\n    // - though maybe we need to include setEncounteredDataAndRecords in\n    //   the dependency array\n  }, []);\n}\n"],"mappings":";;;;;AASA,SAAgB,oBAGd,2BACA,mBACA,8BAGA,WACA;CACA,MAAM,cAAc,wBAAwB;AAC5C,iBAAgB;AACd,SAAO,UACL,aACA,2BACA,oBACC,iCAAiC;AAChC,gCAA6B,6BAA6B;KAE5D,UACD;IAOA,EAAE,CAAC"}