Skip to content

Commit f48dba4

Browse files
committed
Merge branch 'master' into feat/kmp2
2 parents cc43afc + a72d552 commit f48dba4

File tree

6 files changed

+73
-37
lines changed

6 files changed

+73
-37
lines changed

.pubnub.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
---
22
name: swift
33
scm: github.com/pubnub/swift
4-
version: "7.3.2"
4+
version: "7.3.3"
55
schema: 1
66
changelog:
7+
- date: 2024-09-13
8+
version: 7.3.3
9+
changes:
10+
- type: bug
11+
text: "Added synchronized access inside `WeakSet` and for global subscriptions."
712
- date: 2024-07-22
813
version: 7.3.2
914
changes:
@@ -564,7 +569,7 @@ sdks:
564569
- distribution-type: source
565570
distribution-repository: GitHub release
566571
package-name: PubNub
567-
location: https://github.com/pubnub/swift/archive/refs/tags/7.3.2.zip
572+
location: https://github.com/pubnub/swift/archive/refs/tags/7.3.3.zip
568573
supported-platforms:
569574
supported-operating-systems:
570575
macOS:

PubNub.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,7 +3995,7 @@
39953995
"@loader_path/Frameworks",
39963996
);
39973997
MACOSX_DEPLOYMENT_TARGET = 10.15;
3998-
MARKETING_VERSION = 7.3.2;
3998+
MARKETING_VERSION = 7.3.3;
39993999
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
40004000
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
40014001
MTL_FAST_MATH = YES;
@@ -4046,7 +4046,7 @@
40464046
"@loader_path/Frameworks",
40474047
);
40484048
MACOSX_DEPLOYMENT_TARGET = 10.15;
4049-
MARKETING_VERSION = 7.3.2;
4049+
MARKETING_VERSION = 7.3.3;
40504050
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
40514051
MTL_ENABLE_DEBUG_INFO = NO;
40524052
MTL_FAST_MATH = YES;
@@ -4154,7 +4154,7 @@
41544154
"@loader_path/Frameworks",
41554155
);
41564156
MACOSX_DEPLOYMENT_TARGET = 10.15;
4157-
MARKETING_VERSION = 7.3.2;
4157+
MARKETING_VERSION = 7.3.3;
41584158
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
41594159
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
41604160
MTL_FAST_MATH = YES;
@@ -4207,7 +4207,7 @@
42074207
"@loader_path/Frameworks",
42084208
);
42094209
MACOSX_DEPLOYMENT_TARGET = 10.15;
4210-
MARKETING_VERSION = 7.3.2;
4210+
MARKETING_VERSION = 7.3.3;
42114211
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
42124212
MTL_ENABLE_DEBUG_INFO = NO;
42134213
MTL_FAST_MATH = YES;
@@ -4328,7 +4328,7 @@
43284328
"@loader_path/Frameworks",
43294329
);
43304330
MACOSX_DEPLOYMENT_TARGET = 10.15;
4331-
MARKETING_VERSION = 7.3.2;
4331+
MARKETING_VERSION = 7.3.3;
43324332
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
43334333
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
43344334
MTL_FAST_MATH = YES;
@@ -4380,7 +4380,7 @@
43804380
"@loader_path/Frameworks",
43814381
);
43824382
MACOSX_DEPLOYMENT_TARGET = 10.15;
4383-
MARKETING_VERSION = 7.3.2;
4383+
MARKETING_VERSION = 7.3.3;
43844384
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
43854385
MTL_ENABLE_DEBUG_INFO = NO;
43864386
MTL_FAST_MATH = YES;
@@ -4860,7 +4860,7 @@
48604860
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
48614861
);
48624862
MACOSX_DEPLOYMENT_TARGET = 10.15;
4863-
MARKETING_VERSION = 7.3.2;
4863+
MARKETING_VERSION = 7.3.3;
48644864
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++14";
48654865
OTHER_CFLAGS = "$(inherited)";
48664866
OTHER_LDFLAGS = "$(inherited)";
@@ -4902,7 +4902,7 @@
49024902
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
49034903
);
49044904
MACOSX_DEPLOYMENT_TARGET = 10.15;
4905-
MARKETING_VERSION = 7.3.2;
4905+
MARKETING_VERSION = 7.3.3;
49064906
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++14";
49074907
OTHER_CFLAGS = "$(inherited)";
49084908
OTHER_LDFLAGS = "$(inherited)";

PubNubSwift.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'PubNubSwift'
3-
s.version = '7.3.2'
3+
s.version = '7.3.3'
44
s.homepage = 'https://github.com/pubnub/swift'
55
s.documentation_url = 'https://www.pubnub.com/docs/swift-native/pubnub-swift-sdk'
66
s.authors = { 'PubNub, Inc.' => '[email protected]' }

Sources/PubNub/Helpers/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public enum Constant {
5757

5858
static let pubnubSwiftSDKName: String = "PubNubSwift"
5959

60-
static let pubnubSwiftSDKVersion: String = "7.3.2"
60+
static let pubnubSwiftSDKVersion: String = "7.3.3"
6161

6262
static let appBundleId: String = {
6363
if let info = Bundle.main.infoDictionary,

Sources/PubNub/Helpers/WeakBox.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,51 @@ final class WeakBox<Element>: Hashable where Element: AnyObject, Element: Hashab
2929
}
3030

3131
struct WeakSet<Element> where Element: AnyObject, Element: Hashable {
32-
private var elements: Set<WeakBox<Element>> = []
32+
private var elements: Atomic<Set<WeakBox<Element>>> = Atomic([])
3333

3434
init(_ elements: [Element]) {
35-
elements.forEach { self.elements.update(with: WeakBox($0)) }
35+
self.elements.lockedWrite { [elements] currentValue in
36+
elements.forEach { element in
37+
currentValue.update(with: WeakBox(element))
38+
}
39+
}
3640
}
3741

3842
// NSSet Operations
3943
var allObjects: [Element] {
40-
return elements.compactMap { $0.underlying }
44+
return elements.lockedRead { $0.compactMap { $0.underlying } }
4145
}
4246

4347
var count: Int {
44-
return self.elements.count
48+
elements.lockedRead { $0.count }
4549
}
4650

4751
mutating func update(_ element: Element) {
48-
elements.update(with: WeakBox(element))
52+
elements.lockedWrite { [element] in
53+
$0.update(with: WeakBox(element))
54+
}
4955
}
5056

5157
mutating func remove(_ element: Element) {
52-
elements.remove(WeakBox(element))
58+
elements.lockedWrite { [element] in
59+
$0.remove(WeakBox(element))
60+
}
5361
}
5462

5563
mutating func removeAll() {
56-
elements.removeAll()
64+
elements.lockedWrite { $0 = Set<WeakBox<Element>>() }
5765
}
5866
}
5967

6068
extension WeakSet: Collection {
61-
var startIndex: Set<WeakBox<Element>>.Index { return elements.startIndex }
62-
var endIndex: Set<WeakBox<Element>>.Index { return elements.endIndex }
69+
var startIndex: Set<WeakBox<Element>>.Index { return elements.lockedRead { $0.startIndex } }
70+
var endIndex: Set<WeakBox<Element>>.Index { return elements.lockedRead { $0.endIndex } }
6371

6472
subscript(position: Set<WeakBox<Element>>.Index) -> Element? {
65-
return elements[position].underlying
73+
elements.lockedRead { $0[position].underlying }
6674
}
6775

6876
func index(after index: Set<WeakBox<Element>>.Index) -> Set<WeakBox<Element>>.Index {
69-
return elements.index(after: index)
77+
elements.lockedRead { $0.index(after: index) }
7078
}
7179
}

Sources/PubNub/Subscription/SubscriptionSession.swift

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class SubscriptionSession: EventListenerInterface, StatusListenerInterface {
7171
return statusListener
7272
}()
7373

74-
private var globalChannelSubscriptions: [String: Subscription] = [:]
75-
private var globalGroupSubscriptions: [String: Subscription] = [:]
74+
private var globalChannelSubscriptions: Atomic<[String: Subscription]> = Atomic([:])
75+
private var globalGroupSubscriptions: Atomic<[String: Subscription]> = Atomic([:])
7676
private let strategy: any SubscriptionSessionStrategy
7777

7878
init(
@@ -133,16 +133,29 @@ class SubscriptionSession: EventListenerInterface, StatusListenerInterface {
133133
and: channelGroupSubscriptions,
134134
at: cursor?.timetoken
135135
)
136-
for subscription in channelSubscriptions {
137-
subscription.subscriptionNames.compactMap { $0 }.forEach {
138-
globalChannelSubscriptions[$0] = subscription
136+
137+
let channelSubsToMerge = channelSubscriptions.reduce(
138+
into: [String: Subscription]()
139+
) { accumulatedValue, subscription in
140+
subscription.subscriptionNames.forEach {
141+
accumulatedValue[$0] = subscription
139142
}
140143
}
141-
for subscription in channelGroupSubscriptions {
142-
subscription.subscriptionNames.compactMap { $0 }.forEach {
143-
globalGroupSubscriptions[$0] = subscription
144+
145+
let channelGroupSubsToMerge = channelGroupSubscriptions.reduce(
146+
into: [String: Subscription]()
147+
) { accumulatedValue, subscription in
148+
subscription.subscriptionNames.forEach {
149+
accumulatedValue[$0] = subscription
144150
}
145151
}
152+
153+
globalChannelSubscriptions.lockedWrite {
154+
$0.merge(channelSubsToMerge) { _, new in new }
155+
}
156+
globalGroupSubscriptions.lockedWrite {
157+
$0.merge(channelGroupSubsToMerge) { _, new in new }
158+
}
146159
}
147160

148161
// MARK: - Reconnect
@@ -171,15 +184,25 @@ class SubscriptionSession: EventListenerInterface, StatusListenerInterface {
171184
presenceOnly ? [$0.presenceChannelName] : [$0, $0.presenceChannelName]
172185
}
173186
internalUnsubscribe(
174-
from: globalChannelSubscriptions.compactMap { channelNamesToUnsubscribe.contains($0.key) ? $0.value : nil },
175-
and: globalGroupSubscriptions.compactMap { groupNamesToUnsubscribe.contains($0.key) ? $0.value : nil },
187+
from: globalChannelSubscriptions.lockedRead { $0.compactMap {
188+
channelNamesToUnsubscribe.contains($0.key) ? $0.value : nil
189+
} },
190+
and: globalGroupSubscriptions.lockedRead { $0.compactMap {
191+
groupNamesToUnsubscribe.contains($0.key) ? $0.value : nil
192+
} },
176193
presenceOnly: presenceOnly
177194
)
178-
channelNamesToUnsubscribe.forEach {
179-
globalChannelSubscriptions.removeValue(forKey: $0)
195+
196+
globalChannelSubscriptions.lockedWrite { currentContainer in
197+
channelNamesToUnsubscribe.forEach {
198+
currentContainer.removeValue(forKey: $0)
199+
}
180200
}
181-
groupNamesToUnsubscribe.forEach {
182-
globalGroupSubscriptions.removeValue(forKey: $0)
201+
202+
globalGroupSubscriptions.lockedWrite { currentContainer in
203+
groupNamesToUnsubscribe.forEach {
204+
currentContainer.removeValue(forKey: $0)
205+
}
183206
}
184207
}
185208

0 commit comments

Comments
 (0)