2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-undef,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS205: Consider reworking code to avoid use of IIFEs
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
// API for JSON OT
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
let json
|
|
|
|
if (typeof WEB === 'undefined') {
|
|
|
|
json = require('./json')
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
if (typeof WEB !== 'undefined' && WEB !== null) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const { extendDoc } = exports
|
|
|
|
exports.extendDoc = function (name, fn) {
|
|
|
|
SubDoc.prototype[name] = fn
|
|
|
|
return extendDoc(name, fn)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const depath = function (path) {
|
|
|
|
if (path.length === 1 && path[0].constructor === Array) {
|
|
|
|
return path[0]
|
|
|
|
} else {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
class SubDoc {
|
|
|
|
constructor(doc, path) {
|
2020-05-06 06:09:33 -04:00
|
|
|
this.doc = doc
|
|
|
|
this.path = path
|
|
|
|
}
|
|
|
|
|
|
|
|
at(...path) {
|
|
|
|
return this.doc.at(this.path.concat(depath(path)))
|
|
|
|
}
|
|
|
|
|
|
|
|
get() {
|
|
|
|
return this.doc.getAt(this.path)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:15 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// for objects and lists
|
2020-05-06 06:09:33 -04:00
|
|
|
set(value, cb) {
|
|
|
|
return this.doc.setAt(this.path, value, cb)
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// for strings and lists.
|
2020-05-06 06:09:33 -04:00
|
|
|
insert(pos, value, cb) {
|
|
|
|
return this.doc.insertAt(this.path, pos, value, cb)
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// for strings
|
2020-05-06 06:09:33 -04:00
|
|
|
del(pos, length, cb) {
|
|
|
|
return this.doc.deleteTextAt(this.path, length, pos, cb)
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// for objects and lists
|
2020-05-06 06:09:33 -04:00
|
|
|
remove(cb) {
|
|
|
|
return this.doc.removeAt(this.path, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
push(value, cb) {
|
|
|
|
return this.insert(this.get().length, value, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
move(from, to, cb) {
|
|
|
|
return this.doc.moveAt(this.path, from, to, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
add(amount, cb) {
|
|
|
|
return this.doc.addAt(this.path, amount, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
on(event, cb) {
|
|
|
|
return this.doc.addListener(this.path, event, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeListener(l) {
|
|
|
|
return this.doc.removeListener(l)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
// text API compatibility
|
2020-05-06 06:09:33 -04:00
|
|
|
getLength() {
|
|
|
|
return this.get().length
|
|
|
|
}
|
|
|
|
|
|
|
|
getText() {
|
|
|
|
return this.get()
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const traverse = function (snapshot, path) {
|
|
|
|
const container = { data: snapshot }
|
|
|
|
let key = 'data'
|
|
|
|
let elem = container
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const p of Array.from(path)) {
|
2020-05-06 06:09:33 -04:00
|
|
|
elem = elem[key]
|
|
|
|
key = p
|
|
|
|
if (typeof elem === 'undefined') {
|
|
|
|
throw new Error('bad path')
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return { elem, key }
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const pathEquals = function (p1, p2) {
|
|
|
|
if (p1.length !== p2.length) {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
for (let i = 0; i < p1.length; i++) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const e = p1[i]
|
|
|
|
if (e !== p2[i]) {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return true
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
json.api = {
|
2020-05-06 06:09:33 -04:00
|
|
|
provides: { json: true },
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
at(...path) {
|
|
|
|
return new SubDoc(this, depath(path))
|
|
|
|
},
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
get() {
|
|
|
|
return this.snapshot
|
|
|
|
},
|
|
|
|
set(value, cb) {
|
|
|
|
return this.setAt([], value, cb)
|
|
|
|
},
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
getAt(path) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const { elem, key } = traverse(this.snapshot, path)
|
|
|
|
return elem[key]
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setAt(path, value, cb) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const { elem, key } = traverse(this.snapshot, path)
|
|
|
|
const op = { p: path }
|
2020-05-06 06:08:21 -04:00
|
|
|
if (elem.constructor === Array) {
|
2020-05-06 06:09:33 -04:00
|
|
|
op.li = value
|
|
|
|
if (typeof elem[key] !== 'undefined') {
|
|
|
|
op.ld = elem[key]
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (typeof elem === 'object') {
|
2020-05-06 06:09:33 -04:00
|
|
|
op.oi = value
|
|
|
|
if (typeof elem[key] !== 'undefined') {
|
|
|
|
op.od = elem[key]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('bad path')
|
|
|
|
}
|
|
|
|
return this.submitOp([op], cb)
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
removeAt(path, cb) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const { elem, key } = traverse(this.snapshot, path)
|
|
|
|
if (typeof elem[key] === 'undefined') {
|
|
|
|
throw new Error('no element at that path')
|
|
|
|
}
|
|
|
|
const op = { p: path }
|
2020-05-06 06:08:21 -04:00
|
|
|
if (elem.constructor === Array) {
|
2020-05-06 06:09:33 -04:00
|
|
|
op.ld = elem[key]
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (typeof elem === 'object') {
|
2020-05-06 06:09:33 -04:00
|
|
|
op.od = elem[key]
|
|
|
|
} else {
|
|
|
|
throw new Error('bad path')
|
|
|
|
}
|
|
|
|
return this.submitOp([op], cb)
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
insertAt(path, pos, value, cb) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const { elem, key } = traverse(this.snapshot, path)
|
|
|
|
const op = { p: path.concat(pos) }
|
2020-05-06 06:08:21 -04:00
|
|
|
if (elem[key].constructor === Array) {
|
2020-05-06 06:09:33 -04:00
|
|
|
op.li = value
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (typeof elem[key] === 'string') {
|
2020-05-06 06:09:33 -04:00
|
|
|
op.si = value
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return this.submitOp([op], cb)
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
moveAt(path, from, to, cb) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const op = [{ p: path.concat(from), lm: to }]
|
|
|
|
return this.submitOp(op, cb)
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
addAt(path, amount, cb) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const op = [{ p: path, na: amount }]
|
|
|
|
return this.submitOp(op, cb)
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteTextAt(path, length, pos, cb) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const { elem, key } = traverse(this.snapshot, path)
|
|
|
|
const op = [{ p: path.concat(pos), sd: elem[key].slice(pos, pos + length) }]
|
|
|
|
return this.submitOp(op, cb)
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
addListener(path, event, cb) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const l = { path, event, cb }
|
|
|
|
this._listeners.push(l)
|
|
|
|
return l
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
removeListener(l) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const i = this._listeners.indexOf(l)
|
|
|
|
if (i < 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
this._listeners.splice(i, 1)
|
|
|
|
return true
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
|
|
|
_register() {
|
2020-05-06 06:09:33 -04:00
|
|
|
this._listeners = []
|
|
|
|
this.on('change', function (op) {
|
2020-05-06 06:08:21 -04:00
|
|
|
return (() => {
|
2020-05-06 06:09:33 -04:00
|
|
|
const result = []
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const c of Array.from(op)) {
|
2021-10-26 04:08:56 -04:00
|
|
|
let i
|
2020-05-06 06:09:33 -04:00
|
|
|
if (c.na !== undefined || c.si !== undefined || c.sd !== undefined) {
|
2020-05-06 06:08:21 -04:00
|
|
|
// no change to structure
|
2020-05-06 06:09:33 -04:00
|
|
|
continue
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
const toRemove = []
|
2020-05-06 06:08:21 -04:00
|
|
|
for (i = 0; i < this._listeners.length; i++) {
|
|
|
|
// Transform a dummy op by the incoming op to work out what
|
|
|
|
// should happen to the listener.
|
2020-05-06 06:09:33 -04:00
|
|
|
const l = this._listeners[i]
|
|
|
|
const dummy = { p: l.path, na: 0 }
|
|
|
|
const xformed = this.type.transformComponent([], dummy, c, 'left')
|
2020-05-06 06:08:21 -04:00
|
|
|
if (xformed.length === 0) {
|
|
|
|
// The op was transformed to noop, so we should delete the listener.
|
2023-03-21 08:06:13 -04:00
|
|
|
toRemove.push(i)
|
2020-05-06 06:08:21 -04:00
|
|
|
} else if (xformed.length === 1) {
|
|
|
|
// The op remained, so grab its new path into the listener.
|
2020-05-06 06:09:33 -04:00
|
|
|
l.path = xformed[0].p
|
2020-05-06 06:08:21 -04:00
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
throw new Error(
|
|
|
|
"Bad assumption in json-api: xforming an 'si' op will always result in 0 or 1 components."
|
|
|
|
)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
toRemove.sort((a, b) => b - a)
|
2020-05-06 06:09:33 -04:00
|
|
|
result.push(
|
|
|
|
(() => {
|
|
|
|
const result1 = []
|
2023-03-21 08:06:13 -04:00
|
|
|
for (i of Array.from(toRemove)) {
|
2020-05-06 06:09:33 -04:00
|
|
|
result1.push(this._listeners.splice(i, 1))
|
|
|
|
}
|
|
|
|
return result1
|
|
|
|
})()
|
|
|
|
)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return result
|
|
|
|
})()
|
|
|
|
})
|
|
|
|
return this.on('remoteop', function (op) {
|
2020-05-06 06:08:21 -04:00
|
|
|
return (() => {
|
2020-05-06 06:09:33 -04:00
|
|
|
const result = []
|
2021-10-26 04:08:56 -04:00
|
|
|
for (const c of Array.from(op)) {
|
2023-03-21 08:06:13 -04:00
|
|
|
const matchPath =
|
2020-05-06 06:09:33 -04:00
|
|
|
c.na === undefined ? c.p.slice(0, c.p.length - 1) : c.p
|
|
|
|
result.push(
|
|
|
|
(() => {
|
|
|
|
const result1 = []
|
|
|
|
for (const { path, event, cb } of Array.from(this._listeners)) {
|
2021-10-26 04:08:56 -04:00
|
|
|
let common
|
2023-03-21 08:06:13 -04:00
|
|
|
if (pathEquals(path, matchPath)) {
|
2020-05-06 06:09:33 -04:00
|
|
|
switch (event) {
|
|
|
|
case 'insert':
|
|
|
|
if (c.li !== undefined && c.ld === undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.li))
|
|
|
|
} else if (c.oi !== undefined && c.od === undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.oi))
|
|
|
|
} else if (c.si !== undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.si))
|
|
|
|
} else {
|
|
|
|
result1.push(undefined)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'delete':
|
|
|
|
if (c.li === undefined && c.ld !== undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.ld))
|
|
|
|
} else if (c.oi === undefined && c.od !== undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.od))
|
|
|
|
} else if (c.sd !== undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.sd))
|
|
|
|
} else {
|
|
|
|
result1.push(undefined)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'replace':
|
|
|
|
if (c.li !== undefined && c.ld !== undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.ld, c.li))
|
|
|
|
} else if (c.oi !== undefined && c.od !== undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.od, c.oi))
|
|
|
|
} else {
|
|
|
|
result1.push(undefined)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'move':
|
|
|
|
if (c.lm !== undefined) {
|
|
|
|
result1.push(cb(c.p[c.p.length - 1], c.lm))
|
|
|
|
} else {
|
|
|
|
result1.push(undefined)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'add':
|
|
|
|
if (c.na !== undefined) {
|
|
|
|
result1.push(cb(c.na))
|
|
|
|
} else {
|
|
|
|
result1.push(undefined)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
result1.push(undefined)
|
|
|
|
}
|
|
|
|
} else if (
|
2023-03-21 08:06:13 -04:00
|
|
|
(common = this.type.commonPath(matchPath, path)) != null
|
2020-05-06 06:09:33 -04:00
|
|
|
) {
|
|
|
|
if (event === 'child op') {
|
|
|
|
if (
|
2023-03-21 08:06:13 -04:00
|
|
|
matchPath.length === path.length &&
|
2020-05-06 06:09:33 -04:00
|
|
|
path.length === common
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
"paths match length and have commonality, but aren't equal?"
|
|
|
|
)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
const childPath = c.p.slice(common + 1)
|
|
|
|
result1.push(cb(childPath, c))
|
2020-05-06 06:09:33 -04:00
|
|
|
} else {
|
|
|
|
result1.push(undefined)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-06 06:09:33 -04:00
|
|
|
result1.push(undefined)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return result1
|
|
|
|
})()
|
|
|
|
)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return result
|
|
|
|
})()
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|