How to use the automerge.DocSet function in automerge

To help you get started, we’ve selected a few automerge examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github automerge / automerge-net / server.js View on Github external
const Automerge = require('automerge')
const Connection = require('./connection')
const net = require('net')
const HOST = '0.0.0.0'
const PORT = 9876

// Initialise an example document
const docSet = new Automerge.DocSet()
const initDoc = Automerge.change(Automerge.init(), doc => doc.hello = 'Hi!')
docSet.setDoc('example', initDoc)

// Print out the document whenever it changes
docSet.registerHandler((docId, doc) => {
  console.log(`[${docId}] ${JSON.stringify(doc)}`)
})

// Make a change to the document every 3 seconds
setInterval(() => {
  let doc = docSet.getDoc('example')
  doc = Automerge.change(doc, doc => {
    doc.serverNum = (doc.serverNum || 0) + 1
  })
  docSet.setDoc('example', doc)
}, 3000)
github automerge / automerge-net / client.js View on Github external
const Automerge = require('automerge')
const Connection = require('./connection')
const net = require('net')
const HOST = '127.0.0.1'
const PORT = 9876
const docSet = new Automerge.DocSet()

// Print out the document whenever it changes
docSet.registerHandler((docId, doc) => {
  console.log(`[${docId}] ${JSON.stringify(doc)}`)
})

// Make a change to the document every 3 seconds
setInterval(() => {
  let doc = docSet.getDoc('example')
  if (doc) {
    doc = Automerge.change(doc, doc => {
      doc.clientNum = (doc.clientNum || 0) + 1
    })
    docSet.setDoc('example', doc)
  }
}, 3000)
github automerge / mpl / src / mpl / store.js View on Github external
constructor(reducer, network) {
    this.reducer   = reducer
    this.listeners = []
    this.state     = this.newDocument()
    this.docSet    = new Automerge.DocSet()
    this.docSet.setDoc(this.state.docId, this.state)

    this.docSet.registerHandler((docId, doc) => {
      if (docId === this.state.docId && doc !== this.state) {
        this.state = doc
        this.listeners.forEach((listener) => listener())
      }
    })

    this.network = network || new Network(this.docSet)
    this.network.connect({
      // we use our automerge session ID as the peer id,
      // but we probably want to use the network ID for the document actorIds
      name: Config.name,
      peerId: this.state._state.get("actorId")
    })
github humandx / slate-automerge / src / example / server / index.js View on Github external
const SlateAutomergeBridge = require("../../../dist/slateAutomergeBridge")

const { slateCustomToJson } = SlateAutomergeBridge
const Value = Slate.Value

const createNewDocument = function(docId) {
  let doc = Automerge.init(`server-1234`);
  const initialSlateValue = Value.fromJSON(initialValue);
  doc = Automerge.change(doc, "Initialize Slate state", doc => {
      doc.note = slateCustomToJson(initialSlateValue.document);
  })
  docSet.setDoc(docId, doc);
}

let connections = {};
let docSet = new Automerge.DocSet();
createNewDocument(1)

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/index.html');
});


/**
 * @function hasPermission
 * @desc Check to see if the client has permission to view a document.
 *    Current check just makes sure clients with odd ids can only read
 *    documents with odd ids.
 * @param {number} clientId - The ID of the client requesting permission.
 * @param {number} docId - The ID of the document to join.
 */
const hasPermission = (clientId, docId) => {
github aslakhellesoy / automerge-codemirror / stories / index.stories.tsx View on Github external
function createConnectedDocs(count: number, id: string): WatchableDoc[] {
  const docSets: DocSet[] = []
  for (let i = 0; i < count; i++) {
    const docSet = new DocSet()
    docSets.push(docSet)

    if (i > 0) {
      let connectionA: Connection
      let connectionB: Connection

      connectionA = new Connection(docSet, msg => connectionB.receiveMsg(msg))
      connectionB = new Connection(docSets[i - 1], msg =>
        connectionA.receiveMsg(msg)
      )

      connectionA.open()
      connectionB.open()
    }
  }
  return docSets.map(docSet => new DocSetWatchableDoc(docSet, id))
github cudr / slate-collaborative / packages / client / src / Connection.ts View on Github external
onDisconnect,
    cursorAnnotationType,
    annotationDataMixin
  }: ConnectionModel) {
    this.url = url
    this.editor = editor
    this.connectOpts = connectOpts
    this.cursorAnnotationType = cursorAnnotationType
    this.annotationDataMixin = annotationDataMixin

    this.onConnect = onConnect
    this.onDisconnect = onDisconnect

    this.docId = connectOpts.path || new URL(url).pathname

    this.docSet = new Automerge.DocSet()

    this.connect()

    return this
  }
github aslakhellesoy / automerge-codemirror / example / src / app.js View on Github external
const Automerge = require('automerge')
const CodeMirror = require('codemirror')
const automergeCodeMirror = require('../../index')

const docId = 'doc-id'

let leftConnection
let rightConnection

const leftDocSet = new Automerge.DocSet()
leftConnection = new Automerge.Connection(leftDocSet, msg =>
  rightConnection.receiveMsg(msg)
)
leftConnection.open()

const rightDocSet = new Automerge.DocSet()
rightConnection = new Automerge.Connection(rightDocSet, msg =>
  leftConnection.receiveMsg(msg)
)
rightConnection.open()

function createCodeMirror(docSet, domId) {
  const $e = document.getElementById(domId)
  const codeMirror = CodeMirror($e)
  const getDocText = doc => doc.text
  const updateDoc = doc => docSet.setDoc(docId, doc)
github humandx / slate-automerge / src / example / socket / client.js View on Github external
constructor(props) {
        super(props)

        this.clientId = `client:${this.props.clientId}-${uuid()}`
        this.docSet = new Automerge.DocSet()
        this.onChange = this.onChange.bind(this)
        this.socket = null

        this.connection = new Automerge.Connection(
            this.docSet,
            (msg) => {
                this.sendMessage(msg)
            }
        )

        this.state = {
            value: null,
            online: true,
            docId: this.props.initialDocId,
        }
    }
github humandx / slate-automerge / src / example / App.js View on Github external
constructor(props) {
        super(props)

        this.clients = [];
        this.connections = [];
        this.docSet = new Automerge.DocSet();
        this.docSet.setDoc(docId, doc);
        this.sendMessage = this.sendMessage.bind(this);

        this.state = {
            numClients: 1,
            debuggingMode: false,
        }
    }
github cudr / slate-collaborative / packages / backend / src / Connection.ts View on Github external
constructor(options: ConnectionOptions = defaultOptions) {
    this.io = io(options.entry, options.connectOpts)
    this.docSet = new Automerge.DocSet()
    this.connections = {}
    this.options = merge(defaultOptions, options)

    this.configure()

    return this
  }