-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
createExternallyManagedCache new cacheError and cacheValue methods
- Loading branch information
Showing
10 changed files
with
220 additions
and
31 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/suspense-website/src/examples/createExternallyManagedCache/cacheError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { managedCache } from "./createCache"; | ||
|
||
// REMOVE_BEFORE | ||
|
||
const error = Error("Could not load JSON with id:example"); | ||
|
||
managedCache.cacheError(error, "example"); |
7 changes: 7 additions & 0 deletions
7
packages/suspense-website/src/examples/createExternallyManagedCache/cacheValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { managedCache } from "./createCache"; | ||
|
||
const json = {} as JSON; | ||
|
||
// REMOVE_BEFORE | ||
|
||
managedCache.cacheValue(json, "example"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 94 additions & 19 deletions
113
packages/suspense/src/cache/createExternallyManagedCache.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,118 @@ | ||
import { | ||
STATUS_NOT_FOUND, | ||
STATUS_PENDING, | ||
STATUS_REJECTED, | ||
STATUS_RESOLVED, | ||
} from "../constants"; | ||
import { ExternallyManagedCache } from "../types"; | ||
import { createExternallyManagedCache } from "./createExternallyManagedCache"; | ||
|
||
describe("createExternallyManagedCache", () => { | ||
it("should create a cache with a no-op load method", () => { | ||
const cache = createExternallyManagedCache<[string], string>({ | ||
let cache: ExternallyManagedCache<[string], string>; | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
|
||
cache = createExternallyManagedCache({ | ||
debugLabel: "cache", | ||
getKey: ([string]) => string, | ||
timeout: 100, | ||
timeoutMessage: "Custom timeout message", | ||
}); | ||
}); | ||
|
||
it("should cache values", () => { | ||
expect(cache.getValueIfCached("test")).toBeUndefined(); | ||
cache.cacheValue("value", "test"); | ||
expect(cache.getStatus("test")).toBe(STATUS_RESOLVED); | ||
expect(cache.getValue("test")).toBe("value"); | ||
}); | ||
|
||
it("should cache errors", () => { | ||
expect(cache.getValueIfCached("test")).toBeUndefined(); | ||
cache.cache("value", "test"); | ||
expect(cache.getValueIfCached("test")).toBe("value"); | ||
cache.cacheError("expected error", "test"); | ||
expect(cache.getStatus("test")).toBe(STATUS_REJECTED); | ||
expect(() => cache.getValue("test")).toThrow("expected error"); | ||
}); | ||
|
||
describe("subscribeToStatus", () => { | ||
let callback: jest.Mock; | ||
|
||
beforeEach(() => { | ||
callback = jest.fn(); | ||
}); | ||
|
||
it("should update when resolved", async () => { | ||
cache.subscribeToStatus(callback, "test"); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
expect(callback).toHaveBeenCalledWith(STATUS_NOT_FOUND); | ||
|
||
cache.cacheValue("value", "test"); | ||
|
||
await Promise.resolve(); | ||
|
||
expect(callback).toHaveBeenCalledTimes(3); | ||
expect(callback).toHaveBeenCalledWith(STATUS_PENDING); | ||
expect(callback).toHaveBeenCalledWith(STATUS_RESOLVED); | ||
}); | ||
|
||
it("should update when rejected", async () => { | ||
cache.subscribeToStatus(callback, "test"); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
expect(callback).toHaveBeenCalledWith(STATUS_NOT_FOUND); | ||
|
||
cache.cacheError("expected error", "test"); | ||
|
||
await Promise.resolve(); | ||
|
||
expect(callback).toHaveBeenCalledTimes(3); | ||
expect(callback).toHaveBeenCalledWith(STATUS_PENDING); | ||
expect(callback).toHaveBeenCalledWith(STATUS_REJECTED); | ||
}); | ||
}); | ||
|
||
describe("timeout", () => { | ||
it("should reject after the specified timeout", async () => { | ||
const cache = createExternallyManagedCache<[string], string>({ | ||
debugLabel: "cache", | ||
getKey: ([string]) => string, | ||
timeout: 100, | ||
timeoutMessage: "Custom timeout message", | ||
}); | ||
|
||
const promise = cache.readAsync("test"); | ||
|
||
jest.advanceTimersByTime(1_000); | ||
|
||
await expect(promise).rejects.toEqual("Custom timeout message"); | ||
}); | ||
|
||
it("should resolve if cached before the specified timeout", async () => { | ||
const cache = createExternallyManagedCache<[string], string>({ | ||
debugLabel: "cache", | ||
getKey: ([string]) => string, | ||
timeout: 100, | ||
timeoutMessage: "Custom timeout message", | ||
}); | ||
it("should reject if an error is cached before the specified timeout", async () => { | ||
const promise = cache.readAsync("test"); | ||
|
||
cache.cacheError("expected error", "test"); | ||
|
||
let thrown = null; | ||
try { | ||
await promise; | ||
} catch (error) { | ||
thrown = error; | ||
} | ||
|
||
expect(thrown).toBe("expected error"); | ||
}); | ||
|
||
it("should resolve if an error is cached before the specified timeout", async () => { | ||
const promise = cache.readAsync("test"); | ||
|
||
cache.cache("value", "test"); | ||
cache.cacheValue("value", "test"); | ||
|
||
await expect(promise).resolves.toEqual("value"); | ||
}); | ||
|
||
it("should wait forever if no timeout is specified", async () => { | ||
cache = createExternallyManagedCache({ | ||
debugLabel: "cache", | ||
getKey: ([string]) => string, | ||
}); | ||
cache.readAsync("test"); | ||
|
||
jest.advanceTimersByTime(60_000); | ||
}); | ||
}); | ||
}); |
62 changes: 56 additions & 6 deletions
62
packages/suspense/src/cache/createExternallyManagedCache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,73 @@ | ||
import { Cache } from "../types"; | ||
import { createCache, CreateCacheOptions } from "./createCache"; | ||
import { CacheLoadOptions, ExternallyManagedCache } from "../types"; | ||
import { | ||
updateRecordToRejected, | ||
updateRecordToResolved, | ||
} from "../utils/Record"; | ||
import { isPendingRecord } from "../utils/isRecordStatus"; | ||
import { createCache, CreateCacheOptions, InternalCache } from "./createCache"; | ||
|
||
export function createExternallyManagedCache<Params extends Array<any>, Value>( | ||
options: Omit<CreateCacheOptions<Params, Value>, "load"> & { | ||
timeout?: number; | ||
timeoutMessage?: string; | ||
} | ||
): Cache<Params, Value> { | ||
): ExternallyManagedCache<Params, Value> { | ||
const { timeout, timeoutMessage = "Timed out", ...rest } = options; | ||
|
||
return createCache<Params, Value>({ | ||
const decoratedCache = createCache<Params, Value>({ | ||
...rest, | ||
load: async () => | ||
load: async (params: Params, loadOptions: CacheLoadOptions) => | ||
new Promise((resolve, reject) => { | ||
if (timeout != null) { | ||
setTimeout(() => { | ||
reject(timeoutMessage); | ||
if (!loadOptions.signal.aborted) { | ||
reject(timeoutMessage); | ||
} | ||
}, timeout); | ||
} | ||
}), | ||
}); | ||
|
||
const { __getKey, __getOrCreateRecord, __notifySubscribers, __recordMap } = | ||
decoratedCache as InternalCache<Params, Value>; | ||
|
||
const { cache, ...api } = decoratedCache; | ||
|
||
return { | ||
...api, | ||
cacheError(error, ...params) { | ||
const key = __getKey(params); | ||
const record = __getOrCreateRecord(...params); | ||
if (isPendingRecord(record)) { | ||
const { abortController, deferred } = record.data; | ||
|
||
abortController.abort(); | ||
|
||
updateRecordToRejected(record, error); | ||
|
||
// Don't leave any pending request hanging | ||
deferred.reject(error); | ||
} | ||
|
||
__recordMap.set(key, record); | ||
__notifySubscribers(params); | ||
}, | ||
cacheValue(value, ...params) { | ||
const key = __getKey(params); | ||
const record = __getOrCreateRecord(...params); | ||
if (isPendingRecord(record)) { | ||
const { abortController, deferred } = record.data; | ||
|
||
abortController.abort(); | ||
|
||
updateRecordToResolved(record, value); | ||
|
||
// Don't leave any pending request hanging | ||
deferred.resolve(value); | ||
} | ||
|
||
__recordMap.set(key, record); | ||
__notifySubscribers(params); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters