Skip to content

Commit

Permalink
Rename subscribeToDiscontinuities to onDiscontinuity
Browse files Browse the repository at this point in the history
I remember that when I wrote the public API in 20e7f5f, I was a bit
reluctant to copy JS’s `on*` naming, because that read to me more like
an API that was designed for receiving a listener (i.e. “_on_ this
event, call this listener”). But, I think that after some back and forth
with Andy, we decided to stick to the `on*` naming, hence
`onStatusChange`. For some reason, though, I didn’t apply this decision
to `subscribeToDiscontinuties`. So, do that.

Resolves #173.
  • Loading branch information
lawrence-forooghian committed Dec 5, 2024
1 parent d6adda5 commit 61f7793
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 32 deletions.
10 changes: 5 additions & 5 deletions Example/AblyChatExample/Mocks/MockClients.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ actor MockMessages: Messages {
return message
}

func subscribeToDiscontinuities(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
func onDiscontinuity(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
fatalError("Not yet implemented")
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ actor MockRoomReactions: RoomReactions {
.init(mockAsyncSequence: createSubscription())
}

func subscribeToDiscontinuities(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
func onDiscontinuity(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
fatalError("Not yet implemented")
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ actor MockTyping: Typing {
}
}

func subscribeToDiscontinuities(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
func onDiscontinuity(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
fatalError("Not yet implemented")
}
}
Expand Down Expand Up @@ -348,7 +348,7 @@ actor MockPresence: Presence {
.init(mockAsyncSequence: createSubscription())
}

func subscribeToDiscontinuities(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
func onDiscontinuity(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
fatalError("Not yet implemented")
}
}
Expand Down Expand Up @@ -383,7 +383,7 @@ actor MockOccupancy: Occupancy {
OccupancyEvent(connections: 10, presenceMembers: 5)
}

func subscribeToDiscontinuities(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
func onDiscontinuity(bufferingPolicy _: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
fatalError("Not yet implemented")
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AblyChat/DefaultMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ internal final class DefaultMessages: Messages, EmitsDiscontinuities {
}

// (CHA-M7) Users may subscribe to discontinuity events to know when there’s been a break in messages that they need to resolve. Their listener will be called when a discontinuity event is triggered from the room lifecycle.
internal func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.subscribeToDiscontinuities(bufferingPolicy: bufferingPolicy)
internal func onDiscontinuity(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.onDiscontinuity(bufferingPolicy: bufferingPolicy)
}

private func getBeforeSubscriptionStart(_ uuid: UUID, params: QueryOptions) async throws -> any PaginatedResult<Message> {
Expand Down
4 changes: 2 additions & 2 deletions Sources/AblyChat/DefaultOccupancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal final class DefaultOccupancy: Occupancy, EmitsDiscontinuities {
}

// (CHA-O5) Users may subscribe to discontinuity events to know when there’s been a break in occupancy. Their listener will be called when a discontinuity event is triggered from the room lifecycle. For occupancy, there shouldn’t need to be user action as most channels will send occupancy updates regularly as clients churn.
internal func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.subscribeToDiscontinuities(bufferingPolicy: bufferingPolicy)
internal func onDiscontinuity(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.onDiscontinuity(bufferingPolicy: bufferingPolicy)
}
}
4 changes: 2 additions & 2 deletions Sources/AblyChat/DefaultPresence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
}

// (CHA-PR8) Users may subscribe to discontinuity events to know when there’s been a break in presence. Their listener will be called when a discontinuity event is triggered from the room lifecycle. For presence, there shouldn’t need to be user action as the underlying core SDK will heal the presence set.
internal func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.subscribeToDiscontinuities(bufferingPolicy: bufferingPolicy)
internal func onDiscontinuity(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.onDiscontinuity(bufferingPolicy: bufferingPolicy)
}

private func decodePresenceData(from data: Any?) -> PresenceData? {
Expand Down
2 changes: 1 addition & 1 deletion Sources/AblyChat/DefaultRoomLifecycleContributor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal actor DefaultRoomLifecycleContributor: RoomLifecycleContributor, EmitsD
}
}

internal func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
internal func onDiscontinuity(bufferingPolicy: BufferingPolicy) -> Subscription<DiscontinuityEvent> {
let subscription = Subscription<DiscontinuityEvent>(bufferingPolicy: bufferingPolicy)
// TODO: clean up old subscriptions (https://github.com/ably-labs/ably-chat-swift/issues/36)
discontinuitySubscriptions.append(subscription)
Expand Down
4 changes: 2 additions & 2 deletions Sources/AblyChat/DefaultRoomReactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ internal final class DefaultRoomReactions: RoomReactions, EmitsDiscontinuities {
}

// (CHA-ER5) Users may subscribe to discontinuity events to know when there’s been a break in reactions that they need to resolve. Their listener will be called when a discontinuity event is triggered from the room lifecycle.
internal func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.subscribeToDiscontinuities(bufferingPolicy: bufferingPolicy)
internal func onDiscontinuity(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.onDiscontinuity(bufferingPolicy: bufferingPolicy)
}

private enum RoomReactionsError: Error {
Expand Down
4 changes: 2 additions & 2 deletions Sources/AblyChat/DefaultTyping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ internal final class DefaultTyping: Typing {
}

// (CHA-T7) Users may subscribe to discontinuity events to know when there’s been a break in typing indicators. Their listener will be called when a discontinuity event is triggered from the room lifecycle. For typing, there shouldn’t need to be user action as the underlying core SDK will heal the presence set.
internal func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.subscribeToDiscontinuities(bufferingPolicy: bufferingPolicy)
internal func onDiscontinuity(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await featureChannel.onDiscontinuity(bufferingPolicy: bufferingPolicy)
}

private func processPresenceGet(members: [ARTPresenceMessage]?, error: ARTErrorInfo?) throws -> Set<String> {
Expand Down
10 changes: 5 additions & 5 deletions Sources/AblyChat/EmitsDiscontinuities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public struct DiscontinuityEvent: Sendable, Equatable {
}

public protocol EmitsDiscontinuities {
func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent>
/// Same as calling ``subscribeToDiscontinuities(bufferingPolicy:)`` with ``BufferingPolicy.unbounded``.
func onDiscontinuity(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent>
/// Same as calling ``onDiscontinuity(bufferingPolicy:)`` with ``BufferingPolicy.unbounded``.
///
/// The `EmitsDiscontinuities` protocol provides a default implementation of this method.
func subscribeToDiscontinuities() async -> Subscription<DiscontinuityEvent>
func onDiscontinuity() async -> Subscription<DiscontinuityEvent>
}

public extension EmitsDiscontinuities {
func subscribeToDiscontinuities() async -> Subscription<DiscontinuityEvent> {
await subscribeToDiscontinuities(bufferingPolicy: .unbounded)
func onDiscontinuity() async -> Subscription<DiscontinuityEvent> {
await onDiscontinuity(bufferingPolicy: .unbounded)
}
}
4 changes: 2 additions & 2 deletions Sources/AblyChat/RoomFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ internal struct DefaultFeatureChannel: FeatureChannel {
internal var contributor: DefaultRoomLifecycleContributor
internal var roomLifecycleManager: RoomLifecycleManager

internal func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await contributor.subscribeToDiscontinuities(bufferingPolicy: bufferingPolicy)
internal func onDiscontinuity(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
await contributor.onDiscontinuity(bufferingPolicy: bufferingPolicy)
}

internal func waitToBeAbleToPerformPresenceOperations(requestedByFeature requester: RoomFeature) async throws(ARTErrorInfo) {
Expand Down
8 changes: 4 additions & 4 deletions Tests/AblyChatTests/DefaultMessagesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ struct DefaultMessagesTests {

// @spec CHA-M7
@Test
func subscribeToDiscontinuities() async throws {
func onDiscontinuity() async throws {
// Given: A DefaultMessages instance
let realtime = MockRealtime.create()
let chatAPI = ChatAPI(realtime: realtime)
let channel = MockRealtimeChannel()
let featureChannel = MockFeatureChannel(channel: channel)
let messages = await DefaultMessages(featureChannel: featureChannel, chatAPI: chatAPI, roomID: "basketball", clientID: "clientId", logger: TestLogger())

// When: The feature channel emits a discontinuity through `subscribeToDiscontinuities`
// When: The feature channel emits a discontinuity through `onDiscontinuity`
let featureChannelDiscontinuity = DiscontinuityEvent(error: ARTErrorInfo.createUnknownError() /* arbitrary */ )
let messagesDiscontinuitySubscription = await messages.subscribeToDiscontinuities()
let messagesDiscontinuitySubscription = await messages.onDiscontinuity()
await featureChannel.emitDiscontinuity(featureChannelDiscontinuity)

// Then: The DefaultMessages instance emits this discontinuity through `subscribeToDiscontinuities`
// Then: The DefaultMessages instance emits this discontinuity through `onDiscontinuity`
let messagesDiscontinuity = try #require(await messagesDiscontinuitySubscription.first { _ in true })
#expect(messagesDiscontinuity == featureChannelDiscontinuity)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/AblyChatTests/DefaultRoomReactionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ struct DefaultRoomReactionsTests {

// @spec CHA-ER5
@Test
func subscribeToDiscontinuities() async throws {
func onDiscontinuity() async throws {
// all setup values here are arbitrary
// Given: A DefaultRoomReactions instance
let channel = MockRealtimeChannel()
let featureChannel = MockFeatureChannel(channel: channel)
let roomReactions = await DefaultRoomReactions(featureChannel: featureChannel, clientID: "mockClientId", roomID: "basketball", logger: TestLogger())

// When: The feature channel emits a discontinuity through `subscribeToDiscontinuities`
// When: The feature channel emits a discontinuity through `onDiscontinuity`
let featureChannelDiscontinuity = DiscontinuityEvent(error: ARTErrorInfo.createUnknownError() /* arbitrary */ )
let messagesDiscontinuitySubscription = await roomReactions.subscribeToDiscontinuities()
let messagesDiscontinuitySubscription = await roomReactions.onDiscontinuity()
await featureChannel.emitDiscontinuity(featureChannelDiscontinuity)

// Then: The DefaultRoomReactions instance emits this discontinuity through `subscribeToDiscontinuities`
// Then: The DefaultRoomReactions instance emits this discontinuity through `onDiscontinuity`
let messagesDiscontinuity = try #require(await messagesDiscontinuitySubscription.first { _ in true })
#expect(messagesDiscontinuity == featureChannelDiscontinuity)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/AblyChatTests/Mocks/MockFeatureChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final actor MockFeatureChannel: FeatureChannel {
resultOfWaitToBeAbleToPerformPresenceOperations = resultOfWaitToBeAblePerformPresenceOperations
}

func subscribeToDiscontinuities(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
func onDiscontinuity(bufferingPolicy: BufferingPolicy) async -> Subscription<DiscontinuityEvent> {
let subscription = Subscription<DiscontinuityEvent>(bufferingPolicy: bufferingPolicy)
discontinuitySubscriptions.append(subscription)
return subscription
Expand Down

0 comments on commit 61f7793

Please sign in to comment.