interface CartStateInterface {
loaded: boolean;
inProgress: boolean;
cart: any;
}
interface PromiseCallbacks<T = void> {
resolve: (value?: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
}
const fetchDomainOfferCallbacks: PromiseCallbacks[] = [];
async fetchDomainOffer({ state, commit }, payload): Promise<void> {
return new Promise(async (resolve, reject) => {
if (state.loaded) {
return resolve();
}
fetchDomainOfferCallbacks.push({ resolve, reject });
if (state.inProgress) {
return;
}
try {
commit('SET_IN_PROGRESS', true);
const domainOffer: DomainOfferDto = await domainOfferApi.getDomainOffer();
commit('SET_DOMAIN_OFFER', domainOffer);
commit('SET_IN_PROGRESS', false);
fetchDomainOfferCallbacks.forEach((callbacks) => {
callbacks.resolve();
});
} catch (error) {
fetchDomainOfferCallbacks.forEach((callbacks) => {
callbacks.reject(error);
});
} finally {
fetchDomainOfferCallbacks.splice(0, fetchDomainOfferCallbacks.length);
}
});
},
const mutations: MutationTree<CartStateInterface> = {
SET_CART(state, value: any) {
state.cart = value;
state.loaded = true;
},
SET_IN_PROGRESS(state, value: boolean) {
state.inProgress = value;
},
};