react-native-onyx 3.0.41 → 3.0.43
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.
|
@@ -86,7 +86,7 @@ const provider = {
|
|
|
86
86
|
if (!provider.store) {
|
|
87
87
|
throw new Error('Store is not initialized!');
|
|
88
88
|
}
|
|
89
|
-
const query = 'REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?,
|
|
89
|
+
const query = 'REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, ?);';
|
|
90
90
|
const params = pairs.map((pair) => [pair[0], JSON.stringify(pair[1] === undefined ? null : pair[1])]);
|
|
91
91
|
if (utils_1.default.isEmptyObject(params)) {
|
|
92
92
|
return Promise.resolve();
|
|
@@ -100,12 +100,16 @@ const provider = {
|
|
|
100
100
|
const commands = [];
|
|
101
101
|
// Query to merge the change into the DB value.
|
|
102
102
|
const patchQuery = `INSERT INTO keyvaluepairs (record_key, valueJSON)
|
|
103
|
-
VALUES (:key,
|
|
103
|
+
VALUES (:key, :value)
|
|
104
104
|
ON CONFLICT DO UPDATE
|
|
105
|
-
SET valueJSON = JSON_PATCH(valueJSON,
|
|
105
|
+
SET valueJSON = JSON_PATCH(valueJSON, :value);
|
|
106
106
|
`;
|
|
107
107
|
const patchQueryArguments = [];
|
|
108
108
|
// Query to fully replace the nested objects of the DB value.
|
|
109
|
+
// NOTE: The JSON() wrapper around the replacement value is required here. Unlike JSON_PATCH (which
|
|
110
|
+
// parses both arguments as JSON internally), JSON_REPLACE treats a plain TEXT binding as a quoted
|
|
111
|
+
// JSON string. Without JSON(), objects would be stored as string values (e.g. "{...}") instead of
|
|
112
|
+
// actual JSON objects, corrupting the stored data.
|
|
109
113
|
const replaceQuery = `UPDATE keyvaluepairs
|
|
110
114
|
SET valueJSON = JSON_REPLACE(valueJSON, ?, JSON(?))
|
|
111
115
|
WHERE record_key = ?;
|
package/dist/useOnyx.js
CHANGED
|
@@ -164,6 +164,10 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
164
164
|
OnyxConnectionManager_1.default.addToEvictionBlockList(connectionRef.current);
|
|
165
165
|
}
|
|
166
166
|
}, [key, options === null || options === void 0 ? void 0 : options.canEvict]);
|
|
167
|
+
// Tracks the last memoizedSelector reference that getSnapshot() has computed with.
|
|
168
|
+
// When the selector changes, this mismatch forces getSnapshot() to re-evaluate
|
|
169
|
+
// even if all other conditions (isFirstConnection, shouldGetCachedValue, key) are false.
|
|
170
|
+
const lastComputedSelectorRef = (0, react_1.useRef)(memoizedSelector);
|
|
167
171
|
const getSnapshot = (0, react_1.useCallback)(() => {
|
|
168
172
|
var _a, _b, _c, _d;
|
|
169
173
|
// Check if we have any cache for this Onyx key
|
|
@@ -186,10 +190,12 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
186
190
|
// We get the value from cache while the first connection to Onyx is being made or if the key has changed,
|
|
187
191
|
// so we can return any cached value right away. For the case where the key has changed, If we don't return the cached value right away, then the UI will show the incorrect (previous) value for a brief period which looks like a UI glitch to the user. After the connection is made, we only
|
|
188
192
|
// update `newValueRef` when `Onyx.connect()` callback is fired.
|
|
189
|
-
|
|
193
|
+
const hasSelectorChanged = lastComputedSelectorRef.current !== memoizedSelector;
|
|
194
|
+
if (isFirstConnectionRef.current || shouldGetCachedValueRef.current || key !== previousKey || hasSelectorChanged) {
|
|
190
195
|
// Gets the value from cache and maps it with selector. It changes `null` to `undefined` for `useOnyx` compatibility.
|
|
191
196
|
const value = OnyxUtils_1.default.tryGetCachedValue(key);
|
|
192
197
|
const selectedValue = memoizedSelector ? memoizedSelector(value) : value;
|
|
198
|
+
lastComputedSelectorRef.current = memoizedSelector;
|
|
193
199
|
newValueRef.current = (selectedValue !== null && selectedValue !== void 0 ? selectedValue : undefined);
|
|
194
200
|
// We set this flag to `false` again since we don't want to get the newest cached value every time `getSnapshot()` is executed,
|
|
195
201
|
// and only when `Onyx.connect()` callback is fired.
|