Skip to content

Commit

Permalink
createIntervalCache: Edge case bug fix caught by enabling TSC --noUnc…
Browse files Browse the repository at this point in the history
…heckedIndexedAccess flag (#31)
  • Loading branch information
bvaughn authored Apr 25, 2023
2 parents db99d25 + 2b9f33e commit 71e5c07
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 74 deletions.
12 changes: 6 additions & 6 deletions packages/array-sorting-utilities/src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function configure<Value>(
function find(sortedItems: Value[], targetItem: Value): Value | null {
const index = findIndex(sortedItems, targetItem, true);
if (index >= 0) {
return sortedItems[index];
return sortedItems[index]!;
} else {
return null;
}
Expand All @@ -26,7 +26,7 @@ export function configure<Value>(
while (lowIndex <= highIndex) {
middleIndex = (lowIndex + highIndex) >>> 1;

const currentItem = sortedItems[middleIndex];
const currentItem = sortedItems[middleIndex]!;
const value = compareValues(targetItem, currentItem);
if (value === 0) {
return middleIndex;
Expand All @@ -47,7 +47,7 @@ export function configure<Value>(
return 0;
}

const value = compareValues(targetItem, sortedItems[middleIndex]);
const value = compareValues(targetItem, sortedItems[middleIndex]!);
if (value === 0) {
return middleIndex;
} else {
Expand All @@ -60,8 +60,8 @@ export function configure<Value>(
lowIndex = Math.max(0, middleIndex - 1);
}

return Math.abs(compareValues(targetItem, sortedItems[lowIndex])) <
Math.abs(compareValues(targetItem, sortedItems[highIndex]))
return Math.abs(compareValues(targetItem, sortedItems[lowIndex]!)) <
Math.abs(compareValues(targetItem, sortedItems[highIndex]!))
? lowIndex
: highIndex;
}
Expand All @@ -73,7 +73,7 @@ export function configure<Value>(
let highIndex = sortedItems.length;
while (lowIndex < highIndex) {
let middleIndex = (lowIndex + highIndex) >>> 1;
const currentItem = sortedItems[middleIndex];
const currentItem = sortedItems[middleIndex]!;
if (compareValues(item, currentItem) > 0) {
lowIndex = middleIndex + 1;
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/interval-utilities/src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export function configure<Point>(

const merged: Interval<Point>[] = [];

let prevInterval: Interval<Point> = sortedIntervals[0];
let prevInterval: Interval<Point> = sortedIntervals[0]!;
let currentInterval: Interval<Point> | null = null;

for (let index = 1; index < sortedIntervals.length; index++) {
currentInterval = sortedIntervals[index];
currentInterval = sortedIntervals[index]!;

const tempMerged = merge(prevInterval, currentInterval);
prevInterval = tempMerged.pop()!;
Expand Down
12 changes: 6 additions & 6 deletions packages/point-utilities/src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function configure<Point>(
while (lowIndex <= highIndex) {
middleIndex = (lowIndex + highIndex) >>> 1;

const currentPoint = sortedPoints[middleIndex];
const currentPoint = sortedPoints[middleIndex]!;
const value = comparePoints(targetPoint, currentPoint);
if (value === 0) {
return middleIndex;
Expand All @@ -34,7 +34,7 @@ export function configure<Point>(
return -1;
}

const middlePoint = sortedPoints[middleIndex];
const middlePoint = sortedPoints[middleIndex]!;
const value = comparePoints(targetPoint, middlePoint);
if (value === 0) {
return middleIndex;
Expand All @@ -48,8 +48,8 @@ export function configure<Point>(
lowIndex = Math.max(0, middleIndex - 1);
}

const lowPoint = sortedPoints[lowIndex];
const highPoint = sortedPoints[highIndex];
const lowPoint = sortedPoints[lowIndex]!;
const highPoint = sortedPoints[highIndex]!;

return Math.abs(comparePoints(targetPoint, lowPoint)) <
Math.abs(comparePoints(targetPoint, highPoint))
Expand Down Expand Up @@ -89,7 +89,7 @@ export function configure<Point>(

const index = binarySearch(sortedPoints, targetPoint, false);

const foundPoint = sortedPoints[index];
const foundPoint = sortedPoints[index]!;
const comparison = comparePoints(foundPoint, targetPoint);

return comparison < 0 ? index + 1 : index;
Expand All @@ -105,7 +105,7 @@ export function configure<Point>(

const index = binarySearch(sortedPoints, targetPoint, false);

const foundPoint = sortedPoints[index];
const foundPoint = sortedPoints[index]!;
const comparison = comparePoints(foundPoint, targetPoint);

return comparison > 0 ? index - 1 : index;
Expand Down
5 changes: 4 additions & 1 deletion packages/suspense/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Changelog

## 0.0.37
* `createIntervalCache` edge case bug fix caught by enabling TSC `--noUncheckedIndexedAccess` flag.

## 0.0.36
* `createIntervalCache` removes `comparePoints` param (as it is no longer needed).

## 0.0.35
* `createIntervalCache` supports partial results via new options parameter `options.returnAsPartial(resultsArray)`.
* `createIntervalCache` methods `getValue` and `getStatus` better handle sub-regions of already-loaded intervals.
* ⚠️ `createIntervalCache` no longer supports string points, only `number` and `BigInt` types are supported.
* `createIntervalCache` no longer supports string points, only `number` and `BigInt` types are supported.

## 0.0.34
* `createExternallyManagedCache` cache updated to support caching errors (via `cacheError` method); `cache` method renamed to `cacheValue` to differentiate.
Expand Down
2 changes: 1 addition & 1 deletion packages/suspense/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "suspense",
"version": "0.0.36",
"version": "0.0.37",
"description": "Utilities for working with React suspense",
"author": "Brian Vaughn <[email protected]>",
"license": "MIT",
Expand Down
10 changes: 5 additions & 5 deletions packages/suspense/src/cache/createCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ describe("createCache", () => {
// the cache creates two instances of the WeakRefMap
// collecting the first one is enough to trigger the onEvict callback
expect(weakRefArray.length).toBe(2);
weakRefArray[0].collect();
weakRefArray[0]!.collect();

expect(gcCache.getStatus("test")).toBe(STATUS_NOT_FOUND);
});
Expand All @@ -682,7 +682,7 @@ describe("createCache", () => {
// the cache creates two instances of the WeakRefMap, which in turn make it so we have two weak refs
// collecting the first one is enough to trigger the onEvict callback
expect(weakRefArray.length).toBe(2);
weakRefArray[0].collect();
weakRefArray[0]!.collect();

expect(() => gcCache.getValue("test")).toThrow("No record found");
});
Expand All @@ -694,7 +694,7 @@ describe("createCache", () => {
// the cache creates two instances of the WeakRefMap, which in turn make it so we have two weak refs
// collecting the first one is enough to trigger the onEvict callback
expect(weakRefArray.length).toBe(2);
weakRefArray[0].collect();
weakRefArray[0]!.collect();

expect(gcCache.getValueIfCached("test")).toBeUndefined();
});
Expand All @@ -706,7 +706,7 @@ describe("createCache", () => {
// the cache creates two instances of the WeakRefMap, which in turn make it so we have two weak refs
// collecting the first one is enough to trigger the onEvict callback
expect(weakRefArray.length).toBe(2);
weakRefArray[0].collect();
weakRefArray[0]!.collect();

expect(gcCache.getValueIfCached("test")).toBeUndefined();

Expand All @@ -722,7 +722,7 @@ describe("createCache", () => {
// the cache creates two instances of the WeakRefMap, which in turn make it so we have two weak refs
// collecting the first one is enough to trigger the onEvict callback
expect(weakRefArray.length).toBe(2);
weakRefArray[0].collect();
weakRefArray[0]!.collect();

expect(gcCache.getValueIfCached("test")).toBeUndefined();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ describe("createIntervalCache", () => {

expect(cache.abort("one")).toBe(true);

expect(abortSignals[0].aborted).toBe(true);
expect(abortSignals[1].aborted).toBe(false);
expect(abortSignals[0]!.aborted).toBe(true);
expect(abortSignals[1]!.aborted).toBe(false);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export function createIntervalCache<
index >= 0;
index--
) {
const partialInterval = metadata.intervals.partial[index];
const partialInterval = metadata.intervals.partial[index]!;
if (intervalUtils.intersects([start, end], partialInterval)) {
metadata.intervals.partial.splice(index, 1);

Expand All @@ -471,31 +471,24 @@ export function createIntervalCache<
}
}

let todoDebugStrings = [];

// Merge in newly-loaded values; don't add duplicates though.
// Duplicates may slip in at the edges (because of how intervals are split)
// or they may be encountered as ranges of partial results are refined.
for (let index = 0; index < values.length; index++) {
const value = values[index];
const value = values[index]!;
const insertIndex = arraySortUtils.findInsertIndex(
metadata.sortedValues,
value
);
const itemAtIndex = metadata.sortedValues[insertIndex];
if (
itemAtIndex == null ||
comparePoints(
getPointForValue(itemAtIndex),
getPointForValue(value)
) !== 0
) {
metadata.sortedValues.splice(insertIndex, 0, value);
todoDebugStrings.push(
`${index}: "${value}" insert at ${insertIndex}`,
metadata.sortedValues
);
} else {
todoDebugStrings.push(`${index}: "${value}" duplicate`);
}
}
} catch (error) {
Expand Down
Loading

1 comment on commit 71e5c07

@vercel
Copy link

@vercel vercel bot commented on 71e5c07 Apr 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.