@amazon-devices/vega-carousel 0.1.3

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 (71) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +0 -0
  3. package/dist/Carousel/Carousel.d.ts +16 -0
  4. package/dist/Carousel/Carousel.js +102 -0
  5. package/dist/Carousel/Utils.d.ts +4 -0
  6. package/dist/Carousel/Utils.js +35 -0
  7. package/dist/Carousel/index.d.ts +7 -0
  8. package/dist/Carousel/index.js +23 -0
  9. package/dist/Carousel/types.d.ts +406 -0
  10. package/dist/Carousel/types.js +2 -0
  11. package/dist/CarouselData/CarouselDataAdapter.d.ts +141 -0
  12. package/dist/CarouselData/CarouselDataAdapter.js +67 -0
  13. package/dist/CarouselData/CarouselDataResolver.d.ts +100 -0
  14. package/dist/CarouselData/CarouselDataResolver.js +224 -0
  15. package/dist/CarouselData/CarouselDataTypes.d.ts +280 -0
  16. package/dist/CarouselData/CarouselDataTypes.js +84 -0
  17. package/dist/CarouselData/index.d.ts +3 -0
  18. package/dist/CarouselData/index.js +24 -0
  19. package/dist/CarouselScrollable/CarouselScrollable.d.ts +67 -0
  20. package/dist/CarouselScrollable/CarouselScrollable.js +77 -0
  21. package/dist/CarouselScrollable/CarouselScrollableProps.d.ts +499 -0
  22. package/dist/CarouselScrollable/CarouselScrollableProps.js +211 -0
  23. package/dist/CarouselScrollable/CarouselScrollableRef.d.ts +80 -0
  24. package/dist/CarouselScrollable/CarouselScrollableRef.js +7 -0
  25. package/dist/CarouselScrollable/CarouselScrollableTypes.d.ts +224 -0
  26. package/dist/CarouselScrollable/CarouselScrollableTypes.js +116 -0
  27. package/dist/CarouselScrollable/CarouselSimple.d.ts +71 -0
  28. package/dist/CarouselScrollable/CarouselSimple.js +111 -0
  29. package/dist/CarouselScrollable/CarouselSimpleProps.d.ts +29 -0
  30. package/dist/CarouselScrollable/CarouselSimpleProps.js +7 -0
  31. package/dist/CarouselScrollable/index.d.ts +6 -0
  32. package/dist/CarouselScrollable/index.js +27 -0
  33. package/dist/CarouselScrollable/native/CarouselNativeHostComponent.d.ts +109 -0
  34. package/dist/CarouselScrollable/native/CarouselNativeHostComponent.js +82 -0
  35. package/dist/CarouselScrollable/native/CarouselNativeItemHostComponent.d.ts +94 -0
  36. package/dist/CarouselScrollable/native/CarouselNativeItemHostComponent.js +42 -0
  37. package/dist/CarouselScrollable/native/CarouselNativeItemWrapper.d.ts +255 -0
  38. package/dist/CarouselScrollable/native/CarouselNativeItemWrapper.js +276 -0
  39. package/dist/CarouselScrollable/native/CarouselNativeLogic.d.ts +209 -0
  40. package/dist/CarouselScrollable/native/CarouselNativeLogic.js +349 -0
  41. package/dist/CarouselScrollable/native/CarouselNativeWrapper.d.ts +68 -0
  42. package/dist/CarouselScrollable/native/CarouselNativeWrapper.js +164 -0
  43. package/dist/CarouselScrollable/native/index.d.ts +5 -0
  44. package/dist/CarouselScrollable/native/index.js +26 -0
  45. package/dist/CarouselScrollable/recyclerview/JSRecyclerViewList.d.ts +25 -0
  46. package/dist/CarouselScrollable/recyclerview/JSRecyclerViewList.js +150 -0
  47. package/dist/index.d.ts +3 -0
  48. package/dist/index.js +24 -0
  49. package/dist/kepler/aarch64-debug/lib/com.amazon.keplerscript.carousel.so +0 -0
  50. package/dist/kepler/aarch64-debug/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
  51. package/dist/kepler/aarch64-release/lib/com.amazon.keplerscript.carousel.so +0 -0
  52. package/dist/kepler/aarch64-release/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
  53. package/dist/kepler/armv7-debug/lib/com.amazon.keplerscript.carousel.so +0 -0
  54. package/dist/kepler/armv7-debug/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
  55. package/dist/kepler/armv7-release/lib/com.amazon.keplerscript.carousel.so +0 -0
  56. package/dist/kepler/armv7-release/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
  57. package/dist/kepler/tm-manifest.json +1 -0
  58. package/dist/kepler/x86_64-debug/lib/com.amazon.keplerscript.carousel.so +0 -0
  59. package/dist/kepler/x86_64-debug/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
  60. package/dist/kepler/x86_64-release/lib/com.amazon.keplerscript.carousel.so +0 -0
  61. package/dist/kepler/x86_64-release/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
  62. package/dist/utils/CarouselLogger.d.ts +295 -0
  63. package/dist/utils/CarouselLogger.js +370 -0
  64. package/dist/utils/CarouselReactNativeUtilities.d.ts +108 -0
  65. package/dist/utils/CarouselReactNativeUtilities.js +150 -0
  66. package/dist/utils/HelperTypes.d.ts +5 -0
  67. package/dist/utils/HelperTypes.js +7 -0
  68. package/dist/utils/index.d.ts +3 -0
  69. package/dist/utils/index.js +24 -0
  70. package/package.json +1 -0
  71. package/react-native.config.js +22 -0
@@ -0,0 +1,141 @@
1
+ import React from 'react';
2
+ import { CarouselDataErrorNotifyFunc, GetItemFunc, GetItemKeyFunc, CarouselDataChangeCallback, GetItemCountFunc, GetSelectedItemOffsetFunc } from './CarouselDataTypes';
3
+ import { ScaleFactor } from '../CarouselScrollable/CarouselScrollableTypes';
4
+ /**
5
+ * This type represents the _outer properties_ for a {@link CarouselDataAdapter}
6
+ * object, meaning that the properties in this type are used to get data **out**
7
+ * of a {@link CarouselDataAdapter}.
8
+ *
9
+ * @template ItemT The type of data items the list will use to render its items.
10
+ * @template KeyT The type used as items unique identifiers (also called _keys_,
11
+ * or _stable IDs_) in the list;
12
+ *
13
+ * @see {@link CarouselDataAdapter}
14
+ */
15
+ export type CarouselDataAdapterReader<ItemT, KeyT = React.Key> = {
16
+ /**
17
+ * A function that will receive an index, and return an item's data object.
18
+ *
19
+ * The object returned will be used to call other data-access functions to
20
+ * retrieve information about specific items.
21
+ *
22
+ * @function
23
+ * @see {@link GetItemFunc}
24
+ */
25
+ getItem: GetItemFunc<ItemT>;
26
+ getItemCount: GetItemCountFunc;
27
+ /**
28
+ * A function to retrieve a different shift value to be applied for an item.
29
+ *
30
+ * @function
31
+ * @see {@link GetSelectedItemOffsetFunc}
32
+ */
33
+ getSelectedItemOffset?: GetSelectedItemOffsetFunc<ItemT>;
34
+ /**
35
+ * A function to retrieve an item’s unchangeable identifier that is
36
+ * independent of the index position the item is currently located.
37
+ *
38
+ * @function
39
+ * @see {@link GetItemKeyFunc}
40
+ * @see {@link getItem}
41
+ */
42
+ getItemKey: GetItemKeyFunc<ItemT, KeyT>;
43
+ /**
44
+ * This function is called by the list component when an item cannot be used
45
+ * in the list.
46
+ *
47
+ * This happens when either {@link getItem} or {@link renderItem} functions
48
+ * return an `undefined` value.
49
+ *
50
+ * @function
51
+ * @see {@link CarouselDataErrorNotifyFunc}
52
+ * @see {@link getItem}
53
+ * @see {@link renderItem}
54
+ */
55
+ notifyDataError: CarouselDataErrorNotifyFunc;
56
+ };
57
+ /**
58
+ * A type representing a Data Adapter for the {@link CarouselScrollable} component.
59
+ *
60
+ * Because the {@link CarouselScrollable} component is a hybrid between Native and React
61
+ * Native code, it was designed to not pass data representing items to the
62
+ * Native layer.
63
+ *
64
+ * This simplified the design but created a necessity for the Native component
65
+ * to be an _index-based_ list.
66
+ *
67
+ * Due to the dynamic nature of the data where this component will be used, it
68
+ * was necessary to create a _translation layer_ between the real data set, and
69
+ * the sequential data the {@link CarouselScrollable} requires.
70
+ *
71
+ * The `CarouselDataAdapter` is the contract for this _translation layer_.
72
+ *
73
+ * @template ItemT The type of data items the list will use to render its items.
74
+ * @template KeyT The type used as items unique identifiers (also called _keys_,
75
+ * or _stable IDs_) in the list;
76
+ *
77
+ * @see {@link CarouselDataAdapterReader}
78
+ * @see {@link resolveDataItems}
79
+ */
80
+ export type CarouselDataAdapter<ItemT, KeyT = React.Key> = CarouselDataAdapterReader<ItemT, KeyT> & {
81
+ /**
82
+ * Optional function that, when given, will be called by the {@link CarouselScrollable}
83
+ * component to register an internal function that will call the
84
+ * {@link CarouselScrollableRef.notifyDataChange} imperative function on behalf of the
85
+ * adapter.
86
+ *
87
+ * This is useful when you want to provide an implementation of
88
+ * `CarouselDataAdapter` that does not force the user of the object to call the
89
+ * list's imperative function whenever a data change happens; doing so will,
90
+ * essentially, create an adapter that can update a {@link CarouselScrollable}
91
+ * component without user intervention.
92
+ *
93
+ * @param {CarouselDataChangeCallback} callback A function to be called by the
94
+ * data adapter implementation whenever there is a change happening in
95
+ * the data provided to the {@link CarouselScrollable}.
96
+ *
97
+ * @function
98
+ * @see {@link CarouselDataChangeCallback}
99
+ */
100
+ registerDataChangeCallback?: (callback: CarouselDataChangeCallback) => void;
101
+ };
102
+ /**
103
+ * Convenience function that encapsulates the retrieval of an item's stable
104
+ * identifier (its _key_) from a {@link CarouselDataAdapter}.
105
+ *
106
+ * Note that this function will not distinguish from an item that can't be
107
+ * retrieved and an item with a key that can't be retrieved.
108
+ *
109
+ * @template ItemT The type of data items the list will use to render its items.
110
+ * @template KeyT The type used as items unique identifiers (also called _keys_,
111
+ * or _stable IDs_) in the list;
112
+ *
113
+ * @param {CarouselDataAdapterReader<ItemT, KeyT>} adapter The adapter object to be
114
+ * used to read the item and its key.
115
+ * @param {number} index The data index for the item you want to retrieve the
116
+ * stable identifier from.
117
+ *
118
+ * @return {KeyT | undefined} Returns the item's key if it is found; otherwise
119
+ * `undefined`.
120
+ */
121
+ export declare function getItemKey<ItemT, KeyT = React.Key>(adapter: CarouselDataAdapterReader<ItemT, KeyT>, index: number): KeyT | undefined;
122
+ /**
123
+ * Convenience function that encapsulates the retrieval of an item's shift
124
+ * from a {@link CarouselDataAdapter}.
125
+ *
126
+ * Note that this function will not distinguish from an item that can't be
127
+ * retrieved and an item with a shift that can't be retrieved.
128
+ *
129
+ * @template ItemT The type of data items the list will use to render its items.
130
+ * @template KeyT The type used as items unique identifiers (also called _keys_,
131
+ * or _stable IDs_) in the list;
132
+ *
133
+ * @param {CarouselDataAdapterReader<ItemT, KeyT>} adapter The adapter object to be
134
+ * used to read the item and its key.
135
+ * @param {number} index The data index for the item you want to retrieve the
136
+ * stable identifier from.
137
+ *
138
+ * @return {number | undefined} Returns the item's shift if it is found; otherwise
139
+ * `undefined`.
140
+ */
141
+ export declare function getSelectedItemOffset<ItemT, KeyT = React.Key>(adapter: CarouselDataAdapterReader<ItemT, KeyT>, index: number): ScaleFactor | undefined;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024 - 2025 Amazon.com, Inc. or its affiliates. All rights reserved.
4
+ *
5
+ * PROPRIETARY/CONFIDENTIAL. USE IS SUBJECT TO LICENSE TERMS.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.getSelectedItemOffset = exports.getItemKey = void 0;
9
+ const utils_1 = require("../utils");
10
+ /**
11
+ * Convenience function that encapsulates the retrieval of an item's stable
12
+ * identifier (its _key_) from a {@link CarouselDataAdapter}.
13
+ *
14
+ * Note that this function will not distinguish from an item that can't be
15
+ * retrieved and an item with a key that can't be retrieved.
16
+ *
17
+ * @template ItemT The type of data items the list will use to render its items.
18
+ * @template KeyT The type used as items unique identifiers (also called _keys_,
19
+ * or _stable IDs_) in the list;
20
+ *
21
+ * @param {CarouselDataAdapterReader<ItemT, KeyT>} adapter The adapter object to be
22
+ * used to read the item and its key.
23
+ * @param {number} index The data index for the item you want to retrieve the
24
+ * stable identifier from.
25
+ *
26
+ * @return {KeyT | undefined} Returns the item's key if it is found; otherwise
27
+ * `undefined`.
28
+ */
29
+ function getItemKey(adapter, index) {
30
+ const item = adapter.getItem(index);
31
+ if (item !== undefined) {
32
+ return adapter.getItemKey(item, index);
33
+ }
34
+ else {
35
+ utils_1.Log.d('Trying to read the "key" of an item that does not exist in the given adapter: index={0}', index);
36
+ }
37
+ }
38
+ exports.getItemKey = getItemKey;
39
+ /**
40
+ * Convenience function that encapsulates the retrieval of an item's shift
41
+ * from a {@link CarouselDataAdapter}.
42
+ *
43
+ * Note that this function will not distinguish from an item that can't be
44
+ * retrieved and an item with a shift that can't be retrieved.
45
+ *
46
+ * @template ItemT The type of data items the list will use to render its items.
47
+ * @template KeyT The type used as items unique identifiers (also called _keys_,
48
+ * or _stable IDs_) in the list;
49
+ *
50
+ * @param {CarouselDataAdapterReader<ItemT, KeyT>} adapter The adapter object to be
51
+ * used to read the item and its key.
52
+ * @param {number} index The data index for the item you want to retrieve the
53
+ * stable identifier from.
54
+ *
55
+ * @return {number | undefined} Returns the item's shift if it is found; otherwise
56
+ * `undefined`.
57
+ */
58
+ function getSelectedItemOffset(adapter, index) {
59
+ const item = adapter.getItem(index);
60
+ if (item !== undefined) {
61
+ return adapter.getSelectedItemOffset?.(item, index);
62
+ }
63
+ else {
64
+ utils_1.Log.d('Trying to read the "shift" of an item that does not exist in the given adapter: index={0}', index);
65
+ }
66
+ }
67
+ exports.getSelectedItemOffset = getSelectedItemOffset;
@@ -0,0 +1,100 @@
1
+ import { CarouselDataReadConfig, CarouselResolvedDataValues, RenderItemFunc } from './CarouselDataTypes';
2
+ import React from 'react';
3
+ import { CarouselDataAdapterReader } from './CarouselDataAdapter';
4
+ /**
5
+ * Constant representing the default number of times the
6
+ * {@link resolveDataItems} function will call the
7
+ * {@link CarouselDataAdapterReader.getItem} function from the adapter before it
8
+ * considers the item failed to be retrieved.
9
+ *
10
+ * @see {@link resolveDataItems}
11
+ */
12
+ export declare const ITEM_FAILURE_DEFAULT_MAX_RETRIES = 2;
13
+ /**
14
+ * Helper function to create a {@link CarouselDataReadConfig} object that read a
15
+ * single item on the given `index`.
16
+ *
17
+ * @param {number} index The data position to try to read the item.
18
+ * @param {number} maxRetries Optional maximum number of retries that the
19
+ * {@link resolveDataItems} function will attempt before failing.
20
+ *
21
+ * @return {CarouselDataReadConfig} Returns a read configuration object with the
22
+ * specifications for a single item read.
23
+ */
24
+ export declare function dataReadAtTargetConfig(index: number, maxRetries?: number): CarouselDataReadConfig;
25
+ /**
26
+ * Helper function to create a {@link CarouselDataReadConfig} object that read a
27
+ * multiple items, starting from the given `startIndex`, going backwards, and
28
+ * stopping when the `shouldReadNext` predicate returns `false` (or an item
29
+ * failed to resolve `maxRetries` times).
30
+ *
31
+ * @param {number} startIndex The data position to try to read the item.
32
+ * @param {(lastIndexRead: number) => boolean} shouldReadNext A predicate that
33
+ * informs the {@link resolveDataItems} function if it should read the
34
+ * previous item or not.
35
+ * @param {number} maxRetries Optional maximum number of retries that the
36
+ * {@link resolveDataItems} function will attempt before failing.
37
+ *
38
+ * @return {CarouselDataReadConfig} Returns a read configuration object with the
39
+ * specifications for a multiple items backwards read call.
40
+ */
41
+ export declare function dataReadBackwardsConfig(startIndex: number, shouldReadNext: (lastIndexRead: number) => boolean, maxRetries?: number): CarouselDataReadConfig;
42
+ /**
43
+ * Helper function to create a {@link CarouselDataReadConfig} object that read a
44
+ * multiple items, starting from the given `startIndex`, going forward, and
45
+ * stopping when the `shouldReadNext` predicate returns `false` (or an item
46
+ * failed to resolve `maxRetries` times).
47
+ *
48
+ * @param {number} startIndex The data position to try to read the item.
49
+ * @param {(lastIndexRead: number) => boolean} shouldReadNext A predicate that
50
+ * informs the {@link resolveDataItems} function if it should read the
51
+ * next item or not.
52
+ * @param {number} maxRetries Optional maximum number of retries that the
53
+ * {@link resolveDataItems} function will attempt before failing.
54
+ *
55
+ * @return {CarouselDataReadConfig} Returns a read configuration object with the
56
+ * specifications for a multiple items forward read call.
57
+ */
58
+ export declare function dataReadForwardConfig(startIndex: number, shouldReadNext: (lastIndexRead: number) => boolean, maxRetries?: number): CarouselDataReadConfig;
59
+ /**
60
+ * Function used to retrieve and resolve items from an adapter with safeguards
61
+ * for failures, and retry logic.
62
+ *
63
+ * One can retrieve a single item or a series of them (reading backwards or
64
+ * forward) depending on the read configuration given in the `readConfig`
65
+ * parameter.
66
+ *
67
+ * When there is a problem retrieving or resolving an item, this function will
68
+ * call the {@link CarouselDataAdapterReader.notifyDataError} function to give an
69
+ * opportunity for the adapter to adjust its data, and be prepared for a retry.
70
+ *
71
+ * An important point on the _retry logic_ used here, is the concept that, if
72
+ * the function called {@link CarouselDataAdapterReader.notifyDataError}, it is
73
+ * communicating that there is no way for the failed item to be resolved and
74
+ * used from now on, and such an item should not be part of the data set.
75
+ *
76
+ * @template ItemT The type of the data object retrieved by the adapter and
77
+ * resolved by the resolver.
78
+ * @template KeyT Optional type used as data items unique identifiers (also
79
+ * called _stable IDs_, or _keys_); when not provided, defaults to
80
+ * {@link React.Key}.
81
+ *
82
+ * @param {CarouselDataAdapterReader<ItemT, KeyT>} dataAdapter The data adapter
83
+ * reader used to retrieve item objects to resolve.
84
+ * @param {RenderItemFunc<ItemT>} renderItem The resolver function. This
85
+ * function is responsible for reading an item object and resolve it to a
86
+ * {@link React.ReactElement} one.
87
+ * @param {CarouselDataReadConfig} readConfig The object containing the
88
+ * configuration values that will drive the resolve call.
89
+ * @param {(item: React.ReactElement) => void} onItemResolved Callback called
90
+ * when the `resolveDataItems` function finishes retrieving and resolving
91
+ * an item from a Data Adapter.
92
+ *
93
+ * @return {number | undefined} Returns the last index fully resolved, or
94
+ * `undefined` if the function could not resolve anything.
95
+ *
96
+ * @see {@link CarouselDataAdapterReader}
97
+ * @see {@link RenderItemFunc}
98
+ * @see {@link CarouselDataReadConfig}
99
+ */
100
+ export declare function resolveDataItems<ItemT, KeyT = React.Key>(dataAdapter: CarouselDataAdapterReader<ItemT, KeyT>, renderItem: RenderItemFunc<ItemT>, readConfig: CarouselDataReadConfig, onItemResolved: (resolved: CarouselResolvedDataValues<KeyT>) => void): number | undefined;
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024 - 2025 Amazon.com, Inc. or its affiliates. All rights reserved.
4
+ *
5
+ * PROPRIETARY/CONFIDENTIAL. USE IS SUBJECT TO LICENSE TERMS.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.resolveDataItems = exports.dataReadForwardConfig = exports.dataReadBackwardsConfig = exports.dataReadAtTargetConfig = exports.ITEM_FAILURE_DEFAULT_MAX_RETRIES = void 0;
9
+ const CarouselDataTypes_1 = require("./CarouselDataTypes");
10
+ const utils_1 = require("../utils");
11
+ /**
12
+ * Constant representing the default number of times the
13
+ * {@link resolveDataItems} function will call the
14
+ * {@link CarouselDataAdapterReader.getItem} function from the adapter before it
15
+ * considers the item failed to be retrieved.
16
+ *
17
+ * @see {@link resolveDataItems}
18
+ */
19
+ exports.ITEM_FAILURE_DEFAULT_MAX_RETRIES = 2;
20
+ /**
21
+ * Helper function to create a {@link CarouselDataReadConfig} object that read a
22
+ * single item on the given `index`.
23
+ *
24
+ * @param {number} index The data position to try to read the item.
25
+ * @param {number} maxRetries Optional maximum number of retries that the
26
+ * {@link resolveDataItems} function will attempt before failing.
27
+ *
28
+ * @return {CarouselDataReadConfig} Returns a read configuration object with the
29
+ * specifications for a single item read.
30
+ */
31
+ function dataReadAtTargetConfig(index, maxRetries = exports.ITEM_FAILURE_DEFAULT_MAX_RETRIES) {
32
+ return {
33
+ startIndex: index,
34
+ readDirection: CarouselDataTypes_1.CarouselDataReadDirection.AT_TARGET,
35
+ maxRetries,
36
+ };
37
+ }
38
+ exports.dataReadAtTargetConfig = dataReadAtTargetConfig;
39
+ /**
40
+ * Helper function to create a {@link CarouselDataReadConfig} object that read a
41
+ * multiple items, starting from the given `startIndex`, going backwards, and
42
+ * stopping when the `shouldReadNext` predicate returns `false` (or an item
43
+ * failed to resolve `maxRetries` times).
44
+ *
45
+ * @param {number} startIndex The data position to try to read the item.
46
+ * @param {(lastIndexRead: number) => boolean} shouldReadNext A predicate that
47
+ * informs the {@link resolveDataItems} function if it should read the
48
+ * previous item or not.
49
+ * @param {number} maxRetries Optional maximum number of retries that the
50
+ * {@link resolveDataItems} function will attempt before failing.
51
+ *
52
+ * @return {CarouselDataReadConfig} Returns a read configuration object with the
53
+ * specifications for a multiple items backwards read call.
54
+ */
55
+ function dataReadBackwardsConfig(startIndex, shouldReadNext, maxRetries = exports.ITEM_FAILURE_DEFAULT_MAX_RETRIES) {
56
+ return {
57
+ startIndex,
58
+ readDirection: CarouselDataTypes_1.CarouselDataReadDirection.BACKWARDS,
59
+ shouldReadNext,
60
+ maxRetries,
61
+ };
62
+ }
63
+ exports.dataReadBackwardsConfig = dataReadBackwardsConfig;
64
+ /**
65
+ * Helper function to create a {@link CarouselDataReadConfig} object that read a
66
+ * multiple items, starting from the given `startIndex`, going forward, and
67
+ * stopping when the `shouldReadNext` predicate returns `false` (or an item
68
+ * failed to resolve `maxRetries` times).
69
+ *
70
+ * @param {number} startIndex The data position to try to read the item.
71
+ * @param {(lastIndexRead: number) => boolean} shouldReadNext A predicate that
72
+ * informs the {@link resolveDataItems} function if it should read the
73
+ * next item or not.
74
+ * @param {number} maxRetries Optional maximum number of retries that the
75
+ * {@link resolveDataItems} function will attempt before failing.
76
+ *
77
+ * @return {CarouselDataReadConfig} Returns a read configuration object with the
78
+ * specifications for a multiple items forward read call.
79
+ */
80
+ function dataReadForwardConfig(startIndex, shouldReadNext, maxRetries = exports.ITEM_FAILURE_DEFAULT_MAX_RETRIES) {
81
+ return {
82
+ startIndex,
83
+ readDirection: CarouselDataTypes_1.CarouselDataReadDirection.FORWARD,
84
+ shouldReadNext,
85
+ maxRetries,
86
+ };
87
+ }
88
+ exports.dataReadForwardConfig = dataReadForwardConfig;
89
+ /**
90
+ * Helper function that performs the required tasks when an item resolve request
91
+ * fails.
92
+ *
93
+ * It will notify the adapter about the error, and check if the
94
+ * {@link resolveDataItems} should retry the request.
95
+ *
96
+ * @template ItemT The type of the data object retrieved by the adapter and
97
+ * resolved by the resolver.
98
+ * @template KeyT Optional type used as data items unique identifiers (also
99
+ * called _stable IDs_, or _keys_); when not provided, defaults to
100
+ * {@link React.Key}.
101
+ *
102
+ * @param {CarouselDataAdapterReader<ItemT, KeyT>} dataAdapter The data adapter
103
+ * reader used to retrieve the item that failed.
104
+ * @param {CarouselDataReadConfig} readConfig The object containing the
105
+ * configuration values that drove the resolve call.
106
+ * @param {number} currentRetryCount The number of prior attempts the
107
+ * {@link resolveDataItems} function tried to resolve the failed item.
108
+ * @param {number} index The data index of the item that failed to resolve.
109
+ * @param {CarouselDataErrorReason} errorType The reason for the failure that will
110
+ * be passed to the adapter when we notify if about the error.
111
+ *
112
+ * @return {boolean} Returns `true` if the {@link resolveDataItems} function
113
+ * should retry the request, and `false` otherwise.
114
+ *
115
+ * @see {@link resolveDataItems}
116
+ */
117
+ function handleResolveError(dataAdapter, readConfig, currentRetryCount, index, errorType) {
118
+ const shouldRetry = dataAdapter.notifyDataError({
119
+ index,
120
+ readConfig,
121
+ reason: errorType,
122
+ });
123
+ if (shouldRetry && currentRetryCount < readConfig.maxRetries) {
124
+ utils_1.Log.d('Failed to retrieve the data item for the index {0} (retries: {1})... Will retry...', index, currentRetryCount);
125
+ }
126
+ else {
127
+ utils_1.Log.d('Failed to retrieve the data item for the index {0} (retries: {1})', index, currentRetryCount);
128
+ }
129
+ return shouldRetry;
130
+ }
131
+ /**
132
+ * Function used to retrieve and resolve items from an adapter with safeguards
133
+ * for failures, and retry logic.
134
+ *
135
+ * One can retrieve a single item or a series of them (reading backwards or
136
+ * forward) depending on the read configuration given in the `readConfig`
137
+ * parameter.
138
+ *
139
+ * When there is a problem retrieving or resolving an item, this function will
140
+ * call the {@link CarouselDataAdapterReader.notifyDataError} function to give an
141
+ * opportunity for the adapter to adjust its data, and be prepared for a retry.
142
+ *
143
+ * An important point on the _retry logic_ used here, is the concept that, if
144
+ * the function called {@link CarouselDataAdapterReader.notifyDataError}, it is
145
+ * communicating that there is no way for the failed item to be resolved and
146
+ * used from now on, and such an item should not be part of the data set.
147
+ *
148
+ * @template ItemT The type of the data object retrieved by the adapter and
149
+ * resolved by the resolver.
150
+ * @template KeyT Optional type used as data items unique identifiers (also
151
+ * called _stable IDs_, or _keys_); when not provided, defaults to
152
+ * {@link React.Key}.
153
+ *
154
+ * @param {CarouselDataAdapterReader<ItemT, KeyT>} dataAdapter The data adapter
155
+ * reader used to retrieve item objects to resolve.
156
+ * @param {RenderItemFunc<ItemT>} renderItem The resolver function. This
157
+ * function is responsible for reading an item object and resolve it to a
158
+ * {@link React.ReactElement} one.
159
+ * @param {CarouselDataReadConfig} readConfig The object containing the
160
+ * configuration values that will drive the resolve call.
161
+ * @param {(item: React.ReactElement) => void} onItemResolved Callback called
162
+ * when the `resolveDataItems` function finishes retrieving and resolving
163
+ * an item from a Data Adapter.
164
+ *
165
+ * @return {number | undefined} Returns the last index fully resolved, or
166
+ * `undefined` if the function could not resolve anything.
167
+ *
168
+ * @see {@link CarouselDataAdapterReader}
169
+ * @see {@link RenderItemFunc}
170
+ * @see {@link CarouselDataReadConfig}
171
+ */
172
+ function resolveDataItems(dataAdapter, renderItem, readConfig, onItemResolved) {
173
+ const [stepValue, indexAdjustment] = (() => {
174
+ switch (readConfig.readDirection) {
175
+ case CarouselDataTypes_1.CarouselDataReadDirection.AT_TARGET:
176
+ return [0, 0];
177
+ case CarouselDataTypes_1.CarouselDataReadDirection.BACKWARDS:
178
+ return [-1, 1];
179
+ case CarouselDataTypes_1.CarouselDataReadDirection.FORWARD:
180
+ return [1, -1];
181
+ }
182
+ })();
183
+ let itemData;
184
+ let element;
185
+ let index = readConfig.startIndex;
186
+ let retryCount = 0;
187
+ let retry = true;
188
+ while (retry &&
189
+ retryCount < readConfig.maxRetries &&
190
+ (readConfig.shouldReadNext?.(index) ?? true)) {
191
+ itemData = dataAdapter.getItem(index);
192
+ if (itemData !== undefined) {
193
+ element = renderItem(itemData, index);
194
+ if (element !== undefined) {
195
+ onItemResolved({
196
+ index,
197
+ element,
198
+ key: dataAdapter.getItemKey(itemData, index),
199
+ shift: dataAdapter.getSelectedItemOffset?.(itemData, index),
200
+ });
201
+ retryCount = 0; // reset the retry count when we resolve an item
202
+ index += stepValue;
203
+ if (readConfig.readDirection === CarouselDataTypes_1.CarouselDataReadDirection.AT_TARGET) {
204
+ // If this is a targeted read, we know we won't request more items on
205
+ // this call, so we can short-circuit this loop and leave earlier.
206
+ // This will also help not to get stuck on an infinite loop when the
207
+ // user forgets to add the correct _stop check_ to the
208
+ // `canRequestMoreData` function.
209
+ break;
210
+ }
211
+ }
212
+ else {
213
+ retry = handleResolveError(dataAdapter, readConfig, ++retryCount, index, CarouselDataTypes_1.CarouselDataErrorReason.CANNOT_RENDER);
214
+ }
215
+ }
216
+ else {
217
+ retry = handleResolveError(dataAdapter, readConfig, ++retryCount, index, CarouselDataTypes_1.CarouselDataErrorReason.CANNOT_READ);
218
+ }
219
+ }
220
+ if (retryCount < readConfig.maxRetries && itemData !== undefined) {
221
+ return index + indexAdjustment;
222
+ }
223
+ }
224
+ exports.resolveDataItems = resolveDataItems;