Skip to content

Commit

Permalink
update interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mobile-bungalow committed Jan 13, 2025
1 parent 9e6409b commit fe71f17
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import org.junit.Assert.assertEquals
import org.junit.Test
import org.phoenixframework.liveviewnative.core.ChangeType
import org.phoenixframework.liveviewnative.core.ConnectOpts
import org.phoenixframework.liveviewnative.core.ControlFlow
import org.phoenixframework.liveviewnative.core.Document
import org.phoenixframework.liveviewnative.core.DocumentChangeHandler
import org.phoenixframework.liveviewnative.core.LiveChannelStatus
import org.phoenixframework.liveviewnative.core.LiveFile
import org.phoenixframework.liveviewnative.core.LiveSocket
import org.phoenixframework.liveviewnative.core.NavOptions
Expand Down Expand Up @@ -63,9 +61,7 @@ class SimpleChangeHandler : DocumentChangeHandler {
println("${changeType}")
}

override fun `handleChannelStatus`(`channelStatus`: LiveChannelStatus): ControlFlow {
return ControlFlow.ContinueListening
}
override fun `handleNewDocument`(`document`: Document) {}
}

class DocumentTest {
Expand Down
7 changes: 3 additions & 4 deletions crates/core/src/callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use phoenix_channels_client::EventPayload;

use crate::dom::{ffi::Document, NodeData, NodeRef};

/// Provides secure persistent storage for session data like cookies.
Expand Down Expand Up @@ -157,13 +155,14 @@ pub trait DocumentChangeHandler: Send + Sync {

/// Implement this if you need to instrument all replies and status
/// changes on the current live channel.
#[cfg_attr(not(target_family = "wasm"), uniffi::export(callback_interface))]
#[cfg(feature = "liveview-channels")]
#[uniffi::export(callback_interface)]
pub trait LiveChannelEventHandler: Send + Sync {
/// Whenever a server sent event or reply to a user
/// message is receiver the event payload is passed to this
/// callback. by default the client handles diff events and will
/// handle live_patch, live_reload, etc, in the future
fn handle_event(&self, event: EventPayload);
fn handle_event(&self, event: phoenix_channels_client::EventPayload);
/// Whenever the status of the current LiveChannel changes
/// This callback is invoked.
fn handle_status_change(&self, event: LiveChannelStatus);
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/client/inner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl LiveViewClientState {
new_channel.document.arc_set_event_handler(handler.clone());
}

// this lifecyle stuff is going to get finnicky
// this lifecycle stuff is going to get finnicky
let old_channel = self.liveview_channel.try_lock()?.channel.clone();
old_channel.leave().await?;
*self.liveview_channel.try_lock()? = new_channel;
Expand Down
62 changes: 32 additions & 30 deletions crates/core/src/dom/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::{
sync::{Arc, Mutex},
};

use phoenix_channels_client::JSON;

pub use super::{
attribute::Attribute,
node::{Node, NodeData, NodeRef},
Expand Down Expand Up @@ -39,35 +37,12 @@ impl Document {
pub fn arc_set_event_handler(&self, handler: Arc<dyn DocumentChangeHandler>) {
self.inner.lock().expect("lock poisoned!").event_callback = Some(handler);
}
}

#[uniffi::export]
impl Document {
#[uniffi::constructor]
pub fn parse(input: String) -> Result<Arc<Self>, ParseError> {
Ok(Arc::new(Self {
inner: Arc::new(Mutex::new(super::Document::parse(input)?)),
}))
}

#[uniffi::constructor]
pub fn empty() -> Arc<Self> {
Arc::new(Self {
inner: Arc::new(Mutex::new(super::Document::empty())),
})
}

#[uniffi::constructor]
pub fn parse_fragment_json(input: String) -> Result<Arc<Self>, RenderError> {
let inner = Arc::new(Mutex::new(super::Document::parse_fragment_json(input)?));
Ok(Arc::new(Self { inner }))
}

pub fn set_event_handler(&self, handler: Box<dyn DocumentChangeHandler>) {
self.inner.lock().expect("lock poisoned!").event_callback = Some(Arc::from(handler));
}

pub fn merge_deserialized_fragment_json(&self, json: JSON) -> Result<(), RenderError> {
#[cfg(feature = "liveview-channels")]
pub fn merge_deserialized_fragment_json(
&self,
json: phoenix_channels_client::JSON,
) -> Result<(), RenderError> {
let results = self
.inner
.lock()
Expand Down Expand Up @@ -118,6 +93,33 @@ impl Document {

Ok(())
}
}

#[uniffi::export]
impl Document {
#[uniffi::constructor]
pub fn parse(input: String) -> Result<Arc<Self>, ParseError> {
Ok(Arc::new(Self {
inner: Arc::new(Mutex::new(super::Document::parse(input)?)),
}))
}

#[uniffi::constructor]
pub fn empty() -> Arc<Self> {
Arc::new(Self {
inner: Arc::new(Mutex::new(super::Document::empty())),
})
}

#[uniffi::constructor]
pub fn parse_fragment_json(input: String) -> Result<Arc<Self>, RenderError> {
let inner = Arc::new(Mutex::new(super::Document::parse_fragment_json(input)?));
Ok(Arc::new(Self { inner }))
}

pub fn set_event_handler(&self, handler: Box<dyn DocumentChangeHandler>) {
self.inner.lock().expect("lock poisoned!").event_callback = Some(Arc::from(handler));
}

pub fn merge_fragment_json(&self, json: &str) -> Result<(), RenderError> {
let json = serde_json::from_str(json)?;
Expand Down

0 comments on commit fe71f17

Please sign in to comment.