Skip to main content
Module

x/fresh_store/mod.ts>StoreStack

Simple store for Deno Fresh, to pass state between islands
Go to Latest
class StoreStack
import { StoreStack } from "https://deno.land/x/fresh_store@v1.0.1/mod.ts";

A multi Store container.

Methods

Adds a store to the memory stack and assigns it a pointer.

addStoreAtPointer(
newItem: AnyStore,
pointer: Pointer,
options?: { override?: boolean; verbose?: boolean; },
): void

Adds a store to the specified Pointer. If a store already exists at the address, an option can be passed to override it. If override is set to false, but the verbose option is set to true, and the memory is already allocated, the store will NOT be overrided, and an error message will output to the console.

get<T = any>(ptr: Pointer): Store<T> | undefined

Returns a reference to the store at the address if it exists, otherwise undefined. A type can be passed in order to make the return typed.

Example

const ptr = useStore(0);
console.log(Stores.get<number>(ptr).state); // Output: 0
removeStore(ptr: Pointer, options?: { verbose?: boolean; }): void

Removes a store from the memory stack. If the Pointer's address is unallocated, it will output an error message to the console if verbose option is set to true.

upsert<T>(
defaultValue: T,
pointer: Pointer,
...observers: Observer<T>[],
): void

This method makes sure a store if instantiated at the Pointer's address. If no store is instantiated, it will create one holding the defaultValue provided. Otherwise, it will only attach Observers if they are provided.

Static Methods

configure(): void

Checks if the StoreStack is instantiated on window.stores and creates it if it's not.