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

SargonOS v1 integration API #184

Merged
merged 15 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions apple/Sources/Sargon/Drivers/ProfileChange/ProfileChange.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Foundation
import SargonUniFFI
import AsyncExtensions

// Makes it possible to type `.shared` on an initalizer/func taking
// `some EventBusDriver` as parameter.
extension ProfileChangeDriver where Self == ProfileChangeBus {

public static var shared: Self { Self.shared }
}

/// An `EventBusDriver` actor which handles incoming
/// `EventNotifications` and forwards them to any
/// subscriber of `notifications()`, being a multicasted
/// async sequence.
public final actor ProfileChangeBus {
GhenadieVP marked this conversation as resolved.
Show resolved Hide resolved
/// A stream we multicast on.
private let stream = AsyncThrowingPassthroughSubject<Element, any Error>()
private let subject: Subject

#if DEBUG
public init() {
subject = .init()
}
#else
private init() {
subject = .init()
}
#endif
}

extension ProfileChangeBus {

public typealias Element = Profile
public typealias Subject = AsyncPassthroughSubject<Element>

public static let shared = ProfileChangeBus()

/// A multicasted async sequence of `EventNotification` values
/// over time, originally emitted by `SargonOS` (Rust side).
public func profile_change_stream() -> AsyncMulticastSequence<Subject, AsyncThrowingPassthroughSubject<Element, any Error>> {
subject
.multicast(stream)
.autoconnect()
}
}

extension ProfileChangeBus: ProfileChangeDriver {
public func handleProfileChange(changedProfile: Profile) async {
subject.send(changedProfile)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ extension Drivers {
logging: .shared,
eventBus: .shared,
fileSystem: .shared,
unsafeStorage: unsafeStorage
unsafeStorage: unsafeStorage,
profileChangeDriver: .shared
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,3 @@ import SargonUniFFI
public typealias SargonOS = SargonOs

extension SargonOS: @unchecked Sendable {}

extension SargonOS {

@available(*, deprecated, message: "SHOULD migrate to use more specialized methods on SargonOS instead, e.g. `createAndSaveNewAccount` - SargonOS should be the SOLE object to perform the mutation and persisting.")
public func saveChangedProfile(_ profile: Profile) async throws {
try await deprecatedSaveFfiChangedProfile(profile: profile)
}
}
6 changes: 0 additions & 6 deletions apple/Sources/Sargon/SargonOS/SargonOSProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ extension SargonOSProtocol {

// MARK: Extensions
extension SargonOSProtocol {

@available(*, deprecated, message: "SHOULD migrate to use more specialized access methods on SargonOS instead, e.g. `accountsOnCurrentNetwork`.")
public var profile: Profile {
os.profile()
}

public var currentNetworkID: NetworkID {
os.currentNetworkId()
}
Expand Down
2 changes: 2 additions & 0 deletions src/system/clients/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod event_bus_client;
mod file_system_client;
mod host_info_client;
mod http_client;
mod profile_change_client;
mod secure_storage_client;
mod unsafe_storage_client;

Expand All @@ -11,5 +12,6 @@ pub use event_bus_client::*;
pub use file_system_client::*;
pub use host_info_client::*;
pub use http_client::*;
pub use profile_change_client::*;
pub use secure_storage_client::*;
pub use unsafe_storage_client::*;
3 changes: 3 additions & 0 deletions src/system/clients/client/profile_change_client/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod profile_change_client;

pub use profile_change_client::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::prelude::*;

#[derive(Debug)]
pub struct ProfileChangeClient {
GhenadieVP marked this conversation as resolved.
Show resolved Hide resolved
driver: Arc<dyn ProfileChangeDriver>,
}

impl ProfileChangeClient {
pub(crate) fn new(driver: Arc<dyn ProfileChangeDriver>) -> Self {
Self { driver }
}
}

impl ProfileChangeClient {
pub async fn emit(&self, changed_profile: Profile) {
self.driver.handle_profile_change(changed_profile).await
}
}
4 changes: 4 additions & 0 deletions src/system/clients/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Clients {
pub unsafe_storage: UnsafeStorageClient,
pub file_system: FileSystemClient,
pub event_bus: EventBusClient,
pub profile_change: ProfileChangeClient,
}

impl Clients {
Expand All @@ -22,6 +23,8 @@ impl Clients {
UnsafeStorageClient::new(drivers.unsafe_storage.clone());
let file_system = FileSystemClient::new(drivers.file_system.clone());
let event_bus = EventBusClient::new(drivers.event_bus.clone());
let profile_change =
ProfileChangeClient::new(drivers.profile_change_driver.clone());
Self {
host,
secure_storage,
Expand All @@ -30,6 +33,7 @@ impl Clients {
unsafe_storage,
file_system,
event_bus,
profile_change,
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/system/drivers/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Drivers {
pub event_bus: Arc<dyn EventBusDriver>,
pub file_system: Arc<dyn FileSystemDriver>,
pub unsafe_storage: Arc<dyn UnsafeStorageDriver>,
pub profile_change_driver: Arc<dyn ProfileChangeDriver>,
}

#[uniffi::export]
Expand All @@ -25,6 +26,7 @@ impl Drivers {
event_bus: Arc<dyn EventBusDriver>,
file_system: Arc<dyn FileSystemDriver>,
unsafe_storage: Arc<dyn UnsafeStorageDriver>,
profile_change_driver: Arc<dyn ProfileChangeDriver>,
) -> Arc<Self> {
Arc::new(Self {
networking,
Expand All @@ -35,6 +37,7 @@ impl Drivers {
event_bus,
file_system,
unsafe_storage,
profile_change_driver,
})
}
}
Expand All @@ -51,6 +54,7 @@ impl Drivers {
RustEventBusDriver::new(),
RustFileSystemDriver::new(),
EphemeralUnsafeStorage::new(),
RustProfileChangeDriver::new(),
)
}

Expand All @@ -64,6 +68,7 @@ impl Drivers {
RustEventBusDriver::new(),
RustFileSystemDriver::new(),
EphemeralUnsafeStorage::new(),
RustProfileChangeDriver::new(),
)
}

Expand All @@ -79,6 +84,7 @@ impl Drivers {
RustEventBusDriver::new(),
RustFileSystemDriver::new(),
EphemeralUnsafeStorage::new(),
RustProfileChangeDriver::new(),
)
}

Expand All @@ -94,6 +100,7 @@ impl Drivers {
RustEventBusDriver::new(),
RustFileSystemDriver::new(),
EphemeralUnsafeStorage::new(),
RustProfileChangeDriver::new(),
)
}

Expand All @@ -107,6 +114,7 @@ impl Drivers {
RustEventBusDriver::new(),
RustFileSystemDriver::new(),
EphemeralUnsafeStorage::new(),
RustProfileChangeDriver::new(),
)
}

Expand All @@ -120,6 +128,7 @@ impl Drivers {
RustEventBusDriver::new(),
RustFileSystemDriver::new(),
EphemeralUnsafeStorage::new(),
RustProfileChangeDriver::new(),
)
}

Expand All @@ -133,6 +142,7 @@ impl Drivers {
event_bus,
RustFileSystemDriver::new(),
EphemeralUnsafeStorage::new(),
RustProfileChangeDriver::new(),
)
}

Expand All @@ -148,6 +158,7 @@ impl Drivers {
RustEventBusDriver::new(),
file_system,
EphemeralUnsafeStorage::new(),
RustProfileChangeDriver::new(),
)
}

Expand All @@ -163,6 +174,7 @@ impl Drivers {
RustEventBusDriver::new(),
RustFileSystemDriver::new(),
unsafe_storage,
RustProfileChangeDriver::new(),
)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/system/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod file_system_driver;
mod host_info_driver;
mod logging_driver;
mod networking_driver;
mod profile_change_driver;
mod secure_storage_driver;
mod unsafe_storage_driver;

Expand All @@ -15,5 +16,6 @@ pub use file_system_driver::*;
pub use host_info_driver::*;
pub use logging_driver::*;
pub use networking_driver::*;
pub use profile_change_driver::*;
pub use secure_storage_driver::*;
pub use unsafe_storage_driver::*;
5 changes: 5 additions & 0 deletions src/system/drivers/profile_change_driver/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod profile_change_driver;
mod support;

pub use profile_change_driver::*;
pub use support::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::prelude::*;

#[uniffi::export(with_foreign)]
#[async_trait::async_trait]
pub trait ProfileChangeDriver: Send + Sync + std::fmt::Debug {
async fn handle_profile_change(&self, changed_profile: Profile);
GhenadieVP marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 3 additions & 0 deletions src/system/drivers/profile_change_driver/support/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod test;

pub use test::*;
3 changes: 3 additions & 0 deletions src/system/drivers/profile_change_driver/support/test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod rust_profile_change_driver;

pub use rust_profile_change_driver::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::prelude::*;
GhenadieVP marked this conversation as resolved.
Show resolved Hide resolved
use std::sync::RwLock;

#[derive(Debug)]
pub struct RustProfileChangeDriver {
recorded: RwLock<Vec<Profile>>,
spy: fn(Profile) -> (),
}

#[async_trait::async_trait]
impl ProfileChangeDriver for RustProfileChangeDriver {
async fn handle_profile_change(&self, changed_profile: Profile) {
self.recorded
.try_write()
.unwrap()
.push(changed_profile.clone());
(self.spy)(changed_profile)
}
}

impl RustProfileChangeDriver {
pub fn recorded(&self) -> Vec<Profile> {
self.recorded.try_read().unwrap().clone()

Check warning on line 23 in src/system/drivers/profile_change_driver/support/test/rust_profile_change_driver.rs

View check run for this annotation

Codecov / codecov/patch

src/system/drivers/profile_change_driver/support/test/rust_profile_change_driver.rs#L22-L23

Added lines #L22 - L23 were not covered by tests
}
pub fn new() -> Arc<Self> {
Self::with_spy(|_| {})
}
pub fn with_spy(spy: fn(Profile) -> ()) -> Arc<Self> {
Arc::new(Self {
spy,
recorded: RwLock::new(Vec::new()),
})
}
}
Loading
Loading