Skip to content

Commit 0378cee

Browse files
committed
feat: replace usages of stores
1 parent 158b6fd commit 0378cee

File tree

16 files changed

+157
-214
lines changed

16 files changed

+157
-214
lines changed

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"staticlib",
2828
"tauri",
2929
"tungstenite",
30+
"unlisten",
3031
"vtftk",
3132
"vtube",
3233
"webbrowser"

src/lib/api/calibration.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ModelData } from "$shared/appData";
22

3-
import { writable } from "svelte/store";
43
import { invoke } from "@tauri-apps/api/core";
54
import { listen } from "@tauri-apps/api/event";
65
import { createQuery } from "@tanstack/svelte-query";
@@ -22,16 +21,6 @@ export enum CalibrationStep {
2221
Complete = "Complete",
2322
}
2423

25-
export const calibrationState = writable(CalibrationStep.NotStarted);
26-
27-
// Handle logout
28-
listen<{ step: CalibrationStep }>(
29-
"calibration_state",
30-
({ payload: { step } }) => {
31-
calibrationState.set(step);
32-
},
33-
);
34-
3524
const CALIBRATION_DATA_KEY = ["calibration-data"];
3625

3726
export function createModelDataQuery() {

src/lib/api/runtimeAppData.ts

Lines changed: 9 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,34 @@
11
import { getContext } from "svelte";
22
import { invoke } from "@tauri-apps/api/core";
33
import { listen } from "@tauri-apps/api/event";
4-
import { get, derived, type Readable } from "svelte/store";
54
import {
65
createQuery,
76
createMutation,
87
type CreateQueryResult,
98
} from "@tanstack/svelte-query";
109

11-
import type {
12-
AppData,
13-
ModelData,
14-
MainConfig,
15-
ModelConfig,
16-
SoundsConfig,
17-
PhysicsConfig,
18-
RuntimeAppData,
19-
ExternalsConfig,
20-
ThrowablesConfig,
21-
VTubeStudioConfig,
22-
} from "./types";
10+
import type { AppData, ModelData, RuntimeAppData } from "./types";
2311

2412
import { queryClient } from "./client";
2513

26-
export const RUNTIME_APP_DATA_KEY = ["runtime-app-data"];
27-
export const RUNTIME_APP_DATA_CONTEXT = Symbol("runtime-app-data");
14+
export const APP_CONTEXT_KEY = Symbol("app-context");
2815

16+
export const RUNTIME_APP_DATA_KEY = ["runtime-app-data"];
2917
export const APP_DATA_KEY = ["app-data"];
30-
export const APP_DATA_CONTEXT = Symbol("app-data");
3118

3219
// Update the runtime app data when the change event is received
3320
listen<RuntimeAppData>("runtime_app_data_changed", ({ payload }) => {
3421
queryClient.cancelQueries({ queryKey: RUNTIME_APP_DATA_KEY });
3522
queryClient.setQueryData(RUNTIME_APP_DATA_KEY, payload);
3623
});
3724

38-
export function getRuntimeAppData(): Readable<RuntimeAppData> {
39-
return getContext(RUNTIME_APP_DATA_CONTEXT);
40-
}
25+
type AppContext = {
26+
appData: AppData;
27+
runtimeAppData: RuntimeAppData;
28+
};
4129

42-
export function getAppData(): Readable<AppData> {
43-
return getContext(APP_DATA_CONTEXT);
30+
export function getAppContext(): AppContext {
31+
return getContext(APP_CONTEXT_KEY);
4432
}
4533

4634
/**
@@ -114,72 +102,3 @@ export function isModelCalibrated(
114102
const data = modelData.find((data) => data.id === modelId);
115103
return data !== undefined;
116104
}
117-
118-
type AppDataMutation = ReturnType<typeof createAppDateMutation>;
119-
type AppDataMutator<V> = (input: V) => Promise<boolean>;
120-
121-
export function createAppDataMutator<V>(
122-
appData: Readable<AppData>,
123-
appDataMutation: AppDataMutation,
124-
action: (appData: AppData, value: V) => AppData,
125-
): Readable<AppDataMutator<V>> {
126-
return derived(
127-
appDataMutation,
128-
($appDataMutation) => (input: V) =>
129-
$appDataMutation.mutateAsync(action(get(appData), input)),
130-
);
131-
}
132-
133-
type UpdateSettingsMutation = {
134-
throwables_config: Partial<ThrowablesConfig>;
135-
model_config: Partial<ModelConfig>;
136-
sounds_config: Partial<SoundsConfig>;
137-
vtube_studio_config: Partial<VTubeStudioConfig>;
138-
externals_config: Partial<ExternalsConfig>;
139-
main_config: Partial<MainConfig>;
140-
physics_config: Partial<PhysicsConfig>;
141-
};
142-
143-
export function createUpdateSettingsMutation(
144-
appData: Readable<AppData>,
145-
appDataMutation: AppDataMutation,
146-
) {
147-
return createAppDataMutator<UpdateSettingsMutation>(
148-
appData,
149-
appDataMutation,
150-
(
151-
appData,
152-
{
153-
throwables_config,
154-
model_config,
155-
sounds_config,
156-
vtube_studio_config,
157-
externals_config,
158-
main_config,
159-
physics_config,
160-
},
161-
) => ({
162-
...appData,
163-
164-
throwables_config: { ...appData.throwables_config, ...throwables_config },
165-
model_config: { ...appData.model_config, ...model_config },
166-
sounds_config: { ...appData.sounds_config, ...sounds_config },
167-
vtube_studio_config: {
168-
...appData.vtube_studio_config,
169-
...vtube_studio_config,
170-
},
171-
externals_config: {
172-
...appData.externals_config,
173-
...externals_config,
174-
},
175-
main_config: {
176-
...appData.main_config,
177-
...main_config,
178-
},
179-
physics_config: {
180-
...appData.physics_config,
181-
...physics_config,
182-
},
183-
}),
184-
);
185-
}

src/lib/components/GlobalConfirmDialog.svelte

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script module lang="ts">
2-
import { writable } from "svelte/store";
3-
42
type ConfirmMessage = {
53
title: string;
64
description: string;
@@ -11,13 +9,11 @@
119
resolve: (value: boolean) => void;
1210
};
1311
14-
const confirmStore = writable<ConfirmMessage[]>([]);
12+
let confirmStore = $state<ConfirmMessage[]>([]);
1513
1614
export function confirmDialog(message: Omit<ConfirmMessage, "resolve">) {
1715
return new Promise<boolean>((resolve) => {
18-
confirmStore.update((value) => {
19-
return [...value, { ...message, resolve }];
20-
});
16+
confirmStore = [...confirmStore, { ...message, resolve }];
2117
});
2218
}
2319
</script>
@@ -26,13 +22,11 @@
2622
import Dialog from "./Dialog.svelte";
2723
import Button from "./input/Button.svelte";
2824
29-
const currentMessage: ConfirmMessage | undefined = $derived($confirmStore[0]);
25+
const currentMessage: ConfirmMessage | undefined = $derived(confirmStore[0]);
3026
3127
function onResult(message: ConfirmMessage, value: boolean) {
3228
// Pop the message out of the store
33-
confirmStore.update((value) => {
34-
return value.filter((_, index) => index !== 0);
35-
});
29+
confirmStore = confirmStore.filter((_, index) => index !== 0);
3630
3731
message.resolve(value);
3832
}

src/lib/components/nav/StatusDetails.svelte

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
<script lang="ts">
22
import { createModelDataQuery } from "$lib/api/calibration";
33
import { createIsAuthenticatedQuery } from "$lib/api/twitch";
4-
import {
5-
getRuntimeAppData,
6-
isModelCalibrated,
7-
} from "$lib/api/runtimeAppData";
4+
import { getAppContext, isModelCalibrated } from "$lib/api/runtimeAppData";
85
9-
const runtimeAppData = getRuntimeAppData();
10-
const isAuthenticated = createIsAuthenticatedQuery();
6+
const appContext = getAppContext();
7+
const runtimeAppData = $derived(appContext.runtimeAppData);
118
9+
const isAuthenticated = createIsAuthenticatedQuery();
1210
const modelDataQuery = createModelDataQuery();
1311
1412
// Model needs to be calibrated if not available here
1513
const modelCalibrated = $derived(
16-
isModelCalibrated($modelDataQuery.data ?? [], $runtimeAppData.model_id),
14+
isModelCalibrated($modelDataQuery.data ?? [], runtimeAppData.model_id),
1715
);
1816
</script>
1917

@@ -22,8 +20,8 @@
2220
<div>VTube Studio</div>
2321
<div
2422
class="status-indicator"
25-
data-status={$runtimeAppData.vtube_studio_connected
26-
? modelCalibrated && $runtimeAppData.vtube_studio_auth
23+
data-status={runtimeAppData.vtube_studio_connected
24+
? modelCalibrated && runtimeAppData.vtube_studio_auth
2725
? "green"
2826
: "orange"
2927
: "red"}
@@ -33,9 +31,9 @@
3331
<div>Active Overlay</div>
3432
<div
3533
class="status-indicator"
36-
data-status={$runtimeAppData.active_overlay_count > 0 ? "green" : "red"}
34+
data-status={runtimeAppData.active_overlay_count > 0 ? "green" : "red"}
3735
>
38-
{$runtimeAppData.active_overlay_count}
36+
{runtimeAppData.active_overlay_count}
3937
</div>
4038
</div>
4139
<div class="status-item">

src/lib/components/sounds/SoundPicker.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import type { Sound, SoundId } from "$shared/dataV2";
44
55
import getBackendURL from "$lib/utils/url";
6-
import { getAppData } from "$lib/api/runtimeAppData";
76
import { createSoundsQuery } from "$lib/api/soundModel";
7+
import { getAppContext } from "$lib/api/runtimeAppData";
88
99
import Dialog from "../Dialog.svelte";
1010
import Button from "../input/Button.svelte";
@@ -42,7 +42,9 @@
4242
selected = initialSelected;
4343
});
4444
45-
const appData = getAppData();
45+
const appContext = getAppContext();
46+
const appData = $derived(appContext.appData);
47+
4648
const soundsQuery = createSoundsQuery();
4749
const sounds = $derived(filterOptionsSearch($soundsQuery.data ?? [], search));
4850
@@ -135,7 +137,7 @@
135137

136138
<td class="sound-column sound-column--preview">
137139
<SoundPreview
138-
volume={sound.volume * $appData.sounds_config.global_volume}
140+
volume={sound.volume * appData.sounds_config.global_volume}
139141
src={getBackendURL(sound.src)}
140142
/>
141143
</td>

src/lib/sections/AppDataProvider.svelte

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
<script lang="ts">
22
import type { AppData, RuntimeAppData } from "$lib/api/types";
33
4-
import { setContext } from "svelte";
5-
import { derived, type Readable } from "svelte/store";
4+
import { setContext, type Snippet } from "svelte";
65
import {
7-
APP_DATA_CONTEXT,
6+
APP_CONTEXT_KEY,
87
createAppDataQuery,
9-
RUNTIME_APP_DATA_CONTEXT,
108
createRuntimeAppDataQuery,
119
} from "$lib/api/runtimeAppData";
1210
13-
const runtimeAppData = createRuntimeAppDataQuery();
11+
type Props = {
12+
children: Snippet;
13+
};
14+
15+
const { children }: Props = $props();
1416
15-
const runtimeAppDataStore: Readable<RuntimeAppData | undefined> = derived(
16-
runtimeAppData,
17-
($runtimeAppData) => $runtimeAppData.data,
17+
const runtimeAppData = createRuntimeAppDataQuery();
18+
const runtimeAppDataStore: RuntimeAppData | undefined = $derived(
19+
$runtimeAppData.data,
1820
);
1921
2022
const appData = createAppDataQuery();
21-
const appDataStore: Readable<AppData | undefined> = derived(
22-
appData,
23-
($appData) => $appData.data,
24-
);
23+
const appDataStore: AppData | undefined = $derived($appData.data);
24+
25+
setContext(APP_CONTEXT_KEY, {
26+
// Values within the context are guaranteed to be defined when the context is used
27+
get appData() {
28+
return appDataStore!;
29+
},
2530
26-
setContext(RUNTIME_APP_DATA_CONTEXT, runtimeAppDataStore);
27-
setContext(APP_DATA_CONTEXT, appDataStore);
31+
get runtimeAppData() {
32+
return runtimeAppDataStore!;
33+
},
34+
});
2835
</script>
2936

3037
{#if $runtimeAppData.isLoading || $appData.isLoading}
@@ -33,8 +40,8 @@
3340
<div class="skeleton" style="width: 70%; height: 1rem;"></div>
3441
<div class="skeleton" style="width: 80%; height: 1rem;"></div>
3542
</div>
36-
{:else if $runtimeAppDataStore !== undefined || $appDataStore !== undefined}
37-
<slot />
43+
{:else if appDataStore !== undefined || runtimeAppDataStore !== undefined}
44+
{@render children()}
3845
{/if}
3946

4047
<style>

0 commit comments

Comments
 (0)