Advertisement
Guest User

Untitled

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