fix(store): Use event module instead of appWindow, fixes #282 (#283)

* fix(store): Use event module instead of appWindow, fixes #282

* fmt
This commit is contained in:
Fabian-Lars
2023-03-15 18:22:19 +01:00
committed by GitHub
parent a4dfa62486
commit 9b70a79b2c
3 changed files with 19 additions and 24 deletions
+8 -8
View File
@@ -5,18 +5,18 @@ on:
branches: branches:
- dev - dev
paths: paths:
- '.github/workflows/msrv-check.yml' - ".github/workflows/msrv-check.yml"
- 'plugins/*/src/**' - "plugins/*/src/**"
- '**/Cargo.toml' - "**/Cargo.toml"
- '**/Cargo.lock' - "**/Cargo.lock"
pull_request: pull_request:
branches: branches:
- dev - dev
paths: paths:
- '.github/workflows/msrv-check.yml' - ".github/workflows/msrv-check.yml"
- 'plugins/*/src/**' - "plugins/*/src/**"
- '**/Cargo.toml' - "**/Cargo.toml"
- '**/Cargo.lock' - "**/Cargo.lock"
concurrency: concurrency:
group: ${{ github.workflow }}-${{ github.ref }} group: ${{ github.workflow }}-${{ github.ref }}
+2
View File
@@ -64,7 +64,9 @@ await store.save(); // this manually saves the store, otherwise the store is onl
``` ```
### Persisting values ### Persisting values
Values added to the store are not persisted between application loads unless: Values added to the store are not persisted between application loads unless:
1. The application is closed gracefully (plugin automatically saves) 1. The application is closed gracefully (plugin automatically saves)
2. The store is manually saved (using `store.save()`) 2. The store is manually saved (using `store.save()`)
+9 -16
View File
@@ -3,8 +3,7 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
import { invoke } from "@tauri-apps/api/tauri"; import { invoke } from "@tauri-apps/api/tauri";
import { UnlistenFn } from "@tauri-apps/api/event"; import { listen, UnlistenFn } from "@tauri-apps/api/event";
import { appWindow } from "@tauri-apps/api/window";
interface ChangePayload<T> { interface ChangePayload<T> {
path: string; path: string;
@@ -180,14 +179,11 @@ export class Store {
key: string, key: string,
cb: (value: T | null) => void cb: (value: T | null) => void
): Promise<UnlistenFn> { ): Promise<UnlistenFn> {
return await appWindow.listen<ChangePayload<T>>( return await listen<ChangePayload<T>>("store://change", (event) => {
"store://change", if (event.payload.path === this.path && event.payload.key === key) {
(event) => { cb(event.payload.value);
if (event.payload.path === this.path && event.payload.key === key) {
cb(event.payload.value);
}
} }
); });
} }
/** /**
@@ -198,13 +194,10 @@ export class Store {
async onChange<T>( async onChange<T>(
cb: (key: string, value: T | null) => void cb: (key: string, value: T | null) => void
): Promise<UnlistenFn> { ): Promise<UnlistenFn> {
return await appWindow.listen<ChangePayload<T>>( return await listen<ChangePayload<T>>("store://change", (event) => {
"store://change", if (event.payload.path === this.path) {
(event) => { cb(event.payload.key, event.payload.value);
if (event.payload.path === this.path) {
cb(event.payload.key, event.payload.value);
}
} }
); });
} }
} }