Skip to content

Commit 72cda67

Browse files
feat: simplify graphql subscriptions
1 parent 05b30de commit 72cda67

1 file changed

Lines changed: 19 additions & 30 deletions

File tree

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
import { gqlSubscribeClient } from "$lib/graphql-client";
2-
import {
3-
MOVIE_REQUESTED_SUBSCRIPTION,
4-
SHOW_INDEXED_SUBSCRIPTION,
5-
SHOW_REQUESTED_SUBSCRIPTION,
6-
SHOW_REQUEST_UPDATED_SUBSCRIPTION
7-
} from "$lib/services/riven-media";
82

9-
const MEDIA_EVENT_SUBSCRIPTIONS = [
10-
MOVIE_REQUESTED_SUBSCRIPTION,
11-
SHOW_REQUESTED_SUBSCRIPTION,
12-
SHOW_REQUEST_UPDATED_SUBSCRIPTION,
13-
SHOW_INDEXED_SUBSCRIPTION,
14-
`subscription RivenItemScraped {
15-
itemScraped
16-
}`,
17-
`subscription RivenItemDownloaded {
18-
itemDownloaded
19-
}`,
20-
`subscription RivenItemFailed {
21-
itemFailed
22-
}`,
23-
`subscription RivenItemsDeleted {
24-
itemsDeleted
25-
}`
26-
];
3+
/// Unified backend subscription that fans every relevant media-state event
4+
/// (movie/show requested, show indexed, item scraped/downloaded/failed,
5+
/// items deleted) onto a single multipart stream. One connection replaces
6+
/// eight individual subscriptions, which keeps the home/library/dashboard
7+
/// pages under the per-origin connection cap on HTTP/1.1 deployments and
8+
/// cuts subscription-side server work by ~8x on every transport.
9+
const MEDIA_EVENTS_SUBSCRIPTION = `subscription RivenMediaEvents {
10+
mediaEvents { kind itemId }
11+
}`;
12+
13+
type MediaEventPayload = {
14+
mediaEvents: { kind: string; itemId: number | null };
15+
};
2716

2817
export function subscribeToRivenMediaEvents(
2918
refresh: () => void | Promise<void>,
@@ -40,21 +29,21 @@ export function subscribeToRivenMediaEvents(
4029
}, debounceMs);
4130
}
4231

43-
const unsubscribers = MEDIA_EVENT_SUBSCRIPTIONS.map((subscription) =>
44-
gqlSubscribeClient<Record<string, unknown>>(subscription, undefined, {
32+
const unsubscribe = gqlSubscribeClient<MediaEventPayload>(
33+
MEDIA_EVENTS_SUBSCRIPTION,
34+
undefined,
35+
{
4536
onData: refreshSoon,
4637
onError: () => {
4738
// Callers keep their last successful data snapshot. The shared GraphQL
4839
// subscription client owns transport-level retry behaviour where needed.
4940
}
50-
})
41+
}
5142
);
5243

5344
return () => {
5445
active = false;
5546
clearTimeout(refreshTimer);
56-
for (const unsubscribe of unsubscribers) {
57-
unsubscribe();
58-
}
47+
unsubscribe();
5948
};
6049
}

0 commit comments

Comments
 (0)