cachebranch
Advanced tools
Comparing version 1.0.5 to 1.1.0
import type { NullableCacheDataGenerator, CacheDataGenerator, Deferred, CacheDirection } from '../types'; | ||
import { CacheData } from './CacheData'; | ||
export declare abstract class CacheBranch<T> { | ||
export declare abstract class CacheBranch<T extends Record<string, any>> { | ||
protected abstract readonly root: CacheBranch<T>; | ||
protected readonly data: CacheData<T>; | ||
protected readonly data: CacheData<T, keyof T>; | ||
protected readonly branches: Map<string, CacheBranch<T>>; | ||
constructor(data: CacheData<T>); | ||
constructor(data: CacheData<T, keyof T>); | ||
/** | ||
@@ -13,4 +13,4 @@ * The method splits the provided key value into tokens and returns them as an array. | ||
*/ | ||
protected tokens(key: string): string[]; | ||
protected to(key: string, getNextBranch: (branch: CacheBranch<T>, key: string, index: number, last: boolean) => CacheBranch<T> | null): CacheBranch<T> | undefined; | ||
protected tokens(key: keyof T): string[]; | ||
protected to(key: keyof T, getNextBranch: (branch: CacheBranch<T>, key: string, index: number, last: boolean) => CacheBranch<T> | null): CacheBranch<T> | undefined; | ||
/** | ||
@@ -28,3 +28,3 @@ * The method splits the provided key into tokens and traverses the cache's sub-layers according to these tokens. | ||
*/ | ||
protected abstract ensureBranch(key: string, generator: NullableCacheDataGenerator<T>): CacheBranch<T>; | ||
protected abstract ensureBranch<K extends keyof T>(key: K, generator: NullableCacheDataGenerator<T, K>): CacheBranch<T>; | ||
/** | ||
@@ -35,3 +35,3 @@ * The method splits the provided key into tokens and traverses the cache's sub-layers according to these tokens. | ||
*/ | ||
protected abstract getBranch(key: string): CacheBranch<T> | undefined; | ||
protected abstract getBranch<K extends keyof T>(key: K): CacheBranch<T> | undefined; | ||
/** | ||
@@ -50,3 +50,3 @@ * The method re-caches the hierarchical cache data for the given key. | ||
*/ | ||
abstract cache(key: string, recursive?: CacheDirection): Deferred<this>; | ||
abstract cache(key: keyof T, recursive?: CacheDirection): Deferred<this>; | ||
/** | ||
@@ -58,3 +58,3 @@ * If there is no cache generated for the specified key value, it creates one; otherwise, it returns the existing cache data. | ||
*/ | ||
abstract ensure(key: string, generator: CacheDataGenerator<T>): Deferred<CacheData<T>>; | ||
abstract ensure<K extends keyof T>(key: K, generator: CacheDataGenerator<T, K>): Deferred<CacheData<T, K>>; | ||
/** | ||
@@ -64,3 +64,3 @@ * It retrieves the cache for the specified key value. If the cache does not exist, it returns `undefined`. | ||
*/ | ||
abstract get(key: string): Deferred<CacheData<T> | undefined>; | ||
abstract get<K extends keyof T>(key: K): Deferred<CacheData<T, K> | undefined>; | ||
/** | ||
@@ -72,3 +72,3 @@ * It reassigns the cache for the specified key value. | ||
*/ | ||
abstract set(key: string, generator: CacheDataGenerator<T>): Deferred<this>; | ||
abstract set<K extends keyof T>(key: K, generator: CacheDataGenerator<T, K>): Deferred<this>; | ||
/** | ||
@@ -80,3 +80,3 @@ * This deletes the cache associated with the specified key. | ||
*/ | ||
abstract delete(key: string): Deferred<this>; | ||
abstract delete(key: keyof T): Deferred<this>; | ||
} |
import type { NullableCacheDataGenerator, CacheDataCopy } from '../types'; | ||
export type CacheDataCloneStrategy = 'array-shallow-copy' | 'object-shallow-copy' | 'deep-copy'; | ||
export declare abstract class CacheData<T> { | ||
static IsDirty<T>(data: CacheData<T>): boolean; | ||
static SetDirty<T>(data: CacheData<T>, value: boolean): CacheData<T>; | ||
export declare abstract class CacheData<T extends Record<string, any>, K extends keyof T> { | ||
static IsDirty<T extends Record<string, any>, K extends keyof T>(data: CacheData<T, K>): boolean; | ||
static SetDirty<T extends Record<string, any>, K extends keyof T>(data: CacheData<T, K>, value: boolean): CacheData<T, K>; | ||
protected static readonly StructuredClone: typeof structuredClone; | ||
protected _raw: ReturnType<NullableCacheDataGenerator<T>>; | ||
protected generator: NullableCacheDataGenerator<T>; | ||
protected _raw: ReturnType<NullableCacheDataGenerator<T, keyof T>>; | ||
protected generator: NullableCacheDataGenerator<T, keyof T>; | ||
protected dirty: boolean; | ||
constructor(generator: NullableCacheDataGenerator<T>); | ||
constructor(generator: NullableCacheDataGenerator<T, keyof T>); | ||
/** | ||
@@ -16,3 +16,3 @@ * This is cached data. | ||
*/ | ||
get raw(): T; | ||
get raw(): T[K]; | ||
/** | ||
@@ -30,3 +30,3 @@ * The method returns a copied value of the cached data. | ||
*/ | ||
clone(strategy?: CacheDataCloneStrategy | CacheDataCopy<T>): T; | ||
clone(strategy?: CacheDataCloneStrategy | CacheDataCopy<T, K>): T[K]; | ||
} |
import type { NullableCacheDataGeneratorAsync, CacheDataGeneratorAsync, CacheDirection } from './types'; | ||
import { CacheBranch } from './base/CacheBranch'; | ||
import { CacheDataAsync } from './CacheDataAsync'; | ||
export declare class CacheBranchAsync<T = any> extends CacheBranch<T> { | ||
protected readonly data: CacheDataAsync<T>; | ||
export declare class CacheBranchAsync<T extends Record<string, any> = Record<string, any>> extends CacheBranch<T> { | ||
protected readonly data: CacheDataAsync<T, keyof T>; | ||
protected readonly branches: Map<string, CacheBranchAsync<T>>; | ||
protected readonly root: CacheBranchAsync<T>; | ||
constructor(generator?: NullableCacheDataGeneratorAsync<T>, root?: CacheBranchAsync<T>); | ||
protected ensureBranch(key: string, generator: NullableCacheDataGeneratorAsync<T>): CacheBranchAsync<T>; | ||
protected getBranch(key: string): CacheBranchAsync<T> | undefined; | ||
cache(key: string, recursive?: CacheDirection): Promise<this>; | ||
ensure(key: string, generator: CacheDataGeneratorAsync<T>): Promise<CacheDataAsync<T>>; | ||
get(key: string): CacheDataAsync<T> | undefined; | ||
set(key: string, generator: CacheDataGeneratorAsync<T>): Promise<this>; | ||
constructor(generator?: NullableCacheDataGeneratorAsync<T, keyof T>, root?: CacheBranchAsync<T>); | ||
protected ensureBranch<K extends keyof T>(key: K, generator: NullableCacheDataGeneratorAsync<T, K>): CacheBranchAsync<T>; | ||
protected getBranch<K extends keyof T>(key: K): CacheBranchAsync<T> | undefined; | ||
cache(key: keyof T, recursive?: CacheDirection): Promise<this>; | ||
ensure<K extends keyof T>(key: K, generator: CacheDataGeneratorAsync<T, K>): Promise<CacheDataAsync<T, K>>; | ||
get<K extends keyof T>(key: K): CacheDataAsync<T, K> | undefined; | ||
set<K extends keyof T>(key: K, generator: CacheDataGeneratorAsync<T, K>): Promise<this>; | ||
delete(key: string): Promise<this>; | ||
} |
import type { NullableCacheDataGeneratorSync, CacheDataGeneratorSync, CacheDirection } from './types'; | ||
import { CacheBranch } from './base/CacheBranch'; | ||
import { CacheDataSync } from './CacheDataSync'; | ||
export declare class CacheBranchSync<T = any> extends CacheBranch<T> { | ||
protected readonly data: CacheDataSync<T>; | ||
export declare class CacheBranchSync<T extends Record<string, any> = Record<string, any>> extends CacheBranch<T> { | ||
protected readonly data: CacheDataSync<T, keyof T>; | ||
protected readonly branches: Map<string, CacheBranchSync<T>>; | ||
protected readonly root: CacheBranchSync<T>; | ||
constructor(generator?: NullableCacheDataGeneratorSync<T>, root?: CacheBranchSync<T>); | ||
protected ensureBranch(key: string, generator: NullableCacheDataGeneratorSync<T>): CacheBranchSync<T>; | ||
protected getBranch(key: string): CacheBranchSync<T> | undefined; | ||
cache(key: string, recursive?: CacheDirection): this; | ||
ensure(key: string, generator: CacheDataGeneratorSync<T>): CacheDataSync<T>; | ||
get(key: string): CacheDataSync<T> | undefined; | ||
set(key: string, generator: CacheDataGeneratorSync<T>): this; | ||
delete(key: string): this; | ||
constructor(generator?: NullableCacheDataGeneratorSync<T, keyof T>, root?: CacheBranchSync<T>); | ||
protected ensureBranch<K extends keyof T>(key: K, generator: NullableCacheDataGeneratorSync<T, K>): CacheBranchSync<T>; | ||
protected getBranch<K extends keyof T>(key: K): CacheBranchSync<T> | undefined; | ||
cache(key: keyof T, recursive?: CacheDirection): this; | ||
ensure<K extends keyof T>(key: K, generator: CacheDataGeneratorSync<T, K>): CacheDataSync<T, K>; | ||
get<K extends keyof T>(key: K): CacheDataSync<T, K> | undefined; | ||
set<K extends keyof T>(key: K, generator: CacheDataGeneratorSync<T, K>): this; | ||
delete(key: keyof T): this; | ||
} |
import type { NullableCacheDataGeneratorAsync } from './types'; | ||
import { CacheData } from './base/CacheData'; | ||
import { CacheBranchAsync } from './CacheBranchAsync'; | ||
export declare class CacheDataAsync<T> extends CacheData<T> { | ||
export declare class CacheDataAsync<T extends Record<string, any>, K extends keyof T> extends CacheData<T, K> { | ||
static readonly EmptyDataGenerator: () => Promise<undefined>; | ||
static Update<T>(branch: CacheBranchAsync<T>, data: CacheDataAsync<T>, generator: NullableCacheDataGeneratorAsync<T>): Promise<CacheDataAsync<T>>; | ||
static Cache<T>(branch: CacheBranchAsync<T>, data: CacheDataAsync<T>): Promise<CacheDataAsync<T>>; | ||
protected generator: NullableCacheDataGeneratorAsync<T>; | ||
static Update<T extends Record<string, any>, K extends keyof T>(branch: CacheBranchAsync<T>, data: CacheDataAsync<T, K>, generator: NullableCacheDataGeneratorAsync<T, K>): Promise<CacheDataAsync<T, K>>; | ||
static Cache<T extends Record<string, any>, K extends keyof T>(branch: CacheBranchAsync<T>, data: CacheDataAsync<T, K>): Promise<CacheDataAsync<T, K>>; | ||
protected generator: NullableCacheDataGeneratorAsync<T, K>; | ||
} |
import type { NullableCacheDataGeneratorSync } from './types'; | ||
import { CacheData } from './base/CacheData'; | ||
import { CacheBranchSync } from './CacheBranchSync'; | ||
export declare class CacheDataSync<T> extends CacheData<T> { | ||
export declare class CacheDataSync<T extends Record<string, any>, K extends keyof T> extends CacheData<T, K> { | ||
static readonly EmptyDataGenerator: () => undefined; | ||
static Update<T>(branch: CacheBranchSync<T>, data: CacheDataSync<T>, generator: NullableCacheDataGeneratorSync<T>): CacheDataSync<T>; | ||
static Cache<T>(branch: CacheBranchSync<T>, data: CacheDataSync<T>): CacheDataSync<T>; | ||
protected generator: NullableCacheDataGeneratorSync<T>; | ||
static Update<T extends Record<string, any>, K extends keyof T>(branch: CacheBranchSync<T>, data: CacheDataSync<T, K>, generator: NullableCacheDataGeneratorSync<T, K>): CacheDataSync<T, K>; | ||
static Cache<T extends Record<string, any>, K extends keyof T>(branch: CacheBranchSync<T>, data: CacheDataSync<T, K>): CacheDataSync<T, K>; | ||
protected generator: NullableCacheDataGeneratorSync<T, keyof T>; | ||
} |
import { CacheBranchAsync } from '../CacheBranchAsync'; | ||
import { CacheBranchSync } from '../CacheBranchSync'; | ||
export type NullableCacheDataGeneratorAsync<T> = (branch: CacheBranchAsync<T>) => Promise<T | undefined>; | ||
export type NullableCacheDataGeneratorSync<T> = (branch: CacheBranchSync<T>) => T | undefined; | ||
export type NullableCacheDataGenerator<T> = NullableCacheDataGeneratorSync<T> | NullableCacheDataGeneratorAsync<T>; | ||
export type CacheDataGeneratorAsync<T> = (branch: CacheBranchAsync<T>) => Promise<T>; | ||
export type CacheDataGeneratorSync<T> = (branch: CacheBranchSync<T>) => T; | ||
export type CacheDataGenerator<T> = CacheDataGeneratorSync<T> | CacheDataGeneratorAsync<T>; | ||
export type CacheDataCopy<T> = (raw: T) => T; | ||
export type NullableCacheDataGeneratorAsync<T extends Record<string, any>, K extends keyof T> = (branch: CacheBranchAsync<T>) => Promise<T[K] | undefined>; | ||
export type NullableCacheDataGeneratorSync<T extends Record<string, any>, K extends keyof T> = (branch: CacheBranchSync<T>) => T[K] | undefined; | ||
export type NullableCacheDataGenerator<T extends Record<string, any>, K extends keyof T> = NullableCacheDataGeneratorSync<T, K> | NullableCacheDataGeneratorAsync<T, K>; | ||
export type CacheDataGeneratorAsync<T extends Record<string, any>, K extends keyof T> = (branch: CacheBranchAsync<T>) => Promise<T[K]>; | ||
export type CacheDataGeneratorSync<T extends Record<string, any>, K extends keyof T> = (branch: CacheBranchSync<T>) => T[K]; | ||
export type CacheDataGenerator<T extends Record<string, any>, K extends keyof T> = CacheDataGeneratorSync<T, K> | CacheDataGeneratorAsync<T, K>; | ||
export type CacheDataCopy<T extends Record<string, any>, K extends keyof T> = (raw: T[K]) => T[K]; | ||
export type CacheDirection = 'top-down' | 'bottom-up'; | ||
export type Deferred<T> = T | Promise<T>; |
{ | ||
"name": "cachebranch", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "Not only caches data in a key-value format but also supports efficient data management with a hierarchical structure.", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -202,4 +202,43 @@ # cachebranch | ||
## Writing in TypeScript | ||
If you want to infer types for the return values of each cache, you can use TypeScript as follows: | ||
Pass a generic type representing the shape of **Key: return type** record. | ||
```tsx | ||
import { CacheBranchAsync } from 'cachebranch' | ||
const branch = new CacheBranchAsync<{ | ||
'user': { | ||
'name': string | ||
'html': HTMLElement | ||
}, | ||
'user/name': string | ||
'user/name/html': HTMLElement | ||
}>() | ||
await branch.set('user', async (b) => { | ||
const name = await b.ensure('user/name', async () => 'Unknown').raw | ||
const html = await b.ensure('user/name/html', async () => <div>Loading...</div>).clone() | ||
return { | ||
name, | ||
html, | ||
} | ||
}) | ||
await branch.set('user/name', async () => ( | ||
await fetch('...').then(res => res.text()) | ||
)) | ||
await branch.set('user/name/html', async (b) => { | ||
const name = await b.ensure('user/name', async () => 'Unknown').raw | ||
return ( | ||
<div>{name}</div> | ||
) | ||
}) | ||
``` | ||
## License | ||
MIT license |
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
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
51424
244