Skip to content
This repository has been archived by the owner on May 15, 2021. It is now read-only.

Commit

Permalink
signals for projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Connoropolous committed Nov 10, 2020
1 parent 380e9fb commit f52b806
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 313 deletions.
109 changes: 97 additions & 12 deletions crates/dna_help/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
use hdk3::prelude::*;
pub use paste;
use std::fmt;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
#[serde(from = "UIEnum")]
#[serde(into = "UIEnum")]
pub enum ActionType {
Create,
Update,
Delete
}

#[derive(Debug, Serialize, Deserialize, SerializedBytes, Clone, PartialEq)]
pub struct UIEnum(String);

impl From<UIEnum> for ActionType {
fn from(ui_enum: UIEnum) -> Self {
match ui_enum.0.as_str() {
"create" => Self::Create,
"update" => Self::Update,
_ => Self::Delete,
}
}
}

impl From<ActionType> for UIEnum {
fn from(action_type: ActionType) -> Self {
Self(action_type.to_string().to_lowercase())
}
}

impl fmt::Display for ActionType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

pub type EntryAndHash<T> = (T, HeaderHash, EntryHash);
pub type OptionEntryAndHash<T> = Option<EntryAndHash<T>>;
Expand Down Expand Up @@ -72,19 +107,26 @@ impl From<WrappedEntryHash> for UIStringHash {
*/

// sender
pub fn signal_peers<T: TryInto<SerializedBytes>>(signal: T, peers: Vec<AgentPubKey>) -> ExternResult<()> {
pub fn signal_peers<T: TryInto<SerializedBytes>>(signal: T, get_peers: fn() -> ExternResult<Vec<AgentPubKey>>) -> ExternResult<()> {
let peers = get_peers()?;
let zome_info = zome_info!()?;
debug!(format!("PEERS! {:?}", peers))?;
match signal.try_into() {
Ok(sb) => {
for peer in peers {
// ignore errors
let _ = call_remote!(
let res = call_remote!(
peer,
zome_info.zome_name.clone(),
zome::FunctionName("receive_signal".into()),
None,
sb.clone()
);
if res.is_err() {
let _ = debug!(format!("CALL REMOTE ERROR: {:?}", res));
} else {
let _ = debug!(format!("CALL REMOTE SUCCESS {:?}", res));
}
};
Ok(())
},
Expand Down Expand Up @@ -182,7 +224,7 @@ pub fn fetch_links<
#[macro_export]
macro_rules! crud {
(
$crud_type:ident, $i:ident, $path:expr
$crud_type:ident, $i:ident, $path:expr, $get_peers:ident, $convert_to_receiver_signal:ident
) => {

$crate::paste::paste! {
Expand All @@ -195,6 +237,25 @@ macro_rules! crud {
pub entry_address: $crate::WrappedEntryHash
}

// this will be used to send these data structures as signals to the UI
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
// untagged because the useful tagging is done externally on the *Signal object
// as the tag and action
#[serde(untagged)]
pub enum [<$crud_type SignalData>] {
Create([<$crud_type WireEntry>]),
Update([<$crud_type WireEntry>]),
Delete($crate::WrappedHeaderHash),
}

// this will be used to send these data structures as signals to the UI
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
pub struct [<$crud_type Signal>] {
pub entry_type: String,
pub action: $crate::ActionType,
pub data: [<$crud_type SignalData>],
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
pub struct [<$crud_type UpdateInput>] {
pub entry: $crud_type,
Expand All @@ -217,7 +278,7 @@ macro_rules! crud {
/*
CREATE
*/
pub fn [<inner_create_ $i>](entry: $crud_type) -> ExternResult<[<$crud_type WireEntry>]> {
pub fn [<inner_create_ $i>](entry: $crud_type, send_signal: bool) -> ExternResult<[<$crud_type WireEntry>]> {
let address = create_entry!(entry.clone())?;
let entry_hash = hash_entry!(entry.clone())?;
let path_hash = Path::from([<$i:upper _PATH>]).hash()?;
Expand All @@ -227,13 +288,21 @@ macro_rules! crud {
address: $crate::WrappedHeaderHash(address),
entry_address: $crate::WrappedEntryHash(entry_hash)
};
// notify_goal_comment(wire_entry.clone())?;
if (send_signal) {
let signal = $convert_to_receiver_signal([<$crud_type Signal>] {
entry_type: $path.to_string(),
action: $crate::ActionType::Create,
data: [<$crud_type SignalData>]::Create(wire_entry.clone()),
});
debug!(format!("CREATE ACTION SIGNAL PEERS {:?}", signal))?;
let _ = $crate::signal_peers(signal, $get_peers);
}
Ok(wire_entry)
}

#[hdk_extern]
pub fn [<create_ $i>](entry: $crud_type) -> ExternResult<[<$crud_type WireEntry>]> {
[<inner_create_ $i>](entry)
[<inner_create_ $i>](entry, true)
}

/*
Expand All @@ -253,35 +322,51 @@ macro_rules! crud {
/*
UPDATE
*/
pub fn [<inner_update_ $i>](update: [<$crud_type UpdateInput>]) -> ExternResult<[<$crud_type WireEntry>]> {
pub fn [<inner_update_ $i>](update: [<$crud_type UpdateInput>], send_signal: bool) -> ExternResult<[<$crud_type WireEntry>]> {
update_entry!(update.address.0.clone(), update.entry.clone())?;
let entry_address = hash_entry!(update.entry.clone())?;
let wire_entry = [<$crud_type WireEntry>] {
entry: update.entry,
address: update.address,
entry_address: $crate::WrappedEntryHash(entry_address)
};
// notify_goal_comment(wire_entry.clone())?;
if (send_signal) {
let signal = $convert_to_receiver_signal([<$crud_type Signal>] {
entry_type: $path.to_string(),
action: $crate::ActionType::Update,
data: [<$crud_type SignalData>]::Update(wire_entry.clone()),
});
debug!(format!("UPDATE ACTION SIGNAL PEERS {:?}", signal))?;
let _ = $crate::signal_peers(signal, $get_peers);
}
Ok(wire_entry)
}

#[hdk_extern]
pub fn [<update_ $i>](update: [<$crud_type UpdateInput>]) -> ExternResult<[<$crud_type WireEntry>]> {
[<inner_update_ $i>](update)
[<inner_update_ $i>](update, true)
}

/*
DELETE
*/
pub fn [<inner_archive_ $i>](address: $crate::WrappedHeaderHash) -> ExternResult<$crate::WrappedHeaderHash> {
pub fn [<inner_archive_ $i>](address: $crate::WrappedHeaderHash, send_signal: bool) -> ExternResult<$crate::WrappedHeaderHash> {
delete_entry!(address.0.clone())?;
// notify_goal_comment_archived(address.clone())?;
if (send_signal) {
let signal = $convert_to_receiver_signal([<$crud_type Signal>] {
entry_type: $path.to_string(),
action: $crate::ActionType::Delete,
data: [<$crud_type SignalData>]::Delete(address.clone()),
});
debug!(format!("DELETE ACTION SIGNAL PEERS {:?}", signal))?;
let _ = $crate::signal_peers(signal, $get_peers);
}
Ok(address)
}

#[hdk_extern]
pub fn [<archive_ $i>](address: $crate::WrappedHeaderHash) -> ExternResult<$crate::WrappedHeaderHash> {
[<inner_archive_ $i>](address)
[<inner_archive_ $i>](address, true)
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion dnas/profiles/zomes/acorn_profiles/src/profile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn send_agent_signal(wire_entry: WireEntry) -> ExternResult<()> {
signal_peers(AgentSignal {
tag: "agent".to_string(),
data: wire_entry
}, get_peers()?)
}, get_peers)
}

// used to get addresses of agents to send signals to
Expand Down
Loading

0 comments on commit f52b806

Please sign in to comment.