Skip to content

Commit

Permalink
Add measurement for cart update subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
haeky committed Feb 7, 2025
1 parent 5be4190 commit dc64fd1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
6 changes: 3 additions & 3 deletions assets/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CartItems extends HTMLElement {
if (event.source === 'cart-items') {
return;
}
this.onCartUpdate();
return this.onCartUpdate();
});
}

Expand Down Expand Up @@ -89,7 +89,7 @@ class CartItems extends HTMLElement {

onCartUpdate() {
if (this.tagName === 'CART-DRAWER-ITEMS') {
fetch(`${routes.cart_url}?section_id=cart-drawer`)
return fetch(`${routes.cart_url}?section_id=cart-drawer`)
.then((response) => response.text())
.then((responseText) => {
const html = new DOMParser().parseFromString(responseText, 'text/html');
Expand All @@ -106,7 +106,7 @@ class CartItems extends HTMLElement {
console.error(e);
});
} else {
fetch(`${routes.cart_url}?section_id=main-cart-items`)
return fetch(`${routes.cart_url}?section_id=main-cart-items`)
.then((response) => response.text())
.then((responseText) => {
const html = new DOMParser().parseFromString(responseText, 'text/html');
Expand Down
16 changes: 16 additions & 0 deletions assets/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,11 @@ if (!customElements.get('bulk-add')) {
class CartPerformance {
static #metric_prefix = "cart-performance"

static createStartingMarker(benchmarkName) {
const metricName = `${CartPerformance.#metric_prefix}:${benchmarkName}`
return performance.mark(`${metricName}:start`);
}

static measureFromEvent(benchmarkName, event) {
const metricName = `${CartPerformance.#metric_prefix}:${benchmarkName}`
const startMarker = performance.mark(`${metricName}:start`, {
Expand All @@ -1286,6 +1291,17 @@ class CartPerformance {
);
}

static measureFromMarker(benchmarkName, startMarker) {
const metricName = `${CartPerformance.#metric_prefix}:${benchmarkName}`
const endMarker = performance.mark(`${metricName}:end`);

performance.measure(
benchmarkName,
startMarker.name,
`${metricName}:end`
);
}

static measure(benchmarkName, callback) {
const metricName = `${CartPerformance.#metric_prefix}:${benchmarkName}`
const startMarker = performance.mark(`${metricName}:start`);
Expand Down
3 changes: 3 additions & 0 deletions assets/product-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ if (!customElements.get('product-form')) {
return;
}

const startMarker = CartPerformance.createStartingMarker('add:click-event:wait-for-subscribers');
if (!this.error)
publish(PUB_SUB_EVENTS.cartUpdate, {
source: 'product-form',
productVariantId: formData.get('id'),
cartData: response,
}).then(() => {
CartPerformance.measureFromMarker('add:click-event:wait-for-subscribers', startMarker);
});
this.error = false;
const quickAddModal = this.closest('quick-add-modal');
Expand Down
2 changes: 1 addition & 1 deletion assets/product-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ if (!customElements.get('product-info')) {
if (!currentVariantId) return;

this.querySelector('.quantity__rules-cart .loading__spinner').classList.remove('hidden');
fetch(`${this.dataset.url}?variant=${currentVariantId}&section_id=${this.dataset.section}`)
return fetch(`${this.dataset.url}?variant=${currentVariantId}&section_id=${this.dataset.section}`)
.then((response) => response.text())
.then((responseText) => {
const html = new DOMParser().parseFromString(responseText, 'text/html');
Expand Down
6 changes: 3 additions & 3 deletions assets/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function subscribe(eventName, callback) {

function publish(eventName, data) {
if (subscribers[eventName]) {
subscribers[eventName].forEach((callback) => {
callback(data);
});
const promises = subscribers[eventName]
.map((callback) => callback(data))
return Promise.all(promises);
}
}

0 comments on commit dc64fd1

Please sign in to comment.