@growupsell/commerce 1.0.0-alpha.3 → 1.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -599,6 +599,7 @@ function createCartStore(ctx) {
599
599
  const gone = /* @__PURE__ */ new Set();
600
600
  let needsLoad = false;
601
601
  let mergeCheck = null;
602
+ let fetched = null;
602
603
  let version = 0;
603
604
  let memo = null;
604
605
  const touch = () => {
@@ -631,6 +632,7 @@ function createCartStore(ctx) {
631
632
  const pending = () => store.getSnapshot().pending;
632
633
  function reset(patch) {
633
634
  server = null;
635
+ fetched = null;
634
636
  quantities.clear();
635
637
  removals.clear();
636
638
  orderType = null;
@@ -671,6 +673,46 @@ function createCartStore(ctx) {
671
673
  }
672
674
  const isCustomer = () => ctx.identity.getSnapshot().kind === "customer";
673
675
  const findLine = (lineId) => view().cart?.lines.find((line) => line.id === lineId);
676
+ function owner() {
677
+ const customer = ctx.tokens.getCustomerToken();
678
+ if (customer) return `customer:${customer}`;
679
+ const session = ctx.tokens.getSessionToken();
680
+ return session ? `session:${session}` : null;
681
+ }
682
+ function getCart() {
683
+ const request = ctx.client.cart.get();
684
+ const key = owner();
685
+ if (key) {
686
+ const entry = { owner: key, request };
687
+ fetched = entry;
688
+ request.catch(() => {
689
+ if (fetched === entry) fetched = null;
690
+ });
691
+ }
692
+ return request;
693
+ }
694
+ async function ensureCart() {
695
+ const key = owner();
696
+ if (fetched && fetched.owner === key) {
697
+ try {
698
+ await fetched.request;
699
+ return;
700
+ } catch {
701
+ }
702
+ }
703
+ await getCart();
704
+ }
705
+ function mutate(run) {
706
+ return serial(async () => {
707
+ await ensureCart();
708
+ try {
709
+ return await run();
710
+ } catch (error) {
711
+ if (isCartNotFound(error)) fetched = null;
712
+ throw error;
713
+ }
714
+ });
715
+ }
674
716
  async function load() {
675
717
  await ctx.ready();
676
718
  const action = act("refresh", true);
@@ -685,7 +727,7 @@ function createCartStore(ctx) {
685
727
  update(store.getSnapshot().status === "ready" ? { refreshing: true } : { status: "loading" });
686
728
  const settled = settledChanges();
687
729
  try {
688
- const result = await guard(action, () => latest(() => ctx.client.cart.get()));
730
+ const result = await guard(action, () => latest(getCart));
689
731
  if (!result.current || !result.value.current) return fail(SUPERSEDED2);
690
732
  server = result.value.value;
691
733
  for (const forget of settled) forget();
@@ -740,7 +782,7 @@ function createCartStore(ctx) {
740
782
  update({ pending: { ...pending(), adding: [...pending().adding, input.skuId] } });
741
783
  try {
742
784
  if (!isCustomer()) await ctx.ensureSession();
743
- const result = await guard(action, () => serial(() => ctx.client.cart.addItem(input)));
785
+ const result = await guard(action, () => mutate(() => ctx.client.cart.addItem(input)));
744
786
  if (!result.current) return fail(SUPERSEDED2);
745
787
  update({ pending: { ...pending(), adding: withoutOne(pending().adding, input.skuId) } });
746
788
  await load();
@@ -776,7 +818,7 @@ function createCartStore(ctx) {
776
818
  () => coalesce(
777
819
  lineId,
778
820
  quantity,
779
- (value) => serial(async () => {
821
+ (value) => mutate(async () => {
780
822
  if (gone.has(lineId) || removals.has(lineId)) return;
781
823
  await ctx.client.cart.setQuantity(lineId, value);
782
824
  })
@@ -807,7 +849,7 @@ function createCartStore(ctx) {
807
849
  touch();
808
850
  render({ pending: withLine(pending(), lineId, "remove") });
809
851
  try {
810
- const result = await guard(action, () => serial(() => ctx.client.cart.removeItem(lineId)));
852
+ const result = await guard(action, () => mutate(() => ctx.client.cart.removeItem(lineId)));
811
853
  if (!result.current) return fail(SUPERSEDED2);
812
854
  change.settled = true;
813
855
  gone.add(lineId);
@@ -833,7 +875,7 @@ function createCartStore(ctx) {
833
875
  touch();
834
876
  render({ pending: { ...pending(), orderType: true } });
835
877
  try {
836
- const result = await guard(action, () => serial(() => ctx.client.cart.setOrderType(type)));
878
+ const result = await guard(action, () => mutate(() => ctx.client.cart.setOrderType(type)));
837
879
  if (!result.current) return fail(SUPERSEDED2);
838
880
  change.settled = true;
839
881
  update({ pending: { ...pending(), orderType: false } });
@@ -853,7 +895,7 @@ function createCartStore(ctx) {
853
895
  if (!isCustomer()) return action.refuse("signed-out", "Sign in to choose addresses.");
854
896
  update({ pending: { ...pending(), addresses: true } });
855
897
  try {
856
- const result = await guard(action, () => serial(() => ctx.client.cart.setAddresses(input)));
898
+ const result = await guard(action, () => mutate(() => ctx.client.cart.setAddresses(input)));
857
899
  if (!result.current) return fail(SUPERSEDED2);
858
900
  update({ pending: { ...pending(), addresses: false } });
859
901
  await load();
@@ -868,7 +910,7 @@ function createCartStore(ctx) {
868
910
  update({ pending: { ...pending(), taxDetails: true } });
869
911
  try {
870
912
  await ctx.ensureSession();
871
- const result = await guard(action, () => serial(() => ctx.client.cart.setTaxDetails(input)));
913
+ const result = await guard(action, () => mutate(() => ctx.client.cart.setTaxDetails(input)));
872
914
  if (!result.current) return fail(SUPERSEDED2);
873
915
  update({ pending: { ...pending(), taxDetails: false } });
874
916
  return ok(result.value);
@@ -891,7 +933,7 @@ function createCartStore(ctx) {
891
933
  async function sendDiscount(action, send) {
892
934
  let sent;
893
935
  try {
894
- sent = await guard(action, () => serial(send));
936
+ sent = await guard(action, () => mutate(send));
895
937
  } catch (error) {
896
938
  const failed = action.fail(error);
897
939
  if (action.alive()) await load();
@@ -1034,6 +1076,9 @@ function createCartStore(ctx) {
1034
1076
  dismissMergeWarning
1035
1077
  };
1036
1078
  }
1079
+ function isCartNotFound(error) {
1080
+ return error instanceof GrowupsellApiError && error.status === 404 && typeof error.body === "object" && error.body !== null && error.body.detail === "Cart not found.";
1081
+ }
1037
1082
  function storedPayable(error) {
1038
1083
  return error instanceof GrowupsellApiError && error.status === 400 && typeof error.body === "object" && error.body !== null && "final_payable_amount" in error.body;
1039
1084
  }
@@ -2985,4 +3030,4 @@ export {
2985
3030
  REFERRAL_TTL_MS,
2986
3031
  createGrowupsellStores
2987
3032
  };
2988
- //# sourceMappingURL=chunk-HGCKDE7D.js.map
3033
+ //# sourceMappingURL=chunk-SFAKVAKI.js.map