diff --git a/src/commonMain/kotlin/client/PNCounter.kt b/src/commonMain/kotlin/client/PNCounter.kt new file mode 100644 index 0000000000000000000000000000000000000000..f0df00949b2c08a7c882fd7ce2328c97e76242ce --- /dev/null +++ b/src/commonMain/kotlin/client/PNCounter.kt @@ -0,0 +1,30 @@ +package client + +import client.utils.CObjectUId +import crdtlib.crdt.PNCounter + +class PNCounter(val oid: CObjectUId<PNCounter>, val readOnly: Boolean) : + CObject<PNCounter>(oid, readOnly) { + + private val counterCrdt: PNCounter + get() = this.crdt as PNCounter + + fun get(): Int { + this.beforeGetter() + val value = this.counterCrdt.get() + this.afterGetter() + return value + } + + fun increment(amount: Int) { + val ts = this.beforeUpdate() + this.counterCrdt.increment(amount, ts) + this.afterUpdate() + } + + fun decrement(amount: Int) { + val ts = this.beforeUpdate() + this.counterCrdt.decrement(amount, ts) + this.afterUpdate() + } +}