-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1058da
commit 498f627
Showing
17 changed files
with
174 additions
and
361 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
apple/Sources/Sargon/Drivers/ProfileChange/ProfileChange.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
/// 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod profile_change_client; | ||
|
||
pub use profile_change_client::*; |
18 changes: 18 additions & 0 deletions
18
src/system/clients/client/profile_change_client/profile_change_client.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::prelude::*; | ||
|
||
#[derive(Debug)] | ||
pub struct ProfileChangeClient { | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
7 changes: 7 additions & 0 deletions
7
src/system/drivers/profile_change_driver/profile_change_driver.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod test; | ||
|
||
pub use test::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
34 changes: 34 additions & 0 deletions
34
src/system/drivers/profile_change_driver/support/test/rust_profile_change_driver.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::prelude::*; | ||
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
|
||
} | ||
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()), | ||
}) | ||
} | ||
} |
Oops, something went wrong.