{
  "version": 3,
  "sources": ["../../../../src/use-waitable/internal/update-waitable-bindings-with-primary-function.ts"],
  "sourcesContent": ["import isPromise from 'is-promise';\nimport type { MutableRefObject } from 'react';\nimport type { Binding, ReadonlyBinding } from 'react-bindings';\nimport { getLogger } from 'react-bindings';\n\nimport type { UseWaitableOnFailureCallback, UseWaitableOnSuccessCallback } from '../types/args';\nimport type { WaitablePrimaryFunction } from '../types/primary-function';\n\n/**\n * Updates the error or value bindings for a waitable by running the primary function.\n *\n * This function:\n *\n * - marks the waitable as having been run\n * - marks the waitable as busy\n * - runs the primary function, with callbacks that directly affect the waitable's value and error bindings\n * - marks the waitable as no longer busy\n *\n * If an uncaught error is thrown, the value and error bindings are unaffected but the waitable's busy state will be cleared\n */\nexport const updateWaitableBindingsWithPrimaryFunction = <SuccessT, FailureT>({\n  id,\n  primaryFunc,\n  isBusy,\n  error,\n  value,\n  alreadyRanFunc,\n  resetCount,\n  onSuccess,\n  onFailure\n}: {\n  id: string;\n  isBusy: Binding<boolean>;\n  primaryFunc: WaitablePrimaryFunction<SuccessT, FailureT>;\n  error: Binding<FailureT | undefined>;\n  value: Binding<SuccessT | undefined>;\n  alreadyRanFunc: MutableRefObject<boolean>;\n  resetCount: ReadonlyBinding<number>;\n  onSuccess: UseWaitableOnSuccessCallback<SuccessT> | undefined;\n  onFailure: UseWaitableOnFailureCallback<FailureT> | undefined;\n}) => {\n  alreadyRanFunc.current = true;\n\n  isBusy.set(true);\n\n  const initialResetCount = resetCount.get();\n\n  try {\n    const possiblePromise = primaryFunc({\n      setSuccess: (successValue: SuccessT | undefined) => {\n        if (resetCount.get() !== initialResetCount) {\n          return false; // Ignoring this result since there was a reset after this function was called\n        }\n\n        value.set(successValue);\n        if (error.get() !== undefined) {\n          error.set(undefined);\n        }\n        isBusy.set(false);\n\n        onSuccess?.(successValue);\n\n        return true;\n      },\n      setFailure: (errorValue: FailureT) => {\n        if (resetCount.get() !== initialResetCount) {\n          return false; // Ignoring this result since there was a reset after this function was called\n        }\n\n        error.set(errorValue);\n        isBusy.set(false);\n\n        onFailure?.(errorValue);\n\n        return true;\n      },\n      wasReset: () => resetCount.get() !== initialResetCount\n    });\n\n    if (isPromise(possiblePromise)) {\n      return possiblePromise.catch((e) => {\n        getLogger().error?.(`${id} failed with an uncaught exception`, e);\n        isBusy.set(false);\n      });\n    } else {\n      return possiblePromise;\n    }\n  } catch (e) {\n    getLogger().error?.(`${id} failed with an uncaught exception`, e);\n    isBusy.set(false);\n  }\n};\n"],
  "mappings": "AAAA,OAAO,eAAe;AAGtB,SAAS,iBAAiB;AAiBnB,MAAM,4CAA4C,CAAqB;AAAA,EAC5E;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAUM;AACJ,iBAAe,UAAU;AAEzB,SAAO,IAAI,IAAI;AAEf,QAAM,oBAAoB,WAAW,IAAI;AAEzC,MAAI;AACF,UAAM,kBAAkB,YAAY;AAAA,MAClC,YAAY,CAAC,iBAAuC;AAClD,YAAI,WAAW,IAAI,MAAM,mBAAmB;AAC1C,iBAAO;AAAA,QACT;AAEA,cAAM,IAAI,YAAY;AACtB,YAAI,MAAM,IAAI,MAAM,QAAW;AAC7B,gBAAM,IAAI,MAAS;AAAA,QACrB;AACA,eAAO,IAAI,KAAK;AAEhB,oBAAY,YAAY;AAExB,eAAO;AAAA,MACT;AAAA,MACA,YAAY,CAAC,eAAyB;AACpC,YAAI,WAAW,IAAI,MAAM,mBAAmB;AAC1C,iBAAO;AAAA,QACT;AAEA,cAAM,IAAI,UAAU;AACpB,eAAO,IAAI,KAAK;AAEhB,oBAAY,UAAU;AAEtB,eAAO;AAAA,MACT;AAAA,MACA,UAAU,MAAM,WAAW,IAAI,MAAM;AAAA,IACvC,CAAC;AAED,QAAI,UAAU,eAAe,GAAG;AAC9B,aAAO,gBAAgB,MAAM,CAAC,MAAM;AAClC,kBAAU,EAAE,QAAQ,GAAG,EAAE,sCAAsC,CAAC;AAChE,eAAO,IAAI,KAAK;AAAA,MAClB,CAAC;AAAA,IACH,OAAO;AACL,aAAO;AAAA,IACT;AAAA,EACF,SAAS,GAAG;AACV,cAAU,EAAE,QAAQ,GAAG,EAAE,sCAAsC,CAAC;AAChE,WAAO,IAAI,KAAK;AAAA,EAClB;AACF;",
  "names": []
}
