Use lib0/any-encoding instead of JSON

This commit is contained in:
Kevin Jahns 2019-08-09 01:15:33 +02:00
parent 36203af88e
commit 0e7da017fe
8 changed files with 164 additions and 23 deletions

55
package-lock.json generated
View File

@ -1490,7 +1490,8 @@
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@ -1511,12 +1512,14 @@
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -1531,17 +1534,20 @@
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@ -1658,7 +1664,8 @@
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@ -1670,6 +1677,7 @@
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@ -1684,6 +1692,7 @@
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
@ -1691,12 +1700,14 @@
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.3.5", "version": "2.3.5",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@ -1715,6 +1726,7 @@
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@ -1795,7 +1807,8 @@
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@ -1807,6 +1820,7 @@
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -1892,7 +1906,8 @@
"safe-buffer": { "safe-buffer": {
"version": "5.1.2", "version": "5.1.2",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@ -1928,6 +1943,7 @@
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",
@ -1947,6 +1963,7 @@
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@ -1990,12 +2007,14 @@
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.0.3", "version": "3.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
} }
} }
}, },
@ -2577,9 +2596,9 @@
} }
}, },
"lib0": { "lib0": {
"version": "0.0.5", "version": "0.0.6",
"resolved": "https://registry.npmjs.org/lib0/-/lib0-0.0.5.tgz", "resolved": "https://registry.npmjs.org/lib0/-/lib0-0.0.6.tgz",
"integrity": "sha512-3ElV6/t5Lv0Eczlnh/05q+Uq3RxQ/Q0zdN6LVtaUERQIDDZsP/CUXEGLsV8KZTgZwVFNCPGXNWYE+3WTOo+SHw==" "integrity": "sha512-drb8LcwZu2rAmTsXN0d3hFtZVbPE5ZUrsWf307Boc/v7IrmLq3lM5+OOMY672EysHTWeXo/OH54wRHyD6eFXXw=="
}, },
"linkify-it": { "linkify-it": {
"version": "2.1.0", "version": "2.1.0",
@ -4366,6 +4385,14 @@
"dev": true, "dev": true,
"requires": { "requires": {
"lib0": "0.0.5" "lib0": "0.0.5"
},
"dependencies": {
"lib0": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/lib0/-/lib0-0.0.5.tgz",
"integrity": "sha512-3ElV6/t5Lv0Eczlnh/05q+Uq3RxQ/Q0zdN6LVtaUERQIDDZsP/CUXEGLsV8KZTgZwVFNCPGXNWYE+3WTOo+SHw==",
"dev": true
}
} }
}, },
"yallist": { "yallist": {

View File

@ -51,7 +51,7 @@
}, },
"homepage": "http://y-js.org", "homepage": "http://y-js.org",
"dependencies": { "dependencies": {
"lib0": "0.0.5" "lib0": "0.0.6"
}, },
"devDependencies": { "devDependencies": {
"concurrently": "^3.6.1", "concurrently": "^3.6.1",

View File

@ -21,6 +21,7 @@ export {
ContentEmbed, ContentEmbed,
ContentFormat, ContentFormat,
ContentJSON, ContentJSON,
ContentAny,
ContentString, ContentString,
ContentType, ContentType,
AbstractType, AbstractType,

View File

@ -27,6 +27,7 @@ export * from './structs/ContentDeleted.js'
export * from './structs/ContentEmbed.js' export * from './structs/ContentEmbed.js'
export * from './structs/ContentFormat.js' export * from './structs/ContentFormat.js'
export * from './structs/ContentJSON.js' export * from './structs/ContentJSON.js'
export * from './structs/ContentAny.js'
export * from './structs/ContentString.js' export * from './structs/ContentString.js'
export * from './structs/ContentType.js' export * from './structs/ContentType.js'
export * from './structs/Item.js' export * from './structs/Item.js'

108
src/structs/ContentAny.js Normal file
View File

@ -0,0 +1,108 @@
import {
Transaction, Item, StructStore // eslint-disable-line
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
import * as decoding from 'lib0/decoding.js'
/**
* @private
*/
export class ContentAny {
/**
* @param {Array<any>} arr
*/
constructor (arr) {
/**
* @type {Array<any>}
*/
this.arr = arr
}
/**
* @return {number}
*/
getLength () {
return this.arr.length
}
/**
* @return {Array<any>}
*/
getContent () {
return this.arr
}
/**
* @return {boolean}
*/
isCountable () {
return true
}
/**
* @return {ContentAny}
*/
copy () {
return new ContentAny(this.arr)
}
/**
* @param {number} offset
* @return {ContentAny}
*/
splice (offset) {
const right = new ContentAny(this.arr.slice(offset))
this.arr = this.arr.slice(0, offset)
return right
}
/**
* @param {ContentAny} right
* @return {boolean}
*/
mergeWith (right) {
this.arr = this.arr.concat(right.arr)
return true
}
/**
* @param {Transaction} transaction
* @param {Item} item
*/
integrate (transaction, item) {}
/**
* @param {Transaction} transaction
*/
delete (transaction) {}
/**
* @param {StructStore} store
*/
gc (store) {}
/**
* @param {encoding.Encoder} encoder
* @param {number} offset
*/
write (encoder, offset) {
const len = this.arr.length
encoding.writeVarUint(encoder, len - offset)
for (let i = offset; i < len; i++) {
const c = this.arr[i]
encoding.writeAny(encoder, c)
}
}
/**
* @return {number}
*/
getRef () {
return 8
}
}
/**
* @private
*
* @param {decoding.Decoder} decoder
* @return {ContentAny}
*/
export const readContentAny = decoder => {
const len = decoding.readVarUint(decoder)
const cs = []
for (let i = 0; i < len; i++) {
cs.push(decoding.readAny(decoder))
}
return new ContentAny(cs)
}

View File

@ -18,6 +18,7 @@ import {
readContentDeleted, readContentDeleted,
readContentBinary, readContentBinary,
readContentJSON, readContentJSON,
readContentAny,
readContentString, readContentString,
readContentEmbed, readContentEmbed,
readContentFormat, readContentFormat,
@ -561,7 +562,8 @@ export const contentRefs = [
readContentString, readContentString,
readContentEmbed, readContentEmbed,
readContentFormat, readContentFormat,
readContentType readContentType,
readContentAny
] ]
/** /**

View File

@ -7,7 +7,7 @@ import {
nextID, nextID,
isVisible, isVisible,
ContentType, ContentType,
ContentJSON, ContentAny,
ContentBinary, ContentBinary,
createID, createID,
getItemCleanStart, getItemCleanStart,
@ -374,7 +374,7 @@ export const typeListInsertGenericsAfter = (transaction, parent, referenceItem,
let jsonContent = [] let jsonContent = []
const packJsonContent = () => { const packJsonContent = () => {
if (jsonContent.length > 0) { if (jsonContent.length > 0) {
left = new Item(nextID(transaction), left, left === null ? null : left.lastId, right, right === null ? null : right.id, parent, null, new ContentJSON(jsonContent)) left = new Item(nextID(transaction), left, left === null ? null : left.lastId, right, right === null ? null : right.id, parent, null, new ContentAny(jsonContent))
left.integrate(transaction) left.integrate(transaction)
jsonContent = [] jsonContent = []
} }
@ -503,7 +503,7 @@ export const typeMapSet = (transaction, parent, key, value) => {
const left = parent._map.get(key) || null const left = parent._map.get(key) || null
let content let content
if (value == null) { if (value == null) {
content = new ContentJSON([value]) content = new ContentAny([value])
} else { } else {
switch (value.constructor) { switch (value.constructor) {
case Number: case Number:
@ -511,7 +511,7 @@ export const typeMapSet = (transaction, parent, key, value) => {
case Boolean: case Boolean:
case Array: case Array:
case String: case String:
content = new ContentJSON([value]) content = new ContentAny([value])
break break
case Uint8Array: case Uint8Array:
content = new ContentBinary(value) content = new ContentBinary(value)

View File

@ -8,19 +8,21 @@ import {
readContentJSON, readContentJSON,
readContentEmbed, readContentEmbed,
readContentType, readContentType,
readContentFormat readContentFormat,
readContentAny
} from '../src/internals.js' } from '../src/internals.js'
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testStructReferences = tc => { export const testStructReferences = tc => {
t.assert(contentRefs.length === 8) t.assert(contentRefs.length === 9)
t.assert(contentRefs[1] === readContentDeleted) t.assert(contentRefs[1] === readContentDeleted)
t.assert(contentRefs[2] === readContentJSON) t.assert(contentRefs[2] === readContentJSON) // TODO: deprecate content json?
t.assert(contentRefs[3] === readContentBinary) t.assert(contentRefs[3] === readContentBinary)
t.assert(contentRefs[4] === readContentString) t.assert(contentRefs[4] === readContentString)
t.assert(contentRefs[5] === readContentEmbed) t.assert(contentRefs[5] === readContentEmbed)
t.assert(contentRefs[6] === readContentFormat) t.assert(contentRefs[6] === readContentFormat)
t.assert(contentRefs[7] === readContentType) t.assert(contentRefs[7] === readContentType)
t.assert(contentRefs[8] === readContentAny)
} }