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

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

* fmt
pull/286/head
Fabian-Lars 2 years ago committed by GitHub
parent a4dfa62486
commit 9b70a79b2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 }}

@ -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()`)

@ -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);
}
} }
); });
} }
} }

Loading…
Cancel
Save