{"version":3,"file":"methods.mjs","names":[],"sources":["../src/methods.ts"],"sourcesContent":["import { startTransaction, type LdoBase, write, getDataset } from \"@ldo/ldo\";\nimport type { Quad } from \"@rdfjs/types\";\nimport { _proxyContext, getProxyFromObject } from \"@ldo/jsonld-dataset-proxy\";\nimport type { SubscribableDataset } from \"@ldo/subscribable-dataset\";\nimport type { Resource } from \"./Resource\";\nimport type { ConnectedLdoTransactionDataset } from \"./ConnectedLdoTransactionDataset\";\nimport type {\n  AggregateSuccess,\n  SuccessResult,\n} from \"./results/success/SuccessResult\";\nimport type { AggregateError, ErrorResult } from \"./results/error/ErrorResult\";\n\n/**\n * Begins tracking changes to eventually commit.\n *\n * @param input - A linked data object to track changes on\n * @param resource - A resource that all additions will eventually be committed to\n * @param additionalResources - Any additional resources that changes will eventually be committed to\n *\n * @returns A transactable Linked Data Object\n *\n * @example\n * ```typescript\n * import { changeData } from \"@ldo/connected\";\n *\n * // ...\n *\n * const profile = connectedLdoDataset\n *   .using(ProfileShapeType)\n *   .fromSubject(\"https://example.com/profile#me\");\n * const resource = connectedLdoDataset.getResource(\"https://example.com/profile\");\n *\n * const cProfile = changeData(profile, resource);\n * cProfile.name = \"My New Name\";\n * const result = await commitData(cProfile);\n * ```\n */\nexport function changeData<Type extends LdoBase>(\n  input: Type,\n  resource: Resource,\n  ...additionalResources: Resource[]\n): Type {\n  const resources = [resource, ...additionalResources];\n  // Clone the input and set a graph\n  const [transactionLdo] = write(...resources.map((r) => r.uri)).usingCopy(\n    input,\n  );\n  // Start a transaction with the input\n  startTransaction(transactionLdo);\n  // Return\n  return transactionLdo;\n}\n\n/**\n * Commits the transaction to the global dataset, syncing all subscribing\n * components and connected Pods\n *\n * @param input - A transactable linked data object\n *\n * @example\n * ```typescript\n * import { changeData } from \"@ldo/connected\";\n *\n * // ...\n *\n * const profile = connectedLdoDataset\n *   .using(ProfileShapeType)\n *   .fromSubject(\"https://example.com/profile#me\");\n * const resource = connectedLdoDataset.getResource(\"https://example.com/profile\");\n *\n * const cProfile = changeData(profile, resource);\n * cProfile.name = \"My New Name\";\n * const result = await commitData(cProfile);\n * ```\n */\nexport async function commitData(\n  input: LdoBase,\n): Promise<AggregateSuccess<SuccessResult> | AggregateError<ErrorResult>> {\n  const transactionDataset = getDataset(\n    input,\n  ) as ConnectedLdoTransactionDataset<[]>;\n  const result = await transactionDataset.commitToRemote();\n  if (result.isError) return result;\n  // Take the LdoProxy out of commit mode. This uses hidden methods of JSONLD-DATASET-PROXY\n  const proxy = getProxyFromObject(input);\n  proxy[_proxyContext] = proxy[_proxyContext].duplicate({\n    dataset: proxy[_proxyContext].state\n      .parentDataset as SubscribableDataset<Quad>,\n  });\n  return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,WACd,OACA,UACA,GAAG,qBACG;CAGN,MAAM,CAAC,kBAAkB,MAAM,GAAG,CAFf,UAAU,GAAG,oBAEW,CAAC,KAAK,MAAM,EAAE,IAAI,CAAC,CAAC,UAC7D,MACD;AAED,kBAAiB,eAAe;AAEhC,QAAO;;;;;;;;;;;;;;;;;;;;;;;;AAyBT,eAAsB,WACpB,OACwE;CAIxE,MAAM,SAAS,MAHY,WACzB,MAEqC,CAAC,gBAAgB;AACxD,KAAI,OAAO,QAAS,QAAO;CAE3B,MAAM,QAAQ,mBAAmB,MAAM;AACvC,OAAM,iBAAiB,MAAM,eAAe,UAAU,EACpD,SAAS,MAAM,eAAe,MAC3B,eACJ,CAAC;AACF,QAAO"}