lint & refactoring

This commit is contained in:
Kevin Jahns
2020-12-19 16:29:17 +01:00
parent 0fc213e92e
commit 4c929c6808
10 changed files with 96 additions and 145 deletions

View File

@@ -4,7 +4,7 @@ import {
getState,
splitItem,
iterateStructs,
AbstractUpdateDecoder, AbstractDSDecoder, AbstractDSEncoder, DSDecoderV2, DSEncoderV2, Item, GC, StructStore, Transaction, ID // eslint-disable-line
DSDecoderV1, DSEncoderV1, DSDecoderV2, DSEncoderV2, Item, GC, StructStore, Transaction, ID // eslint-disable-line
} from '../internals.js'
import * as array from 'lib0/array.js'
@@ -210,7 +210,7 @@ export const createDeleteSetFromStructStore = ss => {
}
/**
* @param {AbstractDSEncoder} encoder
* @param {DSEncoderV1 | DSEncoderV2} encoder
* @param {DeleteSet} ds
*
* @private
@@ -232,7 +232,7 @@ export const writeDeleteSet = (encoder, ds) => {
}
/**
* @param {AbstractDSDecoder} decoder
* @param {DSDecoderV1 | DSDecoderV2} decoder
* @return {DeleteSet}
*
* @private
@@ -260,7 +260,7 @@ export const readDeleteSet = decoder => {
*/
/**
* @param {AbstractDSDecoder} decoder
* @param {DSDecoderV1 | DSDecoderV2} decoder
* @param {Transaction} transaction
* @param {StructStore} store
*

View File

@@ -15,7 +15,7 @@ import {
findIndexSS,
UpdateEncoderV2,
applyUpdateV2,
AbstractDSDecoder, AbstractDSEncoder, DSEncoderV2, DSDecoderV1, DSDecoderV2, Transaction, Doc, DeleteSet, Item // eslint-disable-line
AbstractDSDecoder, DSEncoderV2, DSDecoderV1, DSDecoderV2, Transaction, Doc, DeleteSet, Item // eslint-disable-line
} from '../internals.js'
import * as map from 'lib0/map.js'
@@ -78,7 +78,7 @@ export const equalSnapshots = (snap1, snap2) => {
/**
* @param {Snapshot} snapshot
* @param {AbstractDSEncoder} [encoder]
* @param {DSEncoderV1 | DSEncoderV2} [encoder]
* @return {Uint8Array}
*/
export const encodeSnapshotV2 = (snapshot, encoder = new DSEncoderV2()) => {

View File

@@ -10,6 +10,9 @@ export class AbstractDSDecoder {
* @param {decoding.Decoder} decoder
*/
constructor (decoder) {
/**
* @type {decoding.Decoder}
*/
this.restDecoder = decoder
error.methodUnimplemented()
}
@@ -280,7 +283,7 @@ export class UpdateDecoderV2 extends DSDecoderV2 {
* @type {Array<string>}
*/
this.keys = []
decoding.readUint8(decoder) // read feature flag - currently unused
decoding.readVarUint(decoder) // read feature flag - currently unused
this.keyClockDecoder = new decoding.IntDiffOptRleDecoder(decoding.readVarUint8Array(decoder))
this.clientDecoder = new decoding.UintOptRleDecoder(decoding.readVarUint8Array(decoder))
this.leftClockDecoder = new decoding.IntDiffOptRleDecoder(decoding.readVarUint8Array(decoder))

View File

@@ -6,110 +6,9 @@ import {
ID // eslint-disable-line
} from '../internals.js'
export class AbstractDSEncoder {
constructor () {
this.restEncoder = encoding.createEncoder()
}
/**
* @return {Uint8Array}
*/
toUint8Array () {
error.methodUnimplemented()
}
/**
* Resets the ds value to 0.
* The v2 encoder uses this information to reset the initial diff value.
*/
resetDsCurVal () { }
/**
* @param {number} clock
*/
writeDsClock (clock) { }
/**
* @param {number} len
*/
writeDsLen (len) { }
}
export class AbstractUpdateEncoder extends AbstractDSEncoder {
/**
* @return {Uint8Array}
*/
toUint8Array () {
error.methodUnimplemented()
}
/**
* @param {ID} id
*/
writeLeftID (id) { }
/**
* @param {ID} id
*/
writeRightID (id) { }
/**
* Use writeClient and writeClock instead of writeID if possible.
* @param {number} client
*/
writeClient (client) { }
/**
* @param {number} info An unsigned 8-bit integer
*/
writeInfo (info) { }
/**
* @param {string} s
*/
writeString (s) { }
/**
* @param {boolean} isYKey
*/
writeParentInfo (isYKey) { }
/**
* @param {number} info An unsigned 8-bit integer
*/
writeTypeRef (info) { }
/**
* Write len of a struct - well suited for Opt RLE encoder.
*
* @param {number} len
*/
writeLen (len) { }
/**
* @param {any} any
*/
writeAny (any) { }
/**
* @param {Uint8Array} buf
*/
writeBuf (buf) { }
/**
* @param {any} embed
*/
writeJSON (embed) { }
/**
* @param {string} key
*/
writeKey (key) { }
}
export class DSEncoderV1 {
constructor () {
this.restEncoder = new encoding.Encoder()
this.restEncoder = encoding.createEncoder()
}
toUint8Array () {
@@ -228,7 +127,7 @@ export class UpdateEncoderV1 extends DSEncoderV1 {
export class DSEncoderV2 {
constructor () {
this.restEncoder = new encoding.Encoder() // encodes all the rest / non-optimized
this.restEncoder = encoding.createEncoder() // encodes all the rest / non-optimized
this.dsCurrVal = 0
}
@@ -288,7 +187,7 @@ export class UpdateEncoderV2 extends DSEncoderV2 {
toUint8Array () {
const encoder = encoding.createEncoder()
encoding.writeUint8(encoder, 0) // this is a feature flag that we might use in the future
encoding.writeVarUint(encoder, 0) // this is a feature flag that we might use in the future
encoding.writeVarUint8Array(encoder, this.keyClockEncoder.toUint8Array())
encoding.writeVarUint8Array(encoder, this.clientEncoder.toUint8Array())
encoding.writeVarUint8Array(encoder, this.leftClockEncoder.toUint8Array())

View File

@@ -33,7 +33,7 @@ import {
DSEncoderV2,
DSDecoderV1,
DSEncoderV1,
AbstractDSEncoder, AbstractDSDecoder, AbstractUpdateEncoder, AbstractUpdateDecoder, AbstractContent, Doc, Transaction, GC, Item, StructStore, ID // eslint-disable-line
AbstractDSDecoder, AbstractUpdateDecoder, Doc, Transaction, GC, Item, StructStore, ID // eslint-disable-line
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
@@ -42,7 +42,7 @@ import * as binary from 'lib0/binary.js'
import * as map from 'lib0/map.js'
/**
* @param {AbstractUpdateEncoder} encoder
* @param {UpdateEncoderV1 | UpdateEncoderV2} encoder
* @param {Array<GC|Item>} structs All structs by `client`
* @param {number} client
* @param {number} clock write structs starting with `ID(client,clock)`
@@ -65,7 +65,7 @@ const writeStructs = (encoder, structs, client, clock) => {
}
/**
* @param {AbstractUpdateEncoder} encoder
* @param {UpdateEncoderV1 | UpdateEncoderV2} encoder
* @param {StructStore} store
* @param {Map<number,number>} _sm
*
@@ -317,7 +317,7 @@ export const tryResumePendingDeleteReaders = (transaction, store) => {
}
/**
* @param {AbstractUpdateEncoder} encoder
* @param {UpdateEncoderV1 | UpdateEncoderV2} encoder
* @param {Transaction} transaction
*
* @private
@@ -462,7 +462,7 @@ export const applyUpdate = (ydoc, update, transactionOrigin) => applyUpdateV2(yd
* Write all the document as a single update message. If you specify the state of the remote client (`targetStateVector`) it will
* only write the operations that are missing.
*
* @param {AbstractUpdateEncoder} encoder
* @param {UpdateEncoderV1 | UpdateEncoderV2} encoder
* @param {Doc} doc
* @param {Map<number,number>} [targetStateVector] The state of the target that receives the update. Leave empty to write all known structs
*
@@ -481,7 +481,7 @@ export const writeStateAsUpdate = (encoder, doc, targetStateVector = new Map())
*
* @param {Doc} doc
* @param {Uint8Array} [encodedTargetStateVector] The state of the target that receives the update. Leave empty to write all known structs
* @param {AbstractUpdateEncoder} [encoder]
* @param {UpdateEncoderV1 | UpdateEncoderV2} [encoder]
* @return {Uint8Array}
*
* @function
@@ -546,7 +546,7 @@ export const decodeStateVectorV2 = decodedState => readStateVector(new DSDecoder
export const decodeStateVector = decodedState => readStateVector(new DSDecoderV1(decoding.createDecoder(decodedState)))
/**
* @param {AbstractDSEncoder} encoder
* @param {DSEncoderV1 | DSEncoderV2} encoder
* @param {Map<number,number>} sv
* @function
*/
@@ -560,7 +560,7 @@ export const writeStateVector = (encoder, sv) => {
}
/**
* @param {AbstractDSEncoder} encoder
* @param {DSEncoderV1 | DSEncoderV2} encoder
* @param {Doc} doc
*
* @function
@@ -571,7 +571,7 @@ export const writeDocumentStateVector = (encoder, doc) => writeStateVector(encod
* Encode State as Uint8Array.
*
* @param {Doc} doc
* @param {AbstractDSEncoder} [encoder]
* @param {DSEncoderV1 | DSEncoderV2} [encoder]
* @return {Uint8Array}
*
* @function

View File

@@ -10,6 +10,8 @@ import {
writeDeleteSet,
Skip,
mergeDeleteSets,
DSEncoderV1,
DSEncoderV2,
Item, GC, AbstractUpdateDecoder, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2 // eslint-disable-line
} from '../internals.js'
@@ -135,6 +137,52 @@ export class LazyStructWriter {
*/
export const mergeUpdates = updates => mergeUpdatesV2(updates, UpdateDecoderV1, UpdateEncoderV1)
/**
* @param {Uint8Array} update
* @param {typeof DSEncoderV1 | typeof DSEncoderV2} YEncoder
* @param {typeof UpdateDecoderV1 | typeof UpdateDecoderV2} YDecoder
* @return {Uint8Array}
*/
export const encodeStateVectorFromUpdateV2 = (update, YEncoder = DSEncoderV2, YDecoder = UpdateDecoderV2) => {
const encoder = new YEncoder()
const updateDecoder = new LazyStructReader(new YDecoder(decoding.createDecoder(update)))
let curr = updateDecoder.curr
if (curr !== null) {
let size = 1
let currClient = curr.id.client
let currClock = curr.id.clock
for (; curr !== null; curr = updateDecoder.next()) {
if (currClient !== curr.id.client) {
size++
// We found a new client
// write what we have to the encoder
encoding.writeVarUint(encoder.restEncoder, currClient)
encoding.writeVarUint(encoder.restEncoder, currClock)
currClient = curr.id.client
}
currClock = curr.id.clock + curr.length
}
// write what we have
encoding.writeVarUint(encoder.restEncoder, currClient)
encoding.writeVarUint(encoder.restEncoder, currClock)
// prepend the size of the state vector
const enc = encoding.createEncoder()
encoding.writeVarUint(enc, size)
encoding.writeBinaryEncoder(enc, encoder.restEncoder)
encoder.restEncoder = enc
return encoder.toUint8Array()
} else {
encoding.writeVarUint(encoder.restEncoder, 0)
return encoder.toUint8Array()
}
}
/**
* @param {Uint8Array} update
* @return {Uint8Array}
*/
export const encodeStateVectorFromUpdate = update => encodeStateVectorFromUpdateV2(update, DSEncoderV1, UpdateDecoderV2)
/**
* This method is intended to slice any kind of struct and retrieve the right part.
* It does not handle side-effects, so it should only be used by the lazy-encoder.