-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Expand file tree
/
Copy pathuse-store-cart-event-listeners.ts
More file actions
121 lines (108 loc) · 3.22 KB
/
Copy pathuse-store-cart-event-listeners.ts
File metadata and controls
121 lines (108 loc) · 3.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* External dependencies
*/
import { useEffect } from '@wordpress/element';
import { cartStore } from '@woocommerce/block-data';
import { dispatch } from '@wordpress/data';
import {
translateJQueryEventToNative,
getNavigationType,
} from '@woocommerce/base-utils';
interface StoreCartListenersType {
// Counts the number of consumers of this hook so we can remove listeners when no longer needed.
count: number;
// Function to remove all registered listeners.
remove: () => void;
}
interface CartDataCustomEvent extends Event {
detail?:
| {
preserveCartData?: boolean | undefined;
}
| undefined;
}
declare global {
interface Window {
wcBlocksStoreCartListeners: StoreCartListenersType;
}
}
const refreshData = ( event: CartDataCustomEvent ): void => {
const eventDetail = event?.detail;
if ( ! eventDetail || ! eventDetail.preserveCartData ) {
dispatch( cartStore ).invalidateResolutionForStore();
}
};
/**
* Refreshes data if the pageshow event is triggered by the browser history.
*
* - In Chrome, `back_forward` will be returned by getNavigationType() when the browser history is used.
* - In safari we instead need to use `event.persisted` which is true when page cache is used.
*/
const refreshCachedCartData = ( event: PageTransitionEvent ): void => {
if ( event?.persisted || getNavigationType() === 'back_forward' ) {
dispatch( cartStore ).invalidateResolutionForStore();
}
};
const setUp = (): void => {
if ( ! window.wcBlocksStoreCartListeners ) {
window.wcBlocksStoreCartListeners = {
count: 0,
remove: () => void null,
};
}
};
// Checks if there are any listeners registered.
const hasListeners = (): boolean => {
return window.wcBlocksStoreCartListeners?.count > 0;
};
// Add listeners if there are none, otherwise just increment the count.
const addListeners = (): void => {
setUp();
if ( hasListeners() ) {
window.wcBlocksStoreCartListeners.count++;
return;
}
document.body.addEventListener( 'wc-blocks_added_to_cart', refreshData );
document.body.addEventListener(
'wc-blocks_removed_from_cart',
refreshData
);
window.addEventListener( 'pageshow', refreshCachedCartData );
const removeJQueryAddedToCartEvent = translateJQueryEventToNative(
'added_to_cart',
`wc-blocks_added_to_cart`
) as () => () => void;
const removeJQueryRemovedFromCartEvent = translateJQueryEventToNative(
'removed_from_cart',
`wc-blocks_removed_from_cart`
) as () => () => void;
window.wcBlocksStoreCartListeners.count = 1;
window.wcBlocksStoreCartListeners.remove = () => {
document.body.removeEventListener(
'wc-blocks_added_to_cart',
refreshData
);
document.body.removeEventListener(
'wc-blocks_removed_from_cart',
refreshData
);
window.removeEventListener( 'pageshow', refreshCachedCartData );
removeJQueryAddedToCartEvent();
removeJQueryRemovedFromCartEvent();
};
};
const removeListeners = (): void => {
if ( window.wcBlocksStoreCartListeners.count === 1 ) {
window.wcBlocksStoreCartListeners.remove();
}
window.wcBlocksStoreCartListeners.count--;
};
/**
* This will keep track of jQuery and DOM events that invalidate the store resolution.
*/
export const useStoreCartEventListeners = (): void => {
useEffect( () => {
addListeners();
return removeListeners;
}, [] );
};