Skip to content

StoreStack

Alexandre Moreau-Lemay edited this page Aug 22, 2022 · 4 revisions
import { StoreStack } from "https://deno.land/x/fresh_store@v1.0.1/mod.ts";

A multi Store container.

Implementation

class StoreStack {
    public addStore(newItem: AnyStore): Pointer;
    public addStoreAtPointer(
        newItem: AnyStore,
        pointer: Pointer,
        options?: {
            override?: boolean;
            verbose?: boolean;
        },
    ): void;
    public get<T = any>(ptr: Pointer): Store<T> | undefined;
    public removeStore(ptr: Pointer, options?: {
        verbose?: boolean;
    }): void;
    public upsert<T>(
        defaultValue: T,
        pointer: Pointer,
        ...observers: Observer<T>[],
    ): void;
 
    static configure(): void;
}

Static Methods

Configure

configure(): void

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

Methods

addStore

addStore(newItem: AnyStore): Pointer

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

Parameters

  1. newItem: AnyStore

The Store to be added to the stack.


addStoreAtPointer

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 overrideis set to false, but the verbose option is set to true, and the memory is already allocated, the store will NOT be overriden, and a message will output to the console.

Parameters

  1. newItem: AnyStore

The Store to be added to the stack.

  1. pointer: Pointer

The Pointer to the memory address where the store should be inserted.

  1. options?: { override?: boolean, verbose?: boolean } (Optional)

Defines if a store should be overrided if it exists at the Pointer and if a verbose error should be logged to the console if override is set to false.

Throws

  1. MemoryAllocationError

If the address is already allocated and override isn't set to true. See MemoryAllocationError.


get

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 value typed.

Example

const ptr = useStore(0);
console.log(Stores.get<number>(ptr).state); // Output: 0

Parameters

  1. ptr: Pointer

The Pointer to the Store.

Returns

The Store, if it exists.


removeStore

removeStore(ptr: Pointer, options?: {verbose?: boolean;}): void

Removes a Store from the memory stack. If the Pointer points to unallocated memory, it will throw an error and output a message to the console if the verbose option is set to true.

Parameters

  1. ptr: Pointer

The Pointer to the memory location of the Store.

  1. options?: {verbose?: boolean;} (Optional)

If the removal fails, should the function output it to the console? Defaults to false

Throws

  1. NullPointerError

If attempting to delete a Store at unallocated memory location. See NullPointerError.

Clone this wiki locally