This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcustomerData.js
More file actions
54 lines (47 loc) · 2.12 KB
/
Copy pathcustomerData.js
File metadata and controls
54 lines (47 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { debounce } from 'throttle-debounce';
export const reloadMagentoMiniCart = () => {
const requireJs = window.require;
// if requireJs is available on the page where this app is loaded, and it uses customer-data, reload the sections needed for the minicart
if (requireJs && requireJs.defined('Magento_Customer/js/customer-data')) {
requireJs(['Magento_Customer/js/customer-data'], function(customerData) {
var sections = ['cart', 'customer'];
customerData.reload(sections, true);
});
}
};
export const reloadInvalidatedSections = () => {
const requireJs = window.require;
// if requireJs is available on the page where this app is loaded, and it uses customer-data, reload the invalidated sections
if (requireJs && requireJs.defined('Magento_Customer/js/customer-data')) {
requireJs(['Magento_Customer/js/customer-data'], function(customerData) {
const sections = customerData.getExpiredSectionNames();
customerData.reload(sections, true);
});
}
};
export const notifyOnSectionUpdate = (sections = ['customer']) => {
// if requireJs is available on the page where this app is loaded, listen for changes/updates to the customer-data sections
// and fire a custom event we can listen to
if (window.require) {
window.require(['Magento_Customer/js/customer-data'], function(
customerData
) {
const notifyEvent = 'customer-data-reloaded';
const notifyUpdate = () => {
const event = window.CustomEvent
? new CustomEvent(notifyEvent, {})
: document
.createEvent('CustomEvent')
.initCustomEvent(notifyEvent, true, true, {});
document.dispatchEvent(event);
};
// debounce the notifyUpdate function so that it is not called more then once per second
// it will execute the last call, so we should then have the latest data to process
const debouncedNotifyUpdate = debounce(1000, false, notifyUpdate);
for (let section of sections) {
const customerDataSection = customerData.get(section);
customerDataSection.subscribe(debouncedNotifyUpdate, this);
}
});
}
};