import{_ as i,o as a,c as t,ae as n}from"./chunks/framework.C8AWLET_.js";const g=JSON.parse('{"title":"Persistence Tool: _storage","description":"","frontmatter":{},"headers":[],"relativePath":"plugins/core.storage.md","filePath":"plugins/core.storage.md"}'),e={name:"plugins/core.storage.md"};function h(l,s,p,k,r,o){return a(),t("div",null,[...s[0]||(s[0]=[n(`
_storage The Storage plugin synchronizes a signal with a specific key in your browser's localStorage. It handles both the initial hydration (loading data when the app starts) and automatic saving whenever the signal's value changes.
When you "attach" a signal to _storage, two things happen:
localStorage. If it does, it parses the JSON and updates the signal immediately.Register the Storage plugin in your main.js. Since this is a logic-only plugin, it doesn't require any CSS or UI dependencies.
import { $ } from 'sigpro';
import { Storage } from 'sigpro/plugins';
$.plugin(Storage).then(() => {
import('./App.js').then(app => $.mount(app.default));
});npm install sigpropnpm add sigproyarn add sigprobun add sigproYou can wrap any signal with _storage. It is common practice to do this right after creating the signal.
export default () => {
// 1. Create a signal with a default value
const $theme = $( 'light' );
// 2. Persist it. If 'user_theme' exists in localStorage,
// $theme will be updated to that value instantly.
_storage($theme, 'user_theme');
return div({ class: () => \`app-\${$theme()}\` }, [
h1(\`Current Theme: \${$theme()}\`),
button({
onclick: () => $theme(t => t === 'light' ? 'dark' : 'light')
}, "Toggle Theme")
]);
};Since the plugin uses JSON.parse and JSON.stringify internally, it works perfectly with complex state structures.
const $settings = $({
notifications: true,
fontSize: 16
});
// Automatically saves the whole object whenever any property changes
_storage($settings, 'app_settings');_storage? localStorage.getItem or setItem logic inside your components._storage returns the signal, you can persist it inline.try/catch block to prevent your app from crashing if the stored JSON is corrupted.localStorage clean.You can chain plugins to create a fully monitored and persistent state:
const $score = _storage($(0), 'high_score');
// Now it's saved to disk AND logged to console on every change
_debug($score, "Game Score");