generated from cprecioso/template-browser-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
67 additions
and
35 deletions.
There are no files selected for viewing
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 { CacheValue } from "../lib/cache-ref"; | ||
import { suspendOnPromise } from "../lib/suspend"; | ||
|
||
export interface KeyedCacheStorage<K, T> { | ||
get(key: K): CacheValue<T> | undefined | null; | ||
set(key: K, value: CacheValue<T>): void; | ||
} | ||
|
||
export const createKeyedCache = <K, T>( | ||
storage: KeyedCacheStorage<K, T> = new Map() | ||
) => { | ||
const use = (key: K, fn: () => Promise<T>) => | ||
suspendOnPromise(fn, storage.get(key), (value) => storage.set(key, value)); | ||
|
||
return { use, storage }; | ||
}; |
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,15 @@ | ||
import { CacheValue } from "../lib/cache-ref"; | ||
import { suspendOnPromise } from "../lib/suspend"; | ||
|
||
export const createSingleValueCache = <T>() => { | ||
const storage = { | ||
current: null as CacheValue<T> | null, | ||
}; | ||
|
||
const use = (fn: () => Promise<T>) => | ||
suspendOnPromise(fn, storage.current, (value) => { | ||
storage.current = value; | ||
}); | ||
|
||
return { use, storage }; | ||
}; |
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 +1,4 @@ | ||
export { createSingleValueSuspense } from "./single-value"; | ||
export { createKeyedCache } from "./cache/keyed"; | ||
export { createSingleValueCache } from "./cache/single-value"; | ||
export { createKeyedSuspense } from "./suspense/keyed"; | ||
export { createSingleValueSuspense } from "./suspense/single-value"; |
File renamed without changes.
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createKeyedCache } from "../cache/keyed"; | ||
|
||
export const createKeyedSuspense = <K extends string, T>( | ||
fn: (key: K) => Promise<T> | ||
) => { | ||
const cache = createKeyedCache<K, T>(); | ||
|
||
const useKeyedSuspense = (key: K) => { | ||
const value = cache.use(key, () => fn(key)); | ||
return { value, cache }; | ||
}; | ||
|
||
return useKeyedSuspense; | ||
}; |
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,12 @@ | ||
import { createSingleValueCache } from "../cache/single-value"; | ||
|
||
export const createSingleValueSuspense = <T>(fn: () => Promise<T>) => { | ||
const cache = createSingleValueCache<T>(); | ||
|
||
const useSingleValueSuspense = () => { | ||
const value = cache.use(fn); | ||
return { value, cache }; | ||
}; | ||
|
||
return useSingleValueSuspense; | ||
}; |