@magento/peregrine 12.0.0-alpha.1 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,19 @@
1
+ import { deleteCacheEntry } from './deleteCacheEntry';
2
+
1
3
  /**
2
- * Deletes all references to Cart from the apollo cache
4
+ * Deletes all references to Cart from the apollo cache including entries that
5
+ * start with "$" which were automatically created by Apollo InMemoryCache.
3
6
  *
4
7
  * @param {ApolloClient} client
5
8
  */
6
9
  export const clearCartDataFromCache = async client => {
7
- // Cached data
8
- client.cache.evict({ id: 'Cart' });
9
- client.cache.evict({
10
- id: 'ROOT_MUTATION',
11
- fieldName: 'placeOrder'
12
- });
13
- // Cached ROOT_QUERY
14
- client.cache.evict({ fieldName: 'cart' });
15
- client.cache.evict({ fieldName: 'customerCart' });
16
-
17
- client.cache.gc();
10
+ await deleteCacheEntry(client, key => key.match(/^\$?Cart/));
18
11
 
19
- if (client.persistor) {
20
- await client.persistor.persist();
21
- }
12
+ // Any cart subtypes that have key fields must be manually cleared.
13
+ // TODO: we may be able to use cache.evict here instead.
14
+ await deleteCacheEntry(client, key => key.match(/^\$?AppliedGiftCard/));
15
+ await deleteCacheEntry(client, key => key.match(/^\$?ShippingCartAddress/));
16
+ await deleteCacheEntry(client, key =>
17
+ key.match(/^\$?AvailableShippingMethod/)
18
+ );
22
19
  };
@@ -1,26 +1,14 @@
1
+ import { deleteCacheEntry } from './deleteCacheEntry';
2
+
1
3
  /**
2
- * Deletes all references to Customer from the apollo cache.
3
- * Related queries that have reference to the customer are also deleted
4
- * through the cascade. Note, however, that all secondary references must
5
- * be deleted in order for garbage collection to do its job.
4
+ * Deletes all references to Customer from the apollo cache including entries
5
+ * that start with "$" which were automatically created by Apollo InMemoryCache.
6
+ * By coincidence this rule additionally clears CustomerAddress entries, but
7
+ * we'll need to keep this in mind by adding additional patterns as MyAccount
8
+ * features are completed.
6
9
  *
7
10
  * @param {ApolloClient} client
8
11
  */
9
12
  export const clearCustomerDataFromCache = async client => {
10
- // Cached data
11
- client.cache.evict({ id: 'Customer' });
12
- // Remove createCustomerAddress which references newly created addresses, avoiding gc
13
- client.cache.evict({
14
- id: 'ROOT_MUTATION',
15
- fieldName: 'createCustomerAddress'
16
- });
17
- // Cached ROOT_QUERY
18
- client.cache.evict({ fieldName: 'customer' });
19
- client.cache.evict({ fieldName: 'customerWishlistProducts' });
20
-
21
- client.cache.gc();
22
-
23
- if (client.persistor) {
24
- await client.persistor.persist();
25
- }
13
+ await deleteCacheEntry(client, key => key.match(/^\$?Customer/i));
26
14
  };
@@ -0,0 +1,87 @@
1
+ import { CACHE_PERSIST_PREFIX } from '@magento/peregrine/lib/Apollo/constants';
2
+
3
+ /**
4
+ * Deletes specific entry/entries from the apollo cache and then tries to
5
+ * persist the deletions.
6
+ *
7
+ * @param {ApolloClient} client apollo client instance
8
+ * @param {Function} predicate a matching function
9
+ */
10
+ export const deleteCacheEntry = async (client, predicate) => {
11
+ await deleteActiveCacheEntry(client, predicate);
12
+ await deleteInactiveCachesEntry(client, predicate);
13
+ };
14
+
15
+ const deleteActiveCacheEntry = async (client, predicate) => {
16
+ // If there is no client or cache then just back out since it doesn't matter :D
17
+ if (
18
+ !client ||
19
+ !client.cache ||
20
+ !client.cache.data ||
21
+ !client.cache.data.data
22
+ ) {
23
+ if (process.env.NODE_ENV === 'development') {
24
+ console.warn(
25
+ 'Apollo Cache entry deletion attempted without client or cache.'
26
+ );
27
+ }
28
+ return;
29
+ }
30
+
31
+ // Remove from the active cache.
32
+ Object.keys(client.cache.data.data).forEach(key => {
33
+ if (predicate(key)) {
34
+ client.cache.data.delete(key);
35
+ }
36
+ });
37
+
38
+ // Remove from ROOT_QUERY cache.
39
+ if (client.cache.data.data.ROOT_QUERY) {
40
+ Object.keys(client.cache.data.data.ROOT_QUERY).forEach(key => {
41
+ if (predicate(key)) {
42
+ client.cache.data.delete('ROOT_QUERY', key);
43
+ }
44
+ });
45
+ }
46
+
47
+ // Immediately persist the cache changes to the active cache storage.
48
+ if (client.persistor) {
49
+ await client.persistor.persist();
50
+ }
51
+ };
52
+
53
+ const deleteInactiveCachesEntry = async (client, predicate) => {
54
+ if (!client || !client.persistor || !globalThis.localStorage) return;
55
+
56
+ const activeApolloCacheLocalStorageKey =
57
+ client.persistor.persistor.storage.key;
58
+
59
+ const isAnInactiveApolloCache = ([key]) => {
60
+ return (
61
+ key.startsWith(CACHE_PERSIST_PREFIX) &&
62
+ key !== activeApolloCacheLocalStorageKey
63
+ );
64
+ };
65
+
66
+ const { localStorage } = globalThis;
67
+
68
+ Object.entries(localStorage)
69
+ .filter(isAnInactiveApolloCache)
70
+ .forEach(([inactiveCacheKey, inactiveCacheValue]) => {
71
+ const inactiveApolloCache = JSON.parse(inactiveCacheValue);
72
+
73
+ Object.keys(inactiveApolloCache).forEach(key => {
74
+ if (predicate(key)) {
75
+ delete inactiveApolloCache[key];
76
+ }
77
+ });
78
+
79
+ // We're done deleting keys that match the predicate,
80
+ // but we've only mutated the object in memory.
81
+ // Write the updated inactive cache back out to localStorage.
82
+ localStorage.setItem(
83
+ inactiveCacheKey,
84
+ JSON.stringify(inactiveApolloCache)
85
+ );
86
+ });
87
+ };
@@ -15,7 +15,6 @@ import { useIntl } from 'react-intl';
15
15
  * [polyfill]: https://www.npmjs.com/package/intl
16
16
  * [Intl.NumberFormat.prototype.formatToParts]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/formatToParts
17
17
  */
18
-
19
18
  const Price = props => {
20
19
  const { locale } = useIntl();
21
20
  const { value, currencyCode, classes } = props;
@@ -23,7 +23,7 @@ import DEFAULT_OPERATIONS from './couponCode.gql';
23
23
  * @return {CouponCodeTalonProps}
24
24
  *
25
25
  * @example <caption>Importing into your project</caption>
26
- * import { useCouponCode } from '@magento/peregrine/lib/talons/CartPage/PriceAdjustments/useCouponCode';
26
+ * import { useCouponCode } from '@magento/peregrine/lib/talons/CartPage/PriceAdjustments/CouponCode/useCouponCode';
27
27
  */
28
28
  export const useCouponCode = props => {
29
29
  const operations = mergeOperations(DEFAULT_OPERATIONS, props.operations);
@@ -120,7 +120,7 @@ export const useShippingMethods = (props = {}) => {
120
120
  * Can be used as a boolean value since having no shipping methods would return 0.
121
121
  * @property {boolean} isShowingForm True if the form should be shown. False otherwise.
122
122
  * @property {SelectShippingFields} selectedShippingFields Values for the select input fields on the shipping form
123
- * @property {String} selectedShippingMethod A serialized string of <carrier-code>|<method-code>, eg. usps|priority.
123
+ * @property {String} selectedShippingMethod A serialized string of <inlineCode>${carrier-code}\|${method-code}</inlineCode>, eg. <inlineCode>usps\|priority</inlineCode>.
124
124
  * @property {Array<Object>} shippingMethods A list of available shipping methods based on the primary shipping address
125
125
  * @property {function} showForm A function that sets the `isShowingForm` value to true.
126
126
  */
@@ -17,7 +17,7 @@ import { useCartContext } from '../../../../context/cart';
17
17
  *
18
18
  * @param {Object} props
19
19
  * @param {function} props.setIsCartUpdating Function for setting the updating state of the shopping cart
20
- * @param {String} props.selectedShippingMethod A serialized string of <carrier-code>|<method-code>, eg. usps|priority.
20
+ * @param {String} props.selectedShippingMethod A serialized string of <inlineCode>${carrier-code}\|${method-code}</inlineCode>, eg. <inlineCode>usps\|priority</inlineCode>.
21
21
  * @param {Array<Object>} props.shippingMethods An array of available shipping methods
22
22
  * @param {ShippingRadiosMutations} props.mutations GraphQL mutations for a shipping radio selector component.
23
23
  *
@@ -20,8 +20,6 @@ module.exports = targets => {
20
20
  *
21
21
  * @member {tapable.AsyncSeriesHook}
22
22
  *
23
- * @see [list of wrappable hooks][]
24
- *
25
23
  * @see [Intercept function signature]{@link hookInterceptFunction}
26
24
  *
27
25
  * @example <caption>Access the tapable object</caption>
@@ -49,8 +47,6 @@ module.exports = targets => {
49
47
  *
50
48
  * @member {tapable.AsyncSeriesHook}
51
49
  *
52
- * @see [list of wrappable talons][]
53
- *
54
50
  * @see [Intercept function signature]{@link hookInterceptFunction}
55
51
  *
56
52
  * @example <caption>Access the tapable object</caption>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magento/peregrine",
3
- "version": "12.0.0-alpha.1",
3
+ "version": "12.0.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },