Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Nov 28, 2024
1 parent 9eac916 commit 73e255a
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 459 deletions.
15 changes: 13 additions & 2 deletions Examples/SlackClone/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ final class AppViewModel {

var realtimeConnectionStatus: RealtimeClientStatus?

let channelStore = ChannelStore()
let messageStore = MessageStore()
let userStore = UserStore()

init() {
channelStore.messages = messageStore
messageStore.channel = channelStore
messageStore.users = userStore

Task {
for await (event, session) in supabase.auth.authStateChanges {
Logger.main.debug("AuthStateChange: \(event.rawValue)")
Expand All @@ -27,15 +35,15 @@ final class AppViewModel {
self.session = session

if session == nil {
for subscription in supabase.channels {
for subscription in await supabase.channels {
await subscription.unsubscribe()
}
}
}
}

Task {
for await status in supabase.realtimeV2.statusChange {
for await status in await supabase.realtimeV2.statusChange {
realtimeConnectionStatus = status
}
}
Expand All @@ -56,6 +64,9 @@ struct AppView: View {
MessagesView(channel: channel).id(channel.id)
}
}
.environment(model.channelStore)
.environment(model.messageStore)
.environment(model.userStore)
} else {
AuthView()
}
Expand Down
3 changes: 1 addition & 2 deletions Examples/SlackClone/ChannelListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct ChannelListView: View {
@Bindable var store = Dependencies.shared.channel
@Environment(ChannelStore.self) var store
@Binding var channel: Channel?

@State private var addChannelPresented = false
Expand All @@ -35,7 +35,6 @@ struct ChannelListView: View {
}
}
}
.toast(state: $store.toast)
}

private var addChannelView: some View {
Expand Down
18 changes: 6 additions & 12 deletions Examples/SlackClone/ChannelStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ import Supabase
@MainActor
@Observable
final class ChannelStore {
static let shared = ChannelStore()

private(set) var channels: [Channel] = []
var toast: ToastState?

var messages: MessageStore { Dependencies.shared.messages }
var messages: MessageStore!

private init() {
init() {
Task {
channels = await fetchChannels()

let channel = supabase.channel("public:channels")
let channel = await supabase.channel("public:channels")

let insertions = channel.postgresChange(InsertAction.self, table: "channels")
let deletions = channel.postgresChange(DeleteAction.self, table: "channels")
let insertions = await channel.postgresChange(InsertAction.self, table: "channels")
let deletions = await channel.postgresChange(DeleteAction.self, table: "channels")

await channel.subscribe()

Expand Down Expand Up @@ -53,7 +50,6 @@ final class ChannelStore {
.execute()
} catch {
dump(error)
toast = .init(status: .error, title: "Error", description: error.localizedDescription)
}
}

Expand All @@ -78,22 +74,20 @@ final class ChannelStore {
channels.append(channel)
} catch {
dump(error)
toast = .init(status: .error, title: "Error", description: error.localizedDescription)
}
}

private func handleDeletedChannel(_ action: DeleteAction) {
guard let id = action.oldRecord["id"]?.intValue else { return }
channels.removeAll { $0.id == id }
messages.removeMessages(for: id)
messages?.removeMessages(for: id)
}

private func fetchChannels() async -> [Channel] {
do {
return try await supabase.from("channels").select().execute().value
} catch {
dump(error)
toast = .init(status: .error, title: "Error", description: error.localizedDescription)
return []
}
}
Expand Down
3 changes: 0 additions & 3 deletions Examples/SlackClone/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import Supabase
class Dependencies {
static let shared = Dependencies()

let channel = ChannelStore.shared
let users = UserStore.shared
let messages = MessageStore.shared
}

struct User: Codable, Identifiable, Hashable {
Expand Down
16 changes: 7 additions & 9 deletions Examples/SlackClone/MessageStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,23 @@ extension Messages {
@MainActor
@Observable
final class MessageStore {
static let shared = MessageStore()

private(set) var messages: [Channel.ID: Messages] = [:]

struct Section {
var author: User
var messages: [Message]
}

var users: UserStore { Dependencies.shared.users }
var channel: ChannelStore { Dependencies.shared.channel }
var users: UserStore!
weak var channel: ChannelStore!

private init() {
init() {
Task {
let channel = supabase.channel("public:messages")
let channel = await supabase.channel("public:messages")

let insertions = channel.postgresChange(InsertAction.self, table: "messages")
let updates = channel.postgresChange(UpdateAction.self, table: "messages")
let deletions = channel.postgresChange(DeleteAction.self, table: "messages")
let insertions = await channel.postgresChange(InsertAction.self, table: "messages")
let updates = await channel.postgresChange(UpdateAction.self, table: "messages")
let deletions = await channel.postgresChange(DeleteAction.self, table: "messages")

await channel.subscribe()

Expand Down
4 changes: 2 additions & 2 deletions Examples/SlackClone/MessagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import Supabase
import SwiftUI

struct MessagesView: View {
let store = Dependencies.shared.messages
let userStore = Dependencies.shared.users
@Environment(MessageStore.self) var store
@Environment(UserStore.self) var userStore

let channel: Channel
@State private var newMessage = ""
Expand Down
12 changes: 5 additions & 7 deletions Examples/SlackClone/UserStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ import Supabase
@MainActor
@Observable
final class UserStore {
static let shared = UserStore()

private(set) var users: [User.ID: User] = [:]
private(set) var presences: [User.ID: UserPresence] = [:]

private init() {
init() {
Task {
let channel = supabase.channel("public:users")
let changes = channel.postgresChange(AnyAction.self, table: "users")
let channel = await supabase.channel("public:users")
let changes = await channel.postgresChange(AnyAction.self, table: "users")

let presences = channel.presenceChange()
let presences = await channel.presenceChange()

await channel.subscribe()

Task {
let statusChange = channel.statusChange
let statusChange = await channel.statusChange
for await _ in statusChange.filter({ $0 == .subscribed }) {
let userId = try await supabase.auth.session.user.id
try await channel.track(UserPresence(userId: userId, onlineAt: Date()))
Expand Down
5 changes: 5 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.2"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.17.2"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),

.package(url: "https://github.com/grdsdev/swift-websocket", branch: "main"),
],
targets: [
.target(
Expand Down Expand Up @@ -122,6 +124,9 @@ let package = Package(
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
"Helpers",

.product(name: "WebSocket", package: "swift-websocket"),
.product(name: "WebSocketFoundation", package: "swift-websocket"),
]
),
.testTarget(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Realtime/V2/PushV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ actor PushV2 {
}

do {
return try await withTimeout(interval: channel.socket.options().timeoutInterval) {
return try await withTimeout(interval: channel.socket.options.timeoutInterval) {
await withCheckedContinuation { continuation in
self.receivedContinuation = continuation
}
Expand Down
Loading

0 comments on commit 73e255a

Please sign in to comment.