///
export type ValueEncoding = BufferEncoding | null;
export type Value = string | Buffer;
/**
* Represents a "value", which is just a file on disk containing something.
*
* A {@linkcode ValueWrapper} does not know anything about where it is stored, or how it is stored.
*/
export interface ValueWrapper {
/**
* Slugified name
*/
id: string;
/**
* Name of value
*/
name: string;
/**
* Encoding of value
*/
encoding: ValueEncoding;
/**
* Read value from disk
*/
look(): Promise;
/**
* Write to value
* @param value New value to write to value
*/
put(value: V): Promise;
/**
* Current value, if any
*/
get value(): V | undefined;
}
/**
* A function which reads from a value
*/
export interface ValueLooker {
(value: ValueWrapper): Promise;
}
/**
* A function which writes to a value
*/
export interface ValuePutter {
(value: ValueWrapper, data: V): Promise;
}
/**
* A value which stores its value in a file on disk
*
* This class is not intended to be instantiated directly
*
*/
export declare class BaseValueWrapper implements ValueWrapper {
#private;
readonly name: string;
readonly encoding: ValueEncoding;
/**
* Slugified name
*/
readonly id: string;
/**
* Function which reads a value
*/
private readonly looker;
/**
* Function which writes a value
*/
private readonly putter;
/**
* Underlying value
*/
readonly value: V;
/**
* Slugifies the name
* @param name Name of value
* @param looker Reader fn
* @param putter Writer fn
* @param encoding Defaults to `utf8`
*/
constructor(name: string, looker: ValueLooker, putter: ValuePutter, encoding?: ValueEncoding);
/**
* {@inheritdoc IValueWrapper.read}
*/
look(): Promise;
/**
* {@inheritdoc IValueWrapper.write}
*/
put(value: V): Promise;
}
/**
* @see {@linkcode ValueBoxOpts}
*/
export declare const DEFAULT_SUFFIX = "valuebox";
/**
* A class which instantiates a {@linkcode ValueWrapper}.
*/
export interface ValueConstructor {
new (name: string, reader: ValueLooker, writer: ValuePutter, encoding?: ValueEncoding): ValueWrapper;
}
/**
* Main entry point for use of this module
*
* Manages multiple values.
*/
export declare class ValueBox {
readonly name: string;
/**
* Slugified name of this container; corresponds to the directory name.
*
* If `dir` is provided, this value is unused.
* If `suffix` is provided, then this will be the parent directory of `suffix`.
*/
readonly containerId: string;
/**
* Override the directory of this container.
*
* If this is present, both `suffix` and `containerId` are unused.
*/
readonly dir: string;
protected ctor?: ValueConstructor;
/**
* Factory function for creating new {@linkcode ValueWrapper}s}
*/
protected wrapperIds: Set;
protected constructor(name: string, { dir, suffix, defaultCtor: ctor }?: ValueBoxOpts);
/**
* "mkdirp"'s the value directory and writes it to disk
* @param value ValueWrapper to write
* @param value Value to write to the value
*/
private put;
/**
* "mkdirp"'s the value directory
*/
private init;
/**
* Removes _all_ values from disk by truncating the value directory.
*
* Convniently removes everything else in the value directory, even if it isn't a value!
*/
recycleAll(): Promise;
/**
* Reads a value in a value from disk
* @param value ValueWrapper to read
*/
protected look(wrapper: ValueWrapper): Promise;
/**
* Removes a value from disk.
*
* This does _not_ destroy the `ValueWrapper` instance in memory, nor does it allow the `id` to be reused.
* @param wrapper ValueWrapper to drop
*/
recycle(wrapper: ValueWrapper): Promise;
/**
* Create a new {@linkcode ValueWrapper}. Reads the value from disk, if present.
* @param name Name of value
* @param encoding Encoding of value; defaults to `utf8`
* @returns New value
*/
createWrapper(name: string, encoding?: ValueEncoding): Promise>;
/**
* Creates a {@linkcode ValueWrapper} then immediately writes a value to it.
* If there was anything on disk, it is overwritten.
* @param name Name of value
* @param value Value to write
* @returns New `ValueWrapper` w/ value of `value`
*/
createWrapperWithValue(name: string, value: V, encoding?: ValueEncoding): Promise>;
/**
* Creates a new {@linkcode ValueBox}
* @param name Name of value container
* @param opts Options
* @returns New value
*/
static create(name: string, opts?: ValueBoxOpts): ValueBox;
}
export interface ValueBox {
/**
* Creates a new {@linkcode ValueBox}
* @param name Name of value container
* @param opts Options
* @returns New value
*/
create(name: string, opts?: ValueBoxOpts): ValueBox;
}
export interface ValueBoxOpts {
/**
* Override default value directory, which is chosen according to environment
*/
dir?: string;
/**
* Extra subdir to append to the auto-generated value directory. Ignored if `dir` is a `string`.
* @defaultValue 'valuebox'
*/
suffix?: string;
defaultCtor?: ValueConstructor;
}
//# sourceMappingURL=valuebox.d.ts.map