@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
83 lines (82 loc) • 1.76 kB
TypeScript
import { StorageCache } from '@johngw/stream/storages/StorageCache';
import { Clearable } from '@johngw/stream/types/Clearable';
/**
* Describes a function to pull data.
*
* @group Sources
*/
export interface CachePuller<T> {
(): CachePullerResult<T> | Promise<CachePullerResult<T>>;
}
/**
* The result of a CachableStream pull.
*
* @group Sources
*/
export type CachePullerResult<T> = IteratorResult<T>;
/**
* The source of a `ReadableStream` that queues items
* only when a timed cache becomes invalid.
*
* @group Sources
* @see {@link StorageCache}
* @see {@link CachableStream:class}
* @example
* Caching for 30 minutes.
*
* ```
* const cache = new StorageCache(
* localStorage,
* 'my-cache',
* 30 * 60_000
* )
*
* let i = 0
* const stream = new ReadableStream(
* new CachableSource<number>(
* cache,
* ['numbers'],
* () => ++i
* )
* )
*
* stream.pipeTo(write(console.info))
* // 1
*
* await setTimeout(30 * 60_000))
* // 2
* ```
* @example
* Manually clearing cache.
*
* ```
* const cache = new StorageCache(
* localStorage,
* 'my-cache',
* 30 * 60_000
* )
*
* let i = 0
* const source = new CachableSource<number>(
* cache,
* ['numbers'],
* () => ++i
* )
*
* const stream = new ReadableStream(source)
*
* stream.pipeTo(write(console.info))
* // 1
*
* source.clear()
* // 2
* ```
*/
export declare class CachableSource<T> implements UnderlyingDefaultSource<T>, Clearable {
#private;
constructor(cache: StorageCache, path: string[], pullResult: CachePuller<T>, ms?: number);
pull(controller: ReadableStreamDefaultController<T>): Promise<void>;
cancel(reason: unknown): void;
clear(): void;
get sourceHasFinished(): boolean;
}