@@ -35,7 +35,11 @@ export class SurrealdbService extends Surreal {
3535 super ( )
3636 }
3737
38- private recordIdToString ( recordId : RecordId < string > | StringRecordId ) : string {
38+ /**
39+ * Convert a RecordId to a string representation.
40+ * Public method to be used by components for consistent RecordId handling.
41+ */
42+ recordIdToString ( recordId : RecordId < string > | StringRecordId ) : string {
3943 const asString = ( recordId as unknown as { toString ?: ( ) => string } ) ?. toString ?.( )
4044 if ( typeof asString === 'string' ) {
4145 return asString
@@ -312,9 +316,19 @@ export class SurrealdbService extends Surreal {
312316 const queryUuid = await super . live < T > (
313317 table ,
314318 ( action , result ) => {
315- const update : LiveQueryUpdate < T > = {
316- action : action as 'CREATE' | 'UPDATE' | 'DELETE' ,
317- result : result as T
319+ // Handle all possible actions including CLOSE
320+ let update : LiveQueryUpdate < T >
321+
322+ if ( action === 'CLOSE' ) {
323+ update = { action : 'CLOSE' }
324+ // Clean up on CLOSE
325+ this . liveQueryCallbacks . delete ( queryKey )
326+ this . liveQueryUuids . delete ( queryKey )
327+ } else {
328+ update = {
329+ action : action as 'CREATE' | 'UPDATE' | 'DELETE' ,
330+ result : result as T
331+ }
318332 }
319333
320334 // Notify all callbacks for this query
@@ -331,6 +345,17 @@ export class SurrealdbService extends Surreal {
331345 this . liveQueryUuids . set ( queryKey , queryUuid )
332346 } catch ( error ) {
333347 console . error ( 'Failed to create live query:' , error )
348+ // Notify listeners of failure via CLOSE event
349+ const update : LiveQueryUpdate < T > = { action : 'CLOSE' }
350+ const callbacks = this . liveQueryCallbacks . get ( queryKey )
351+ if ( callbacks ) {
352+ callbacks . forEach ( callback => {
353+ ( callback as LiveQueryCallback < T > ) ( update )
354+ } )
355+ }
356+ // Clean up
357+ this . liveQueryCallbacks . delete ( queryKey )
358+ this . liveQueryUuids . delete ( queryKey )
334359 throw error
335360 }
336361 }
0 commit comments