{"version":3,"file":"PromiseWrapper.mjs","names":["wrapper: PromiseWrapper<T, any>"],"sources":["../../src/core/PromiseWrapper.ts"],"sourcesContent":["export type AnyError = any;\n\nexport const NOT_SET = Symbol('NOT_SET');\nexport type NotSet = typeof NOT_SET;\n\nexport type Result<T, E> =\n  | {\n      readonly kind: 'Ok';\n      readonly value: T;\n    }\n  | {\n      readonly kind: 'Err';\n      readonly error: E;\n    };\n\n/**\n * Invariant:\n * Before the promise is resolved, value becomes non-null.\n */\nexport type PromiseWrapper<T, E = any> = {\n  readonly promise: Promise<Exclude<T, NotSet>>;\n  result: Result<Exclude<T, NotSet>, E> | NotSet;\n};\n\nexport interface PromiseWrapperOk<T, E = any> extends PromiseWrapper<T, E> {\n  result: {\n    readonly kind: 'Ok';\n    readonly value: Exclude<T, NotSet>;\n  };\n}\n\nexport function wrapPromise<T>(\n  promise: Promise<Exclude<T, NotSet>>,\n): PromiseWrapper<T, unknown> {\n  // TODO confirm suspense works if the promise is already resolved.\n  const wrapper: PromiseWrapper<T, any> = { promise, result: NOT_SET };\n  promise\n    .then((v) => {\n      wrapper.result = { kind: 'Ok', value: v };\n    })\n    .catch((e) => {\n      wrapper.result = { kind: 'Err', error: e };\n    });\n  return wrapper;\n}\n\nexport function wrapResolvedValue<T>(\n  value: Exclude<T, NotSet>,\n): PromiseWrapperOk<T, never> {\n  return {\n    promise: Promise.resolve(value),\n    result: {\n      kind: 'Ok',\n      value,\n    },\n  };\n}\n\nexport function readPromise<T, E>(p: PromiseWrapper<T, E>): T {\n  const { result } = p;\n  if (result !== NOT_SET) {\n    const resultKind = result;\n    if (resultKind.kind === 'Ok') {\n      return resultKind.value;\n    } else {\n      throw resultKind.error;\n    }\n  }\n\n  throw p.promise;\n}\n\nexport type PromiseState<T, E> =\n  | {\n      readonly kind: 'Pending';\n      readonly promise: Promise<T>;\n    }\n  | Result<T, E>;\n\nexport function getPromiseState<T, E>(\n  p: PromiseWrapper<T, E>,\n): PromiseState<T, E> {\n  if (p.result !== NOT_SET) {\n    return p.result;\n  }\n  return {\n    kind: 'Pending',\n    promise: p.promise,\n  };\n}\n"],"mappings":";AAEA,MAAa,UAAU,OAAO,UAAU;AA6BxC,SAAgB,YACd,SAC4B;CAE5B,MAAMA,UAAkC;EAAE;EAAS,QAAQ;EAAS;AACpE,SACG,MAAM,MAAM;AACX,UAAQ,SAAS;GAAE,MAAM;GAAM,OAAO;GAAG;GACzC,CACD,OAAO,MAAM;AACZ,UAAQ,SAAS;GAAE,MAAM;GAAO,OAAO;GAAG;GAC1C;AACJ,QAAO;;AAGT,SAAgB,kBACd,OAC4B;AAC5B,QAAO;EACL,SAAS,QAAQ,QAAQ,MAAM;EAC/B,QAAQ;GACN,MAAM;GACN;GACD;EACF;;AAGH,SAAgB,YAAkB,GAA4B;CAC5D,MAAM,EAAE,WAAW;AACnB,KAAI,WAAW,SAAS;EACtB,MAAM,aAAa;AACnB,MAAI,WAAW,SAAS,KACtB,QAAO,WAAW;MAElB,OAAM,WAAW;;AAIrB,OAAM,EAAE;;AAUV,SAAgB,gBACd,GACoB;AACpB,KAAI,EAAE,WAAW,QACf,QAAO,EAAE;AAEX,QAAO;EACL,MAAM;EACN,SAAS,EAAE;EACZ"}