Skip to content

🕳️🥊🐦‍⬛ CRDT on Yjs shared via hyperswarmRouter and stored using standard Yjs storage adaptors.

Notifications You must be signed in to change notification settings

benzmuircroft/hyperswarmCRDT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 

Repository files navigation

🕳️🥊hyperswarmCRDT 🐦‍⬛

CRDT for y-map and y-array on Yjs shared via hyperswarmRouter and stored using standard y-leveldb storage adaptor.

TODO

  • WIP: allow a leader with the optional observerFunction to also broadcast task-at-hand

Installation

npm install "github:benzmuircroft/hyperswarmCRDT"

Usage

// First peer
(async () => {
  const router = await require('hyperswarmRouter')('c915296031bf40b58ef7f1d6b883512e799c1982b83acdc7ce27a2079a8c196f'); // any 64 hex
  const crdt = await require('hyperswarmCRDT')({
    network: router,
    join: 'same-room-as-each-other-with-other-peers',
    // leveldb: './leveldb', // using RAM instead
    observerFunction: function(output) {
      console.log(output);
    }
  });
  console.log('... waiting');
  // will print output from the observer function
  // "myDoc {}"
  // "myDoc { myKey: 'myValue' }"
  // (note: this user can access all methods just as the second user below ...)
})();
// Second peer
(async () => {
  const router = await require('hyperswarmRouter')('c915296031bf40b58ef7f1d6b883512e799c1982b83acdc7ce27a2079a8c196f'); // any 64 hex
  const crdt = await require('hyperswarmCRDT')({
    network: router,
    leveldb: './leveldb', // using persistance (name the folder what you like)
    join: 'same-room-as-each-other-with-other-peers'
  });
  console.log('... ready to share');
  console.log('0 crdt:', crdt); // 0 crdt: {}
  crdt.map('myDoc');
  console.log('1 crdt:', crdt); // 1 crdt: { myDoc: {} }
  console.log('shared:', crdt.myDoc ? crdt.myDoc : 'nothing yet');
  await crdt.set('myDoc', 'myKey', 'myValue');
  console.log('2 crdt:', crdt); // 2 crdt: { myDoc: { myKey: 'MyVal' } }
})();

API

crdt.map('myMap'); // create a map

await crdt.set('myMap', 'myKey', 'myValue'); // add or update a key

await crdt.del('myMap', 'myKey'); // delete a key

await crdt.array('myArray'); // creates a array

await crdt.insert('myArray', 1, ['a', 'b', 'c']); // inserts content at index

await crdt.push('myArray', 'd'); // push value

await crdt.unshift('myArray', 'd'); // unshift value

await crdt.cut('myArray', 1); // remove an index

await crdt.set('myMap', 'myArray', ['v', 'a', 'l', 'u', 'e', 's'], 'push'); // set a new or existing yarray inside an ymap via push

console.log(crdt); // will print the whole shared object

console.log(crdt.myMap); // prints a map 

console.log(crdt.myMap.myKey); // prints the value of a map's key

console.log(crdt.myMap.myArray[0]); // prints v

About

🕳️🥊🐦‍⬛ CRDT on Yjs shared via hyperswarmRouter and stored using standard Yjs storage adaptors.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published