Skip to content

Commit 394c7d3

Browse files
committed
Add id to subscription callback
1 parent f4e640e commit 394c7d3

File tree

21 files changed

+144
-114
lines changed

21 files changed

+144
-114
lines changed

vault-android/app/src/androidTest/java/net/koofr/vault/tests/helpers/MobileVaultHelper.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,20 @@ class MobileVaultHelper constructor(private val mobileVault: MobileVault) {
3131
id = subscribe(
3232
mobileVault,
3333
object : SubscriptionCallback {
34-
override fun onChange() {
35-
id?.let { id ->
36-
val data = getData(mobileVault, id)
34+
override fun onChange(id: UInt) {
35+
val data = getData(mobileVault, id)
3736

38-
data?.let {
39-
lock.lock()
37+
data?.let {
38+
lock.lock()
4039

41-
try {
42-
mobileVault.unsubscribe(id = id)
40+
try {
41+
mobileVault.unsubscribe(id = id)
4342

44-
callbackData = it
43+
callbackData = it
4544

46-
condition.signal()
47-
} finally {
48-
lock.unlock()
49-
}
45+
condition.signal()
46+
} finally {
47+
lock.unlock()
5048
}
5149
}
5250
}

vault-android/app/src/main/java/net/koofr/vault/features/mobilevault/Subscription.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class Subscription<T> constructor(
2121
id = subscribe(
2222
mobileVault,
2323
object : SubscriptionCallback {
24-
override fun onChange() {
24+
override fun onChange(id: UInt) {
2525
coroutineScope.launch {
26-
update()
26+
update(id)
2727
}
2828
}
2929
},
@@ -32,14 +32,12 @@ class Subscription<T> constructor(
3232
data.value = getData(mobileVault, id!!)
3333
}
3434

35-
private fun update() {
36-
id?.let {
37-
val data = getData(mobileVault, it)
35+
private fun update(id: UInt) {
36+
val data = getData(mobileVault, id)
3837

39-
this.data.value = data
38+
this.data.value = data
4039

41-
onData?.let { it(data) }
42-
}
40+
onData?.let { it(data) }
4341
}
4442

4543
fun setOnData(onData: (T?) -> Unit) {

vault-android/app/src/main/java/net/koofr/vault/features/shareactivity/ShareActivityViewModel.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,19 @@ class ShareActivityViewModel @Inject constructor(
4343
private var transfersAborted = false
4444

4545
init {
46-
transfersIsActiveSubscriptionId =
47-
mobileVault.transfersIsActiveSubscribe(
48-
cb = object : SubscriptionCallback {
49-
override fun onChange() {
50-
viewModelScope.launch {
51-
transfersIsActiveSubscriptionId?.let {
52-
handleTransfersIsActive(it)
53-
}
54-
}
46+
val id = mobileVault.transfersIsActiveSubscribe(
47+
cb = object : SubscriptionCallback {
48+
override fun onChange(id: UInt) {
49+
viewModelScope.launch {
50+
handleTransfersIsActive(id)
5551
}
56-
},
57-
)
52+
}
53+
},
54+
)
55+
56+
transfersIsActiveSubscriptionId = id
5857

59-
handleTransfersIsActive(transfersIsActiveSubscriptionId!!)
58+
handleTransfersIsActive(id)
6059
}
6160

6261
override fun onCleared() {

vault-desktop-server/src/callbacks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use vault_web_api::web_vault_base::Callback;
1313
pub struct CallbackId(pub String);
1414

1515
pub struct Callbacks {
16-
senders: Arc<Mutex<HashMap<String, mpsc::UnboundedSender<String>>>>,
16+
senders: Arc<Mutex<HashMap<String, mpsc::UnboundedSender<(String, u32)>>>>,
1717
}
1818

1919
impl Callbacks {
@@ -26,16 +26,16 @@ impl Callbacks {
2626
pub fn cb(&self, callback_id: CallbackId) -> Callback {
2727
let callbacks_senders = self.senders.clone();
2828

29-
Box::new(move || {
29+
Box::new(move |subscription_id| {
3030
let senders = callbacks_senders.lock().unwrap();
3131

3232
for sender in senders.values() {
33-
let _ = sender.unbounded_send(callback_id.0.clone());
33+
let _ = sender.unbounded_send((callback_id.0.clone(), subscription_id));
3434
}
3535
})
3636
}
3737

38-
pub fn stream(&self) -> impl Stream<Item = String> + use<> {
38+
pub fn stream(&self) -> impl Stream<Item = (String, u32)> + use<> {
3939
let callbacks_senders = self.senders.clone();
4040

4141
let (callbacks_sender, callbacks_receiver) = mpsc::unbounded();

vault-desktop-server/src/sessions.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub enum SessionMessage {
2929
Callback {
3030
#[serde(rename = "callbackId")]
3131
callback_id: String,
32+
#[serde(rename = "subscriptionId")]
33+
subscription_id: u32,
3234
},
3335
}
3436

@@ -79,9 +81,13 @@ impl Sessions {
7981
let session_id = session.id.clone();
8082

8183
let callbacks = session.callbacks.clone();
82-
let callbacks_stream = callbacks
83-
.stream()
84-
.map(|callback_id| SessionMessage::Callback { callback_id });
84+
let callbacks_stream =
85+
callbacks
86+
.stream()
87+
.map(|(callback_id, subscription_id)| SessionMessage::Callback {
88+
callback_id,
89+
subscription_id,
90+
});
8591

8692
let start_session_id = session_id.clone();
8793

vault-ios/Vault.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
A1B280472E47727000C1696A /* RecentRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B280462E47727000C1696A /* RecentRow.swift */; };
143143
A1B7A9E42B19F56000C6F98E /* LifecycleHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B7A9E32B19F56000C6F98E /* LifecycleHandler.swift */; };
144144
A1C8654E2ABF721F0032F64B /* WindowUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1C8654D2ABF721F0032F64B /* WindowUtils.swift */; };
145+
A1C8CA972F17D8BF00E8AA3A /* Atomics in Frameworks */ = {isa = PBXBuildFile; productRef = A1C8CA962F17D8BF00E8AA3A /* Atomics */; };
145146
A1CC129E2A8AA0EA00C0E1A0 /* ImageData.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1CC129D2A8AA0EA00C0E1A0 /* ImageData.swift */; };
146147
A1CC12A02A8BB69900C0E1A0 /* ShareTargetFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1CC129F2A8BB69900C0E1A0 /* ShareTargetFile.swift */; };
147148
A1CF9AB82ACFF64F00D006AA /* VaultCommon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A1AD92DD2A828283006172BF /* VaultCommon.framework */; };
@@ -417,6 +418,7 @@
417418
buildActionMask = 2147483647;
418419
files = (
419420
A1149A232B1F18FA0058BC84 /* libvault_mobile.a in Frameworks */,
421+
A1C8CA972F17D8BF00E8AA3A /* Atomics in Frameworks */,
420422
);
421423
runOnlyForDeploymentPostprocessing = 0;
422424
};
@@ -1149,6 +1151,7 @@
11491151
mainGroup = A18A8CDD2917988100B8F8B9;
11501152
packageReferences = (
11511153
A1FA15172B05199000F95F2E /* XCRemoteSwiftPackageReference "SwiftUINavController" */,
1154+
A1C8CA952F17D8BF00E8AA3A /* XCRemoteSwiftPackageReference "swift-atomics" */,
11521155
);
11531156
productRefGroup = A18A8CE72917988100B8F8B9 /* Products */;
11541157
projectDirPath = "";
@@ -2043,6 +2046,14 @@
20432046
/* End XCConfigurationList section */
20442047

20452048
/* Begin XCRemoteSwiftPackageReference section */
2049+
A1C8CA952F17D8BF00E8AA3A /* XCRemoteSwiftPackageReference "swift-atomics" */ = {
2050+
isa = XCRemoteSwiftPackageReference;
2051+
repositoryURL = "https://github.com/apple/swift-atomics.git";
2052+
requirement = {
2053+
kind = upToNextMajorVersion;
2054+
minimumVersion = 1.3.0;
2055+
};
2056+
};
20462057
A1FA15172B05199000F95F2E /* XCRemoteSwiftPackageReference "SwiftUINavController" */ = {
20472058
isa = XCRemoteSwiftPackageReference;
20482059
repositoryURL = "https://github.com/bancek/SwiftUINavController";
@@ -2054,6 +2065,11 @@
20542065
/* End XCRemoteSwiftPackageReference section */
20552066

20562067
/* Begin XCSwiftPackageProductDependency section */
2068+
A1C8CA962F17D8BF00E8AA3A /* Atomics */ = {
2069+
isa = XCSwiftPackageProductDependency;
2070+
package = A1C8CA952F17D8BF00E8AA3A /* XCRemoteSwiftPackageReference "swift-atomics" */;
2071+
productName = Atomics;
2072+
};
20572073
A1FA15182B05199000F95F2E /* SwiftUINavController */ = {
20582074
isa = XCSwiftPackageProductDependency;
20592075
package = A1FA15172B05199000F95F2E /* XCRemoteSwiftPackageReference "SwiftUINavController" */;

vault-ios/Vault.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vault-ios/VaultMobile/Callbacks.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import Foundation
22

33
public final class SubscriptionCallbackFn: SubscriptionCallback {
4-
private let fn: @Sendable () -> Void
4+
private let fn: @Sendable (UInt32) -> Void
55

6-
public init(_ fn: @Sendable @escaping () -> Void) {
6+
public init(_ fn: @Sendable @escaping (UInt32) -> Void) {
77
self.fn = fn
88
}
99

10-
public func onChange() {
10+
public func onChange(id: UInt32) {
1111
DispatchQueue.main.async {
12-
self.fn()
12+
self.fn(id)
1313
}
1414
}
1515
}

vault-ios/VaultMobile/Subscription.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Atomics
12
import Combine
23
import Foundation
34

@@ -17,15 +18,15 @@ public class Subscription<T>: ObservableObject {
1718

1819
self.id = subscribe(
1920
mobileVault,
20-
SubscriptionCallbackFn { [weak self] in
21-
self?.update()
21+
SubscriptionCallbackFn { [weak self] id in
22+
self?.update(id)
2223
})
2324

2425
self.data = getData(mobileVault, self.id!)
2526
}
2627

27-
private func update() {
28-
let data = getData(mobileVault, id!)
28+
private func update(_ id: UInt32) {
29+
let data = getData(mobileVault, id)
2930

3031
self.data = data
3132

@@ -50,25 +51,28 @@ public func subscriptionWait<T>(
5051
getData: @escaping (MobileVault, UInt32) -> T?
5152
) async -> T {
5253
await withCheckedContinuation({ continuation in
53-
var id: UInt32?
54-
var resumed = false
54+
let resumed = ManagedAtomic(false)
5555

56-
let cb = {
57-
let data = getData(mobileVault, id!)
56+
let cb = { @Sendable (id: UInt32) in
57+
let data = getData(mobileVault, id)
5858

5959
if let data = data {
60-
mobileVault.unsubscribe(id: id!)
60+
mobileVault.unsubscribe(id: id)
6161

62-
if !resumed {
63-
resumed = true
62+
let didResume = resumed.compareExchange(
63+
expected: false,
64+
desired: true,
65+
ordering: .acquiringAndReleasing
66+
).exchanged
6467

68+
if didResume {
6569
continuation.resume(returning: data)
6670
}
6771
}
6872
}
6973

70-
id = subscribe(mobileVault, SubscriptionCallbackFn(cb))
74+
let id = subscribe(mobileVault, SubscriptionCallbackFn(cb))
7175

72-
cb()
76+
cb(id)
7377
})
7478
}

vault-mobile/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub trait SecureStorage: Send + Sync + Debug {
143143
// subscription
144144

145145
pub trait SubscriptionCallback: Send + Sync + Debug {
146-
fn on_change(&self);
146+
fn on_change(&self, id: u32);
147147
}
148148

149149
// status

0 commit comments

Comments
 (0)