Advertisement
Guest User

Untitled

a guest
Nov 13th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const id = coll.collection.id;
  2. const { loading, error, data, fetchMore, extensions, refetch } = useQuery(GET_COLLECTION, {
  3.     variables: { id }
  4. });
  5.  
  6. const [updatePrice] = useMutation(UPDATE_PRICE);
  7.  
  8. // Given a product, returns a promise that resolves when all variants are processed
  9. function process_product(product){
  10.     const variants = product.node.variants.edges;
  11.     return Promise.all(variants.map(process_variant));
  12. }
  13.  
  14. // Given a variant, returns a promise after the product is processed
  15. async function process_variant(variant){
  16.     if (variant) {
  17.         console.log('doing stuff')
  18.     }
  19.     else {
  20.         console.log('doing other stuff')
  21.     }
  22.     const productVariableInput = {};
  23.     const variables = { input: productVariableInput };
  24.  
  25.     try {
  26.         const {data, extensions} = await updatePrice({ variables });
  27.         const remaining_throttle = extensions.cost.throttleStatus.currentlyAvailable;
  28.         console.log("Remaining", remaining_throttle)
  29.         console.log(data)
  30.  
  31.         // Change to a while loop to make sure you actually wait until resources are available
  32.         if (remaining_throttle < 100) {
  33.             console.log("WAITING")
  34.             await wait(18000);
  35.         }
  36.     } catch (e) {
  37.         console.log('error:', e);
  38.     }
  39.     console.log("AFTER")
  40.     return 0;
  41. }
  42.  
  43. const redirectToModify = async (data, totalProducts) => {
  44.     await wait(20000);
  45.     let cursor;
  46.     const products = data.collection.product.edges;
  47.  
  48.     totalProducts = totalProducts - products.length;
  49.  
  50.     // Wait for all products to finish processing
  51.     await Promise.all(products.map(process_product));
  52.  
  53.     if (totalProducts > 0) {
  54.         console.log("Calling")
  55.         await wait(15000);
  56.         handleDiscountMore(data, cursor, totalProducts)
  57.     }
  58. };
  59.  
  60. //Below function is Just for reference. It gets called before checking the throttleStatus above. afaik there's no problem with this
  61. function handleDiscountMore(data, cursor, pc) {
  62.     console.log("Call received")
  63.     const variables = { id: data.collection.id, cursor };
  64.     const updateQuery = (previousResult, { fetchMoreResult }) => {
  65.         console.log("adding", fetchMoreResult);
  66.         redirectToModify(fetchMoreResult, pc);
  67.         // return fetchMoreResult;
  68.     }
  69.     fetchMore({ variables, updateQuery })
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement