Skip to content

Commit

Permalink
removed default post_commit handler
Browse files Browse the repository at this point in the history
  • Loading branch information
matthme committed Nov 4, 2024
1 parent e080b52 commit cf556dc
Showing 1 changed file with 101 additions and 115 deletions.
216 changes: 101 additions & 115 deletions dnas/generic_dna/zomes/coordinator/generic_zome/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,122 +27,108 @@ pub struct Thing {
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Signal {
ThingCreated {
thing: Thing,
},
ThingUpdated {
thing: Thing,
},
ThingDeleted {
id: ActionHash,
},
LinksCreated {
links: Vec<NodeLink>,
},
LinksDeleted {
links: Vec<NodeLink>,
},
LinkCreated {
action: SignedActionHashed,
link_type: LinkTypes,
},
ThingCreated { thing: Thing },
ThingUpdated { thing: Thing },
ThingDeleted { id: ActionHash },
LinksCreated { links: Vec<NodeLink> },
LinksDeleted { links: Vec<NodeLink> },
}

/// Whenever an action is committed, we emit a signal to the UI elements to reactively update them
#[hdk_extern(infallible)]
pub fn post_commit(committed_actions: Vec<SignedActionHashed>) {
/// Don't modify this loop if you want the scaffolding tool to generate appropriate signals for your entries and links
for action in committed_actions {
if let Err(err) = signal_action(action) {
error!("Error signaling new action: {:?}", err);
}
}
}
// /// Whenever an action is committed, we emit a signal to the UI elements to reactively update them
// #[hdk_extern(infallible)]
// pub fn post_commit(committed_actions: Vec<SignedActionHashed>) {
// /// Don't modify this loop if you want the scaffolding tool to generate appropriate signals for your entries and links
// for action in committed_actions {
// if let Err(err) = signal_action(action) {
// error!("Error signaling new action: {:?}", err);
// }
// }
// }

/// Don't modify this function if you want the scaffolding tool to generate appropriate signals for your entries and links
fn signal_action(action: SignedActionHashed) -> ExternResult<()> {
match action.hashed.content.clone() {
Action::CreateLink(create_link) => {
if let Ok(Some(link_type)) =
LinkTypes::from_type(create_link.zome_index, create_link.link_type)
{
emit_signal(Signal::LinkCreated { action, link_type })?;
}
Ok(())
}
Action::DeleteLink(delete_link) => {
let record = get(delete_link.link_add_address.clone(), GetOptions::default())?.ok_or(
wasm_error!(WasmErrorInner::Guest(
"Failed to fetch CreateLink action".to_string()
)),
)?;
match record.action() {
Action::CreateLink(create_link) => {
if let Ok(Some(link_type)) =
LinkTypes::from_type(create_link.zome_index, create_link.link_type)
{
emit_signal(Signal::LinkDeleted {
action,
link_type,
create_link_action: record.signed_action.clone(),
})?;
}
Ok(())
}
_ => Err(wasm_error!(WasmErrorInner::Guest(
"Create Link should exist".to_string()
))),
}
}
Action::Create(_create) => {
if let Ok(Some(app_entry)) = get_entry_for_action(&action.hashed.hash) {
emit_signal(Signal::EntryCreated { action, app_entry })?;
}
Ok(())
}
Action::Update(update) => {
if let Ok(Some(app_entry)) = get_entry_for_action(&action.hashed.hash) {
if let Ok(Some(original_app_entry)) =
get_entry_for_action(&update.original_action_address)
{
emit_signal(Signal::EntryUpdated {
action,
app_entry,
original_app_entry,
})?;
}
}
Ok(())
}
Action::Delete(delete) => {
if let Ok(Some(original_app_entry)) = get_entry_for_action(&delete.deletes_address) {
emit_signal(Signal::EntryDeleted {
action,
original_app_entry,
})?;
}
Ok(())
}
_ => Ok(()),
}
}
// /// Don't modify this function if you want the scaffolding tool to generate appropriate signals for your entries and links
// fn signal_action(action: SignedActionHashed) -> ExternResult<()> {
// match action.hashed.content.clone() {
// Action::CreateLink(create_link) => {
// if let Ok(Some(link_type)) =
// LinkTypes::from_type(create_link.zome_index, create_link.link_type)
// {
// emit_signal(Signal::LinkCreated { action, link_type })?;
// }
// Ok(())
// }
// Action::DeleteLink(delete_link) => {
// let record = get(delete_link.link_add_address.clone(), GetOptions::default())?.ok_or(
// wasm_error!(WasmErrorInner::Guest(
// "Failed to fetch CreateLink action".to_string()
// )),
// )?;
// match record.action() {
// Action::CreateLink(create_link) => {
// if let Ok(Some(link_type)) =
// LinkTypes::from_type(create_link.zome_index, create_link.link_type)
// {
// emit_signal(Signal::LinkDeleted {
// action,
// link_type,
// create_link_action: record.signed_action.clone(),
// })?;
// }
// Ok(())
// }
// _ => Err(wasm_error!(WasmErrorInner::Guest(
// "Create Link should exist".to_string()
// ))),
// }
// }
// Action::Create(_create) => {
// if let Ok(Some(app_entry)) = get_entry_for_action(&action.hashed.hash) {
// emit_signal(Signal::EntryCreated { action, app_entry })?;
// }
// Ok(())
// }
// Action::Update(update) => {
// if let Ok(Some(app_entry)) = get_entry_for_action(&action.hashed.hash) {
// if let Ok(Some(original_app_entry)) =
// get_entry_for_action(&update.original_action_address)
// {
// emit_signal(Signal::EntryUpdated {
// action,
// app_entry,
// original_app_entry,
// })?;
// }
// }
// Ok(())
// }
// Action::Delete(delete) => {
// if let Ok(Some(original_app_entry)) = get_entry_for_action(&delete.deletes_address) {
// emit_signal(Signal::EntryDeleted {
// action,
// original_app_entry,
// })?;
// }
// Ok(())
// }
// _ => Ok(()),
// }
// }

fn get_entry_for_action(action_hash: &ActionHash) -> ExternResult<Option<EntryTypes>> {
let record = match get_details(action_hash.clone(), GetOptions::default())? {
Some(Details::Record(record_details)) => record_details.record,
_ => return Ok(None),
};
let entry = match record.entry().as_option() {
Some(entry) => entry,
None => return Ok(None),
};
let (zome_index, entry_index) = match record.action().entry_type() {
Some(EntryType::App(AppEntryDef {
zome_index,
entry_index,
..
})) => (zome_index, entry_index),
_ => return Ok(None),
};
EntryTypes::deserialize_from_type(*zome_index, *entry_index, entry)
}
// fn get_entry_for_action(action_hash: &ActionHash) -> ExternResult<Option<EntryTypes>> {
// let record = match get_details(action_hash.clone(), GetOptions::default())? {
// Some(Details::Record(record_details)) => record_details.record,
// _ => return Ok(None),
// };
// let entry = match record.entry().as_option() {
// Some(entry) => entry,
// None => return Ok(None),
// };
// let (zome_index, entry_index) = match record.action().entry_type() {
// Some(EntryType::App(AppEntryDef {
// zome_index,
// entry_index,
// ..
// })) => (zome_index, entry_index),
// _ => return Ok(None),
// };
// EntryTypes::deserialize_from_type(*zome_index, *entry_index, entry)
// }

0 comments on commit cf556dc

Please sign in to comment.