Skip to content

Commit

Permalink
Fixes according to review #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Jan 19, 2024
1 parent 1a29d5a commit db9d96a
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 110 deletions.
4 changes: 4 additions & 0 deletions PubNub.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@
3DACC7F72AB88F8E00210B14 /* Data+CommonCrypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DACC7F62AB88F8E00210B14 /* Data+CommonCrypto.swift */; };
3DBB2C212ABD8053008A100E /* PubNubCryptoModuleContractTestSteps.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DBB2C202ABD8053008A100E /* PubNubCryptoModuleContractTestSteps.swift */; };
3DBB2C222ABD8053008A100E /* PubNubCryptoModuleContractTestSteps.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DBB2C202ABD8053008A100E /* PubNubCryptoModuleContractTestSteps.swift */; };
3DD1FB992B5A7804005A14E3 /* PubNubPresenceStateContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DD1FB982B5A7804005A14E3 /* PubNubPresenceStateContainer.swift */; };
3DFB01942B0E30EE00146B57 /* subscription_encrypted_message_success.json in Resources */ = {isa = PBXBuildFile; fileRef = 3DFB01932B0E30EE00146B57 /* subscription_encrypted_message_success.json */; };
4C2A8D84BCD39B07A66FD9B4 /* Pods_PubNubContractTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E7F3D449F2D66FC29674EF6 /* Pods_PubNubContractTests.framework */; };
79407BD2271D4CFA0032076C /* PubNubContractTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79407BBF271D4CFA0032076C /* PubNubContractTestCase.swift */; };
Expand Down Expand Up @@ -1020,6 +1021,7 @@
3D9134962A1216F7000A5124 /* PubNubPushTargetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PubNubPushTargetTests.swift; sourceTree = "<group>"; };
3DACC7F62AB88F8E00210B14 /* Data+CommonCrypto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+CommonCrypto.swift"; sourceTree = "<group>"; };
3DBB2C202ABD8053008A100E /* PubNubCryptoModuleContractTestSteps.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PubNubCryptoModuleContractTestSteps.swift; sourceTree = "<group>"; };
3DD1FB982B5A7804005A14E3 /* PubNubPresenceStateContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PubNubPresenceStateContainer.swift; sourceTree = "<group>"; };
3DE632651BA8B2E27ACFC4AD /* Pods-PubNubContractTestsBeta.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PubNubContractTestsBeta.release.xcconfig"; path = "Target Support Files/Pods-PubNubContractTestsBeta/Pods-PubNubContractTestsBeta.release.xcconfig"; sourceTree = "<group>"; };
3DFB01932B0E30EE00146B57 /* subscription_encrypted_message_success.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = subscription_encrypted_message_success.json; sourceTree = "<group>"; };
793079152667C63700F23B72 /* CODEOWNERS */ = {isa = PBXFileReference; lastKnownFileType = text; path = CODEOWNERS; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2067,6 +2069,7 @@
isa = PBXGroup;
children = (
3D389FD52B35AF4A006928E7 /* Presence.swift */,
3DD1FB982B5A7804005A14E3 /* PubNubPresenceStateContainer.swift */,
3D389FD62B35AF4A006928E7 /* PresenceTransition.swift */,
3D389FD72B35AF4A006928E7 /* Effects */,
3D389FDD2B35AF4A006928E7 /* Helpers */,
Expand Down Expand Up @@ -3468,6 +3471,7 @@
3D389FF32B35AF4A006928E7 /* WaitEffect.swift in Sources */,
35089A0B22E56F1F002BCC94 /* Constants.swift in Sources */,
358C6421238C6787009CE354 /* PubNubPushMessage.swift in Sources */,
3DD1FB992B5A7804005A14E3 /* PubNubPresenceStateContainer.swift in Sources */,
3D758DC82AB06A12005D2B36 /* CryptoInputStream.swift in Sources */,
35E4604F234B8B9D005D04AE /* ErrorDescription.swift in Sources */,
3D389FE52B35AF4A006928E7 /* EventEngineFactory.swift in Sources */,
Expand Down
12 changes: 6 additions & 6 deletions PubNubMembership/Sources/Membership+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchMultipleValueResponseDecoder<PubNubMembership.PartialSpace>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -320,7 +320,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchMultipleValueResponseDecoder<PubNubMembership.PartialUser>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -366,7 +366,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -403,7 +403,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -466,7 +466,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -503,7 +503,7 @@ public extension PubNubMembershipInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down
8 changes: 4 additions & 4 deletions PubNubSpace/Sources/Space+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public extension PubNubSpaceInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchMultipleValueResponseDecoder<PubNubSpace>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand All @@ -237,7 +237,7 @@ public extension PubNubSpaceInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchSingleValueResponseDecoder<PubNubSpace>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -274,7 +274,7 @@ public extension PubNubSpaceInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchSingleValueResponseDecoder<PubNubSpace>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -319,7 +319,7 @@ public extension PubNubSpaceInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down
8 changes: 4 additions & 4 deletions PubNubUser/Sources/User+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public extension PubNubUserInterface {
(requestConfig.customSession ?? networkSession)?
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchMultipleValueResponseDecoder<PubNubUser>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -247,7 +247,7 @@ public extension PubNubUserInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchSingleValueResponseDecoder<PubNubUser>(),
responseQueue: requestConfig.responseQueue
) {
Expand Down Expand Up @@ -288,7 +288,7 @@ public extension PubNubUserInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchSingleValueResponseDecoder<PubNubUser>(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down Expand Up @@ -337,7 +337,7 @@ public extension PubNubUserInterface {
(requestConfig.customSession ?? networkSession)
.route(
router,
requestOperator: configuration.automaticRetry?[.appContext],
requestOperator: configuration.automaticRetry?.retryOperator(for: .appContext),
responseDecoder: FetchStatusResponseDecoder(),
responseQueue: requestConfig.responseQueue
) { result in
Expand Down
8 changes: 4 additions & 4 deletions Sources/PubNub/APIs/File+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public extension PubNub {
) {
route(
FileManagementRouter(.list(channel: channel, limit: limit, next: next), configuration: configuration),
requestOperator: configuration.automaticRetry?[.files],
requestOperator: configuration.automaticRetry?.retryOperator(for: .files),
responseDecoder: FileListResponseDecoder(),
custom: requestConfig
) { result in
Expand Down Expand Up @@ -61,7 +61,7 @@ public extension PubNub {
) {
route(
FileManagementRouter(.delete(channel: channel, fileId: fileId, filename: filename), configuration: configuration),
requestOperator: configuration.automaticRetry?[.files],
requestOperator: configuration.automaticRetry?.retryOperator(for: .files),
responseDecoder: FileGeneralSuccessResponseDecoder(),
custom: requestConfig
) { result in
Expand Down Expand Up @@ -139,7 +139,7 @@ public extension PubNub {
.generateURL(channel: channel, body: .init(name: remoteFilename)),
configuration: configuration
),
requestOperator: configuration.automaticRetry?[.files],
requestOperator: configuration.automaticRetry?.retryOperator(for: .files),
responseDecoder: FileGenerateResponseDecoder(),
custom: requestConfig
) { [configuration] result in
Expand Down Expand Up @@ -230,7 +230,7 @@ public extension PubNub {

route(
router,
requestOperator: configuration.automaticRetry?[.files],
requestOperator: configuration.automaticRetry?.retryOperator(for: .files),
responseDecoder: PublishResponseDecoder(),
custom: request.customRequestConfig
) { result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import Foundation
class PresenceEffectFactory: EffectHandlerFactory {
private let session: SessionReplaceable
private let sessionResponseQueue: DispatchQueue
private let presenceStateContainer: PresenceStateContainer
private let presenceStateContainer: PubNubPresenceStateContainer

init(
session: SessionReplaceable,
sessionResponseQueue: DispatchQueue = .global(qos: .default),
presenceStateContainer: PresenceStateContainer
presenceStateContainer: PubNubPresenceStateContainer
) {
self.session = session
self.sessionResponseQueue = sessionResponseQueue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,20 @@

import Foundation

// MARK: - PresenceStateContainer

class PresenceStateContainer {
private var channelStates: Atomic<[String: [String: JSONCodableScalar]]> = Atomic([:])

static var shared: PresenceStateContainer = PresenceStateContainer()
private init() {}

func registerState(_ state: [String: JSONCodableScalar], forChannels channels: [String]) {
channelStates.lockedWrite { channelStates in
channels.forEach {
channelStates[$0] = state
}
}
}

func removeState(forChannels channels: [String]) {
channelStates.lockedWrite { channelStates in
channels.map {
channelStates[$0] = nil
}
}
}

func getStates(forChannels channels: [String]) -> [String: [String: JSONCodableScalar]] {
channelStates.lockedRead {
$0.filter {
channels.contains($0.key)
}
}
}
}

// MARK: - PresenceHeartbeatRequest

class PresenceHeartbeatRequest {
let channels: [String]
let groups: [String]
let configuration: SubscriptionConfiguration

private let session: SessionReplaceable
private let sessionResponseQueue: DispatchQueue
private let channelStates: [String: [String: JSONCodableScalar]]
private let channelStates: [String: JSONCodable]
private var request: RequestReplaceable?

init(
channels: [String],
groups: [String],
channelStates: [String: [String: JSONCodableScalar]],
channelStates: [String: JSONCodable],
configuration: SubscriptionConfiguration,
session: SessionReplaceable,
sessionResponseQueue: DispatchQueue
Expand Down Expand Up @@ -100,7 +65,7 @@ class PresenceHeartbeatRequest {
guard let automaticRetry = configuration.automaticRetry else {
return nil
}
guard automaticRetry[.presence] != nil else {
guard automaticRetry.retryOperator(for: .presence) != nil else {
return nil
}
guard automaticRetry.retryLimit > retryAttempt else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// PresenceStateContainer.swift
//
// Copyright (c) PubNub Inc.
// All rights reserved.
//
// This source code is licensed under the license found in the
// LICENSE file in the root directory of this source tree.
//

import Foundation

class PubNubPresenceStateContainer {
static let shared: PubNubPresenceStateContainer = PubNubPresenceStateContainer()

private var channelStates: Atomic<[String: JSONCodable]> = Atomic([:])
private init() {}

func registerState(_ state: JSONCodable, forChannels channels: [String]) {
channelStates.lockedWrite { channelStates in
channels.forEach {
channelStates[$0] = state
}
}
}

func removeState(forChannels channels: [String]) {
channelStates.lockedWrite { channelStates in
channels.map {
channelStates[$0] = nil
}
}
}

func getStates(forChannels channels: [String]) -> [String: JSONCodable] {
channelStates.lockedRead {
$0.filter {
channels.contains($0.key)
}
}
}

func removeAll() {
channelStates.lockedWrite {
$0.removeAll()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class SubscribeEffectFactory: EffectHandlerFactory {
private let session: SessionReplaceable
private let sessionResponseQueue: DispatchQueue
private let messageCache: MessageCache
private let presenceStateContainer: PresenceStateContainer
private let presenceStateContainer: PubNubPresenceStateContainer

init(
session: SessionReplaceable,
sessionResponseQueue: DispatchQueue = .global(qos: .default),
messageCache: MessageCache = MessageCache(),
presenceStateContainer: PresenceStateContainer
presenceStateContainer: PubNubPresenceStateContainer
) {
self.session = session
self.sessionResponseQueue = sessionResponseQueue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SubscribeRequest {
private let configuration: SubscriptionConfiguration
private let session: SessionReplaceable
private let sessionResponseQueue: DispatchQueue
private let channelStates: [String: [String: JSONCodableScalar]]
private let channelStates: [String: JSONCodable]

private var request: RequestReplaceable?

Expand All @@ -30,7 +30,7 @@ class SubscribeRequest {
configuration: SubscriptionConfiguration,
channels: [String],
groups: [String],
channelStates: [String: [String: JSONCodableScalar]],
channelStates: [String: JSONCodable],
timetoken: Timetoken? = nil,
region: Int? = nil,
session: SessionReplaceable,
Expand All @@ -56,7 +56,7 @@ class SubscribeRequest {
guard let automaticRetry = configuration.automaticRetry else {
return nil
}
guard automaticRetry[.subscribe] != nil else {
guard automaticRetry.retryOperator(for: .subscribe) != nil else {
return nil
}
guard automaticRetry.retryLimit > retryAttempt else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public struct AutomaticRetry: RequestOperator, Hashable {
}
}

public subscript(endpoint: AutomaticRetry.Endpoint) -> RequestOperator? {
public func retryOperator(for endpoint: AutomaticRetry.Endpoint) -> RequestOperator? {
excluded.contains(endpoint) ? nil : self
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/PubNub/Networking/Routers/PresenceRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Foundation
struct PresenceRouter: HTTPRouter {
// Nested Endpoint
enum Endpoint: CustomStringConvertible {
case heartbeat(channels: [String], groups: [String], channelStates: [String: [String:JSONCodableScalar]], presenceTimeout: UInt?)
case heartbeat(channels: [String], groups: [String], channelStates: [String: JSONCodable], presenceTimeout: UInt?)
case leave(channels: [String], groups: [String])
case hereNow(channels: [String], groups: [String], includeUUIDs: Bool, includeState: Bool)
case hereNowGlobal(includeUUIDs: Bool, includeState: Bool)
Expand Down Expand Up @@ -139,7 +139,7 @@ struct PresenceRouter: HTTPRouter {
)
query.appendIfPresent(
key: .state,
value: try? channelStates.mapValues { $0.mapValues { $0.codableValue } }.encodableJSONString.get(),
value: try? channelStates.mapValues { $0.codableValue }.encodableJSONString.get(),
when: configuration.enableEventEngine && configuration.maintainPresenceState && !channelStates.isEmpty
)
case let .leave(_, groups):
Expand Down
Loading

0 comments on commit db9d96a

Please sign in to comment.