Skip to content

Commit 6cd6310

Browse files
committed
update: simplify logic.
1 parent 407163c commit 6cd6310

16 files changed

Lines changed: 199 additions & 225 deletions

File tree

src/lib/components/backupRestoreBox.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
126126
onMount(() => {
127127
// fast path: don't subscribe if org is on a free plan or is self-hosted.
128-
if (isSelfHosted || (isCloud && $organization.billingPlan === BillingPlan.FREE)) return;
128+
if (isSelfHosted || (isCloud && $organization?.billingPlan === BillingPlan.FREE)) return;
129129
130130
return realtime
131131
.forProject(page.params.region, page.params.project)

src/lib/components/csvImportBox.svelte

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import { onMount } from 'svelte';
33
import { base } from '$app/paths';
44
import { page } from '$app/state';
5-
import { type AppwriteRealtimeSubscription, sdk } from '$lib/stores/sdk';
65
import { Dependencies } from '$lib/constants';
6+
import { realtime, sdk } from '$lib/stores/sdk';
77
import { goto, invalidate } from '$app/navigation';
88
import { getProjectId } from '$lib/helpers/project';
99
import { writable, type Writable } from 'svelte/store';
@@ -157,7 +157,7 @@
157157
}
158158
}
159159
160-
onMount(async () => {
160+
onMount(() => {
161161
sdk.forProject(page.params.region, page.params.project)
162162
.migrations.list({
163163
queries: [
@@ -169,16 +169,12 @@
169169
migrations.migrations.forEach(updateOrAddItem);
170170
});
171171
172-
const subscription: AppwriteRealtimeSubscription = await sdk
173-
.forConsoleIn(page.params.region)
174-
.realtime.subscribe('console', (response) => {
175-
if (!response.channels.includes(`projects.${getProjectId()}`)) return;
176-
if (response.events.includes('migrations.*')) {
177-
updateOrAddItem(response.payload as Payload);
178-
}
179-
});
180-
181-
return await subscription.close();
172+
return realtime.forConsole(page.params.region, 'console', (response) => {
173+
if (!response.channels.includes(`projects.${getProjectId()}`)) return;
174+
if (response.events.includes('migrations.*')) {
175+
updateOrAddItem(response.payload as Payload);
176+
}
177+
});
182178
});
183179
184180
$: isOpen = true;

src/lib/stores/sdk.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isMultiRegionSupported, VARS } from '$lib/system';
1+
import { isDev, isMultiRegionSupported, VARS } from '$lib/system';
22
import {
33
Account,
44
Assistant,
@@ -147,6 +147,33 @@ export const realtime = {
147147
clientRealtime.setEndpoint(endpoint);
148148
}
149149
return clientRealtime;
150+
},
151+
152+
forConsole(
153+
region: string,
154+
channels: string | string[],
155+
// the generic `<T>` is too strict, any is too loose!
156+
callback: Parameters<Realtime['subscribe']>[1]
157+
): () => void {
158+
let closed = false;
159+
160+
const channelsArray = Array.isArray(channels) ? channels : [channels];
161+
const subscriptionPromise = sdk
162+
.forConsoleIn(region)
163+
.realtime.subscribe(channelsArray, callback);
164+
165+
return () => {
166+
if (closed) return;
167+
closed = true;
168+
169+
subscriptionPromise
170+
.then((sub) => sub.close())
171+
.catch((error) => {
172+
if (isDev) {
173+
console.log(error.message);
174+
}
175+
});
176+
};
150177
}
151178
};
152179

@@ -191,11 +218,6 @@ export enum RuleTrigger {
191218
MANUAL = 'manual'
192219
}
193220

194-
/**
195-
* Some type imports are broken on the SDK, this works correctly for the time being!
196-
*/
197-
export type AppwriteRealtimeSubscription = Awaited<ReturnType<Realtime['subscribe']>>;
198-
199221
export const createAdminClient = () => {
200222
return new Client().setEndpoint(getApiEndpoint()).setMode('admin').setProject(getProjectId());
201223
};

src/routes/(console)/project-[region]-[project]/functions/function-[function]/executions/+page.svelte

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Dependencies } from '$lib/constants';
55
import { Button } from '$lib/elements/forms';
66
import { Container, ResponsiveContainerHeader } from '$lib/layout';
7-
import { type AppwriteRealtimeSubscription, sdk } from '$lib/stores/sdk';
7+
import { realtime } from '$lib/stores/sdk';
88
import { onMount } from 'svelte';
99
import { project } from '$routes/(console)/project-[region]-[project]/store';
1010
import { base } from '$app/paths';
@@ -13,20 +13,16 @@
1313
import Table from './table.svelte';
1414
import { columns } from './store';
1515
import type { PageProps } from './$types';
16+
import { page } from '$app/state';
1617
1718
let { data }: PageProps = $props();
1819
19-
onMount(async () => {
20-
const subscription: AppwriteRealtimeSubscription = await sdk.forConsole.realtime.subscribe(
21-
'console',
22-
(response) => {
23-
if (response.events.includes('functions.*.executions.*')) {
24-
invalidate(Dependencies.EXECUTIONS);
25-
}
20+
onMount(() => {
21+
return realtime.forConsole(page.params.region, 'console', (response) => {
22+
if (response.events.includes('functions.*.executions.*')) {
23+
invalidate(Dependencies.EXECUTIONS);
2624
}
27-
);
28-
29-
return await subscription.close();
25+
});
3026
});
3127
</script>
3228

src/routes/(console)/project-[region]-[project]/overview/platforms/createAndroid.svelte

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import { IconAndroid, IconAppwrite, IconInfo } from '@appwrite.io/pink-icons-svelte';
1717
import { Card } from '$lib/components';
1818
import { page } from '$app/state';
19-
import { onDestroy, onMount } from 'svelte';
20-
import { type AppwriteRealtimeSubscription, sdk } from '$lib/stores/sdk';
19+
import { onMount } from 'svelte';
20+
import { realtime, sdk } from '$lib/stores/sdk';
2121
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
2222
import { addNotification } from '$lib/stores/notifications';
2323
import { fade } from 'svelte/transition';
@@ -80,23 +80,21 @@ const val APPWRITE_PUBLIC_ENDPOINT = "${sdk.forProject(page.params.region, page.
8080
createPlatform.reset();
8181
}
8282
83-
onMount(async () => {
84-
const subscription: AppwriteRealtimeSubscription = await sdk.forConsole.realtime.subscribe(
85-
'console',
86-
(response) => {
87-
if (response.events.includes(`projects.${projectId}.ping`)) {
88-
connectionSuccessful = true;
89-
invalidate(Dependencies.ORGANIZATION);
90-
invalidate(Dependencies.PROJECT);
91-
subscription.close();
92-
}
83+
onMount(() => {
84+
const subscription = realtime.forConsole(page.params.region, 'console', (response) => {
85+
if (response.events.includes(`projects.${projectId}.ping`)) {
86+
connectionSuccessful = true;
87+
invalidate(Dependencies.ORGANIZATION);
88+
invalidate(Dependencies.PROJECT);
89+
subscription();
9390
}
94-
);
91+
});
9592
96-
return await subscription.close();
93+
return () => {
94+
subscription();
95+
resetPlatformStore();
96+
};
9797
});
98-
99-
onDestroy(resetPlatformStore);
10098
</script>
10199

102100
<Wizard

src/routes/(console)/project-[region]-[project]/overview/platforms/createApple.svelte

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import { IconApple, IconAppwrite, IconInfo } from '@appwrite.io/pink-icons-svelte';
1818
import { Card } from '$lib/components';
1919
import { page } from '$app/state';
20-
import { onDestroy, onMount } from 'svelte';
21-
import { type AppwriteRealtimeSubscription, sdk } from '$lib/stores/sdk';
20+
import { onMount } from 'svelte';
21+
import { realtime, sdk } from '$lib/stores/sdk';
2222
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
2323
import { addNotification } from '$lib/stores/notifications';
2424
import { fade } from 'svelte/transition';
@@ -89,23 +89,21 @@ APPWRITE_PUBLIC_ENDPOINT: "${sdk.forProject(page.params.region, page.params.proj
8989
createPlatform.reset();
9090
}
9191
92-
onMount(async () => {
93-
const subscription: AppwriteRealtimeSubscription = await sdk.forConsole.realtime.subscribe(
94-
'console',
95-
(response) => {
96-
if (response.events.includes(`projects.${projectId}.ping`)) {
97-
connectionSuccessful = true;
98-
invalidate(Dependencies.ORGANIZATION);
99-
invalidate(Dependencies.PROJECT);
100-
subscription.close();
101-
}
92+
onMount(() => {
93+
const subscription = realtime.forConsole(page.params.region, 'console', (response) => {
94+
if (response.events.includes(`projects.${projectId}.ping`)) {
95+
connectionSuccessful = true;
96+
invalidate(Dependencies.ORGANIZATION);
97+
invalidate(Dependencies.PROJECT);
98+
subscription();
10299
}
103-
);
100+
});
104101
105-
return await subscription.close();
102+
return () => {
103+
subscription();
104+
resetPlatformStore();
105+
};
106106
});
107-
108-
onDestroy(resetPlatformStore);
109107
</script>
110108

111109
<Wizard

src/routes/(console)/project-[region]-[project]/overview/platforms/createFlutter.svelte

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import { IconFlutter, IconAppwrite, IconInfo } from '@appwrite.io/pink-icons-svelte';
1818
import { Card } from '$lib/components';
1919
import { page } from '$app/state';
20-
import { onDestroy, onMount } from 'svelte';
21-
import { type AppwriteRealtimeSubscription, sdk } from '$lib/stores/sdk';
20+
import { onMount } from 'svelte';
21+
import { realtime, sdk } from '$lib/stores/sdk';
2222
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
2323
import { addNotification } from '$lib/stores/notifications';
2424
import { fade } from 'svelte/transition';
@@ -155,23 +155,21 @@
155155
createPlatform.reset();
156156
}
157157
158-
onMount(async () => {
159-
const subscription: AppwriteRealtimeSubscription = await sdk.forConsole.realtime.subscribe(
160-
'console',
161-
(response) => {
162-
if (response.events.includes(`projects.${projectId}.ping`)) {
163-
connectionSuccessful = true;
164-
invalidate(Dependencies.ORGANIZATION);
165-
invalidate(Dependencies.PROJECT);
166-
subscription.close();
167-
}
158+
onMount(() => {
159+
const subscription = realtime.forConsole(page.params.region, 'console', (response) => {
160+
if (response.events.includes(`projects.${projectId}.ping`)) {
161+
connectionSuccessful = true;
162+
invalidate(Dependencies.ORGANIZATION);
163+
invalidate(Dependencies.PROJECT);
164+
subscription();
168165
}
169-
);
166+
});
170167
171-
return await subscription.close();
168+
return () => {
169+
subscription();
170+
resetPlatformStore();
171+
};
172172
});
173-
174-
onDestroy(resetPlatformStore);
175173
</script>
176174

177175
<Wizard

src/routes/(console)/project-[region]-[project]/overview/platforms/createReactNative.svelte

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import { IconReact, IconAppwrite, IconInfo } from '@appwrite.io/pink-icons-svelte';
1818
import { Card } from '$lib/components';
1919
import { page } from '$app/state';
20-
import { onDestroy, onMount } from 'svelte';
21-
import { type AppwriteRealtimeSubscription, sdk } from '$lib/stores/sdk';
20+
import { onMount } from 'svelte';
21+
import { realtime, sdk } from '$lib/stores/sdk';
2222
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
2323
import { addNotification } from '$lib/stores/notifications';
2424
import { fade } from 'svelte/transition';
@@ -116,23 +116,21 @@ EXPO_PUBLIC_APPWRITE_ENDPOINT=${sdk.forProject(page.params.region, page.params.p
116116
createPlatform.reset();
117117
}
118118
119-
onMount(async () => {
120-
const subscription: AppwriteRealtimeSubscription = await sdk.forConsole.realtime.subscribe(
121-
'console',
122-
(response) => {
123-
if (response.events.includes(`projects.${projectId}.ping`)) {
124-
connectionSuccessful = true;
125-
invalidate(Dependencies.ORGANIZATION);
126-
invalidate(Dependencies.PROJECT);
127-
subscription.close();
128-
}
119+
onMount(() => {
120+
const subscription = realtime.forConsole(page.params.region, 'console', (response) => {
121+
if (response.events.includes(`projects.${projectId}.ping`)) {
122+
connectionSuccessful = true;
123+
invalidate(Dependencies.ORGANIZATION);
124+
invalidate(Dependencies.PROJECT);
125+
subscription();
129126
}
130-
);
127+
});
131128
132-
return await subscription.close();
129+
return () => {
130+
subscription();
131+
resetPlatformStore();
132+
};
133133
});
134-
135-
onDestroy(resetPlatformStore);
136134
</script>
137135

138136
<Wizard

src/routes/(console)/project-[region]-[project]/overview/platforms/createWeb.svelte

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
IconJs
2828
} from '@appwrite.io/pink-icons-svelte';
2929
import { page } from '$app/state';
30-
import { onDestroy, onMount } from 'svelte';
31-
import { type AppwriteRealtimeSubscription, sdk } from '$lib/stores/sdk';
30+
import { onMount } from 'svelte';
31+
import { realtime, sdk } from '$lib/stores/sdk';
3232
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
3333
import { addNotification } from '$lib/stores/notifications';
3434
import { fade } from 'svelte/transition';
@@ -201,23 +201,21 @@ ${prefix}APPWRITE_ENDPOINT = "${sdk.forProject(page.params.region, page.params.p
201201
createPlatform.reset();
202202
}
203203
204-
onMount(async () => {
205-
const subscription: AppwriteRealtimeSubscription = await sdk.forConsole.realtime.subscribe(
206-
'console',
207-
(response) => {
208-
if (response.events.includes(`projects.${projectId}.ping`)) {
209-
connectionSuccessful = true;
210-
invalidate(Dependencies.ORGANIZATION);
211-
invalidate(Dependencies.PROJECT);
212-
subscription?.close();
213-
}
204+
onMount(() => {
205+
const subscription = realtime.forConsole(page.params.region, 'console', (response) => {
206+
if (response.events.includes(`projects.${projectId}.ping`)) {
207+
connectionSuccessful = true;
208+
invalidate(Dependencies.ORGANIZATION);
209+
invalidate(Dependencies.PROJECT);
210+
subscription();
214211
}
215-
);
212+
});
216213
217-
return subscription?.close();
214+
return () => {
215+
subscription();
216+
resetPlatformStore();
217+
};
218218
});
219-
220-
onDestroy(resetPlatformStore);
221219
</script>
222220

223221
<Wizard

0 commit comments

Comments
 (0)