Skip to content

Commit

Permalink
timer and timer interval done
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Oct 29, 2024
1 parent d539c1a commit 3277079
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 126 deletions.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::call::msg_cycles_accept128;
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, |max_amount_string: String| {
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, |max_amount_string: String| {
let max_amount: u128 = max_amount_string.parse().unwrap();

ic_cdk::api::call::msg_cycles_accept128(max_amount).to_string()
msg_cycles_accept128(max_amount).to_string()
})
.unwrap()
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::call::msg_cycles_available128;
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, || {
ic_cdk::api::call::msg_cycles_available128().to_string()
})
.unwrap()
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, || msg_cycles_available128().to_string())
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::call::msg_cycles_refunded128;
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, || {
ic_cdk::api::call::msg_cycles_refunded128().to_string()
})
.unwrap()
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, || msg_cycles_refunded128().to_string())
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use rquickjs::{Ctx, Function};
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, |counter_type: u32| {
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, |counter_type: u32| -> u64 {
ic_cdk::api::performance_counter(counter_type)
})
.unwrap()
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use rquickjs::{Ctx, Function};
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> rquickjs::Function {
Function::new(context, |message: String| {
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, |message: String| {
ic_cdk::print(message);
})
.unwrap()
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::call::reject;
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, |message: String| {
ic_cdk::api::call::reject(&message);
})
.unwrap()
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, |message: String| reject(&message))
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::call::reject_code;
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, || {
let reject_code = ic_cdk::api::call::reject_code();
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, || {
let reject_code = reject_code();

let reject_code_number = match reject_code {
match reject_code {
ic_cdk::api::call::RejectionCode::NoError => 0,
ic_cdk::api::call::RejectionCode::SysFatal => 1,
ic_cdk::api::call::RejectionCode::SysTransient => 2,
ic_cdk::api::call::RejectionCode::DestinationInvalid => 3,
ic_cdk::api::call::RejectionCode::CanisterReject => 4,
ic_cdk::api::call::RejectionCode::CanisterError => 5,
ic_cdk::api::call::RejectionCode::Unknown => 6,
};

reject_code_number
}
})
.unwrap()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::call::reject_message;
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, || ic_cdk::api::call::reject_message()).unwrap()
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, || reject_message())
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::call::reply_raw;
use rquickjs::{Ctx, Function, Result, TypedArray};

pub fn get_function(context: Ctx) -> rquickjs::Function {
Function::new(context, |bytes: rquickjs::TypedArray<u8>| {
ic_cdk::api::call::reply_raw(bytes.as_ref());
})
.unwrap()
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, |bytes: TypedArray<u8>| reply_raw(bytes.as_ref()))
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::set_certified_data;
use rquickjs::{Ctx, Function, Result, TypedArray};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, |bytes: rquickjs::TypedArray<u8>| {
ic_cdk::api::set_certified_data(bytes.as_ref());
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, |bytes: TypedArray<u8>| {
set_certified_data(bytes.as_ref())
})
.unwrap()
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
use rquickjs::{Ctx, Function};
use slotmap::Key;

use crate::{error::quickjs_call_with_error_handling, quickjs_with_ctx};

pub fn get_function(ctx: Ctx) -> Function {
Function::new(ctx, |delay: String, callback_id: String| {
let delay: u64 = delay.parse().unwrap();
let delay_duration = core::time::Duration::new(delay, 0);
use core::time::Duration;

let closure = move || {
quickjs_with_ctx(|ctx| {
let global = ctx.globals();
let timer_callbacks = global
.get::<_, rquickjs::Object>("_azleTimerCallbacks")
.unwrap();
let timer_callback: Function = timer_callbacks.get(callback_id.as_str()).unwrap();

let result =
quickjs_call_with_error_handling(ctx.clone(), timer_callback, ()).unwrap();
use ic_cdk_timers::{set_timer, TimerId};
use rquickjs::{Ctx, Function, Object, Result};
use slotmap::Key;

if result.is_exception() {
panic!("Timer callback threw an exception");
use crate::{error::quickjs_call_with_error_handling, ic::throw_error, quickjs_with_ctx};

pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(
ctx.clone(),
move |delay: String, callback_id: String| -> Result<u64> {
let delay: u64 = delay.parse().map_err(|e| throw_error(ctx.clone(), e))?;
let delay_duration = Duration::new(delay, 0);

let closure = move || {
let result = quickjs_with_ctx(|ctx| {
let globals = ctx.globals();

let timer_callbacks: Object =
globals.get("_azleTimerCallbacks").map_err(|e| {
format!("Failed to get globalThis._azleTimerCallbacks: {e}")
})?;
let timer_callback: Function =
timer_callbacks.get(callback_id.as_str()).map_err(|e| {
format!(
"Failed to get globalThis._azleTimerCallbacks[{callback_id}]: {e}"
)
})?;

quickjs_call_with_error_handling(ctx, timer_callback, ())?;

Ok(())
});

if let Err(e) = result {
ic_cdk::trap(&format!("Azle TimerError: {e}"));
}
};

Ok(())
})
.unwrap();
};

let timer_id: ic_cdk_timers::TimerId = ic_cdk_timers::set_timer(delay_duration, closure);
let timer_id_u64: u64 = timer_id.data().as_ffi();
let timer_id: TimerId = set_timer(delay_duration, closure);
let timer_id_u64: u64 = timer_id.data().as_ffi();

timer_id_u64
})
.unwrap()
Ok(timer_id_u64)
},
)
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
use rquickjs::{Ctx, Function};
use slotmap::Key;

use crate::{error::quickjs_call_with_error_handling, quickjs_with_ctx};

pub fn get_function(ctx: Ctx) -> Function {
Function::new(ctx, |interval: String, callback_id: String| {
let interval: u64 = interval.parse().unwrap();
let interval_duration = core::time::Duration::new(interval, 0);
use core::time::Duration;

let closure = move || {
quickjs_with_ctx(|ctx| {
let global = ctx.globals();
let timer_callbacks = global
.get::<_, rquickjs::Object>("_azleTimerCallbacks")
.unwrap();
let timer_callback: Function = timer_callbacks.get(callback_id.as_str()).unwrap();

let result =
quickjs_call_with_error_handling(ctx.clone(), timer_callback, ()).unwrap();
use ic_cdk_timers::{set_timer_interval, TimerId};
use rquickjs::{Ctx, Function, Object, Result};
use slotmap::Key;

if result.is_exception() {
panic!("Timer interval callback threw an exception");
use crate::{error::quickjs_call_with_error_handling, ic::throw_error, quickjs_with_ctx};

pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(
ctx.clone(),
move |interval: String, callback_id: String| -> Result<u64> {
let interval: u64 = interval.parse().map_err(|e| throw_error(ctx.clone(), e))?;
let interval_duration = Duration::new(interval, 0);

let closure = move || {
let result = quickjs_with_ctx(|ctx| {
let globals = ctx.globals();

let timer_callbacks: Object =
globals.get("_azleTimerCallbacks").map_err(|e| {
format!("Failed to get globalThis._azleTimerCallbacks: {e}")
})?;
let timer_callback: Function =
timer_callbacks.get(callback_id.as_str()).map_err(|e| {
format!(
"Failed to get globalThis._azleTimerCallbacks[{callback_id}]: {e}"
)
})?;

quickjs_call_with_error_handling(ctx, timer_callback, ())?;

Ok(())
});

if let Err(e) = result {
ic_cdk::trap(&format!("Azle TimerIntervalError: {e}"));
}
};

Ok(())
})
.unwrap();
};

let timer_id: ic_cdk_timers::TimerId =
ic_cdk_timers::set_timer_interval(interval_duration, closure);
let timer_id_u64: u64 = timer_id.data().as_ffi();
let timer_id: TimerId = set_timer_interval(interval_duration, closure);
let timer_id_u64: u64 = timer_id.data().as_ffi();

timer_id_u64
})
.unwrap()
Ok(timer_id_u64)
},
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::time;
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, || ic_cdk::api::time()).unwrap()
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, || time())
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use rquickjs::{Ctx, Function};
use ic_cdk::api::trap;
use rquickjs::{Ctx, Function, Result};

pub fn get_function(context: Ctx) -> Function {
Function::new(context, |message: String| {
ic_cdk::api::trap(&message);
})
.unwrap()
pub fn get_function(ctx: Ctx) -> Result<Function> {
Function::new(ctx, |message: String| trap(&message))
}

0 comments on commit 3277079

Please sign in to comment.