Skip to content

Commit

Permalink
createIntervalCache: Remove comparePoints param (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn authored Apr 25, 2023
2 parents bbd4475 + a615901 commit 0e35897
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 66 deletions.

This file was deleted.

6 changes: 0 additions & 6 deletions packages/suspense-website/src/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ const createIntervalCache = {
cache: processExample(
readFileSync(join(__dirname, "createIntervalCache", "cache.ts"), "utf8")
),
cacheWithBigIntInterval: processExample(
readFileSync(
join(__dirname, "createIntervalCache", "cacheWithBigIntInterval.ts"),
"utf8"
)
),
cacheWithPartialResults: processExample(
readFileSync(
join(__dirname, "createIntervalCache", "cacheWithPartialResults.ts"),
Expand Down
34 changes: 9 additions & 25 deletions packages/suspense-website/src/routes/api/createIntervalCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ export default function Route() {
<Block>
<p>
An "interval cache" is a specialized cache that incrementally loads
and merges sets of values over time.
and merges sets of values over time. Values are loaded for "intervals"
which are a range of either numbers (or{" "}
<code>
<ExternalLink to="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt">
BigInts
</ExternalLink>
</code>
)
</p>
<p>
An example of this is{" "}
An example of an interval cache can be found in{" "}
<ExternalLink to="https://replay.io">Replay.io</ExternalLink> which
fetches console logs for the region of a recording a user has
"focused" on. If the user changes the focused region, additional logs
Expand All @@ -41,29 +48,6 @@ export default function Route() {
</p>
<Code code={createIntervalCache.cache} />
</Block>
<Block>
<SubHeading title="Custom comparisons" />
<p>
Points in an interval are typically numbers (e.g. 1, 3.5) but they can
also be{" "}
<code>
<ExternalLink to="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt">
BigInts
</ExternalLink>
</code>
. In that case a custom comparison function should be provided.
</p>
<p>
Here is an example using the NPM package{" "}
<code>
<ExternalLink to="https://www.npmjs.com/package/extra-bigint">
extra-bigint
</ExternalLink>
</code>
:
</p>
<Code code={createIntervalCache.cacheWithBigIntInterval} />
</Block>
<Block>
<SubHeading title="Aborting requests" />
<p>
Expand Down
3 changes: 3 additions & 0 deletions packages/suspense/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 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.
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.35",
"version": "0.0.36",
"description": "Utilities for working with React suspense",
"author": "Brian Vaughn <[email protected]>",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,37 +126,40 @@ describe("createIntervalCache", () => {
});

describe("configuration", () => {
it("should support bigint points via a custom comparePoints", async () => {
const comparePoints = jest.fn(compareBigInt);

it("should support bigint points", async () => {
const load = jest.fn();
load.mockImplementation((start: number, end: number, id: string) => [
start,
]);

const bigIntCache = createIntervalCache<bigint, [id: string], bigint>({
comparePoints,
getPointForValue: (value) => value,
load,
});

await bigIntCache.readAsync(BigInt("2"), BigInt("4"), "test");

expect(comparePoints).toHaveBeenCalled();
await bigIntCache.readAsync(
BigInt("10000000000000000000000000000000002"),
BigInt("10000000000000000000000000000000004"),
"test"
);

expect(load).toHaveBeenCalledTimes(1);
expect(load).toHaveBeenCalledWith(
BigInt("2"),
BigInt("4"),
BigInt("10000000000000000000000000000000002"),
BigInt("10000000000000000000000000000000004"),
"test",
expect.anything()
);

await bigIntCache.readAsync(BigInt("3"), BigInt("7"), "test");
await bigIntCache.readAsync(
BigInt("10000000000000000000000000000000003"),
BigInt("10000000000000000000000000000000007"),
"test"
);
expect(load).toHaveBeenCalledTimes(2);
expect(load).toHaveBeenCalledWith(
BigInt("4"),
BigInt("7"),
BigInt("10000000000000000000000000000000004"),
BigInt("10000000000000000000000000000000007"),
"test",
expect.anything()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from "../../constants";
import {
IntervalCacheLoadOptions,
ComparisonFunction,
GetPointForValue,
PendingRecord,
IntervalCache,
Expand Down Expand Up @@ -72,7 +71,6 @@ export function createIntervalCache<
Params extends Array<any>,
Value
>(options: {
comparePoints?: ComparisonFunction<Point>;
debugLabel?: string;
getKey?: (...params: Params) => string;
getPointForValue: GetPointForValue<Point, Value>;
Expand All @@ -83,7 +81,6 @@ export function createIntervalCache<
) => PromiseLike<ValuesArray<Value>> | ValuesArray<Value>;
}): IntervalCache<Point, Params, Value> {
const {
comparePoints = defaultComparePoints,
debugLabel,
getKey = defaultGetKey,
getPointForValue,
Expand Down Expand Up @@ -766,6 +763,6 @@ export function createIntervalCache<
};
}

function defaultComparePoints(a: any, b: any): number {
return a - b;
function comparePoints(a: any, b: any): number {
return Number(a - b);
}

1 comment on commit 0e35897

@vercel
Copy link

@vercel vercel bot commented on 0e35897 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.