Skip to content

Commit

Permalink
fix: focus, pubsub, and select box
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Mar 13, 2023
1 parent acd8055 commit 4c71948
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
12 changes: 5 additions & 7 deletions internal/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,23 @@ func (p *Pub) unsubscribeFunc(topics []Topic, sub []*Sub) func() {
p.subsMapMu.Lock()
for i, sub := range sub {
topic := topics[i]
// There should only be 1 or 0 sub in next because the unsubscribe function might be called twice
next := p.subsMap[topic]
// Sub found in first place
if next == sub {
p.subsMap[topic] = next.next
if next == nil {
continue
}
if next == nil {
if next == sub {
p.subsMap[topic] = next.next
continue
}

// Will never be nil
prev := next

for next = next.next; next != nil; next = next.next {
// Sub found in second place or more
if next == sub {
prev.next = next.next
break
}
prev = next
}
}
p.subsMapMu.Unlock()
Expand Down
40 changes: 20 additions & 20 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,17 @@ const RadioSelect: Component<
> = (props) => {
let select: HTMLSelectElement | undefined;

// Prevent select.value from defaulting to the first option when props.radios() changes
createEffect(
on(
() => !props.radios.error && props.radios(),
() => {
select && (select.value = props.radioUUID());
},
{ defer: true }
)
);

return (
<select
class={mergeClass("select-primary select", props.class)}
Expand All @@ -483,24 +494,11 @@ const RadioSelect: Component<
Select Radio
</option>
<Show when={!props.radios.error}>
{() => {
// Prevent select.value from defaulting to the first option when radios.data changes
createEffect(
on(
props.radios,
() => {
select && (select.value = props.radioUUID());
},
{ defer: true }
)
);

return (
<For each={props.radios()}>
{(radio) => <option value={radio.uuid}>{radio.name}</option>}
</For>
);
}}
{() => (
<For each={props.radios()}>
{(radio) => <option value={radio.uuid}>{radio.name}</option>}
</For>
)}
</Show>
</select>
);
Expand All @@ -514,10 +512,10 @@ const App: Component = () => {
localStorage.setItem("lastRadioUUID", radioUUID());
});

const [data, ws] = useWS(radioUUID);
// WebSocket
const [{ state, discovering }, ws] = useWS(radioUUID);
const wsReconnecting = () => ws.connecting() && ws.disconnected();

const { state, discovering } = data;
const radioLoading = () => state.uuid != radioUUID() || ws.connecting();
const radioLoaded = () => radioUUID() == state.uuid && ws.connected();

Expand All @@ -541,8 +539,10 @@ const App: Component = () => {
}
};
document.addEventListener("visibilitychange", onVisibilityChange);
window.addEventListener("focus", onVisibilityChange);
onCleanup(() => {
document.removeEventListener("visibilitychange", onVisibilityChange);
window.removeEventListener("focus", onVisibilityChange);
});

return (
Expand Down
21 changes: 12 additions & 9 deletions web/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ export function useWS(radioUUID: Accessor<string>): WSReturn {
console.log("WS: Message");
batch(() => {
const msg = JSON.parse(event.data as string) as WsEvent;
if (msg.topic == PubsubTopic.StateTopic) {
const data = msg.data as StateState;
if (data.uuid == radioUUID()) {
setState(
produce((state) => Object.assign(state, msg.data as StateState))
);
}
} else if (msg.topic == PubsubTopic.DiscoverTopic) {
setDiscovering(msg.data as boolean);
switch (msg.topic) {
case PubsubTopic.StateTopic:
const data = msg.data as StateState;
if (data.uuid == radioUUID()) {
setState(
produce((state) => Object.assign(state, msg.data as StateState))
);
}
break;
case PubsubTopic.DiscoverTopic:
setDiscovering(msg.data as boolean);
break;
}
setSynced(true);
});
Expand Down

0 comments on commit 4c71948

Please sign in to comment.