Skip to content

Commit

Permalink
addEventListener event union (kaspanet#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder83singh authored Mar 30, 2024
1 parent 69e269d commit daf142c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 17 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 42 additions & 7 deletions wallet/core/src/wasm/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ use wasm_bindgen::prelude::*;

cfg_if! {
if #[cfg(any(feature = "wasm32-core", feature = "wasm32-sdk"))] {

// #[wasm_bindgen]
// #[derive(Copy, Clone, Debug)]
// pub enum UtxoProcessorEventType{
// All="all",
// Connect="connect",
// Disconnect="disconnect",
// // UtxoIndexNotEnabled,
// // SyncState,
// // ServerStatus,
// // UtxoProcError,
// // DaaScoreChange,
// // Pending,
// // Reorg,
// // Stasis,
// // Maturity,
// Discovery="discovery",
// // Balance,
// // Error,
// }


#[wasm_bindgen(typescript_custom_section)]
const TS_NOTIFY: &'static str = r#"
Expand Down Expand Up @@ -51,14 +73,27 @@ cfg_if! {
| IErrorEvent
;
/**
* {@link UtxoProcessor} notification event interface.
* @category Wallet SDK
*/
export interface IUtxoProcessorEvent {
event : string;
data? : UtxoProcessorEventData;
export type UtxoProcessorEventMap = {
"connect":IConnectEvent,
"disconnect": IDisconnectEvent,
"utxo-index-not-enabled": IUtxoIndexNotEnabledEvent,
"sync-state": ISyncStateEvent,
"server-status": IServerStatusEvent,
"utxo-proc-error": IUtxoProcErrorEvent,
"daa-score-change": IDaaScoreChangeEvent,
"pending": IPendingEvent,
"reorg": IReorgEvent,
"stasis": IStasisEvent,
"maturity": IMaturityEvent,
"discovery": IDiscoveryEvent,
"balance": IBalanceEvent,
"error": IErrorEvent
}
type IUtxoProcessorEvent = {
[K in keyof UtxoProcessorEventMap]: { event: K, data: UtxoProcessorEventMap[K] }
}[keyof UtxoProcessorEventMap];
/**
* {@link UtxoProcessor} notification callback type.
Expand Down
24 changes: 23 additions & 1 deletion wallet/core/src/wasm/utxo/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ impl Inner {
}
}

cfg_if! {
if #[cfg(any(feature = "wasm32-core", feature = "wasm32-sdk"))] {
#[wasm_bindgen(typescript_custom_section)]
const TS_NOTIFY: &'static str = r#"
interface UtxoProcessor {
/**
* @param {UtxoProcessorNotificationCallback} callback
*/
addEventListener(callback:UtxoProcessorNotificationCallback): void;
/**
* @param {UtxoProcessorEventType} event
* @param {UtxoProcessorNotificationCallback} [callback]
*/
addEventListener<M extends keyof UtxoProcessorEventMap>(
event: M,
callback: (eventData: UtxoProcessorEventMap[M]) => void
)
}"#;
}
}

///
/// UtxoProcessor class is the main coordinator that manages UTXO processing
/// between multiple UtxoContext instances. It acts as a bridge between the
Expand Down Expand Up @@ -224,9 +245,10 @@ impl UtxoProcessor {
}
}


#[wasm_bindgen]
impl UtxoProcessor {
#[wasm_bindgen(js_name = "addEventListener")]
#[wasm_bindgen(js_name = "addEventListener", skip_typescript)]
pub fn add_event_listener(
&self,
event: UtxoProcessorNotificationTypeOrCallback,
Expand Down
22 changes: 15 additions & 7 deletions wasm/examples/nodejs/typescript/src/utxo-context-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,25 @@ let { encoding, networkId, destinationAddress } = parseArgs();
let context = new UtxoContext({ processor });

// 4) Register a listener with the UtxoProcessor::events
processor.addEventListener(({ event, data }: IUtxoProcessorEvent) => {
processor.addEventListener(({event, data}) => {
// handle the event
// since the event is a union type, you can switch on the
// event type and cast the data to the appropriate type
switch (event) {
case UtxoProcessorEventType.Discovery: {
let record = (data as IDiscoveryEvent).record;
//string enums are not working for @Matoo
//case UtxoProcessorEventType.Discovery: {
case "discovery": {
let record = data.record;
console.log("Discovery event record:", record);
} break;
case UtxoProcessorEventType.Pending: {
let record = (data as IPendingEvent).record;
//case UtxoProcessorEventType.Pending: {
case "pending":{
let record = data.record;
console.log("Pending event record:", record);
} break;
case UtxoProcessorEventType.Maturity: {
let record = (data as IMaturityEvent).record;
//case UtxoProcessorEventType.Maturity: {
case "maturity": {
let record = data.record;
console.log("Maturity event record:", record);
} break;
default: {
Expand All @@ -81,6 +85,10 @@ let { encoding, networkId, destinationAddress } = parseArgs();
}
});

processor.addEventListener("discovery", (data) => {
console.log("Discovery event record:", data.record);
});

console.log(processor);

// 5) Once the environment is setup, connect to RPC
Expand Down

0 comments on commit daf142c

Please sign in to comment.