persistent-typescript
Version:
An easy way to have persistent data on the browser and on nodejs
150 lines (149 loc) • 4.68 kB
TypeScript
/**
* Persistent options
*/
export interface IPersistentOptions {
plugin: IPersistentPlugin;
path: string;
debug: boolean;
}
/**
* This decorator permits to tell the lib to save and load your class.
* @param options Persistent option, with theses options you can choose your saver, loader and the path to the file
*/
export declare function Persistent(options?: IPersistentOptions): <T extends new (...args: any[]) => {}>(target: T) => any;
export declare class Storage {
private persistentObjects;
private persistentObjectsMetadata;
constructor();
/**
* Loads a persistent file to retrieve the classes information stored in it
* @param options Persistent option, with theses options you can choose your saver, loader and the path to the file
* @param force Force to reload the file
*/
loadPersistentFile(options: IPersistentOptions, force: boolean): void;
/**
* Stores the class instance to the storage
* @param classInstance class instance to store
* @param options Persistent option, with theses options you can choose your saver, loader and the path to the file
*/
store(classInstance: any, options: IPersistentOptions): void;
/**
* Retrieves a class instance by its name and its options
* @param className class name
* @param options Persistent option, with theses options you can choose your saver, loader and the path to the file
*/
getInstanceOfClass(className: string, options: IPersistentOptions): any | null;
/**
* Saves all classes stored in the storage using the plugin specified
*/
save(): void;
/**
* Returns the number of class saved
*/
getSize(): number;
}
export declare let storage: Storage;
export interface IPersistentPlugin {
/**
* Init a storage object if no one has ever been created in a file.
*/
init: () => object;
/**
* Serializes the storage object.
*/
serialize: (object: object) => string;
/**
* Deserializes the storage object.
*/
deserialize: (serializedObject: string) => object | null;
/**
* Retrieves class' data from the storage object and the class name.
*/
get: (object: object, className: string) => object | null;
/**
* Puts the class instance's data inside the storage object.
*/
put: (object: object, className: string, classInstance: object) => void;
}
export declare class JsonPlugin implements IPersistentPlugin {
private minified;
constructor(isMinified: boolean);
init(): object;
serialize(object: object): string;
deserialize(serializedObject: string): object | null;
get(object: object, className: string): object | null;
put(object: object, className: string, classInstance: object): void;
}
export declare class Map {
private map;
private mapSize;
constructor();
/**
* Puts a key and its value in the map
* @param {string} key
* @param {any} value
*/
put(key: string, value: any): boolean;
/**
* Clears the map
*/
clear(): boolean;
/**
* Returns an array of all the keys stored in the map
*/
keys(): any[];
/**
* Returns an array of all the values stored in the map
*/
values(): any[];
/**
* Checks if the map is empty or not
*/
isEmpty(): boolean;
/**
* Removes a key and its value from the map
* @param {string} key
* @return returns the stored value if not it returns null
*/
removeKey(key: string): any | null;
/**
* Checks if the map is containing the specified key
* @param {string} key
*/
containsKey(key: string): boolean;
/**
* Checks if the map is containing the specified value
* @param {any} value
*/
containsValue(value: any): boolean;
/**
* Returns the size of the map
*/
size(): number;
/**
* Returns the value associated to the specified key
* @param {string} key
* @return returns the value associated if not it returns null
*/
getValue(key: string): any | null;
/**
* Saves the map to json
*/
toJson(isMinified: boolean): string;
/**
* Creates an map from its serialization
* @param data serialized map
*/
static fromJson(serializedHm: string): Map | null;
}
export declare class Utils {
/**
* Retrieves the class name from a class instance
* @param instance class instance
*/
static getClassName(instance: object): string | null;
/**
* Checks if the library is running on the browser
*/
static isBrowser(): boolean;
}