3.0.30 • Published 6 years ago

ditto-crdt-lib v3.0.30

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

Ditto-CRDT-lib

Sync Docs with Ditto! Contains TypeScript support as well!

TypeScript

Install in NodeJS

  • We recommend that you have Node 10.x
  • We recommend that you have Npm 6.x

npm install ditto-crdt-lib -S or yarn ditto-crdt-lib

const { Doc } = require('ditto-crdt-lib');

const newDoc = new Doc({
	name: 'Max',
	friend: 'Adam',
	age: 45
})

Install for Webpack for Browser Consumption

  • You will need Webpack 4.0.11 or higher

Important!

In the main entry point of your webpack application, you can only import ditto-crdt-lib with a dynamic import. This is an asynchronous module.

const { Doc } = await import('ditto-crdt-lib')
const newDoc = new Doc({
  name: 'max'
})

or

const lib = import('ditto-crdt-lib')
lib.then((l) => {
  const Doc = l.Doc; 
  const newDoc = new Doc({
    name: 'max'
  })
  // then do stuff
})

Usage

Creating a Doc from arbitray JSON

const newDoc = new Doc({
	name: 'Max',
	friend: 'Adam',
	age: 45
})
console.log(newDoc.data);
// { name: 'Max', friend: 'Adam', age: 45 }

Creating a Doc from State

const newDoc = new Doc({
  name: 'Max',
  friend: 'Adam',
  age: 45
})
// this is a static method that takes in 
// first param: state
// second param: siteId
const anotherDoc = Doc.fromState(newDoc.state, 2)

Syncing via Ops

const doc1 = new Doc({
  name: 'Max',
  age: 56
})
// let's make doc2 from a clone of doc1 state with a siteId 2 
const doc2 = Doc.fromState(doc1.state, 2);

// at this point doc1 and doc2 are the same in terms of state
// doc1 will have a siteId of 1 (that is the default value)
// doc2 will have a siteId of 2 


// lets change some data from doc1
// doc2 is in the old state
const enqueuedOpsFromDoc1 = await doc1.write((d) => {
  d.age = 99
  d.name = "Maximilian"
})

// let's send these ops to doc2;
// THESE OPS MUST BE DELIVERED IN ORDER
for (let remoteOp of enqueuedOpsFromDoc1) {
  doc2.executeOp(remoteOp) 
}

//Behold! they are now in sync
console.log(doc1.data)
console.log(doc2.data)

Syncing State

When you don't feel like syncing ops you can sync the entire state. This can be expensive but it might be cheaper if you had a ton of ops.

const doc1 = new Doc({
  name: 'Max',
  age: 56
})
// let's make doc2 from a clone of doc1 state with a siteId 2 
const doc2 = Doc.fromState(doc1.state, 2);
// at this point doc1 and doc2 are the same in terms of state
// doc1 will have a siteId of 1 (that is the default value)
// doc2 will have a siteId of 2 


// lets change some data from doc1, but we don't care about the ops
// doc2 is in the old state
await doc1.write((d) => {
  d.age = 99
  d.name = "Maximilian"
})

// call doc2 to merge doc1 state
doc2.merge(doc1.state)


//Behold! they are now in sync
console.log(doc1.data)
console.log(doc2.data)
3.0.30

6 years ago

3.0.28

6 years ago

3.0.27

6 years ago

3.0.26

6 years ago

3.0.25

6 years ago

2.0.0

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.111

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago