Skip to content

Commit

Permalink
resolve latest swift format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode committed Oct 24, 2024
1 parent 180b3e5 commit f944c94
Show file tree
Hide file tree
Showing 124 changed files with 2,071 additions and 1,540 deletions.
14 changes: 8 additions & 6 deletions Amplify/Categories/Auth/Request/AuthSignOutRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import AuthenticationServices
import Foundation

/// Request for sign out user
public struct AuthSignOutRequest: AmplifyOperationRequest {
Expand Down Expand Up @@ -37,9 +37,11 @@ public extension AuthSignOutRequest {
/// in the presentation anchor provided.
public let presentationAnchorForWebUI: AuthUIPresentationAnchor?

public init(globalSignOut: Bool = false,
presentationAnchor: AuthUIPresentationAnchor? = nil,
pluginOptions: Any? = nil) {
public init(
globalSignOut: Bool = false,
presentationAnchor: AuthUIPresentationAnchor? = nil,
pluginOptions: Any? = nil
) {
self.globalSignOut = globalSignOut
self.pluginOptions = pluginOptions
self.presentationAnchorForWebUI = presentationAnchor
Expand All @@ -55,8 +57,8 @@ public extension AuthSignOutRequest {
}

#if os(iOS) || os(macOS) || os(visionOS)
extension AuthSignOutRequest.Options {
public static func presentationAnchor(_ anchor: AuthUIPresentationAnchor) -> AuthSignOutRequest.Options {
public extension AuthSignOutRequest.Options {
static func presentationAnchor(_ anchor: AuthUIPresentationAnchor) -> AuthSignOutRequest.Options {
return AuthSignOutRequest.Options(presentationAnchor: anchor)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Categories/DataStore/Query/ModelKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public protocol ModelKey: CodingKey, CaseIterable, QueryFieldOperation {}
public extension CodingKey where Self: ModelKey {

// MARK: - attributeExists
public func attributeExists(_ value: Bool) -> QueryPredicateOperation {
func attributeExists(_ value: Bool) -> QueryPredicateOperation {
return field(stringValue).attributeExists(value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public extension StorageListRequest {
targetIdentityId: String? = nil,
path: String? = nil,
subpathStrategy: SubpathStrategy = .include,
pageSize: UInt = 1000,
pageSize: UInt = 1_000,
nextToken: String? = nil,
pluginOptions: Any? = nil
) {
Expand All @@ -123,7 +123,7 @@ public extension StorageListRequest {
/// - Tag: StorageListRequestOptions.init
public init(
subpathStrategy: SubpathStrategy = .include,
pageSize: UInt = 1000,
pageSize: UInt = 1_000,
bucket: some StorageBucket,
nextToken: String? = nil,
pluginOptions: Any? = nil
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Core/Configuration/Internal/Amplify+Reset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension Amplify {
ModelListDecoderRegistry.reset()
ModelProviderRegistry.reset()
log.verbose("Resetting ModelRegistry, ModelListDecoderRegistry, ModelProviderRegistry finished")

#if os(iOS) && !os(visionOS)
await MainActor.run {
devMenu = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
// SPDX-License-Identifier: Apache-2.0
//


import Foundation
import Amplify
import Combine
import Foundation
@_spi(WebSocket) import AWSPluginsCore

/**
Expand Down Expand Up @@ -41,21 +40,21 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
private var cancellablesBindToConnection = Set<AnyCancellable>()

/// AppSync RealTime server endpoint
internal let endpoint: URL
let endpoint: URL
/// Interceptor for decorating AppSyncRealTimeRequest
internal let requestInterceptor: AppSyncRequestInterceptor
let requestInterceptor: AppSyncRequestInterceptor

/// WebSocketClient offering connections at the WebSocket protocol level
internal var webSocketClient: AppSyncWebSocketClientProtocol
var webSocketClient: AppSyncWebSocketClientProtocol
/// Writable data stream convert WebSocketEvent to AppSyncRealTimeResponse
internal let subject = PassthroughSubject<Result<AppSyncRealTimeResponse, Error>, Never>()
let subject = PassthroughSubject<Result<AppSyncRealTimeResponse, Error>, Never>()

var isConnected: Bool {
self.state.value == .connected
state.value == .connected
}

internal var numberOfSubscriptions: Int {
self.subscriptions.count
var numberOfSubscriptions: Int {
subscriptions.count
}

/**
Expand Down Expand Up @@ -89,7 +88,7 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
Connecting to remote AppSync real-time server.
*/
func connect() async throws {
switch self.state.value {
switch state.value {
case .connecting, .connected:
log.debug("[AppSyncRealTimeClient] client is already connecting or connected")
return
Expand All @@ -99,12 +98,12 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
break
}

guard self.state.value != .connecting else {
guard state.value != .connecting else {
log.debug("[AppSyncRealTimeClient] actor reentry, state has been changed to connecting")
return
}

self.state.send(.connecting)
state.send(.connecting)
log.debug("[AppSyncRealTimeClient] client start connecting")

try await RetryWithJitter.execute { [weak self] in
Expand All @@ -121,7 +120,7 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
Disconnect only when there are no subscriptions exist.
*/
func disconnectWhenIdel() async {
if self.subscriptions.isEmpty {
if subscriptions.isEmpty {
log.debug("[AppSyncRealTimeClient] no subscription exist, client is trying to disconnect")
await disconnect()
} else {
Expand All @@ -133,17 +132,17 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
Disconnect from AppSync real-time server.
*/
func disconnect() async {
guard self.state.value != .disconnecting else {
guard state.value != .disconnecting else {
log.debug("[AppSyncRealTimeClient] client already disconnecting")
return
}

defer { self.state.send(.disconnected) }

log.debug("[AppSyncRealTimeClient] client start disconnecting")
self.state.send(.disconnecting)
self.cancellablesBindToConnection = Set()
await self.webSocketClient.disconnect()
state.send(.disconnecting)
cancellablesBindToConnection = Set()
await webSocketClient.disconnect()
log.debug("[AppSyncRealTimeClient] client is disconnected")
}

Expand Down Expand Up @@ -171,7 +170,7 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
try await connect()
try await waitForState(.connected)
}
await self.storeInConnectionCancellables(try await self.startSubscription(id))
try await self.storeInConnectionCancellables(await self.startSubscription(id))
}
self.storeInConnectionCancellables(task.toAnyCancellable)
}
Expand All @@ -184,7 +183,7 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
private func waitForState(_ targetState: State) async throws {
var cancellables = Set<AnyCancellable>()

try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Swift.Error>) -> Void in
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Swift.Error>) in
state.filter { $0 == targetState }
.setFailureType(to: AppSyncRealTimeRequest.Error.self)
.timeout(.seconds(10), scheduler: DispatchQueue.global())
Expand Down Expand Up @@ -237,7 +236,7 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
}

private func subscribeToWebSocketEvent() async {
let cancellable = await self.webSocketClient.publisher.sink { [weak self] _ in
let cancellable = await webSocketClient.publisher.sink { [weak self] _ in
self?.log.debug("[AppSyncRealTimeClient] WebSocketClient terminated")
} receiveValue: { webSocketEvent in
Task { [weak self] in
Expand All @@ -247,12 +246,12 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
await self?.storeInCancellables(task.toAnyCancellable)
}
}
self.storeInCancellables(cancellable)
storeInCancellables(cancellable)
}

private func resumeExistingSubscriptions() {
log.debug("[AppSyncRealTimeClient] Resuming existing subscriptions")
for (id, _) in self.subscriptions {
for (id, _) in subscriptions {
Task { [weak self] in
do {
if let cancellable = try await self?.startSubscription(id) {
Expand All @@ -266,13 +265,13 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {

}

nonisolated private func writeAppSyncEvent(_ event: AppSyncRealTimeRequest) async throws {
guard await self.webSocketClient.isConnected else {
private nonisolated func writeAppSyncEvent(_ event: AppSyncRealTimeRequest) async throws {
guard await webSocketClient.isConnected else {
log.debug("[AppSyncRealTimeClient] Attempting to write to a webSocket haven't been connected.")
return
}

let interceptedEvent = await self.requestInterceptor.interceptRequest(event: event, url: self.endpoint)
let interceptedEvent = await requestInterceptor.interceptRequest(event: event, url: endpoint)
let eventString = try String(data: Self.jsonEncoder.encode(interceptedEvent), encoding: .utf8)!
log.debug("[AppSyncRealTimeClient] Writing AppSyncEvent \(eventString)")
try await webSocketClient.write(message: eventString)
Expand Down Expand Up @@ -365,7 +364,7 @@ extension AppSyncRealTimeClient {
switch event {
case .connected:
log.debug("[AppSyncRealTimeClient] WebSocket connected")
if self.state.value == .connectionDropped {
if state.value == .connectionDropped {
log.debug("[AppSyncRealTimeClient] reconnecting appSyncClient after connection drop")
Task { [weak self] in
let task = Task { [weak self] in
Expand All @@ -377,15 +376,15 @@ extension AppSyncRealTimeClient {

case let .disconnected(closeCode, reason): //
log.debug("[AppSyncRealTimeClient] WebSocket disconnected with closeCode: \(closeCode), reason: \(String(describing: reason))")
if self.state.value != .disconnecting || self.state.value != .disconnected {
self.state.send(.connectionDropped)
if state.value != .disconnecting || state.value != .disconnected {
state.send(.connectionDropped)
}
self.cancellablesBindToConnection = Set()
cancellablesBindToConnection = Set()

case .error(let error):
// Propagate connection error to downstream for Sync engine to restart
log.debug("[AppSyncRealTimeClient] WebSocket error event: \(error)")
self.subject.send(.failure(error))
subject.send(.failure(error))
case .string(let string):
guard let data = string.data(using: .utf8) else {
log.debug("[AppSyncRealTimeClient] Failed to decode string \(string)")
Expand All @@ -395,14 +394,14 @@ extension AppSyncRealTimeClient {
log.debug("[AppSyncRealTimeClient] Failed to decode string to AppSync event")
return
}
self.onAppSyncRealTimeResponse(response)
onAppSyncRealTimeResponse(response)

case .data(let data):
guard let response = try? Self.jsonDecoder.decode(AppSyncRealTimeResponse.self, from: data) else {
log.debug("[AppSyncRealTimeClient] Failed to decode data to AppSync event")
return
}
self.onAppSyncRealTimeResponse(response)
onAppSyncRealTimeResponse(response)
}
}

Expand All @@ -417,12 +416,12 @@ extension AppSyncRealTimeClient {
log.debug("[AppSyncRealTimeClient] AppSync connected: \(String(describing: event.payload))")
subject.send(.success(event))

self.resumeExistingSubscriptions()
self.state.send(.connected)
self.monitorHeartBeats(event.payload)
resumeExistingSubscriptions()
state.send(.connected)
monitorHeartBeats(event.payload)

case .keepAlive:
self.heartBeats.send(())
heartBeats.send(())

default:
log.debug("[AppSyncRealTimeClient] AppSync received response: \(event)")
Expand All @@ -445,25 +444,25 @@ extension AppSyncRealTimeClient {
await self?.storeInCancellables(task.toAnyCancellable)
}
})
self.storeInConnectionCancellables(cancellable)
storeInConnectionCancellables(cancellable)
// start counting down
heartBeats.send(())
}
}

extension AppSyncRealTimeClient {
private func storeInCancellables(_ cancellable: AnyCancellable) {
self.cancellables.insert(cancellable)
cancellables.insert(cancellable)
}

private func storeInConnectionCancellables(_ cancellable: AnyCancellable) {
self.cancellablesBindToConnection.insert(cancellable)
cancellablesBindToConnection.insert(cancellable)
}
}

extension Publisher where Output == AppSyncRealTimeSubscription.State, Failure == Never {
func toAppSyncSubscriptionEventStream() -> AnyPublisher<AppSyncSubscriptionEvent, Never> {
self.compactMap { subscriptionState -> AppSyncSubscriptionEvent? in
compactMap { subscriptionState -> AppSyncSubscriptionEvent? in
switch subscriptionState {
case .subscribing: return .subscribing
case .subscribed: return .subscribed
Expand Down Expand Up @@ -495,7 +494,7 @@ extension AppSyncRealTimeClient: Resettable {
}
}

fileprivate extension Task {
private extension Task {
var toAnyCancellable: AnyCancellable {
AnyCancellable {
if !self.isCancelled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import Amplify
import AWSPluginsCore
import InternalAmplifyCredentials
import Foundation
import SmithyHTTPAPI
import InternalAmplifyCredentials
import Smithy
import SmithyHTTPAPI

typealias AWSRegionType = String

Expand All @@ -20,9 +20,11 @@ struct IAMURLRequestInterceptor: URLRequestInterceptor {
let endpointType: AWSAPICategoryPluginEndpointType
private let userAgent = AmplifyAWSServiceConfiguration.userAgentLib

init(iamCredentialsProvider: IAMCredentialsProvider,
region: AWSRegionType,
endpointType: AWSAPICategoryPluginEndpointType) {
init(
iamCredentialsProvider: IAMCredentialsProvider,
region: AWSRegionType,
endpointType: AWSAPICategoryPluginEndpointType
) {
self.iamCredentialsProvider = iamCredentialsProvider
self.region = region
self.endpointType = endpointType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension APIKeyAuthInterceptor: WebSocketInterceptor {

func interceptConnection(request: URLRequest) async -> URLRequest {
guard let url = request.url else { return request }

let authHeader = getAuthHeader(apiKey, AppSyncRealTimeClientFactory.appSyncApiEndpoint(url).host!)
return request.injectAppSyncAuthToRequestHeader(auth: .apiKey(authHeader))
}
Expand Down
Loading

0 comments on commit f944c94

Please sign in to comment.