Skip to content

Commit

Permalink
Use StateObject for RoomScope
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Jun 24, 2024
1 parent 96dfc4e commit 7a82b22
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Sources/LiveKitComponents/Scopes/RoomScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import SwiftUI

public struct RoomScope<Content: View>: View {
private let _content: () -> Content
private let _room: Room
@StateObject private var _room: Room

private let _url: String?
private let _token: String?
Expand All @@ -37,7 +37,7 @@ public struct RoomScope<Content: View>: View {
roomOptions: RoomOptions? = nil,
@ViewBuilder _ content: @escaping () -> Content)
{
_room = room ?? Room(roomOptions: roomOptions)
__room = StateObject(wrappedValue: room ?? Room(roomOptions: roomOptions))
_url = url
_token = token
_connect = connect
Expand All @@ -49,19 +49,19 @@ public struct RoomScope<Content: View>: View {
public var body: some View {
_content()
.environmentObject(_room)
.onAppear(perform: {
.onAppear {
if _connect, let url = _url, let token = _token {
Task {
try await _room.connect(url: url, token: token)
if _enableCamera { try await _room.localParticipant.setCamera(enabled: true) }
if _enableMicrophone { try await _room.localParticipant.setMicrophone(enabled: true) }
}
}
})
.onDisappear(perform: {
}
.onDisappear {
Task {
await _room.disconnect()
}
})
}
}
}

0 comments on commit 7a82b22

Please sign in to comment.