use wasm_bindgen::prelude::*;
use crate::Crdt;
#[wasm_bindgen(js_name = GCounter)]
pub struct WasmGCounter {
inner: crate::GCounter,
}
#[wasm_bindgen(js_class = GCounter)]
impl WasmGCounter {
#[wasm_bindgen(constructor)]
pub fn new(actor: u64) -> Self {
Self {
inner: crate::GCounter::new(actor),
}
}
pub fn increment(&mut self) {
self.inner.increment();
}
#[wasm_bindgen(js_name = incrementBy)]
pub fn increment_by(&mut self, n: u64) {
self.inner.increment_by(n);
}
pub fn value(&self) -> u64 {
self.inner.value()
}
pub fn merge(&mut self, other: &WasmGCounter) {
self.inner.merge(&other.inner);
}
}
#[wasm_bindgen(js_name = PNCounter)]
pub struct WasmPNCounter {
inner: crate::PNCounter,
}
#[wasm_bindgen(js_class = PNCounter)]
impl WasmPNCounter {
#[wasm_bindgen(constructor)]
pub fn new(actor: u64) -> Self {
Self {
inner: crate::PNCounter::new(actor),
}
}
pub fn increment(&mut self) {
self.inner.increment();
}
pub fn decrement(&mut self) {
self.inner.decrement();
}
pub fn value(&self) -> i64 {
self.inner.value()
}
pub fn merge(&mut self, other: &WasmPNCounter) {
self.inner.merge(&other.inner);
}
}
#[wasm_bindgen(js_name = LWWRegister)]
pub struct WasmLWWRegister {
inner: crate::LWWRegister<String>,
clock: crate::clock::HybridClock,
}
#[wasm_bindgen(js_class = LWWRegister)]
impl WasmLWWRegister {
#[wasm_bindgen(constructor)]
pub fn new(node_id: u16, value: &str) -> Self {
let mut clock = crate::clock::HybridClock::new(node_id as u64);
let inner = crate::LWWRegister::new(value.to_string(), &mut clock);
Self { inner, clock }
}
pub fn set(&mut self, value: &str) {
self.inner.set(value.to_string(), &mut self.clock);
}
pub fn value(&self) -> String {
self.inner.value().clone()
}
pub fn merge(&mut self, other: &WasmLWWRegister) {
self.inner.merge(&other.inner);
}
}
#[wasm_bindgen(js_name = GSet)]
pub struct WasmGSet {
inner: crate::GSet<String>,
}
impl Default for WasmGSet {
fn default() -> Self {
Self::new()
}
}
#[wasm_bindgen(js_class = GSet)]
impl WasmGSet {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
inner: crate::GSet::new(),
}
}
pub fn insert(&mut self, value: &str) -> bool {
self.inner.insert(value.to_string())
}
pub fn contains(&self, value: &str) -> bool {
self.inner.contains(&value.to_string())
}
pub fn len(&self) -> usize {
self.inner.len()
}
#[wasm_bindgen(js_name = isEmpty)]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn merge(&mut self, other: &WasmGSet) {
self.inner.merge(&other.inner);
}
#[wasm_bindgen(js_name = toArray)]
pub fn to_array(&self) -> Box<[JsValue]> {
self.inner
.iter()
.map(|s| JsValue::from_str(s))
.collect::<Vec<_>>()
.into_boxed_slice()
}
}
#[wasm_bindgen(js_name = ORSet)]
pub struct WasmORSet {
inner: crate::ORSet<String>,
}
#[wasm_bindgen(js_class = ORSet)]
impl WasmORSet {
#[wasm_bindgen(constructor)]
pub fn new(actor: u64) -> Self {
Self {
inner: crate::ORSet::new(actor),
}
}
pub fn insert(&mut self, value: &str) {
self.inner.insert(value.to_string());
}
pub fn remove(&mut self, value: &str) -> bool {
self.inner.remove(&value.to_string())
}
pub fn contains(&self, value: &str) -> bool {
self.inner.contains(&value.to_string())
}
pub fn len(&self) -> usize {
self.inner.len()
}
#[wasm_bindgen(js_name = isEmpty)]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn merge(&mut self, other: &WasmORSet) {
self.inner.merge(&other.inner);
}
#[wasm_bindgen(js_name = toArray)]
pub fn to_array(&self) -> Box<[JsValue]> {
self.inner
.iter()
.map(|s| JsValue::from_str(s))
.collect::<Vec<_>>()
.into_boxed_slice()
}
}
#[wasm_bindgen(js_name = TextCrdt)]
pub struct WasmTextCrdt {
inner: crate::TextCrdt,
}
#[wasm_bindgen(js_class = TextCrdt)]
impl WasmTextCrdt {
#[wasm_bindgen(constructor)]
pub fn new(actor: u64) -> Self {
Self {
inner: crate::TextCrdt::new(actor),
}
}
pub fn insert(&mut self, pos: usize, ch: char) {
let _ = self.inner.insert(pos, ch);
}
#[wasm_bindgen(js_name = insertStr)]
pub fn insert_str(&mut self, pos: usize, text: &str) {
let _ = self.inner.insert_str(pos, text);
}
pub fn remove(&mut self, pos: usize) {
let _ = self.inner.remove(pos);
}
#[wasm_bindgen(js_name = toString)]
pub fn to_string_js(&self) -> String {
use alloc::string::ToString;
self.inner.to_string()
}
pub fn len(&self) -> usize {
self.inner.len()
}
#[wasm_bindgen(js_name = isEmpty)]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn merge(&mut self, other: &WasmTextCrdt) {
self.inner.merge(&other.inner);
}
}