fix: call onError for errors during applyOperation

This commit is contained in:
Eric Maciel 2020-10-30 10:34:19 -04:00
parent 2917c98010
commit c3425096e2
3 changed files with 28 additions and 24 deletions

View File

@ -57,29 +57,25 @@ export const AutomergeEditor = {
operations: Operation[],
cursorData?: CursorData
) => {
try {
const doc = e.docSet.getDoc(docId)
const doc = e.docSet.getDoc(docId)
if (!doc) {
throw new TypeError(`Unknown docId: ${docId}!`)
}
let changed
for await (let op of operations) {
changed = Automerge.change<SyncDoc>(changed || doc, d =>
applyOperation(d.children, op)
)
}
changed = Automerge.change(changed || doc, d => {
setCursor(e.clientId, e.selection, d, operations, cursorData || {})
})
e.docSet.setDoc(docId, changed as any)
} catch (e) {
console.error(e)
if (!doc) {
throw new TypeError(`Unknown docId: ${docId}!`)
}
let changed
for await (let op of operations) {
changed = Automerge.change<SyncDoc>(changed || doc, d =>
applyOperation(d.children, op)
)
}
changed = Automerge.change(changed || doc, d => {
setCursor(e.clientId, e.selection, d, operations, cursorData || {})
})
e.docSet.setDoc(docId, changed as any)
},
/**

View File

@ -10,6 +10,7 @@ export interface AutomergeOptions {
docId: string
cursorData?: CursorData
preserveExternalHistory?: boolean
onError?: (msg: string | Error) => void
}
/**
@ -24,7 +25,12 @@ const withAutomerge = <T extends Editor>(
const { onChange } = e
const { docId, cursorData, preserveExternalHistory } = options || {}
const {
docId,
cursorData,
preserveExternalHistory,
onError = (err: string | Error) => console.log('AutomergeEditor error', err)
} = options || {}
e.docSet = new Automerge.DocSet()
@ -77,7 +83,9 @@ const withAutomerge = <T extends Editor>(
const operations: any = e.operations
if (!e.isRemote) {
AutomergeEditor.applySlateOps(e, docId, operations, cursorData)
AutomergeEditor.applySlateOps(e, docId, operations, cursorData).catch(
onError
)
onChange()
}

View File

@ -12,7 +12,7 @@ export interface SocketIOPluginOptions {
onConnect?: () => void
onDisconnect?: () => void
onError?: (msg: string) => void
onError?: (msg: string | Error) => void
}
export interface WithSocketIOEditor {