import { IPerspective, IPerspectiveVisualStyle } from "./perspective"; /** * Iterator function that is invoked for each of the perspectives in a * perspective storage. */ export type PerspectiveIteratorCallback = (perspective: IPerspective, id: string, storage: IPerspectiveStorage) => T; /** * Object that maps string keys to IPerspective objects. */ export interface IPerspectiveMapping { [key: string]: IPerspective; } /** * Object that specifies the position of a perspective in the perspective * storage backend during a move operation. */ export type PerspectivePosition = "first" | "last" | number | { before: string; } | { after: string; } | { at: number; }; /** * Feature constants that a perspective storage backend may support. */ export type PerspectiveStorageFeature = "reordering"; /** * Interface specification for objects that know how to store perspectives * in some storage backend. * * Each perspective stored in the backend has a mandatory "base" state and an * optional "modified" state that is typically different from the base state. * When a perspective is loaded, the storage must return the "modified" state * if it exists or the base state otherwise. When a perspective is saved, it * is saved into the "modified" state and one must call the * persistModifications() method to replace the base state with the * modified state. Alternatively, one can call the revertModifications() * method to forget the modified state and switch back to the base state. */ export interface IPerspectiveStorage { /** * Iterates over all the perspectives stored in the storage backend, and * invokes a function for each of them. Perspectives with pending modifications * will present the modified state and not the base state. * * @param func a function that will be called with each of the perspectives in * the storage backend * @return a promise that resolves when all the perspectives have been * iterated over */ forEach: (func: PerspectiveIteratorCallback) => Promise; /** * Iterates over all the perspectives stored in the storage backend, and * invokes a function for the _original_ state of each of them, even if they * are modified. * * @param func a function that will be called with each of the perspectives in * the storage backend * @return a promise that resolves when all the perspectives have been * iterated over */ forEachOriginal: (func: PerspectiveIteratorCallback) => Promise; /** * Retrieves the perspective object with the given ID and returns a promise * that resolves to the retrieved perspective or undefined if there is no * such perspective. * * Perspectives with pending modifications will present the modified state * and not the base state when the function is invoked. */ get: (id: string) => Promise; /** * Retrieves the perspective object with the given ID and returns a promise * that resolves to the retrieved perspective or undefined if there is no * such perspective. * * Perspectives with pending modifications will present their _original_ * base state and not the modified state when the function is invoked. */ getOriginal: (id: string) => Promise; /** * Returns whether the perspective with the given ID has modifications that * are not persisted yet. * * The function must return false for perspective IDs that do not exist. */ isModified: (id?: string) => boolean; /** * Retrieves the contents of the perspective with the given ID from the * storage. The operation may be asynchronous for certain storage backends, * therefore the function will return a promise that resolves to the * retrieved perspective. */ load: (id: string) => Promise; /** * Iterates over all the perspectives stored in the storage backend, and * invokes a function for each of them. Collects the return values of the * function in an array and returns a promise that resolves to the array. * * Perspectives with pending modifications will present the modified state * and not the base state when the function is invoked. * * @param func a function that will be called with each of the perspectives in * the storage backend * @return a promise that resolves to the return values of the invoked * function for each perspective. */ map: (func: PerspectiveIteratorCallback) => Promise; /** * Iterates over all the perspectives stored in the storage backend, and * invokes a function for each of them. Collects the return values of the * function in an array and returns a promise that resolves to the array. * * Perspectives with pending modifications will present their _original_ * base state and not the modified state when the function is invoked. * * @param func a function that will be called with each of the perspectives in * the storage backend * @return a promise that resolves to the return values of the invoked * function for each perspective. */ mapOriginal: (func: PerspectiveIteratorCallback) => Promise; /** * Moves a perspective to a new position in the order in which the storage * presents the list of perspectives when iterated over. * * May throw an Error if the perspective storage does not implement * reordering. Call `supports()` to test whether reordering is supported. */ move: (id: string, position: PerspectivePosition) => Promise; /** * Copies the modified state of the perspective with the given ID over to the * base state of the perspective. * * The operation may be asynchronous for certain storage backends, * therefore the function will return a promise that resolves when the * operation was successful. */ persistModifications: (id: string) => Promise; /** * Removes the perspective with the given ID. * * The operation may be asynchronous for certain storage backends, * therefore the function will return a promise that resolves when the * operation was successful. * * @param id the ID of the perspective to remove */ remove: (id: string) => Promise; /** * Renames the perspective with the given ID. * * The operation may be asynchronous for certain storage backends, * therefore the function will return a promise that resolves when the * operation was successful. * * @param id the ID of the perspective * @param label the new label of the perspective */ rename: (id: string, label: string) => Promise; /** * Clears the modified state of the perspective with the given ID. * * The operation may be asynchronous for certain storage backends, * therefore the function will return a promise that resolves when the * operation was successful. */ revertModifications: (id: string) => Promise; /** * Saves the given perspective data into the storage. * * When the ID is specified and it refers to an existing perspective, the * method will update the base state of the perspective and forgets its * modified state (if any). When the ID is not specified or it refers to a * perspective that does not exist yet, the perspective will be saved into * the base state. * * The operation may be asynchronous for certain storage backends, * therefore the function will return a promise that resolves when the * operation was successful. * * @param perspective the perspective to save * @param id the ID of the perspective * @return the ID of the perspective that was saved */ save: (perspective: IPerspective, id?: string) => Promise; /** * Adds a function to be called whenever the perspective storage is modified. */ subscribe: (callback: () => void) => void; /** * Tests whether the perspective storage supports a given feature. */ supports: (feature: PerspectiveStorageFeature) => boolean; /** * Removes a function to be called whenever the perspective storage is modified. */ unsubscribe: (callback: () => void) => void; /** * Updates the state stored in the perspective with the given ID in the * storage. * * The new state will be stored in the modified state of the perspective; the * base state will be left intact. * * The operation may be asynchronous for certain storage backends, * therefore the function will return a promise that resolves when the * operation was successful. * * @param id the ID of the perspective * @param state the new state object to store in the perspective */ update: (id: string, state: any) => Promise; /** * Renames a perspective or performs a modification of its visual style. * * The operation may be asynchronous for certain storage backends, * therefore the function will return a promise that resolves when the * operation was successful. * * @param id the ID of the perspective * @param updates the new visual styles of the perspective */ updateVisualStyle: (id: string, updates: Partial) => Promise; } /** * Factory methods for different kinds of perspective storage objects. */ export declare class PerspectiveStorage { /** * Returns a perspective storage object that stores perspectives in the * given array. */ static fromArray(array?: IPerspective[]): IPerspectiveStorage; }