@suspensive/cache
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -1,16 +0,89 @@ | ||
import * as react_jsx_runtime from 'react/jsx-runtime'; | ||
import { ResolvedCached } from './CacheStore.js'; | ||
import { CacheKey, CacheOptions } from './types.js'; | ||
type Listener = (...args: any[]) => unknown; | ||
type Unsubscribe = () => void; | ||
type Subscribe<TListener extends Listener> = (listener: TListener) => Unsubscribe; | ||
declare class Subscribable<TListener extends Listener> { | ||
protected listeners: Set<TListener>; | ||
subscribe: Subscribe<TListener>; | ||
protected notify: () => void; | ||
} | ||
type ExtractPartial<TTarget extends Record<string, unknown>, TExtractor extends Partial<TTarget>> = Extract<TTarget, TExtractor>; | ||
declare const hashCacheKey: (key: CacheKey) => string; | ||
declare const enum CacheStatus { | ||
Idle = "idle", | ||
Pending = "pending", | ||
Resolved = "resolved", | ||
Rejected = "rejected" | ||
} | ||
type AllStatusCached<TData, TCacheKey extends CacheKey = CacheKey> = { | ||
status: CacheStatus.Idle; | ||
promiseToSuspend: undefined; | ||
cacheKey: TCacheKey; | ||
hashedCacheKey: ReturnType<typeof hashCacheKey>; | ||
state: { | ||
promise: undefined; | ||
data: undefined; | ||
error: undefined; | ||
}; | ||
} | { | ||
status: CacheStatus.Pending; | ||
promiseToSuspend: Promise<void>; | ||
cacheKey: TCacheKey; | ||
hashedCacheKey: ReturnType<typeof hashCacheKey>; | ||
state: { | ||
promise: Promise<TData>; | ||
data: undefined; | ||
error: undefined; | ||
}; | ||
} | { | ||
status: CacheStatus.Resolved; | ||
promiseToSuspend: Promise<void>; | ||
cacheKey: TCacheKey; | ||
hashedCacheKey: ReturnType<typeof hashCacheKey>; | ||
state: { | ||
promise: Promise<TData>; | ||
data: TData; | ||
error: undefined; | ||
}; | ||
} | { | ||
status: CacheStatus.Rejected; | ||
promiseToSuspend: Promise<void>; | ||
cacheKey: TCacheKey; | ||
hashedCacheKey: ReturnType<typeof hashCacheKey>; | ||
state: { | ||
promise: Promise<TData>; | ||
data: undefined; | ||
error: unknown; | ||
}; | ||
}; | ||
type IdleCached<TData, TCacheKey extends CacheKey = CacheKey> = ExtractPartial<AllStatusCached<TData, TCacheKey>, { | ||
status: CacheStatus.Idle; | ||
}>; | ||
type PendingCached<TData, TCacheKey extends CacheKey = CacheKey> = ExtractPartial<AllStatusCached<TData, TCacheKey>, { | ||
status: CacheStatus.Pending; | ||
}>; | ||
type ResolvedCached<TData, TCacheKey extends CacheKey = CacheKey> = ExtractPartial<AllStatusCached<TData, TCacheKey>, { | ||
status: CacheStatus.Resolved; | ||
}>; | ||
type RejectedCached<TData, TCacheKey extends CacheKey = CacheKey> = ExtractPartial<AllStatusCached<TData, TCacheKey>, { | ||
status: CacheStatus.Rejected; | ||
}>; | ||
type Cached<TData, TCacheKey extends CacheKey = CacheKey> = IdleCached<TData, TCacheKey> | PendingCached<TData, TCacheKey> | ResolvedCached<TData, TCacheKey> | RejectedCached<TData, TCacheKey>; | ||
/** | ||
* @experimental This is experimental feature. | ||
*/ | ||
interface CacheProps<TData, TCacheKey extends CacheKey> extends CacheOptions<TData, TCacheKey> { | ||
children: (props: ResolvedCached<TData, TCacheKey>['state']) => JSX.Element; | ||
declare class Cache extends Subscribable<() => void> { | ||
private cache; | ||
reset: (options?: Pick<CacheOptions<unknown, CacheKey>, "cacheKey">) => void; | ||
remove: (options: Pick<CacheOptions<unknown, CacheKey>, "cacheKey">) => void; | ||
clearError: (options?: Pick<CacheOptions<unknown, CacheKey>, "cacheKey">) => void; | ||
suspend: <TData, TCacheKey extends CacheKey = CacheKey>({ cacheKey, cacheFn, }: CacheOptions<TData, TCacheKey>) => ResolvedCached<TData, TCacheKey>; | ||
getData: (options: Pick<CacheOptions<unknown, CacheKey>, "cacheKey">) => unknown; | ||
getError: (options: Pick<CacheOptions<unknown, CacheKey>, "cacheKey">) => unknown; | ||
} | ||
/** | ||
* @experimental This is experimental feature. | ||
*/ | ||
declare function Cache<TData, TCacheKey extends CacheKey>({ children, ...options }: CacheProps<TData, TCacheKey>): react_jsx_runtime.JSX.Element; | ||
export { Cache, type CacheProps }; | ||
export { type AllStatusCached, Cache, type Cached, type IdleCached, type PendingCached, type RejectedCached, type ResolvedCached }; |
"use client" | ||
import { | ||
Cache | ||
} from "./chunk-H3HMOTE6.js"; | ||
import "./chunk-HXWWGQRA.js"; | ||
import "./chunk-FFFIOY4B.js"; | ||
import "./chunk-J4LYH5KM.js"; | ||
} from "./chunk-6YXFRBCR.js"; | ||
import "./chunk-QETBZSG5.js"; | ||
@@ -9,0 +6,0 @@ export { |
@@ -0,9 +1,9 @@ | ||
export { Read } from './Read.js'; | ||
export { useRead } from './useRead.js'; | ||
export { cacheOptions } from './cacheOptions.js'; | ||
export { Cache } from './Cache.js'; | ||
export { CacheProvider } from './CacheProvider.js'; | ||
export { useCache } from './useCache.js'; | ||
export { cacheOptions } from './cacheOptions.js'; | ||
export { CacheStore } from './CacheStore.js'; | ||
export { CacheStoreProvider } from './CacheStoreProvider.js'; | ||
export { useCacheStore } from './useCacheStore.js'; | ||
import 'react/jsx-runtime'; | ||
import './types.js'; | ||
import 'react'; |
"use client" | ||
import { | ||
Cache | ||
} from "./chunk-H3HMOTE6.js"; | ||
} from "./chunk-6YXFRBCR.js"; | ||
import { | ||
CacheStore | ||
} from "./chunk-NL3MXUED.js"; | ||
CacheProvider | ||
} from "./chunk-ETI64SK7.js"; | ||
import { | ||
CacheStoreProvider | ||
} from "./chunk-JVEIDWQ2.js"; | ||
Read | ||
} from "./chunk-5VXONFWZ.js"; | ||
import { | ||
@@ -15,17 +15,17 @@ cacheOptions | ||
import { | ||
useRead | ||
} from "./chunk-27UYQ7PV.js"; | ||
import { | ||
useCache | ||
} from "./chunk-HXWWGQRA.js"; | ||
import { | ||
useCacheStore | ||
} from "./chunk-FFFIOY4B.js"; | ||
import "./chunk-J4LYH5KM.js"; | ||
} from "./chunk-QQAFR2PG.js"; | ||
import "./chunk-DP7DNUTU.js"; | ||
import "./chunk-QETBZSG5.js"; | ||
export { | ||
Cache, | ||
CacheStore, | ||
CacheStoreProvider, | ||
CacheProvider, | ||
Read, | ||
cacheOptions, | ||
useCache, | ||
useCacheStore | ||
useRead | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
import { ResolvedCached } from './CacheStore.js'; | ||
import { CacheKey, CacheOptions } from './types.js'; | ||
import { Cache } from './Cache.js'; | ||
import './types.js'; | ||
@@ -7,4 +7,4 @@ /** | ||
*/ | ||
declare function useCache<TData, TCacheKey extends CacheKey>(options: CacheOptions<TData, TCacheKey>): ResolvedCached<TData, TCacheKey>['state']; | ||
declare function useCache(): Cache; | ||
export { useCache }; |
"use client" | ||
import { | ||
useCache | ||
} from "./chunk-HXWWGQRA.js"; | ||
import "./chunk-FFFIOY4B.js"; | ||
import "./chunk-J4LYH5KM.js"; | ||
} from "./chunk-QQAFR2PG.js"; | ||
import "./chunk-DP7DNUTU.js"; | ||
import "./chunk-QETBZSG5.js"; | ||
@@ -8,0 +7,0 @@ export { |
{ | ||
"name": "@suspensive/cache", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Useful interfaces for React Suspense", | ||
@@ -46,4 +46,4 @@ "keywords": [ | ||
"react": "^18.3.1", | ||
"@suspensive/react": "2.14.0", | ||
"@suspensive/eslint-config": "0.0.0", | ||
"@suspensive/react": "2.14.0", | ||
"@suspensive/tsconfig": "0.0.0-development", | ||
@@ -50,0 +50,0 @@ "@suspensive/tsup": "0.0.0" |
@@ -1,1 +0,1 @@ | ||
export { CacheStoreContext } from './CacheStoreContext' | ||
export { CacheContext } from './CacheContext' |
@@ -0,6 +1,6 @@ | ||
export { Read } from './Read' | ||
export { useRead } from './useRead' | ||
export { cacheOptions } from './cacheOptions' | ||
export { Cache } from './Cache' | ||
export { CacheProvider } from './CacheProvider' | ||
export { useCache } from './useCache' | ||
export { cacheOptions } from './cacheOptions' | ||
export { CacheStore } from './CacheStore' | ||
export { CacheStoreProvider } from './CacheStoreProvider' | ||
export { useCacheStore } from './useCacheStore' |
@@ -1,5 +0,4 @@ | ||
import { useSyncExternalStore } from 'react' | ||
import type { ResolvedCached } from './CacheStore' | ||
import type { CacheKey, CacheOptions } from './types' | ||
import { useCacheStore } from './useCacheStore' | ||
import { useContext } from 'react' | ||
import type { Cache } from './Cache' | ||
import { CacheContext } from './contexts' | ||
@@ -9,11 +8,8 @@ /** | ||
*/ | ||
export function useCache<TData, TCacheKey extends CacheKey>( | ||
options: CacheOptions<TData, TCacheKey> | ||
): ResolvedCached<TData, TCacheKey>['state'] { | ||
const cacheStore = useCacheStore() | ||
return useSyncExternalStore<ResolvedCached<TData, TCacheKey>>( | ||
cacheStore.subscribe, | ||
() => cacheStore.suspend<TData, TCacheKey>(options), | ||
() => cacheStore.suspend<TData, TCacheKey>(options) | ||
).state | ||
export function useCache(): Cache { | ||
const cache = useContext(CacheContext) | ||
if (cache == null) { | ||
throw new Error('CacheProvider should be in parent') | ||
} | ||
return cache | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122262
1