import { doQuery } from './doQuery' ; let nbRemainingStackedCalls = 0 ; export const getRemainingStackedCalls = ( ) => nbRemainingStackedCalls ; const stackedCalls = [ ] ; export const stackCall = params => { stackedCalls . push ( params ) ; nbRemainingStackedCalls ++ ; } ; const stackedOptimisticCalls = [ ] ; export const stackOptimisticCall = params => { stackedOptimisticCalls . push ( params ) ; nbRemainingStackedCalls ++ ; } ; export const replayStackedCalls = async ( ) => { let clone ; if ( stackedOptimisticCalls . length > 0 ) { clone = [ ... stackedOptimisticCalls ] ; stackedOptimisticCalls . splice ( 0 , stackedOptimisticCalls . length ) ; await Promise . all ( clone . map ( params => Promise . resolve ( doQuery . call ( null , params ) ) ) ) ; nbRemainingStackedCalls -= clone . length ; } else { clone = [ ... stackedCalls ] ; stackedCalls . splice ( 0 , stackedCalls . length ) ; await Promise . all ( clone . map ( params => Promise . resolve ( doQuery . call ( null , params ) ) ) ) ; nbRemainingStackedCalls -= clone . length ; } } ;