Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sidebar forecast type race condition, closes #478 #479

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/lib/Sidebar/WeatherForecast.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { WeatherIconSet, WeatherIconConditions, WeatherIconMapping } from '$lib/Weather';
import Icon from '@iconify/svelte';
import { getSupport } from '$lib/Utils';
import { onDestroy, tick } from 'svelte';
import { onDestroy } from 'svelte';
import { writable } from 'svelte/store';

export let sel: any;
Expand Down Expand Up @@ -32,9 +32,15 @@
$: if (sel?.entity_id) $entity_id = sel?.entity_id;
$: if (!sel?.forecast_type || sel?.forecast_type) $forecast_type = sel?.forecast_type;

// forecast_type fallback
$: defaultForecastType = Object.keys(supports)
?.find((key) => supports?.[key])
?.replace('FORECAST_', '')
?.toLowerCase();

// get forecast when not dragging
// and when entity_id or forecast_type changes
$: if (($forecast_type || !$forecast_type) && $entity_id && !$dragging) {
$: if (($forecast_type || !$forecast_type) && $entity_id && !$dragging && defaultForecastType) {
getForecast();

if (debug) {
Expand All @@ -50,20 +56,12 @@
if (busy) return;
busy = true;

await tick();

// cleanup old sub
if (unsubscribe) {
if (debug) console.debug('forecast unsubscribed');
unsubscribe?.();
}

// forecast_type fallback
const defaultForecastType = Object.keys(supports)
?.find((key) => supports[key])
?.replace('FORECAST_', '')
?.toLowerCase();

try {
unsubscribe = await $connection?.subscribeMessage(
(data: any) => {
Expand Down