Skip to content

Commit

Permalink
Merge branch 'main' into kevmo314-patch-3
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmo314 authored Mar 3, 2024
2 parents 88238fc + b1c9351 commit 75e6cfa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 200 deletions.
189 changes: 0 additions & 189 deletions pkg/protocol/protocol.go

This file was deleted.

23 changes: 12 additions & 11 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@ import { RangeResolver } from "./resolver";
export function cache(resolver: RangeResolver): RangeResolver {
const cache: [
[number, number],
Promise<{ data: ArrayBuffer; totalLength: number }>,
Promise<{ data: ArrayBuffer; totalLength: number }[]>,
][] = [];

return async ({
start,
end,
}): Promise<{ data: ArrayBuffer; totalLength: number }> => {
return async ([{ start, end }]): Promise<
{ data: ArrayBuffer; totalLength: number }[]
> => {
// check if start-end is contained in any of the cached ranges
const cached = cache.find(([[s, e]]) => s <= start && end <= e);
if (cached) {
return cached[1].then((cachedData) => {
const data = cachedData.data.slice(
const data = cachedData[0].data.slice(
start - cached[0][0],
end - cached[0][0],
);
return {
data,
totalLength: cachedData.totalLength,
};
return [
{
data,
totalLength: cachedData[0].totalLength,
},
];
});
}

// TODO: check if start-end overlaps with any of the cached ranges

const promise = resolver({ start, end });
const promise = resolver([{ start, end }]);
cache.push([[start, end], promise]);
return promise;
};
Expand Down

0 comments on commit 75e6cfa

Please sign in to comment.