use crate::ids::WasmStoreGcMode;
use canic_core::cdk::structures::{DefaultMemoryImpl, cell::Cell, memory::VirtualMemory};
use canic_core::eager_static;
use canic_core::{
impl_storable_bounded, role_contract::allocation::memory::template::WASM_STORE_GC_STATE_ID,
};
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
eager_static! {
static WASM_STORE_GC_STATE: RefCell<
Cell<WasmStoreGcStateRecord, VirtualMemory<DefaultMemoryImpl>>
> = RefCell::new(Cell::init(
canic_core::ic_memory_key!("canic.control_plane.wasm_store_gc_state.v1", WasmStoreGcStateRecord, WASM_STORE_GC_STATE_ID),
WasmStoreGcStateRecord::default(),
));
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct WasmStoreGcStateRecord {
pub mode: WasmStoreGcMode,
pub changed_at: u64,
pub prepared_at: Option<u64>,
pub started_at: Option<u64>,
pub completed_at: Option<u64>,
pub runs_completed: u32,
}
impl_storable_bounded!(WasmStoreGcStateRecord, 64, true);
pub struct WasmStoreGcStateStore;
impl WasmStoreGcStateStore {
#[must_use]
pub fn get() -> WasmStoreGcStateRecord {
WASM_STORE_GC_STATE.with_borrow(|cell| cell.get().clone())
}
pub fn set(record: WasmStoreGcStateRecord) {
WASM_STORE_GC_STATE.with_borrow_mut(|cell| {
cell.set(record);
});
}
#[cfg(test)]
pub fn clear_for_test() {
Self::set(WasmStoreGcStateRecord::default());
}
}