@wordpress/compose 5.15.2 → 5.16.1-next.4d3b314fd5.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +86 -8
  3. package/build/higher-order/compose.js +5 -4
  4. package/build/higher-order/compose.js.map +1 -1
  5. package/build/higher-order/pipe.js +93 -0
  6. package/build/higher-order/pipe.js.map +1 -0
  7. package/build/hooks/use-debounce/index.js +11 -10
  8. package/build/hooks/use-debounce/index.js.map +1 -1
  9. package/build/hooks/use-disabled/index.js +3 -1
  10. package/build/hooks/use-disabled/index.js.map +1 -1
  11. package/build/hooks/use-drop-zone/index.js +4 -10
  12. package/build/hooks/use-drop-zone/index.js.map +1 -1
  13. package/build/hooks/use-fixed-window-list/index.js +5 -5
  14. package/build/hooks/use-fixed-window-list/index.js.map +1 -1
  15. package/build/hooks/use-instance-id/index.js +22 -20
  16. package/build/hooks/use-instance-id/index.js.map +1 -1
  17. package/build/hooks/use-throttle/index.js +12 -8
  18. package/build/hooks/use-throttle/index.js.map +1 -1
  19. package/build/index.js +37 -0
  20. package/build/index.js.map +1 -1
  21. package/build/index.native.js +37 -0
  22. package/build/index.native.js.map +1 -1
  23. package/build/utils/debounce/index.js +230 -0
  24. package/build/utils/debounce/index.js.map +1 -0
  25. package/build/utils/throttle/index.js +98 -0
  26. package/build/utils/throttle/index.js.map +1 -0
  27. package/build-module/higher-order/compose.js +4 -3
  28. package/build-module/higher-order/compose.js.map +1 -1
  29. package/build-module/higher-order/pipe.js +85 -0
  30. package/build-module/higher-order/pipe.js.map +1 -0
  31. package/build-module/hooks/use-debounce/index.js +10 -9
  32. package/build-module/hooks/use-debounce/index.js.map +1 -1
  33. package/build-module/hooks/use-disabled/index.js +3 -2
  34. package/build-module/hooks/use-disabled/index.js.map +1 -1
  35. package/build-module/hooks/use-drop-zone/index.js +4 -10
  36. package/build-module/hooks/use-drop-zone/index.js.map +1 -1
  37. package/build-module/hooks/use-fixed-window-list/index.js +5 -5
  38. package/build-module/hooks/use-fixed-window-list/index.js.map +1 -1
  39. package/build-module/hooks/use-instance-id/index.js +20 -19
  40. package/build-module/hooks/use-instance-id/index.js.map +1 -1
  41. package/build-module/hooks/use-throttle/index.js +11 -7
  42. package/build-module/hooks/use-throttle/index.js.map +1 -1
  43. package/build-module/index.js +7 -2
  44. package/build-module/index.js.map +1 -1
  45. package/build-module/index.native.js +7 -2
  46. package/build-module/index.native.js.map +1 -1
  47. package/build-module/utils/debounce/index.js +221 -0
  48. package/build-module/utils/debounce/index.js.map +1 -0
  49. package/build-module/utils/throttle/index.js +88 -0
  50. package/build-module/utils/throttle/index.js.map +1 -0
  51. package/build-types/higher-order/compose.d.ts +2 -5
  52. package/build-types/higher-order/compose.d.ts.map +1 -1
  53. package/build-types/higher-order/pipe.d.ts +61 -0
  54. package/build-types/higher-order/pipe.d.ts.map +1 -0
  55. package/build-types/hooks/use-debounce/index.d.ts +6 -6
  56. package/build-types/hooks/use-debounce/index.d.ts.map +1 -1
  57. package/build-types/hooks/use-disabled/index.d.ts.map +1 -1
  58. package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
  59. package/build-types/hooks/use-instance-id/index.d.ts +11 -6
  60. package/build-types/hooks/use-instance-id/index.d.ts.map +1 -1
  61. package/build-types/hooks/use-throttle/index.d.ts +6 -6
  62. package/build-types/hooks/use-throttle/index.d.ts.map +1 -1
  63. package/build-types/index.d.ts +3 -0
  64. package/build-types/utils/debounce/index.d.ts +104 -0
  65. package/build-types/utils/debounce/index.d.ts.map +1 -0
  66. package/build-types/utils/throttle/index.d.ts +77 -0
  67. package/build-types/utils/throttle/index.d.ts.map +1 -0
  68. package/package.json +8 -8
  69. package/src/higher-order/compose.ts +5 -3
  70. package/src/higher-order/pipe.ts +76 -0
  71. package/src/higher-order/test/compose.ts +39 -0
  72. package/src/higher-order/test/pipe.ts +37 -0
  73. package/src/hooks/use-debounce/index.js +11 -9
  74. package/src/hooks/use-disabled/index.js +3 -2
  75. package/src/hooks/use-drop-zone/index.js +6 -13
  76. package/src/hooks/use-fixed-window-list/index.js +5 -5
  77. package/src/hooks/use-instance-id/index.ts +59 -0
  78. package/src/hooks/use-throttle/index.js +11 -7
  79. package/src/index.js +6 -1
  80. package/src/index.native.js +6 -1
  81. package/src/utils/debounce/index.ts +260 -0
  82. package/src/utils/debounce/test/index.ts +367 -0
  83. package/src/utils/throttle/index.ts +95 -0
  84. package/src/utils/throttle/test/index.ts +256 -0
  85. package/tsconfig.tsbuildinfo +1 -1
  86. package/src/hooks/use-instance-id/index.js +0 -42
@@ -107,11 +107,6 @@ export default function useDropZone( {
107
107
 
108
108
  isDragging = true;
109
109
 
110
- ownerDocument.removeEventListener(
111
- 'dragenter',
112
- maybeDragStart
113
- );
114
-
115
110
  // Note that `dragend` doesn't fire consistently for file and
116
111
  // HTML drag events where the drag origin is outside the browser
117
112
  // window. In Firefox it may also not fire if the originating
@@ -161,6 +156,8 @@ export default function useDropZone( {
161
156
  // leaving the drop zone, which means the `relatedTarget`
162
157
  // (element that has been entered) should be outside the drop
163
158
  // zone.
159
+ // Note: This is not entirely reliable in Safari due to this bug
160
+ // https://bugs.webkit.org/show_bug.cgi?id=66547
164
161
  if ( isElementInZone( event.relatedTarget ) ) {
165
162
  return;
166
163
  }
@@ -200,7 +197,6 @@ export default function useDropZone( {
200
197
 
201
198
  isDragging = false;
202
199
 
203
- ownerDocument.addEventListener( 'dragenter', maybeDragStart );
204
200
  ownerDocument.removeEventListener( 'dragend', maybeDragEnd );
205
201
  ownerDocument.removeEventListener( 'mousemove', maybeDragEnd );
206
202
 
@@ -219,12 +215,6 @@ export default function useDropZone( {
219
215
  ownerDocument.addEventListener( 'dragenter', maybeDragStart );
220
216
 
221
217
  return () => {
222
- onDropRef.current = null;
223
- onDragStartRef.current = null;
224
- onDragEnterRef.current = null;
225
- onDragLeaveRef.current = null;
226
- onDragEndRef.current = null;
227
- onDragOverRef.current = null;
228
218
  delete element.dataset.isDropZone;
229
219
  element.removeEventListener( 'drop', onDrop );
230
220
  element.removeEventListener( 'dragenter', onDragEnter );
@@ -232,7 +222,10 @@ export default function useDropZone( {
232
222
  element.removeEventListener( 'dragleave', onDragLeave );
233
223
  ownerDocument.removeEventListener( 'dragend', maybeDragEnd );
234
224
  ownerDocument.removeEventListener( 'mousemove', maybeDragEnd );
235
- ownerDocument.addEventListener( 'dragenter', maybeDragStart );
225
+ ownerDocument.removeEventListener(
226
+ 'dragenter',
227
+ maybeDragStart
228
+ );
236
229
  };
237
230
  },
238
231
  [ isDisabled ]
@@ -1,8 +1,3 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { debounce } from 'lodash';
5
-
6
1
  /**
7
2
  * WordPress dependencies
8
3
  */
@@ -10,6 +5,11 @@ import { useState, useLayoutEffect } from '@wordpress/element';
10
5
  import { getScrollContainer } from '@wordpress/dom';
11
6
  import { PAGEUP, PAGEDOWN, HOME, END } from '@wordpress/keycodes';
12
7
 
8
+ /**
9
+ * Internal dependencies
10
+ */
11
+ import { debounce } from '../../utils/debounce';
12
+
13
13
  const DEFAULT_INIT_WINDOW_SIZE = 30;
14
14
 
15
15
  /**
@@ -0,0 +1,59 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useMemo } from '@wordpress/element';
5
+
6
+ const instanceMap = new WeakMap< object, number >();
7
+
8
+ /**
9
+ * Creates a new id for a given object.
10
+ *
11
+ * @param object Object reference to create an id for.
12
+ * @return The instance id (index).
13
+ */
14
+ function createId( object: object ): number {
15
+ const instances = instanceMap.get( object ) || 0;
16
+ instanceMap.set( object, instances + 1 );
17
+ return instances;
18
+ }
19
+
20
+ /**
21
+ * Specify the useInstanceId *function* signatures.
22
+ *
23
+ * More accurately, useInstanceId distinguishes between three different
24
+ * signatures:
25
+ *
26
+ * 1. When only object is given, the returned value is a number
27
+ * 2. When object and prefix is given, the returned value is a string
28
+ * 3. When preferredId is given, the returned value is the type of preferredId
29
+ */
30
+ function useInstanceId( object: object ): number;
31
+ function useInstanceId( object: object, prefix: string ): string;
32
+ function useInstanceId< T extends string | number >(
33
+ object: object,
34
+ prefix: string,
35
+ preferredId?: T
36
+ ): T;
37
+
38
+ /**
39
+ * Provides a unique instance ID.
40
+ *
41
+ * @param object Object reference to create an id for.
42
+ * @param [prefix] Prefix for the unique id.
43
+ * @param [preferredId] Default ID to use.
44
+ * @return The unique instance id.
45
+ */
46
+ function useInstanceId(
47
+ object: object,
48
+ prefix?: string,
49
+ preferredId?: string | number
50
+ ): string | number {
51
+ return useMemo( () => {
52
+ if ( preferredId ) return preferredId;
53
+ const id = createId( object );
54
+
55
+ return prefix ? `${ prefix }-${ id }` : id;
56
+ }, [ object ] );
57
+ }
58
+
59
+ export default useInstanceId;
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import { throttle } from 'lodash';
5
4
  import { useMemoOne } from 'use-memo-one';
6
5
 
7
6
  /**
@@ -10,7 +9,12 @@ import { useMemoOne } from 'use-memo-one';
10
9
  import { useEffect } from '@wordpress/element';
11
10
 
12
11
  /**
13
- * Throttles a function with Lodash's `throttle`. A new throttled function will
12
+ * Internal dependencies
13
+ */
14
+ import { throttle } from '../../utils/throttle';
15
+
16
+ /**
17
+ * Throttles a function similar to Lodash's `throttle`. A new throttled function will
14
18
  * be returned and any scheduled calls cancelled if any of the arguments change,
15
19
  * including the function to throttle, so please wrap functions created on
16
20
  * render in components in `useCallback`.
@@ -19,14 +23,14 @@ import { useEffect } from '@wordpress/element';
19
23
  *
20
24
  * @template {(...args: any[]) => void} TFunc
21
25
  *
22
- * @param {TFunc} fn The function to throttle.
23
- * @param {number} [wait] The number of milliseconds to throttle invocations to.
24
- * @param {import('lodash').ThrottleSettings} [options] The options object. See linked documentation for details.
25
- * @return {import('lodash').DebouncedFunc<TFunc>} Throttled function.
26
+ * @param {TFunc} fn The function to throttle.
27
+ * @param {number} [wait] The number of milliseconds to throttle invocations to.
28
+ * @param {import('../../utils/throttle').ThrottleOptions} [options] The options object. See linked documentation for details.
29
+ * @return {import('../../utils/debounce').DebouncedFunc<TFunc>} Throttled function.
26
30
  */
27
31
  export default function useThrottle( fn, wait, options ) {
28
32
  const throttled = useMemoOne(
29
- () => throttle( fn, wait, options ),
33
+ () => throttle( fn, wait ?? 0, options ),
30
34
  [ fn, wait, options ]
31
35
  );
32
36
  useEffect( () => () => throttled.cancel(), [ throttled ] );
package/src/index.js CHANGED
@@ -1,8 +1,13 @@
1
1
  // The `createHigherOrderComponent` helper and helper types.
2
2
  export * from './utils/create-higher-order-component';
3
+ // The `debounce` helper and its types.
4
+ export * from './utils/debounce';
5
+ // The `throttle` helper and its types.
6
+ export * from './utils/throttle';
3
7
 
4
- // Compose helper (aliased flowRight from Lodash)
8
+ // The `compose` and `pipe` helpers (inspired by `flowRight` and `flow` from Lodash).
5
9
  export { default as compose } from './higher-order/compose';
10
+ export { default as pipe } from './higher-order/pipe';
6
11
 
7
12
  // Higher-order components.
8
13
  export { default as ifCondition } from './higher-order/if-condition';
@@ -1,8 +1,13 @@
1
1
  // The `createHigherOrderComponent` helper and helper types.
2
2
  export * from './utils/create-higher-order-component';
3
+ // The `debounce` helper and its types.
4
+ export * from './utils/debounce';
5
+ // The `throttle` helper and its types.
6
+ export * from './utils/throttle';
3
7
 
4
- // Compose helper (aliased flowRight from Lodash)
8
+ // The `compose` and `pipe` helpers (inspired by `flowRight` and `flow` from Lodash).
5
9
  export { default as compose } from './higher-order/compose';
10
+ export { default as pipe } from './higher-order/pipe';
6
11
 
7
12
  // Higher-order components.
8
13
  export { default as ifCondition } from './higher-order/if-condition';
@@ -0,0 +1,260 @@
1
+ /**
2
+ * Parts of this source were derived and modified from lodash,
3
+ * released under the MIT license.
4
+ *
5
+ * https://github.com/lodash/lodash
6
+ *
7
+ * Copyright JS Foundation and other contributors <https://js.foundation/>
8
+ *
9
+ * Based on Underscore.js, copyright Jeremy Ashkenas,
10
+ * DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>
11
+ *
12
+ * This software consists of voluntary contributions made by many
13
+ * individuals. For exact contribution history, see the revision history
14
+ * available at https://github.com/lodash/lodash
15
+ *
16
+ * The following license applies to all parts of this software except as
17
+ * documented below:
18
+ *
19
+ * ====
20
+ *
21
+ * Permission is hereby granted, free of charge, to any person obtaining
22
+ * a copy of this software and associated documentation files (the
23
+ * "Software"), to deal in the Software without restriction, including
24
+ * without limitation the rights to use, copy, modify, merge, publish,
25
+ * distribute, sublicense, and/or sell copies of the Software, and to
26
+ * permit persons to whom the Software is furnished to do so, subject to
27
+ * the following conditions:
28
+ *
29
+ * The above copyright notice and this permission notice shall be
30
+ * included in all copies or substantial portions of the Software.
31
+ *
32
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39
+ */
40
+
41
+ export interface DebounceOptions {
42
+ leading: boolean;
43
+ maxWait: number;
44
+ trailing: boolean;
45
+ }
46
+
47
+ export interface DebouncedFunc< T extends ( ...args: any[] ) => any > {
48
+ /**
49
+ * Call the original function, but applying the debounce rules.
50
+ *
51
+ * If the debounced function can be run immediately, this calls it and returns its return
52
+ * value.
53
+ *
54
+ * Otherwise, it returns the return value of the last invocation, or undefined if the debounced
55
+ * function was not invoked yet.
56
+ */
57
+ ( ...args: Parameters< T > ): ReturnType< T > | undefined;
58
+
59
+ /**
60
+ * Throw away any pending invocation of the debounced function.
61
+ */
62
+ cancel(): void;
63
+
64
+ /**
65
+ * If there is a pending invocation of the debounced function, invoke it immediately and return
66
+ * its return value.
67
+ *
68
+ * Otherwise, return the value from the last invocation, or undefined if the debounced function
69
+ * was never invoked.
70
+ */
71
+ flush(): ReturnType< T > | undefined;
72
+ }
73
+
74
+ /**
75
+ * A simplified and properly typed version of lodash's `debounce`, that
76
+ * always uses timers instead of sometimes using rAF.
77
+ *
78
+ * Creates a debounced function that delays invoking `func` until after `wait`
79
+ * milliseconds have elapsed since the last time the debounced function was
80
+ * invoked. The debounced function comes with a `cancel` method to cancel delayed
81
+ * `func` invocations and a `flush` method to immediately invoke them. Provide
82
+ * `options` to indicate whether `func` should be invoked on the leading and/or
83
+ * trailing edge of the `wait` timeout. The `func` is invoked with the last
84
+ * arguments provided to the debounced function. Subsequent calls to the debounced
85
+ * function return the result of the last `func` invocation.
86
+ *
87
+ * **Note:** If `leading` and `trailing` options are `true`, `func` is
88
+ * invoked on the trailing edge of the timeout only if the debounced function
89
+ * is invoked more than once during the `wait` timeout.
90
+ *
91
+ * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
92
+ * until the next tick, similar to `setTimeout` with a timeout of `0`.
93
+ *
94
+ * @param {Function} func The function to debounce.
95
+ * @param {number} wait The number of milliseconds to delay.
96
+ * @param {Partial< DebounceOptions >} options The options object.
97
+ * @param {boolean} options.leading Specify invoking on the leading edge of the timeout.
98
+ * @param {number} options.maxWait The maximum time `func` is allowed to be delayed before it's invoked.
99
+ * @param {boolean} options.trailing Specify invoking on the trailing edge of the timeout.
100
+ *
101
+ * @return Returns the new debounced function.
102
+ */
103
+ export const debounce = < FunctionT extends ( ...args: unknown[] ) => unknown >(
104
+ func: FunctionT,
105
+ wait: number,
106
+ options?: Partial< DebounceOptions >
107
+ ) => {
108
+ let lastArgs: Parameters< FunctionT > | undefined;
109
+ let lastThis: unknown | undefined;
110
+ let maxWait = 0;
111
+ let result: ReturnType< FunctionT >;
112
+ let timerId: ReturnType< typeof setTimeout > | undefined;
113
+ let lastCallTime: number | undefined;
114
+
115
+ let lastInvokeTime = 0;
116
+ let leading = false;
117
+ let maxing = false;
118
+ let trailing = true;
119
+
120
+ if ( options ) {
121
+ leading = !! options.leading;
122
+ maxing = 'maxWait' in options;
123
+ if ( options.maxWait !== undefined ) {
124
+ maxWait = Math.max( options.maxWait, wait );
125
+ }
126
+ trailing = 'trailing' in options ? !! options.trailing : trailing;
127
+ }
128
+
129
+ function invokeFunc( time: number ) {
130
+ const args = lastArgs;
131
+ const thisArg = lastThis;
132
+
133
+ lastArgs = undefined;
134
+ lastThis = undefined;
135
+ lastInvokeTime = time;
136
+
137
+ result = func.apply( thisArg, args! ) as ReturnType< FunctionT >;
138
+ return result;
139
+ }
140
+
141
+ function startTimer(
142
+ pendingFunc: () => void,
143
+ waitTime: number | undefined
144
+ ) {
145
+ timerId = setTimeout( pendingFunc, waitTime );
146
+ }
147
+
148
+ function cancelTimer() {
149
+ if ( timerId !== undefined ) {
150
+ clearTimeout( timerId );
151
+ }
152
+ }
153
+
154
+ function leadingEdge( time: number ) {
155
+ // Reset any `maxWait` timer.
156
+ lastInvokeTime = time;
157
+ // Start the timer for the trailing edge.
158
+ startTimer( timerExpired, wait );
159
+ // Invoke the leading edge.
160
+ return leading ? invokeFunc( time ) : result;
161
+ }
162
+
163
+ function getTimeSinceLastCall( time: number ) {
164
+ return time - ( lastCallTime || 0 );
165
+ }
166
+
167
+ function remainingWait( time: number ) {
168
+ const timeSinceLastCall = getTimeSinceLastCall( time );
169
+ const timeSinceLastInvoke = time - lastInvokeTime;
170
+ const timeWaiting = wait - timeSinceLastCall;
171
+
172
+ return maxing
173
+ ? Math.min( timeWaiting, maxWait - timeSinceLastInvoke )
174
+ : timeWaiting;
175
+ }
176
+
177
+ function shouldInvoke( time: number ) {
178
+ const timeSinceLastCall = getTimeSinceLastCall( time );
179
+ const timeSinceLastInvoke = time - lastInvokeTime;
180
+
181
+ // Either this is the first call, activity has stopped and we're at the
182
+ // trailing edge, the system time has gone backwards and we're treating
183
+ // it as the trailing edge, or we've hit the `maxWait` limit.
184
+ return (
185
+ lastCallTime === undefined ||
186
+ timeSinceLastCall >= wait ||
187
+ timeSinceLastCall < 0 ||
188
+ ( maxing && timeSinceLastInvoke >= maxWait )
189
+ );
190
+ }
191
+
192
+ function timerExpired() {
193
+ const time = Date.now();
194
+ if ( shouldInvoke( time ) ) {
195
+ return trailingEdge( time );
196
+ }
197
+ // Restart the timer.
198
+ startTimer( timerExpired, remainingWait( time ) );
199
+ return undefined;
200
+ }
201
+
202
+ function clearTimer() {
203
+ timerId = undefined;
204
+ }
205
+
206
+ function trailingEdge( time: number ) {
207
+ clearTimer();
208
+
209
+ // Only invoke if we have `lastArgs` which means `func` has been
210
+ // debounced at least once.
211
+ if ( trailing && lastArgs ) {
212
+ return invokeFunc( time );
213
+ }
214
+ lastArgs = lastThis = undefined;
215
+ return result;
216
+ }
217
+
218
+ function cancel() {
219
+ cancelTimer();
220
+ lastInvokeTime = 0;
221
+ clearTimer();
222
+ lastArgs = lastCallTime = lastThis = undefined;
223
+ }
224
+
225
+ function flush() {
226
+ return pending() ? trailingEdge( Date.now() ) : result;
227
+ }
228
+
229
+ function pending() {
230
+ return timerId !== undefined;
231
+ }
232
+
233
+ function debounced( this: unknown, ...args: Parameters< FunctionT > ) {
234
+ const time = Date.now();
235
+ const isInvoking = shouldInvoke( time );
236
+
237
+ lastArgs = args;
238
+ lastThis = this;
239
+ lastCallTime = time;
240
+
241
+ if ( isInvoking ) {
242
+ if ( ! pending() ) {
243
+ return leadingEdge( lastCallTime );
244
+ }
245
+ if ( maxing ) {
246
+ // Handle invocations in a tight loop.
247
+ startTimer( timerExpired, wait );
248
+ return invokeFunc( lastCallTime );
249
+ }
250
+ }
251
+ if ( ! pending() ) {
252
+ startTimer( timerExpired, wait );
253
+ }
254
+ return result;
255
+ }
256
+ debounced.cancel = cancel;
257
+ debounced.flush = flush;
258
+ debounced.pending = pending;
259
+ return debounced;
260
+ };