-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Counter
Mark Nadal edited this page Jun 18, 2018
·
1 revision
Implementing other CRDTs on top of GUN's CRDT can be done in just 12 lines of code, and automatically synchronizes in realtime across a P2P mesh network:
Gun.chain.count = function (num) {
if (typeof num === 'number') {
this.set(num);
}
if (typeof num === 'function') {
var sum = 0;
this.map().once(function (val) {
num(sum += val);
});
}
return this;
};
Example usage:
var db = gun.get('count')
db.count(+5)
db.count(-8)
db.count(function (value) {
console.log(value) // 5, -3
})
// prints: 5
// prints: -3
db.count(+10)
// prints: 7