Skip to content

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

!!! Attention !!!

Please read the docs at gun.eco/docs !

This wiki is only for editing to improve the docs.

Click on the "> Pages" button above to find the page to edit.

Or add a page with "New Page" button.

!!! Attention !!!

Clone this wiki locally
You can’t perform that action at this time.