-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
130 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { test, vi, expect } from "vitest"; | ||
import { cachePromise } from "./cache-promise.ts"; | ||
|
||
test("should cache the resolved value of a promise", async () => { | ||
const mockFunction = vi.fn(); | ||
mockFunction.mockResolvedValueOnce("Cached value"); | ||
|
||
const promise = new Promise<string>((resolve) => { | ||
resolve(mockFunction()); | ||
}); | ||
|
||
const cached = cachePromise<string>(); | ||
|
||
// First call, should invoke the promise | ||
const result1 = await cached(promise); | ||
expect(result1).toBe("Cached value"); | ||
expect(mockFunction).toHaveBeenCalledTimes(1); | ||
|
||
// Second call, should use cached value | ||
const result2 = await cached(promise); | ||
expect(result2).toBe("Cached value"); | ||
// The mock function should not have been called again | ||
expect(mockFunction).toHaveBeenCalledTimes(1); | ||
}); |
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,14 @@ | ||
export const cachePromise = <T>() => { | ||
let cache: T | null = null; | ||
let isCacheSet = false; | ||
|
||
return async function (promise: Promise<T>): Promise<T> { | ||
if (isCacheSet) { | ||
return cache as T; | ||
} | ||
|
||
cache = await promise; | ||
isCacheSet = true; | ||
return cache; | ||
}; | ||
}; |
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,16 @@ | ||
import { TextField } from "./mobx-form.ts"; | ||
import { makePersistable } from "mobx-persist-store"; | ||
|
||
export const persistableField = <T>( | ||
field: TextField<T>, | ||
storageKey: string, | ||
): TextField<T> => { | ||
makePersistable(field, { | ||
name: storageKey, | ||
properties: ["value"], | ||
storage: window.localStorage, | ||
expireIn: 86400000, // One day in milliseconds | ||
}); | ||
|
||
return field; | ||
}; |
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
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