Skip to content

Commit d874016

Browse files
committed
A bit of redux housekeeping
We've had this weird function signature in the core of the redux state management. In this commit we remove that extra function, while preserving reactivity as is. Also cleans up a few typos, and reduces a bit of code duplication.
1 parent 18565b2 commit d874016

File tree

4 files changed

+29
-39
lines changed

4 files changed

+29
-39
lines changed

apps/desktop/src/lib/state/clientState.svelte.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import type { IrcClient } from '$lib/irc/ircClient.svelte';
2424
import type { ReduxError } from '$lib/state/reduxError';
2525

2626
/**
27-
* GitHub API object that enables the declaration and usage of endpoints
27+
* Backend API object that enables the declaration and usage of endpoints
2828
* colocated with the feature they support.
2929
*/
3030
export type BackendApi = ReturnType<typeof createBackendApi>;
@@ -78,9 +78,8 @@ export class ClientState {
7878
posthog: PostHogWrapper
7979
) {
8080
const butlerMod = butlerModule({
81-
// Reactive loop without nested function.
82-
// TODO: Can it be done without nesting?
83-
getState: () => () => this.rootState as any as RootState<any, any, any>,
81+
// Returns a function that returns the current state (required by butlerModule API)
82+
getState: () => this.rootState as unknown as RootState<any, any, any>,
8483
getDispatch: () => this.dispatch,
8584
posthog
8685
});
@@ -177,7 +176,6 @@ function createStore(params: {
177176
}
178177
});
179178

180-
// persistStore(store);
181179
return { store, reducer };
182180
}
183181

@@ -207,8 +205,8 @@ function createBackendApi(butlerMod: ReturnType<typeof butlerModule>) {
207205
}
208206

209207
// Default cache expiration for unused items is 60 seconds. This is too little
210-
// for forge data.
211-
const KEEP_UNUSED_SECONDS = 24 * 60 * 60;
208+
// for forge data, so we keep forge data cached for 24 hours.
209+
const FORGE_CACHE_TTL_SECONDS = 24 * 60 * 60; // 24 hours
212210

213211
// Fake base query that allows us to use the same error type when the query
214212
// definitions only use `queryFn` instead of `query`.
@@ -217,22 +215,25 @@ const fakeBaseQuery: BaseQueryFn = () => {
217215
return { data: undefined } as QueryReturnValue<never, ReduxError, any>;
218216
};
219217

218+
// Common API configuration for forge APIs
219+
const FORGE_API_CONFIG = {
220+
tagTypes: Object.values(ReduxTag),
221+
invalidationBehavior: 'immediately' as const,
222+
baseQuery: fakeBaseQuery,
223+
refetchOnFocus: true,
224+
refetchOnReconnect: true,
225+
keepUnusedDataFor: FORGE_CACHE_TTL_SECONDS,
226+
endpoints: (_: any) => ({})
227+
};
228+
220229
export function createGitHubApi(butlerMod: ReturnType<typeof butlerModule>) {
221230
return buildCreateApi(
222231
coreModule(),
223232
butlerMod
224233
)({
225234
reducerPath: 'github',
226-
tagTypes: Object.values(ReduxTag),
227-
invalidationBehavior: 'immediately',
228-
// TODO: This should only be set for backend api.
229-
baseQuery: fakeBaseQuery,
230-
refetchOnFocus: true,
231-
refetchOnReconnect: true,
232-
keepUnusedDataFor: KEEP_UNUSED_SECONDS,
233-
endpoints: (_) => {
234-
return {};
235-
}
235+
// Using fake base query for forge APIs (GitHub/GitLab) since they use queryFn
236+
...FORGE_API_CONFIG
236237
});
237238
}
238239

@@ -242,15 +243,7 @@ function createGitLabApi(butlerMod: ReturnType<typeof butlerModule>) {
242243
butlerMod
243244
)({
244245
reducerPath: 'gitlab',
245-
tagTypes: Object.values(ReduxTag),
246-
invalidationBehavior: 'immediately',
247-
// TODO: This should only be set for backend api.
248-
baseQuery: fakeBaseQuery,
249-
refetchOnFocus: true,
250-
refetchOnReconnect: true,
251-
keepUnusedDataFor: KEEP_UNUSED_SECONDS,
252-
endpoints: (_) => {
253-
return {};
254-
}
246+
// Using fake base query for forge APIs (GitHub/GitLab) since they use queryFn
247+
...FORGE_API_CONFIG
255248
});
256249
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { PostHogWrapper } from '$lib/analytics/posthog';
2-
import type { EntityState, ThunkDispatch, UnknownAction } from '@reduxjs/toolkit';
3-
import type { CombinedState } from '@reduxjs/toolkit/query';
2+
import type { ThunkDispatch, UnknownAction } from '@reduxjs/toolkit';
3+
import type { RootState } from '@reduxjs/toolkit/query';
44

55
/**
66
* The api is necessary to create the store, so we need to provide
@@ -11,7 +11,7 @@ import type { CombinedState } from '@reduxjs/toolkit/query';
1111
*/
1212
export type HookContext = {
1313
/** Without the nested function we get looping reactivity. */
14-
getState: () => () => { [k: string]: CombinedState<any, any, any> | EntityState<any, any> };
14+
getState: () => RootState<any, any, any>;
1515
getDispatch: () => ThunkDispatch<any, any, UnknownAction>;
1616
posthog?: PostHogWrapper;
1717
};

apps/desktop/src/lib/state/customHooks.svelte.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
type QueryActionCreatorResult,
1515
type QueryArgFrom,
1616
type ResultTypeFrom,
17-
type RootState,
1817
type StartQueryActionCreatorOptions
1918
} from '@reduxjs/toolkit/query';
2019
import { createSubscriber } from 'svelte/reactivity';
@@ -49,7 +48,6 @@ export function buildQueryHooks<Definitions extends ExtensionDefinitions>({
4948
ctx: HookContext;
5049
}) {
5150
const endpoint = api.endpoints[endpointName]!;
52-
const state = getState() as any as () => RootState<any, any, any>;
5351

5452
const { initiate, select } = endpoint as ApiEndpointQuery<CustomQuery<any>, Definitions>;
5553

@@ -128,7 +126,7 @@ export function buildQueryHooks<Definitions extends ExtensionDefinitions>({
128126
}
129127

130128
const selector = $derived(select(queryArg));
131-
const result = $derived(selector(state()));
129+
const result = $derived(selector(getState()));
132130

133131
const output = $derived.by(() => {
134132
let data = result.data;
@@ -181,7 +179,7 @@ export function buildQueryHooks<Definitions extends ExtensionDefinitions>({
181179

182180
const results = queryArgs.map((queryArg) => {
183181
const selector = $derived(select(queryArg));
184-
const result = $derived(selector(state()));
182+
const result = $derived(selector(getState()));
185183
const output = $derived.by(() => {
186184
let data = result.data;
187185
if (options?.transform && data) {
@@ -202,7 +200,7 @@ export function buildQueryHooks<Definitions extends ExtensionDefinitions>({
202200

203201
function useQueryState<T extends TranformerFn>(queryArg: unknown, options?: { transform?: T }) {
204202
const selector = $derived(select(queryArg));
205-
const result = $derived(selector(state()));
203+
const result = $derived(selector(getState()));
206204
const output = $derived.by(() => {
207205
let data = result.data;
208206
if (options?.transform && data) {
@@ -219,7 +217,7 @@ export function buildQueryHooks<Definitions extends ExtensionDefinitions>({
219217

220218
function useQueryTimeStamp(queryArg: unknown) {
221219
const selector = $derived(select(queryArg));
222-
const result = $derived(selector(state()));
220+
const result = $derived(selector(getState()));
223221
return reactive(() => result.startedTimeStamp);
224222
}
225223

@@ -344,7 +342,6 @@ export function buildMutationHook<
344342
ctx: HookContext;
345343
}): MutationHook<D> {
346344
const endpoint = api.endpoints[endpointName]!;
347-
const state = getState() as any as () => RootState<any, any, any>;
348345

349346
const { initiate, select } = endpoint as unknown as ApiEndpointMutation<D, Definitions>;
350347

@@ -454,7 +451,7 @@ export function buildMutationHook<
454451
}
455452

456453
const selector = $derived(select({ requestId: promise?.requestId, fixedCacheKey }));
457-
const result = $derived(selector(state()));
454+
const result = $derived(selector(getState()));
458455

459456
const subscribe = createSubscriber(() => {
460457
return () => {

apps/desktop/src/lib/testing/mockGitHubApi.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function setupMockGitHubApi() {
3131
const gitHubClient = new GitHubClient({ client: octokit });
3232
gitHubClient.setRepo({ owner: 'test-owner', repo: 'test-repo' });
3333
const gitHubApi = createGitHubApi(
34-
butlerModule({ getDispatch: () => dispatch!, getState: () => () => state })
34+
butlerModule({ getDispatch: () => dispatch!, getState: () => state })
3535
);
3636

3737
const store = configureStore({

0 commit comments

Comments
 (0)