-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
index.ts
294 lines (275 loc) · 8.55 KB
/
index.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { CURRENT_USER_IS_ADMIN } from '@woocommerce/settings';
import deprecated from '@wordpress/deprecated';
import isShallowEqual from '@wordpress/is-shallow-equal';
import type { ComparableObject } from '@wordpress/is-shallow-equal';
import { isNull, isObject, objectHasProp } from '@woocommerce/types';
/**
* A function that always return true.
* We need to have a single instance of this function so it doesn't
* invalidate our memo comparison.
*/
const returnTrue = (): true => true;
type CheckoutFilterFunction< U = unknown > = < T >(
value: T | U,
extensions: Record< string, unknown >,
args?: CheckoutFilterArguments
) => T | U;
type CheckoutFilterArguments =
| ( Record< string, unknown > & {
context?: string;
} )
| null;
let checkoutFilters: Record<
string,
Record< string, CheckoutFilterFunction >
> = {};
let cachedValues: Record< string, unknown > = {};
/**
* Register filters for a specific extension.
*/
export const registerCheckoutFilters = (
namespace: string,
filters: Record< string, CheckoutFilterFunction >
): void => {
/**
* Let the user know couponName is no longer available as a filter.
*
* See https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/4312
*/
if ( Object.keys( filters ).includes( 'couponName' ) ) {
deprecated( 'couponName', {
alternative: 'coupons',
plugin: 'WooCommerce Blocks',
link: 'https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/bb921d21f42e21f38df2b1c87b48e07aa4cb0538/docs/extensibility/available-filters.md#coupons',
} );
}
// Clear cached values when registering new filters because otherwise we get outdated results when applying them.
cachedValues = {};
checkoutFilters = {
...checkoutFilters,
[ namespace ]: filters,
};
};
/**
* Backward compatibility for __experimentalRegisterCheckoutFilters, this has been graduated to stable now.
* Remove after July 2023.
*/
export const __experimentalRegisterCheckoutFilters = (
namespace: string,
filters: Record< string, CheckoutFilterFunction >
) => {
deprecated( '__experimentalRegisterCheckoutFilters', {
alternative: 'registerCheckoutFilters',
plugin: 'WooCommerce Blocks',
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8346',
since: '9.6.0',
hint: '__experimentalRegisterCheckoutFilters has graduated to stable and this experimental function will be removed.',
} );
registerCheckoutFilters( namespace, filters );
};
/**
* Get all filters with a specific name.
*
* @param {string} filterName Name of the filter to search for.
* @return {Function[]} Array of functions that are registered for that filter
* name.
*/
const getCheckoutFilters = ( filterName: string ): CheckoutFilterFunction[] => {
const namespaces = Object.keys( checkoutFilters );
const filters = namespaces
.map( ( namespace ) => checkoutFilters[ namespace ][ filterName ] )
.filter( Boolean );
return filters;
};
const cachedFilterRuns: Record<
string,
{
arg?: CheckoutFilterArguments;
extensions?: Record< string, unknown > | null;
defaultValue: unknown;
} & Record< string, unknown >
> = {};
const updatePreviousFilterRun = < T >(
filterName: string,
arg: CheckoutFilterArguments,
extensions: Record< string, unknown > | null,
defaultValue: T
): void => {
cachedFilterRuns[ filterName ] = {
arg,
extensions,
defaultValue,
};
};
/**
* A function that checks the shallow equality of an object's members.
*/
const checkMembersShallowEqual = <
T extends Record< string, unknown > | null,
U extends Record< string, unknown > | null
>(
a: T,
b: U
) => {
// For the case when extensions is null across runs.
if ( isNull( a ) && isNull( b ) ) {
return true;
}
return (
isObject( a ) &&
isObject( b ) &&
Object.keys( a ).length === Object.keys( b ).length &&
Object.keys( a ).every( ( aKey ) => {
return (
objectHasProp( b, aKey ) &&
isShallowEqual(
a[ aKey ] as ComparableObject,
b[ aKey ] as ComparableObject
)
);
} )
);
};
/**
* A function that checks the arg and extensions that were passed the last time a specific filter ran.
* If they are shallowly equal, then return the cached value and prevent third party code running. If they are
* different then the third party filters are run and the result is cached.
*/
const shouldReRunFilters = < T >(
filterName: string,
arg: CheckoutFilterArguments,
extensions: Record< string, unknown > | null,
defaultValue: T
): boolean => {
const previousFilterRun = cachedFilterRuns[ filterName ];
if ( ! previousFilterRun ) {
// This is the first time the filter is running so let it continue;
updatePreviousFilterRun( filterName, arg, extensions, defaultValue );
return true;
}
const {
arg: previousArg = {} as Record< string, unknown >,
extensions: previousExtensions = {} as Record< string, unknown >,
defaultValue: previousDefaultValue = null,
} = previousFilterRun;
// Check length of arg and previousArg, and that all keys are present in both arg and previousArg
const argIsEqual = checkMembersShallowEqual( arg, previousArg );
if ( ! argIsEqual ) {
updatePreviousFilterRun( filterName, arg, extensions, defaultValue );
return true;
}
// Check length of arg and previousArg, and that all keys are present in both arg and previousArg
const defaultValueIsEqual = defaultValue === previousDefaultValue;
if ( ! defaultValueIsEqual ) {
updatePreviousFilterRun( filterName, arg, extensions, defaultValue );
return true;
}
const extensionsIsEqual = checkMembersShallowEqual(
extensions,
previousExtensions
);
if ( ! extensionsIsEqual ) {
updatePreviousFilterRun( filterName, arg, extensions, defaultValue );
return true;
}
return false;
};
/**
* Apply a filter.
*/
export const applyCheckoutFilter = < T >( {
filterName,
defaultValue,
extensions = null,
arg = null,
validation = returnTrue,
}: {
/** Name of the filter to apply. */
filterName: string;
/** Default value to filter. */
defaultValue: T;
/** Values extend to REST API response. */
extensions?: Record< string, unknown > | null;
/** Object containing arguments for the filter function. */
arg?: CheckoutFilterArguments;
/** Function that needs to return true when the filtered value is passed in order for the filter to be applied. */
validation?: ( value: T ) => true | Error;
} ): T => {
if (
! shouldReRunFilters( filterName, arg, extensions, defaultValue ) &&
cachedValues[ filterName ] !== undefined
) {
return cachedValues[ filterName ] as T;
}
const filters = getCheckoutFilters( filterName );
let value = defaultValue;
filters.forEach( ( filter ) => {
try {
const newValue = filter( value, extensions || {}, arg ) as T;
if ( typeof newValue !== typeof value ) {
throw new Error(
sprintf(
/* translators: %1$s is the type of the variable passed to the filter function, %2$s is the type of the value returned by the filter function. */
__(
'The type returned by checkout filters must be the same as the type they receive. The function received %1$s but returned %2$s.',
'woocommerce'
),
typeof value,
typeof newValue
)
);
}
value = validation( newValue ) ? newValue : value;
} catch ( e ) {
if ( CURRENT_USER_IS_ADMIN ) {
throw e;
} else {
// eslint-disable-next-line no-console
console.error( e );
}
}
} );
cachedValues[ filterName ] = value;
return value;
};
/**
* Backward compatibility for __experimentalApplyCheckoutFilter, this has been graduated to stable now.
* Remove after July 2023.
*/
export const __experimentalApplyCheckoutFilter = < T >( {
filterName,
defaultValue,
extensions = null,
arg = null,
validation = returnTrue,
}: {
/** Name of the filter to apply. */
filterName: string;
/** Default value to filter. */
defaultValue: T;
/** Values extend to REST API response. */
extensions?: Record< string, unknown > | null;
/** Object containing arguments for the filter function. */
arg?: CheckoutFilterArguments;
/** Function that needs to return true when the filtered value is passed in order for the filter to be applied. */
validation?: ( value: T ) => true | Error;
} ): T => {
deprecated( '__experimentalApplyCheckoutFilter', {
alternative: 'applyCheckoutFilter',
plugin: 'WooCommerce Blocks',
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/8346',
since: '9.6.0',
hint: '__experimentalApplyCheckoutFilter has graduated to stable and this experimental function will be removed.',
} );
return applyCheckoutFilter( {
filterName,
defaultValue,
extensions,
arg,
validation,
} );
};