narraleaf-react
Version:
A React visual novel player framework
152 lines (151 loc) • 3.83 kB
TypeScript
import { Lambda } from "../condition";
import { ScriptCtx } from "../script";
import { Service } from "../service";
type GalleryActions<T extends Record<string, any>> = {
"add": [name: string, metadata: T | ((ctx: ScriptCtx) => T)];
"remove": [name: string];
"clear": [];
};
/**
* A utility to manage a gallery of items
* @template Metadata - The metadata of the items
* @class
* @example
* ```ts
* const gallery = new Gallery<{timestamp: number}>();
*
* scene.action([
* gallery.add("item", () => ({
* timestamp: Date.now(),
* }))
* ]);
*
* scene.action([
* gallery.remove("item")
* ]);
*
* scene.action([
* Condition.If(gallery.has("item"), [
* // ...
* ])
* ]);
* ```
*
* to use this class, you need to register it in the story:
* ```ts
* story.registerService("gallery", gallery);
* ```
*
* After registering, you can access the gallery using game context:
* ```ts
* const liveGame = useLiveGame();
*
* const gallery = liveGame.story?.getService<Gallery<{timestamp: number}>>("gallery");
*
* if (gallery) {
* console.log("All items in the gallery:", gallery.$getAll());
* }
* ```
*/
export declare class Gallery<Metadata extends Record<string, any>> extends Service<GalleryActions<Metadata>> {
private unlocked;
constructor();
serialize(): Record<string, any> | null;
deserialize(data: Record<string, any>): void;
/**
* Add an item to the gallery
* @chainable
* @param name - The name of the item to add
* @example
* ```ts
* scene.action([
* gallery.add("item", {
* // ...
* })
* ]);
*
* // or
*
* scene.action([
* gallery.add("item", (ctx) => {
* return {
* timestamp: Date.now(),
* };
* })
* ]);
* ```
*/
add(name: string, metadata: Metadata | ((ctx: ScriptCtx) => Metadata)): never;
/**
* Check if an item is in the gallery
* @param name - The name of the item to check
* @returns A lambda that returns true if the item is in the gallery, false otherwise
* @example
* ```ts
* Condition.If(gallery.has("item"), [
* // ...
* ])
* ```
*/
has(name: string): Lambda<boolean>;
/**
* Remove an item from the gallery
* @chainable
* @param name - The name of the item to remove
* @example
* ```ts
* scene.action([
* gallery
* .remove("item")
* .remove("item2"),
* ]);
* ```
*/
remove(name: string): never;
/**
* Clear the gallery
* @chainable
* @example
* ```ts
* scene.action([
* gallery.clear()
* ]);
* ```
*/
clear(): never;
/**
* Remove an item from the gallery INSTANTLY
* @param name - The name of the item to remove
*/
$remove(name: string): void;
/**
* Clear the gallery
*
* After calling this method, the gallery will be empty INSTANTLY
*/
$clear(): void;
/**
* Get the metadata of an item
* @param name - The name of the item to get the metadata of
* @returns The metadata of the item
*/
$get(name: string): Metadata | undefined;
/**
* Set the metadata of an item
* @param name - The name of the item to set the metadata of
* @param metadata - The metadata of the item to set
*/
$set(name: string, metadata: Metadata): void;
/**
* Get all the items in the gallery
* @returns All the items in the gallery
*/
$getAll(): Record<string, Metadata>;
/**
* Check if an item is in the gallery
* @param name - The name of the item to check
* @returns True if the item is in the gallery, false otherwise
*/
$has(name: string): boolean;
}
export {};