Skip to main content
Module

x/fresh_store/mod.ts>Store

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

A simple store that follows the Observer pattern.

Constructors

new
Store(state: T)

Creates a new Store (aka Subject) that can be subscribed to, or observed for a state change.

Properties

readonly
state: T

A deep copy of the internal state, to prevent referenced objects to be directly mutated.

Methods

attach(observer: Observer<T>): void

Attaches/subscribes a new Observer to the store.

detach(observer: Observer<T>): void

Detaches/unsubscribes an Observer from the store.

notify(): void

Notifies all the observers of a change. Will trigger automatically when the state is changed through Store.set, but it can be forced by calling this method directly.

set(options: T | ((prevState: T) => T)): void

Method to mutate the internal state. If a value is directly provided, assign the internal state to that value. A function can also be provided to access a dereferenced copy of the previous state.

Static Methods

newPointer(): string

Convenience method to create a unique pointer.