Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: kickoff release #3560

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,41 @@ struct RemoteSyncReconciler {
}
}


/// Reconciles the incoming `remoteModels` against the local metadata to get the disposition
///
/// GroupBy the remoteModels by model identifier and apply only the latest version of the remoteModel
///
/// - Parameters:
/// - remoteModels: models retrieved from the remote store
/// - localMetadatas: metadata retrieved from the local store
/// - Returns: disposition of models to apply locally
static func getDispositions(_ remoteModels: [RemoteModel],
localMetadatas: [LocalMetadata]) -> [Disposition] {
guard !remoteModels.isEmpty else {
static func getDispositions(
_ remoteModels: [RemoteModel],
localMetadatas: [LocalMetadata]
) -> [Disposition] {
let remoteModelsGroupByIdentifier = remoteModels.reduce([String: [RemoteModel]]()) {
$0.merging([
$1.model.identifier: ($0[$1.model.identifier] ?? []) + [$1]
], uniquingKeysWith: { $1 })
}

let optimizedRemoteModels = remoteModelsGroupByIdentifier.values.compactMap {
$0.sorted(by: { $0.syncMetadata.version > $1.syncMetadata.version }).first
}

guard !optimizedRemoteModels.isEmpty else {
return []
}

guard !localMetadatas.isEmpty else {
return remoteModels.compactMap { getDisposition($0, localMetadata: nil) }
return optimizedRemoteModels.compactMap { getDisposition($0, localMetadata: nil) }
}

let metadataBymodelId = localMetadatas.reduce(into: [:]) { $0[$1.modelId] = $1 }
let dispositions = remoteModels.compactMap { getDisposition($0, localMetadata: metadataBymodelId[$0.model.identifier]) }


let metadataByModelId = localMetadatas.reduce(into: [:]) { $0[$1.modelId] = $1 }
let dispositions = optimizedRemoteModels.compactMap {
getDisposition($0, localMetadata: metadataByModelId[$0.model.identifier])
}

return dispositions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,33 @@ class RemoteSyncReconcilerTests: XCTestCase {
}
waitForExpectations(timeout: 1)
}

func testGetDispositions_emptyLocal_singleModelAddedAndDeleted() {
let sameId = UUID().uuidString

let remoteModels = [
makeRemoteModel(modelId: sameId, deleted: false, version: 1),
makeRemoteModel(modelId: sameId, deleted: true, version: 2)
]

let dispositions = RemoteSyncReconciler.getDispositions(remoteModels, localMetadatas: [])

XCTAssertTrue(dispositions.isEmpty)
}

func testGetDispositions_emptyLocal_oneModelAdded_SecondModelAddedAndDeleted() {
let sameId = UUID().uuidString

let remoteModels = [
makeRemoteModel(deleted: false, version: 1),
makeRemoteModel(modelId: sameId, deleted: false, version: 1),
makeRemoteModel(modelId: sameId, deleted: true, version: 2)
]

let dispositions = RemoteSyncReconciler.getDispositions(remoteModels, localMetadatas: [])

XCTAssertTrue(dispositions.count == 1)
}

// MARK: - Utilities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public final class FaceLivenessSession: LivenessService {
let baseURL: URL
var serverEventListeners: [LivenessEventKind.Server: (FaceLivenessSession.SessionConfiguration) -> Void] = [:]
var onComplete: (ServerDisconnection) -> Void = { _ in }

private let livenessServiceDispatchQueue = DispatchQueue(
label: "com.amazon.aws.amplify.liveness.service",
target: .global()
)

init(
websocket: WebSocketSession,
Expand Down Expand Up @@ -77,36 +82,38 @@ public final class FaceLivenessSession: LivenessService {

public func send<T>(
_ event: LivenessEvent<T>,
eventDate: () -> Date = Date.init
eventDate: @escaping () -> Date = Date.init
) {
let encodedPayload = eventStreamEncoder.encode(
payload: event.payload,
headers: [
":content-type": .string("application/json"),
":event-type": .string(event.eventTypeHeader),
":message-type": .string("event")
]
)

let eventDate = eventDate()

let signedPayload = signer.signWithPreviousSignature(
payload: encodedPayload,
dateHeader: (key: ":date", value: eventDate)
)

let encodedEvent = eventStreamEncoder.encode(
payload: encodedPayload,
headers: [
":date": .timestamp(eventDate),
":chunk-signature": .data(signedPayload)
]
)

websocket.send(
message: .data(encodedEvent),
onError: { _ in }
)
livenessServiceDispatchQueue.async {
let encodedPayload = self.eventStreamEncoder.encode(
payload: event.payload,
headers: [
":content-type": .string("application/json"),
":event-type": .string(event.eventTypeHeader),
":message-type": .string("event")
]
)

let eventDate = eventDate()

let signedPayload = self.signer.signWithPreviousSignature(
payload: encodedPayload,
dateHeader: (key: ":date", value: eventDate)
)

let encodedEvent = self.eventStreamEncoder.encode(
payload: encodedPayload,
headers: [
":date": .timestamp(eventDate),
":chunk-signature": .data(signedPayload)
]
)

self.websocket.send(
message: .data(encodedEvent),
onError: { _ in }
)
}
}

private func fallbackDecoding(_ message: EventStream.Message) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Amplify
public protocol LivenessService {
func send<T>(
_ event: LivenessEvent<T>,
eventDate: () -> Date
eventDate: @escaping () -> Date
)

var onServiceException: (FaceLivenessSessionError) -> Void { get set }
Expand Down
Loading