How to use automerge - 10 common examples

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 ahixon / slate-sync-bridge / test / slate / automerge.js View on Github external
fixtures(__dirname, 'operations', ({ module }) => {
      const { input, output } = module
      const operations = module.default

      const inputSyncDoc = toSyncDocument(input.document.toJSON())
      const initAutomergeDoc = Automerge.init();
      const emptyAutomergeDoc = Automerge.change(initAutomergeDoc, automergeDoc => {
        automergeDoc.doc = inputSyncDoc;
      });

      const changedAutomergeDoc = Automerge.change(emptyAutomergeDoc, automergeDoc => {
        operations.forEach(op => applyOperation(automergeDoc.doc, op));
      })

      // check our rep matched
      const expectedDoc = output.document.toJSON();
      const actualDoc = toSlateDocument(changedAutomergeDoc.doc);
      assert.deepEqual(actualDoc, expectedDoc);
    })
github ahixon / slate-sync-bridge / test / slate-diff.jsx View on Github external
)

    const { transformer, loadedDoc } = setup(input);

    // insert text into the second nested paragraph
    const op = {
      type: "insert_text",
      path: [0, 1, 0],
      offset: 5,
      text: 'h',
    }

    // load the cache
    // transformer.apply(loadedDoc.doc);

    const changedDoc = Automerge.change(loadedDoc, doc => applyOperation(doc.doc, op))
    const changes = Automerge.getChanges(loadedDoc, changedDoc);

    const transformed = transformer.apply(changedDoc.doc, changes);
    expect(transformed.toJSON()).toEqual(output.toJSON());
  })
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 inkandswitch / pushpin / src / renderer / components / content-types / TextContent.tsx View on Github external
handle.change((doc) => {
    doc.text = new Automerge.Text()
    if (text) {
      doc.text.insertAt!(0, ...text.split(''))

      if (!text || !text.endsWith('\n')) {
        doc.text.insertAt!(text ? text.length : 0, '\n') // Quill prefers an ending newline
      }
    }
  })
}
github humandx / slate-automerge / src / example / server / index.js View on Github external
socket.on('join_document', function({clientId, docId}, callback) {
    // Permissions check
    if (!hasPermission(clientId, docId)) { return }

    docId  = Number(docId)
    if (!docSet.getDoc(docId)) {
      createNewDocument(docId)
    }

    if (!connections[clientId]) {
      connections[clientId] = new Automerge.Connection(
          docSet,
          (message) => {
              if (!hasPermission(clientId, message.docId)) { return }
              socket.emit("send_operation", message)
          }
      )
      connections[clientId].open()
    }

    socket.join(docId)
    callback(true)
  });
github bkniffler / debe / src / delta / index.ts View on Github external
}
        const {
          rev,
          id,
          // rem,
          [mergeField]: automerge,
          [actorField]: actor,
          ...item
        } = i;
        const doc = automerge ? Automerge.load(automerge) : Automerge.init();
        const newDoc = Automerge.change(doc, message, (change: any) => {
          for (var key in item) {
            change[key] = item[key];
          }
        });
        const changes = Automerge.getChanges(doc, newDoc);
        const latestActor = changes.length
          ? changes[changes.length - 1].actor
          : actor;
        delta.push([id, changes, rev]);
        return {
          ...item,
          [actorField]: latestActor,
          id,
          // rem,
          rev,
          [mergeField]: Automerge.save(newDoc)
        };
      });
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 bkniffler / debe / src / delta / merge.ts View on Github external
return;
      }
      let docOld = undefined;
      const isUpdate = map[id + ''] && map[id + ''].merge;
      if (isUpdate) {
        docOld = Automerge.load(map[id + ''].merge);
        updated.push(id);
      } else {
        docOld = Automerge.init();
      }
      const docInt =
        typeof changes === 'string' ? Automerge.load(changes) : undefined;

      const docNew = docInt
        ? Automerge.merge(docOld, docInt)
        : Automerge.applyChanges(docOld, changes);
      if (Object.keys(Automerge.getMissingDeps(docNew)).length) {
        unsuccessful.push(id);
        return;
      }

      /* if (!history.length) {
      return {
        ...snapshot,
        actor: undefined,
        id,
        merge: Automerge.save(docNew)
      };
    }*/
      const history = Automerge.getHistory(docNew);
      const { change, snapshot } = history[history.length - 1];
      return {
github bkniffler / debe / src / delta / merge.ts View on Github external
.map(([id, changes]) => {
      if (!changes) {
        return;
      }
      let docOld = undefined;
      const isUpdate = map[id + ''] && map[id + ''].merge;
      if (isUpdate) {
        docOld = Automerge.load(map[id + ''].merge);
        updated.push(id);
      } else {
        docOld = Automerge.init();
      }
      const docInt =
        typeof changes === 'string' ? Automerge.load(changes) : undefined;

      const docNew = docInt
        ? Automerge.merge(docOld, docInt)
        : Automerge.applyChanges(docOld, changes);
      if (Object.keys(Automerge.getMissingDeps(docNew)).length) {
        unsuccessful.push(id);
        return;
      }

      /* if (!history.length) {
      return {
        ...snapshot,
        actor: undefined,
        id,
        merge: Automerge.save(docNew)
      };
github bkniffler / debe / src / delta / merge.ts View on Github external
.map(([id, changes]) => {
      if (!changes) {
        return;
      }
      let docOld = undefined;
      const isUpdate = map[id + ''] && map[id + ''].merge;
      if (isUpdate) {
        docOld = Automerge.load(map[id + ''].merge);
        updated.push(id);
      } else {
        docOld = Automerge.init();
      }
      const docInt =
        typeof changes === 'string' ? Automerge.load(changes) : undefined;

      const docNew = docInt
        ? Automerge.merge(docOld, docInt)
        : Automerge.applyChanges(docOld, changes);
      if (Object.keys(Automerge.getMissingDeps(docNew)).length) {
        unsuccessful.push(id);
        return;
      }

      /* if (!history.length) {