@@ -42,7 +42,6 @@ import {
4242 SUBDOMAIN_TOR
4343} from '$lib/constants' ;
4444import { building } from '$app/environment' ;
45- import { getProjectId } from '$lib/helpers/project' ;
4645
4746export function getApiEndpoint ( region ?: string ) : string {
4847 if ( building ) return '' ;
@@ -141,12 +140,32 @@ const sdkForProject = {
141140} ;
142141
143142export const realtime = {
144- forProject ( region : string , _projectId : string ) {
143+ forProject (
144+ region : string ,
145+ channels : string | string [ ] ,
146+ callback : AppwriteRealtimeResponseEvent
147+ ) {
145148 const endpoint = getApiEndpoint ( region ) ;
146149 if ( endpoint !== clientRealtime . config . endpoint ) {
147150 clientRealtime . setEndpoint ( endpoint ) ;
148151 }
149- return clientRealtime ;
152+
153+ // because uses a different client!
154+ const realtime = new Realtime ( clientRealtime ) ;
155+
156+ return createRealtimeSubscription ( realtime , channels , callback ) ;
157+ } ,
158+
159+ forConsole (
160+ region : string ,
161+ channels : string | string [ ] ,
162+ callback : AppwriteRealtimeResponseEvent
163+ ) : ( ) => void {
164+ const realtimeInstance = region
165+ ? sdk . forConsoleIn ( region ) . realtime
166+ : sdk . forConsole . realtime ;
167+
168+ return createRealtimeSubscription ( realtimeInstance , channels , callback ) ;
150169 }
151170} ;
152171
@@ -176,8 +195,8 @@ export const sdk = {
176195} ;
177196
178197export enum RuleType {
179- DEPLOYMENT = 'deployment' ,
180198 API = 'api' ,
199+ DEPLOYMENT = 'deployment' ,
181200 REDIRECT = 'redirect'
182201}
183202
@@ -191,11 +210,24 @@ export enum RuleTrigger {
191210 MANUAL = 'manual'
192211}
193212
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-
199- export const createAdminClient = ( ) => {
200- return new Client ( ) . setEndpoint ( getApiEndpoint ( ) ) . setMode ( 'admin' ) . setProject ( getProjectId ( ) ) ;
213+ export type RealtimeResponse = {
214+ events : string [ ] ;
215+ channels : string [ ] ;
216+ timestamp : string ;
217+ payload : unknown ;
201218} ;
219+
220+ export type AppwriteRealtimeResponseEvent = ( response : RealtimeResponse ) => void ;
221+
222+ function createRealtimeSubscription (
223+ realtimeInstance : Realtime ,
224+ channels : string | string [ ] ,
225+ callback : AppwriteRealtimeResponseEvent
226+ ) : ( ) => void {
227+ const channelsArray = Array . isArray ( channels ) ? channels : [ channels ] ;
228+ const subscriptionPromise = realtimeInstance . subscribe ( channelsArray , callback ) ;
229+
230+ return ( ) => {
231+ subscriptionPromise . then ( ( sub ) => sub . close ( ) ) ;
232+ } ;
233+ }
0 commit comments