Crates.io | tauri-plugin-state |
lib.rs | tauri-plugin-state |
version | 0.1.0 |
source | src |
created_at | 2024-06-26 13:16:35.499221 |
updated_at | 2024-06-26 13:16:35.499221 |
description | Dead simple state management for tauri |
homepage | https://github.com/glowsquid-launcher/glowsquid/tree/dev/libs/tauri-plugin-state |
repository | https://github.com/glowsquid-launcher/glowsquid |
max_upload_size | |
id | 1284507 |
size | 24,632 |
Let's say you want to manage state between your frontend and backend, but you want to keep it in sync and completely managed from rust.
This plugin is for you.
By default, we use a simple Map
object to store state on the frontend.
If you need reactivity, frameworks have their own reactive Map implementations that you can use.
import { Map } from 'svelte/reactivity';
reactive
object. setupState(reactive(new Map()));
import { ReactiveMap } from "@solid-primitives/map";
All state changes are broadcasted to the frontend via Tauri's native event API. You can listen for these changes by listening to the plugin:state-change
event.
import { listen } from '@tauri-apps/api/event'
listen<{ key: string, value: unknown }>('plugin:state-change', (event) => {
const { key, value } = event.payload
console.log(`State change for key ${key} with value ${value}`)
})
How you type value
is up to you. We recommend using a discriminated union to type the value.
This plugin considers not being able to sync state as an invariant violation and will panic if it can't sync state. We always want to be in sync to make sure nothing goes wrong.