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
6 changed files
with
142 additions
and
82 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
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 |
---|---|---|
@@ -1,15 +1,47 @@ | ||
import { CacheValue } from "../lib/cache-ref"; | ||
import { suspendOnPromise } from "../lib/suspend"; | ||
|
||
export const createSingleValueCache = <T>() => { | ||
const storage = { | ||
current: null as CacheValue<T> | null, | ||
}; | ||
export interface SingleValueCacheStorage<T> { | ||
get(): CacheValue<T> | undefined | null; | ||
set(value: CacheValue<T> | null): void; | ||
} | ||
|
||
export declare namespace SingleValueCacheStorage { | ||
export type Any = SingleValueCacheStorage<any>; | ||
|
||
export type ValueType<S extends Any> = S extends SingleValueCacheStorage< | ||
infer T | ||
> | ||
? T | ||
: never; | ||
} | ||
|
||
export type SingleValueUseFn<T> = (fn: () => Promise<T>) => T; | ||
|
||
const use = (fn: () => Promise<T>) => | ||
suspendOnPromise(fn, storage.current, (value) => { | ||
storage.current = value; | ||
}); | ||
export interface SingleValueCache<Storage extends SingleValueCacheStorage.Any> { | ||
storage: Storage; | ||
use: SingleValueUseFn<SingleValueCacheStorage.ValueType<Storage>>; | ||
} | ||
|
||
return { use, storage }; | ||
export const createSingleValueCacheWithStorage = < | ||
Storage extends SingleValueCacheStorage.Any | ||
>( | ||
storage: Storage | ||
): SingleValueCache<Storage> => ({ | ||
storage, | ||
use: (fn) => suspendOnPromise(fn, storage.get(), storage.set), | ||
}); | ||
|
||
const makeSimpleStorage = <T>(): SingleValueCacheStorage<T> => { | ||
let storage: CacheValue<T> | null; | ||
|
||
return { | ||
get: () => storage, | ||
set: (value) => { | ||
storage = value; | ||
}, | ||
}; | ||
}; | ||
|
||
export const createSingleValueCache = <T>() => | ||
createSingleValueCacheWithStorage(makeSimpleStorage<T>()); |
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,4 +1,4 @@ | ||
export { createKeyedCache } from "./cache/keyed"; | ||
export { createSingleValueCache } from "./cache/single-value"; | ||
export { createKeyedSuspense } from "./suspense/keyed"; | ||
export { createSingleValueSuspense } from "./suspense/single-value"; | ||
export * from "./cache/keyed"; | ||
export * from "./cache/single-value"; | ||
export * from "./suspense/keyed"; | ||
export * from "./suspense/single-value"; |
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 |
---|---|---|
@@ -1,12 +1,7 @@ | ||
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; | ||
const { storage, use } = createSingleValueCache<T>(); | ||
const useSingleValueSuspense = () => use(fn); | ||
return { use: useSingleValueSuspense, storage }; | ||
}; |