@@ -15,6 +15,8 @@ public struct RealtimeChannelConfig: Sendable {
15
15
}
16
16
17
17
public actor RealtimeChannelV2 {
18
+ public typealias Subscription = ObservationToken
19
+
18
20
public enum Status : Sendable {
19
21
case unsubscribed
20
22
case subscribing
@@ -340,94 +342,85 @@ public actor RealtimeChannelV2 {
340
342
}
341
343
342
344
/// Listen for clients joining / leaving the channel using presences.
343
- public func presenceChange( ) - > AsyncStream< any PresenceAction> {
344
- let ( stream, continuation) = AsyncStream < any PresenceAction > . makeStream ( )
345
-
346
- let id = callbackManager. addPresenceCallback {
347
- continuation. yield ( $0)
348
- }
349
-
350
- let logger = logger
351
-
352
- continuation. onTermination = { [ weak callbackManager] _ in
345
+ public func onPresenceChange(
346
+ _ callback: @escaping @Sendable ( any PresenceAction ) -> Void
347
+ ) - > Subscription {
348
+ let id = callbackManager. addPresenceCallback ( callback: callback)
349
+ return Subscription { [ weak callbackManager, logger] in
353
350
logger? . debug ( " Removing presence callback with id: \( id) " )
354
351
callbackManager? . removeCallback ( id: id)
355
352
}
356
-
357
- return stream
358
353
}
359
354
360
355
/// Listen for postgres changes in a channel.
361
- public func postgresChange (
356
+ public func onPostgresChange (
362
357
_: InsertAction . Type,
363
358
schema: String = " public " ,
364
359
table: String? = nil ,
365
- filter: String? = nil
366
- ) - > AsyncStream< InsertAction> {
367
- postgresChange ( event: . insert, schema: schema, table: table, filter: filter)
368
- . compactMap { $0. wrappedAction as? InsertAction }
369
- . eraseToStream ( )
360
+ filter: String? = nil ,
361
+ callback: @escaping @Sendable ( InsertAction) - > Void
362
+ ) - > Subscription {
363
+ _onPostgresChange (
364
+ event: . insert,
365
+ schema: schema,
366
+ table: table,
367
+ filter: filter
368
+ ) {
369
+ guard case let . insert( action) = $0 else { return }
370
+ callback ( action)
371
+ }
370
372
}
371
373
372
374
/// Listen for postgres changes in a channel.
373
- public func postgresChange (
375
+ public func onPostgresChange (
374
376
_: UpdateAction . Type,
375
377
schema: String = " public " ,
376
378
table: String? = nil ,
377
- filter: String? = nil
378
- ) - > AsyncStream< UpdateAction> {
379
- postgresChange ( event: . update, schema: schema, table: table, filter: filter)
380
- . compactMap { $0. wrappedAction as? UpdateAction }
381
- . eraseToStream ( )
379
+ filter: String? = nil ,
380
+ callback: @escaping @Sendable ( UpdateAction) - > Void
381
+ ) - > Subscription {
382
+ _onPostgresChange (
383
+ event: . update,
384
+ schema: schema,
385
+ table: table,
386
+ filter: filter
387
+ ) {
388
+ guard case let . update( action) = $0 else { return }
389
+ callback ( action)
390
+ }
382
391
}
383
392
384
393
/// Listen for postgres changes in a channel.
385
- public func postgresChange (
394
+ public func onPostgresChange (
386
395
_: DeleteAction . Type,
387
396
schema: String = " public " ,
388
397
table: String? = nil ,
389
- filter: String? = nil
390
- ) - > AsyncStream< DeleteAction> {
391
- postgresChange ( event: . delete, schema: schema, table: table, filter: filter)
392
- . compactMap { $0. wrappedAction as? DeleteAction }
393
- . eraseToStream ( )
394
- }
395
-
396
- /// Listen for postgres changes in a channel.
397
- public func postgresChange(
398
- _: SelectAction . Type,
399
- schema: String = " public " ,
400
- table: String? = nil ,
401
- filter: String? = nil
402
- ) - > AsyncStream< SelectAction> {
403
- postgresChange ( event: . select, schema: schema, table: table, filter: filter)
404
- . compactMap { $0. wrappedAction as? SelectAction }
405
- . eraseToStream ( )
406
- }
407
-
408
- /// Listen for postgres changes in a channel.
409
- public func postgresChange(
410
- _: AnyAction . Type,
411
- schema: String = " public " ,
412
- table: String? = nil ,
413
- filter: String? = nil
414
- ) - > AsyncStream< AnyAction> {
415
- postgresChange ( event: . all, schema: schema, table: table, filter: filter)
398
+ filter: String? = nil ,
399
+ callback: @escaping @Sendable ( DeleteAction) - > Void
400
+ ) - > Subscription {
401
+ _onPostgresChange (
402
+ event: . delete,
403
+ schema: schema,
404
+ table: table,
405
+ filter: filter
406
+ ) {
407
+ guard case let . delete( action) = $0 else { return }
408
+ callback ( action)
409
+ }
416
410
}
417
411
418
- private func postgresChange (
412
+ func _onPostgresChange (
419
413
event: PostgresChangeEvent,
420
414
schema: String,
421
415
table: String? ,
422
- filter: String?
423
- ) - > AsyncStream< AnyAction> {
416
+ filter: String? ,
417
+ callback: @escaping @Sendable ( AnyAction) - > Void
418
+ ) - > Subscription {
424
419
precondition (
425
420
status != . subscribed,
426
421
" You cannot call postgresChange after joining the channel "
427
422
)
428
423
429
- let ( stream, continuation) = AsyncStream< AnyAction> . makeStream( )
430
-
431
424
let config = PostgresJoinConfig (
432
425
event: event,
433
426
schema: schema,
@@ -437,44 +430,23 @@ public actor RealtimeChannelV2 {
437
430
438
431
clientChanges. append ( config)
439
432
440
- let id = callbackManager. addPostgresCallback ( filter: config) { action in
441
- continuation. yield ( action)
442
- }
443
-
444
- let logger = logger
445
-
446
- continuation. onTermination = { [ weak callbackManager] _ in
433
+ let id = callbackManager. addPostgresCallback ( filter: config, callback: callback)
434
+ return Subscription { [ weak callbackManager, logger] in
447
435
logger? . debug ( " Removing postgres callback with id: \( id) " )
448
436
callbackManager? . removeCallback ( id: id)
449
437
}
450
-
451
- return stream
452
438
}
453
439
454
- /// Listen for broadcast messages sent by other clients within the same channel under a specific
455
- /// `event`.
456
- public func broadcastStream( event: String) - > AsyncStream< JSONObject> {
457
- let ( stream, continuation) = AsyncStream< JSONObject> . makeStream( )
458
-
459
- let id = callbackManager. addBroadcastCallback ( event: event) {
460
- continuation. yield ( $0)
461
- }
462
-
463
- let logger = logger
464
-
465
- continuation. onTermination = { [ weak callbackManager] _ in
440
+ /// Listen for broadcast messages sent by other clients within the same channel under a specific `event`.
441
+ public func onBroadcast(
442
+ event: String,
443
+ callback: @escaping @Sendable ( JSONObject) - > Void
444
+ ) - > Subscription {
445
+ let id = callbackManager. addBroadcastCallback ( event: event, callback: callback)
446
+ return Subscription { [ weak callbackManager, logger] in
466
447
logger? . debug ( " Removing broadcast callback with id: \( id) " )
467
448
callbackManager? . removeCallback ( id: id)
468
449
}
469
-
470
- return stream
471
- }
472
-
473
- /// Listen for broadcast messages sent by other clients within the same channel under a specific
474
- /// `event`.
475
- @available ( * , deprecated, renamed: " broadcastStream(event:) " )
476
- public func broadcast( event: String) - > AsyncStream< JSONObject> {
477
- broadcastStream ( event: event)
478
450
}
479
451
480
452
@discardableResult
0 commit comments