# Astro State
Astro State is a TypeScript utility for managing application state in Astro projects. It provides subscription capabilities, middleware support, and optional local storage backup.
## Features
- π Designed for Astro projects
- π State management with getter and setter methods
- π‘ Subscription system for state changes
- π Middleware support for intercepting and modifying state updates
- πΎ Optional local storage backup with stale time checks
- π TypeScript generics for type-safe state handling
## Installation
Since this package is only published on GitHub, you can install it directly from the repository:
```bash
pnpm add github:tysonjf/astro-state
npm install github:tysonjf/astro-state
```import { State } from "astro-state";
const initialState = { count: 0 };
const state = new State(initialState, {
autoUpdate: true,
localBackup: true,
localkey: "myAstroState",
// 5 * 60 * 1000 = 5 minutes
staleTime: 5 * 60 * 1000,
});const countElement = document.getElementById("count");
state.subscribe((newState, prevState, initialState) => {
// Update the UI
countElement.textContent = newState.count.toString();
console.log("State updated:", newState);
});// valid
state.set((prevState) => ({ count: prevState.count + 1 }));
state.set({ count: 10 });
// invalid
state.set((prevState) => ({ count: prevState.count++ }));state.addMiddleware((newState, prevState, initialState) => {
// Modify or validate state here
if (newState.count < 0) {
return { count: 0 };
}
if (newState.count > 10) {
return initialState;
}
return newState;
});state.getLocal(); // Load state from local storage
state.resetLocal(); // Reset state to initial local storage value
state.clearLocal(); // Clear state from local storageCreates a new State instance.
Adds a subscriber to the state changes.
Removes a subscriber.
Adds a middleware function to intercept state updates.
Returns the current state.
Updates the state and returns a promise with the new and previous states.
Saves the current state to local storage.
Loads the state from local storage.
Resets the state to the initial value stored in local storage.
Removes the state from local storage.
type TSubscriber<T> = (newState: T, prevState: T, initialState: T) => void;
type TSetter<T> = T | TSetterFunc<T>;
type TSetterFunc<T> = (prev: T, initialState: T) => T;
type TMiddleware<T> = (
newState: T,
prevState: T,
initialState: T,
) => T | Promise<T>;
type TConstructorOptions<TState> = {
callback?: TSubscriber<TState>;
autoUpdate?: boolean;
localBackup?: boolean;
localkey?: string;
staleTime?: number;
};To start the development server:
npm run devTo build the project:
npm run buildTo preview the build:
npm run previewContributions are welcome! Please feel free to submit a Pull Request.
https://github.com/tysonjf/astro-state
1.0.0
- @astrojs/check: ^0.8.1
- astro: ^4.11.5
- lodash: ^4.17.21
- typescript: ^5.5.3
- @types/lodash: ^4.17.6
This README now:
1. Uses "State" instead of "StateFactory" in the examples.
2. Shows how to import the `State` class from the package.
3. Provides instructions for installing the package directly from GitHub instead of npm.
Is there anything else you'd like me to adjust in this README?