fixed linter errors

This commit is contained in:
Bartosz Sypytkowski 2023-08-17 20:20:49 +02:00
parent 10cf4b44f8
commit d25307c355
10 changed files with 300 additions and 285 deletions

View File

@ -8,10 +8,9 @@ import {
readYXmlFragment, readYXmlFragment,
readYXmlHook, readYXmlHook,
readYXmlText, readYXmlText,
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, StructStore, Transaction, Item, YEvent, AbstractType, // eslint-disable-line UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, StructStore, Transaction, Item, YEvent, AbstractType, ID, // eslint-disable-line
readYWeakLink, readYWeakLink,
unlinkFrom, unlinkFrom
ID
} from '../internals.js' } from '../internals.js'
import * as error from 'lib0/error' import * as error from 'lib0/error'
@ -113,7 +112,7 @@ export class ContentType {
if (this.type.constructor === WeakLink) { if (this.type.constructor === WeakLink) {
// when removing weak links, remove references to them // when removing weak links, remove references to them
// from type they're pointing to // from type they're pointing to
const type = /** @type {WeakLink<any>} */ (this.type); const type = /** @type {WeakLink<any>} */ (this.type)
const end = /** @type {ID} */ (type._quoteEnd.item) const end = /** @type {ID} */ (type._quoteEnd.item)
for (let item = type._firstItem; item !== null; item = item.right) { for (let item = type._firstItem; item !== null; item = item.right) {
if (item.linked) { if (item.linked) {
@ -121,7 +120,7 @@ export class ContentType {
} }
const lastId = item.lastId const lastId = item.lastId
if (lastId.client === end.client && lastId.clock === end.clock) { if (lastId.client === end.client && lastId.clock === end.clock) {
break; break
} }
} }
type._firstItem = null type._firstItem = null

View File

@ -326,13 +326,13 @@ export class Item extends AbstractStruct {
* *
* @type {boolean} * @type {boolean}
*/ */
set linked(isLinked) { set linked (isLinked) {
if (((this.info & binary.BIT9) > 0) !== isLinked) { if (((this.info & binary.BIT9) > 0) !== isLinked) {
this.info ^= binary.BIT9 this.info ^= binary.BIT9
} }
} }
get linked() { get linked () {
return (this.info & binary.BIT9) > 0 return (this.info & binary.BIT9) > 0
} }
@ -581,7 +581,7 @@ export class Item extends AbstractStruct {
// notify links about changes // notify links about changes
const linkedBy = transaction.doc.store.linkedBy.get(this) const linkedBy = transaction.doc.store.linkedBy.get(this)
if (linkedBy !== undefined) { if (linkedBy !== undefined) {
for (let link of linkedBy) { for (const link of linkedBy) {
addChangedTypeToTransaction(transaction, link, this.parentSub) addChangedTypeToTransaction(transaction, link, this.parentSub)
} }
} }
@ -695,7 +695,7 @@ export class Item extends AbstractStruct {
const allLinks = transaction.doc.store.linkedBy const allLinks = transaction.doc.store.linkedBy
const linkedBy = allLinks.get(this) const linkedBy = allLinks.get(this)
if (linkedBy !== undefined) { if (linkedBy !== undefined) {
for (let link of linkedBy) { for (const link of linkedBy) {
addChangedTypeToTransaction(transaction, link, this.parentSub) addChangedTypeToTransaction(transaction, link, this.parentSub)
} }
allLinks.delete(this) allLinks.delete(this)

View File

@ -246,7 +246,7 @@ export const callTypeObservers = (type, transaction, event, visitedLinks = null)
} else if (type._item.linked) { } else if (type._item.linked) {
const linkedBy = transaction.doc.store.linkedBy.get(type._item) const linkedBy = transaction.doc.store.linkedBy.get(type._item)
if (linkedBy !== undefined) { if (linkedBy !== undefined) {
for (let link of linkedBy) { for (const link of linkedBy) {
if (visitedLinks === null || !visitedLinks.has(link)) { if (visitedLinks === null || !visitedLinks.has(link)) {
visitedLinks = visitedLinks !== null ? visitedLinks : new Set() visitedLinks = visitedLinks !== null ? visitedLinks : new Set()
visitedLinks.add(link) visitedLinks.add(link)

View File

@ -16,8 +16,7 @@ import {
YArrayRefID, YArrayRefID,
callTypeObservers, callTypeObservers,
transact, transact,
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, // eslint-disable-line ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, YWeakLink, // eslint-disable-line
YWeakLink,
arrayWeakLink arrayWeakLink
} from '../internals.js' } from '../internals.js'
import { typeListSlice } from './AbstractType.js' import { typeListSlice } from './AbstractType.js'

View File

@ -14,8 +14,7 @@ import {
YMapRefID, YMapRefID,
callTypeObservers, callTypeObservers,
transact, transact,
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, // eslint-disable-line UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item, YWeakLink, // eslint-disable-line
YWeakLink,
mapWeakLink mapWeakLink
} from '../internals.js' } from '../internals.js'

View File

@ -27,14 +27,13 @@ import {
typeMapGetAll, typeMapGetAll,
updateMarkerChanges, updateMarkerChanges,
ContentType, ContentType,
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, ID, Doc, Item, Snapshot, Transaction, // eslint-disable-line ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, ID, Doc, Item, Snapshot, Transaction, YWeakLink, // eslint-disable-line
quoteText quoteText
} from '../internals.js' } from '../internals.js'
import * as object from 'lib0/object' import * as object from 'lib0/object'
import * as map from 'lib0/map' import * as map from 'lib0/map'
import * as error from 'lib0/error' import * as error from 'lib0/error'
import { WeakLink } from 'yjs'
/** /**
* @param {any} a * @param {any} a
@ -1067,7 +1066,7 @@ export class YText extends AbstractType {
* *
* @param {number} index The index where quoted range should start * @param {number} index The index where quoted range should start
* @param {number} length Number of quoted elements * @param {number} length Number of quoted elements
* @return {WeakLink<string>} * @return {YWeakLink<string>}
* *
* @public * @public
*/ */
@ -1263,6 +1262,7 @@ export const rangeDelta = (parent, start, end, snapshot, prevSnapshot, computeYC
// startOffset represents offset at current block from which we're intersted in picking string // startOffset represents offset at current block from which we're intersted in picking string
// if it's -1 it means, we're out of scope and we should break at this point // if it's -1 it means, we're out of scope and we should break at this point
let startOffset = start === null ? 0 : -1 let startOffset = start === null ? 0 : -1
// eslint-disable-next-line no-labels
loop: while (n !== null) { loop: while (n !== null) {
if (startOffset < 0 && start !== null) { if (startOffset < 0 && start !== null) {
if (start.client === n.id.client && start.clock >= n.id.clock && start.clock < n.id.clock + n.length) { if (start.client === n.id.client && start.clock >= n.id.clock && start.clock < n.id.clock + n.length) {
@ -1287,7 +1287,7 @@ export const rangeDelta = (parent, start, end, snapshot, prevSnapshot, computeYC
packStr() packStr()
currentAttributes.delete('ychange') currentAttributes.delete('ychange')
} }
let s = /** @type {ContentString} */ (n.content).str const s = /** @type {ContentString} */ (n.content).str
if (startOffset > 0) { if (startOffset > 0) {
str += s.slice(startOffset) str += s.slice(startOffset)
startOffset = 0 startOffset = 0
@ -1296,8 +1296,9 @@ export const rangeDelta = (parent, start, end, snapshot, prevSnapshot, computeYC
const endOffset = n.id.clock + n.length - end.clock - 1 const endOffset = n.id.clock + n.length - end.clock - 1
str += s.slice(0, s.length + endOffset) // scope is negative str += s.slice(0, s.length + endOffset) // scope is negative
packStr() packStr()
// eslint-disable-next-line no-labels
break loop break loop
} else if (startOffset == 0) { } else if (startOffset === 0) {
str += s str += s
} }
break break
@ -1330,7 +1331,7 @@ export const rangeDelta = (parent, start, end, snapshot, prevSnapshot, computeYC
} }
} else if (end !== null && end.client === n.id.client && end.clock >= n.id.clock && end.clock < n.id.clock + n.length) { } else if (end !== null && end.client === n.id.client && end.clock >= n.id.clock && end.clock < n.id.clock + n.length) {
// block may not passed visibility check, but we still need to verify boundaries // block may not passed visibility check, but we still need to verify boundaries
break; break
} }
n = n.right n = n.right
} }

View File

@ -1,8 +1,8 @@
import { decoding, encoding, error } from "lib0" import { decoding, encoding, error } from 'lib0'
import * as map from 'lib0/map' import * as map from 'lib0/map'
import * as set from 'lib0/set' import * as set from 'lib0/set'
import { import {
YEvent, Transaction, ID, GC, AbstractType, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Item, YEvent, AbstractType,
transact, transact,
getItemCleanEnd, getItemCleanEnd,
createID, createID,
@ -12,14 +12,13 @@ import {
writeID, writeID,
readID, readID,
RelativePosition, RelativePosition,
ItemTextListPosition,
ContentString, ContentString,
rangeDelta, rangeDelta,
formatXmlString, formatXmlString,
Snapshot,
YText, YText,
YXmlText YXmlText,
} from "../internals.js" Transaction, Item, Doc, ID, Snapshot, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, ItemTextListPosition // eslint-disable-line
} from '../internals.js'
/** /**
* @template T extends AbstractType<any> * @template T extends AbstractType<any>
@ -31,6 +30,7 @@ export class YWeakLinkEvent extends YEvent {
* @param {YWeakLink<T>} ylink The YWeakLink to which this event was propagated to. * @param {YWeakLink<T>} ylink The YWeakLink to which this event was propagated to.
* @param {Transaction} transaction * @param {Transaction} transaction
*/ */
// eslint-disable-next-line no-useless-constructor
constructor (ylink, transaction) { constructor (ylink, transaction) {
super(ylink, transaction) super(ylink, transaction)
} }
@ -48,7 +48,7 @@ export class YWeakLink extends AbstractType {
* @param {RelativePosition} end * @param {RelativePosition} end
* @param {Item|null} firstItem * @param {Item|null} firstItem
*/ */
constructor(start, end, firstItem) { constructor (start, end, firstItem) {
super() super()
this._quoteStart = start this._quoteStart = start
this._quoteEnd = end this._quoteEnd = end
@ -84,7 +84,7 @@ export class YWeakLink extends AbstractType {
} }
} }
return undefined; return undefined
} }
/** /**
@ -96,14 +96,14 @@ export class YWeakLink extends AbstractType {
let result = /** @type {Array<any>} */ ([]) let result = /** @type {Array<any>} */ ([])
let item = this._firstItem let item = this._firstItem
const end = /** @type {ID} */ (this._quoteEnd.item) const end = /** @type {ID} */ (this._quoteEnd.item)
//TODO: moved elements // TODO: moved elements
while (item !== null) { while (item !== null) {
if (!item.deleted) { if (!item.deleted) {
result = result.concat(item.content.getContent()) result = result.concat(item.content.getContent())
} }
const lastId = item.lastId const lastId = item.lastId
if (lastId.client === end.client && lastId.clock === end.clock) { if (lastId.client === end.client && lastId.clock === end.clock) {
break; break
} }
item = item.right item = item.right
} }
@ -139,12 +139,12 @@ export class YWeakLink extends AbstractType {
/** @type {Item|null} */ /** @type {Item|null} */
let item = firstItem let item = firstItem
let end = /** @type {ID} */ (this._quoteEnd.item) const end = /** @type {ID} */ (this._quoteEnd.item)
for (;item !== null; item = item.right) { for (;item !== null; item = item.right) {
createLink(transaction, item, this) createLink(transaction, item, this)
const lastId = item.lastId const lastId = item.lastId
if (lastId.client === end.client && lastId.clock === end.clock) { if (lastId.client === end.client && lastId.clock === end.clock) {
break; break
} }
} }
}) })
@ -182,7 +182,7 @@ export class YWeakLink extends AbstractType {
_write (encoder) { _write (encoder) {
encoder.writeTypeRef(YWeakLinkRefID) encoder.writeTypeRef(YWeakLinkRefID)
const isSingle = this.isSingle() const isSingle = this.isSingle()
const info = (isSingle ? 0 : 1) | (this._quoteStart.assoc >= 0 ? 2 : 0) | (this._quoteEnd.assoc >= 0 ? 4 :0) const info = (isSingle ? 0 : 1) | (this._quoteStart.assoc >= 0 ? 2 : 0) | (this._quoteEnd.assoc >= 0 ? 4 : 0)
encoding.writeUint8(encoder.restEncoder, info) encoding.writeUint8(encoder.restEncoder, info)
writeID(encoder.restEncoder, /** @type {ID} */ (this._quoteStart.item)) writeID(encoder.restEncoder, /** @type {ID} */ (this._quoteStart.item))
if (!isSingle) { if (!isSingle) {
@ -198,7 +198,7 @@ export class YWeakLink extends AbstractType {
toString () { toString () {
if (this._firstItem !== null) { if (this._firstItem !== null) {
switch (/** @type {AbstractType<any>} */ (this._firstItem.parent).constructor) { switch (/** @type {AbstractType<any>} */ (this._firstItem.parent).constructor) {
case YText: case YText: {
let str = '' let str = ''
/** /**
* @type {Item|null} * @type {Item|null}
@ -211,12 +211,12 @@ export class YWeakLink extends AbstractType {
} }
const lastId = n.lastId const lastId = n.lastId
if (lastId.client === end.client && lastId.clock === end.clock) { if (lastId.client === end.client && lastId.clock === end.clock) {
break; break
} }
n = n.right n = n.right
} }
return str return str
}
case YXmlText: case YXmlText:
return this.toDelta().map(delta => formatXmlString(delta)).join('') return this.toDelta().map(delta => formatXmlString(delta)).join('')
} }
@ -233,7 +233,7 @@ export class YWeakLink extends AbstractType {
* @param {function('removed' | 'added', ID):any} [computeYChange] * @param {function('removed' | 'added', ID):any} [computeYChange]
* @returns {Array<any>} * @returns {Array<any>}
*/ */
toDelta(snapshot, prevSnapshot, computeYChange) { toDelta (snapshot, prevSnapshot, computeYChange) {
if (this._firstItem !== null && this._quoteStart.item !== null && this._quoteEnd.item !== null) { if (this._firstItem !== null && this._quoteStart.item !== null && this._quoteEnd.item !== null) {
const parent = /** @type {AbstractType<any>} */ (this._firstItem.parent) const parent = /** @type {AbstractType<any>} */ (this._firstItem.parent)
return rangeDelta(parent, this._quoteStart.item, this._quoteEnd.item, snapshot, prevSnapshot, computeYChange) return rangeDelta(parent, this._quoteStart.item, this._quoteEnd.item, snapshot, prevSnapshot, computeYChange)
@ -243,7 +243,6 @@ export class YWeakLink extends AbstractType {
} }
} }
/** /**
* @param {UpdateDecoderV1 | UpdateDecoderV2} decoder * @param {UpdateDecoderV1 | UpdateDecoderV2} decoder
* @return {YWeakLink<any>} * @return {YWeakLink<any>}
@ -280,7 +279,7 @@ export const arrayWeakLink = (transaction, parent, index, length = 1) => {
if (index > 0) { if (index > 0) {
startItem = getItemCleanStart(transaction, createID(startItem.id.client, startItem.id.clock + index)) startItem = getItemCleanStart(transaction, createID(startItem.id.client, startItem.id.clock + index))
} }
break; break
} }
index -= startItem.length index -= startItem.length
} }
@ -293,7 +292,7 @@ export const arrayWeakLink = (transaction, parent, index, length = 1) => {
remaining -= endItem.length remaining -= endItem.length
} else { } else {
endItem = getItemCleanEnd(transaction, transaction.doc.store, createID(endItem.id.client, endItem.id.clock + remaining - 1)) endItem = getItemCleanEnd(transaction, transaction.doc.store, createID(endItem.id.client, endItem.id.clock + remaining - 1))
break; break
} }
} }
} }
@ -308,7 +307,7 @@ export const arrayWeakLink = (transaction, parent, index, length = 1) => {
createLink(transaction, item, link) createLink(transaction, item, link)
const lastId = item.lastId const lastId = item.lastId
if (lastId.client === end.client && lastId.clock === end.clock) { if (lastId.client === end.client && lastId.clock === end.clock) {
break; break
} }
} }
}) })
@ -350,7 +349,7 @@ export const quoteText = (transaction, parent, pos, length) => {
createLink(transaction, item, link) createLink(transaction, item, link)
const lastId = item.lastId const lastId = item.lastId
if (lastId.client === end.client && lastId.clock === end.clock) { if (lastId.client === end.client && lastId.clock === end.clock) {
break; break
} }
} }
}) })
@ -438,12 +437,12 @@ export const joinLinkedRange = (transaction, item) => {
const rightLinks = allLinks.get(/** @type {Item} */ (item.right)) const rightLinks = allLinks.get(/** @type {Item} */ (item.right))
if (leftLinks && rightLinks) { if (leftLinks && rightLinks) {
const common = new Set() const common = new Set()
for (let link of leftLinks) { for (const link of leftLinks) {
if (rightLinks.has(link)) { if (rightLinks.has(link)) {
common.add(link) common.add(link)
} }
} }
if (common.size != 0) { if (common.size !== 0) {
allLinks.set(item, common) allLinks.set(item, common)
} }
} }

View File

@ -2,8 +2,7 @@
import { import {
GC, GC,
splitItem, splitItem,
Transaction, ID, Item, DSDecoderV2, // eslint-disable-line Transaction, ID, Item, DSDecoderV2, YWeakLink // eslint-disable-line
YWeakLink
} from '../internals.js' } from '../internals.js'
import * as math from 'lib0/math' import * as math from 'lib0/math'

View File

@ -1,6 +1,6 @@
import * as Y from '../src/index.js' import * as Y from '../src/index.js'
import * as t from 'lib0/testing' import * as t from 'lib0/testing'
import { init, compare } from './testHelper.js' import { init } from './testHelper.js'
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
@ -25,8 +25,8 @@ export const testBasicMap = tc => {
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testBasicArray = tc => { export const testBasicArray = tc => {
const { testConnector, array0, array1 } = init(tc, {users:2}) const { testConnector, array0, array1 } = init(tc, { users: 2 })
array0.insert(0, [1,2,3]) array0.insert(0, [1, 2, 3])
array0.insert(3, [array0.quote(1)]) array0.insert(3, [array0.quote(1)])
t.compare(array0.get(0), 1) t.compare(array0.get(0), 1)
@ -46,7 +46,7 @@ export const testBasicArray = tc => {
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testArrayQuoteMultipleElements = tc => { export const testArrayQuoteMultipleElements = tc => {
const { testConnector, array0, array1 } = init(tc, {users:2}) const { testConnector, array0, array1 } = init(tc, { users: 2 })
const nested = new Y.Map([['key', 'value']]) const nested = new Y.Map([['key', 'value']])
array0.insert(0, [1, 2, nested, 3]) array0.insert(0, [1, 2, nested, 3])
array0.insert(0, [array0.quote(1, 3)]) array0.insert(0, [array0.quote(1, 3)])
@ -63,11 +63,11 @@ export const testArrayQuoteMultipleElements = tc => {
const link1 = array1.get(0) const link1 = array1.get(0)
let unquoted = link1.unquote() let unquoted = link1.unquote()
t.compare(unquoted[0], 2) t.compare(unquoted[0], 2)
t.compare(unquoted[1].toJSON(), {'key':'value'}) t.compare(unquoted[1].toJSON(), { key: 'value' })
t.compare(unquoted[2], 3) t.compare(unquoted[2], 3)
t.compare(array1.get(1), 1) t.compare(array1.get(1), 1)
t.compare(array1.get(2), 2) t.compare(array1.get(2), 2)
t.compare(array1.get(3).toJSON(), {'key':'value'}) t.compare(array1.get(3).toJSON(), { key: 'value' })
t.compare(array1.get(4), 3) t.compare(array1.get(4), 3)
array1.insert(3, ['A', 'B']) array1.insert(3, ['A', 'B'])
@ -75,7 +75,7 @@ export const testArrayQuoteMultipleElements = tc => {
t.compare(unquoted[0], 2) t.compare(unquoted[0], 2)
t.compare(unquoted[1], 'A') t.compare(unquoted[1], 'A')
t.compare(unquoted[2], 'B') t.compare(unquoted[2], 'B')
t.compare(unquoted[3].toJSON(), {'key':'value'}) t.compare(unquoted[3].toJSON(), { key: 'value' })
t.compare(unquoted[4], 3) t.compare(unquoted[4], 3)
testConnector.flushAllMessages() testConnector.flushAllMessages()
@ -87,7 +87,7 @@ export const testArrayQuoteMultipleElements = tc => {
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testSelfQuotation = tc => { export const testSelfQuotation = tc => {
const { testConnector, array0, array1 } = init(tc, {users:2}) const { testConnector, array0, array1 } = init(tc, { users: 2 })
array0.insert(0, [1, 2, 3, 4]) array0.insert(0, [1, 2, 3, 4])
const link0 = array0.quote(0, 3) const link0 = array0.quote(0, 3)
array0.insert(1, [link0]) // link is inserted into its own range array0.insert(1, [link0]) // link is inserted into its own range
@ -102,7 +102,7 @@ export const testSelfQuotation = tc => {
testConnector.flushAllMessages() testConnector.flushAllMessages()
const link1 = array1.get(1) const link1 = array1.get(1)
let unquoted = link1.unquote() const unquoted = link1.unquote()
t.compare(unquoted, [1, link1, 2, 3]) t.compare(unquoted, [1, link1, 2, 3])
t.compare(array1.get(0), 1) t.compare(array1.get(0), 1)
t.compare(array1.get(1), link1) t.compare(array1.get(1), link1)
@ -115,7 +115,7 @@ export const testSelfQuotation = tc => {
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testUpdate = tc => { export const testUpdate = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 2 }) const { testConnector, map0, map1 } = init(tc, { users: 2 })
map0.set('a', new Y.Map([['a1', 'hello']])) map0.set('a', new Y.Map([['a1', 'hello']]))
const link0 = /** @type {Y.WeakLink<Y.Map<any>>} */ (map0.link('a')) const link0 = /** @type {Y.WeakLink<Y.Map<any>>} */ (map0.link('a'))
map0.set('b', link0) map0.set('b', link0)
@ -139,7 +139,7 @@ export const testUpdate = tc => {
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testDeleteWeakLink = tc => { export const testDeleteWeakLink = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 2 }) const { testConnector, map0, map1 } = init(tc, { users: 2 })
map0.set('a', new Y.Map([['a1', 'hello']])) map0.set('a', new Y.Map([['a1', 'hello']]))
const link0 = /** @type {Y.WeakLink<Y.Map<any>>} */ (map0.link('a')) const link0 = /** @type {Y.WeakLink<Y.Map<any>>} */ (map0.link('a'))
map0.set('b', link0) map0.set('b', link0)
@ -164,15 +164,15 @@ export const testDeleteWeakLink = tc => {
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testDeleteSource = tc => { export const testDeleteSource = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 2 }) const { testConnector, map0, map1 } = init(tc, { users: 2 })
map0.set('a', new Y.Map([['a1', 'hello']])) map0.set('a', new Y.Map([['a1', 'hello']]))
const link0 = /** @type {Y.WeakLink<Y.Map<any>>} */ (map0.link('a')) const link0 = /** @type {Y.WeakLink<Y.Map<any>>} */ (map0.link('a'))
map0.set('b', link0) map0.set('b', link0)
testConnector.flushAllMessages() testConnector.flushAllMessages()
const link1 = /** @type {Y.WeakLink<Y.Map<any>>} */ (map1.get('b')) const link1 = /** @type {Y.WeakLink<Y.Map<any>>} */ (map1.get('b'))
let l1 = /** @type {Y.Map<any>} */ (link1.deref()) const l1 = /** @type {Y.Map<any>} */ (link1.deref())
let l0 = /** @type {Y.Map<any>} */ (link0.deref()) const l0 = /** @type {Y.Map<any>} */ (link0.deref())
t.compare(l1.get('a1'), l0.get('a1')) t.compare(l1.get('a1'), l0.get('a1'))
map1.delete('a') // delete source of the link map1.delete('a') // delete source of the link
@ -188,7 +188,7 @@ export const testDeleteSource = tc => {
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testObserveMapUpdate = tc => { export const testObserveMapUpdate = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 2 }) const { testConnector, map0, map1 } = init(tc, { users: 2 })
map0.set('a', 'value') map0.set('a', 'value')
const link0 = /** @type {Y.WeakLink<String>} */ (map0.link('a')) const link0 = /** @type {Y.WeakLink<String>} */ (map0.link('a'))
map0.set('b', link0) map0.set('b', link0)
@ -196,17 +196,21 @@ export const testObserveMapUpdate = tc => {
* @type {any} * @type {any}
*/ */
let target0 let target0
link0.observe((e) => target0 = e.target) link0.observe((e) => {
target0 = e.target
})
testConnector.flushAllMessages() testConnector.flushAllMessages()
let link1 = /** @type {Y.WeakLink<String>} */ (map1.get('b')) const link1 = /** @type {Y.WeakLink<String>} */ (map1.get('b'))
t.compare(link1.deref(), 'value') t.compare(link1.deref(), 'value')
/** /**
* @type {any} * @type {any}
*/ */
let target1 let target1
link1.observe((e) => target1 = e.target) link1.observe((e) => {
target1 = e.target
})
map0.set('a', 'value2') map0.set('a', 'value2')
t.compare(target0.deref(), 'value2') t.compare(target0.deref(), 'value2')
@ -219,7 +223,7 @@ export const testObserveMapUpdate = tc => {
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testObserveMapDelete = tc => { export const testObserveMapDelete = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 2 }) const { testConnector, map0, map1 } = init(tc, { users: 2 })
map0.set('a', 'value') map0.set('a', 'value')
const link0 = /** @type {Y.WeakLink<String>} */ (map0.link('a')) const link0 = /** @type {Y.WeakLink<String>} */ (map0.link('a'))
map0.set('b', link0) map0.set('b', link0)
@ -227,17 +231,21 @@ export const testObserveMapDelete = tc => {
* @type {any} * @type {any}
*/ */
let target0 let target0
link0.observe((e) => target0 = e.target) link0.observe((e) => {
target0 = e.target
})
testConnector.flushAllMessages() testConnector.flushAllMessages()
let link1 = /** @type {Y.WeakLink<String>} */ (map1.get('b')) const link1 = /** @type {Y.WeakLink<String>} */ (map1.get('b'))
t.compare(link1.deref(), 'value') t.compare(link1.deref(), 'value')
/** /**
* @type {any} * @type {any}
*/ */
let target1 let target1
link1.observe((e) => target1 = e.target) link1.observe((e) => {
target1 = e.target
})
map0.delete('a') map0.delete('a')
t.compare(target0.deref(), undefined) t.compare(target0.deref(), undefined)
@ -250,24 +258,28 @@ export const testObserveMapDelete = tc => {
*/ */
export const testObserveArray = tc => { export const testObserveArray = tc => {
const { testConnector, array0, array1 } = init(tc, { users: 2 }) const { testConnector, array0, array1 } = init(tc, { users: 2 })
array0.insert(0, ['A','B','C']) array0.insert(0, ['A', 'B', 'C'])
const link0 = /** @type {Y.WeakLink<String>} */ (array0.quote(1, 2)) const link0 = /** @type {Y.WeakLink<String>} */ (array0.quote(1, 2))
array0.insert(0, [link0]) array0.insert(0, [link0])
/** /**
* @type {any} * @type {any}
*/ */
let target0 let target0
link0.observe((e) => target0 = e.target) link0.observe((e) => {
target0 = e.target
})
testConnector.flushAllMessages() testConnector.flushAllMessages()
let link1 = /** @type {Y.WeakLink<String>} */ (array1.get(0)) const link1 = /** @type {Y.WeakLink<String>} */ (array1.get(0))
t.compare(link1.unquote(), ['B','C']) t.compare(link1.unquote(), ['B', 'C'])
/** /**
* @type {any} * @type {any}
*/ */
let target1 let target1
link1.observe((e) => target1 = e.target) link1.observe((e) => {
target1 = e.target
})
array0.delete(2) array0.delete(2)
t.compare(target0.unquote(), ['C']) t.compare(target0.unquote(), ['C'])
@ -315,7 +327,9 @@ export const testDeepObserveTransitive = tc => {
* @type {Array<any>} * @type {Array<any>}
*/ */
let events = [] let events = []
link2.observeDeep((e) => events = e) link2.observeDeep((e) => {
events = e
})
map2.set('key', 'value2') map2.set('key', 'value2')
const values = events.map((e) => e.target.deref()) const values = events.map((e) => e.target.deref())
t.compare(values, ['value2']) t.compare(values, ['value2'])
@ -354,7 +368,9 @@ export const testDeepObserveTransitive2 = tc => {
* @type {Array<any>} * @type {Array<any>}
*/ */
let events = [] let events = []
link3.observeDeep((e) => events = e) link3.observeDeep((e) => {
events = e
})
map2.set('key', 'value2') map2.set('key', 'value2')
const values = events.map((e) => e.target.deref()) const values = events.map((e) => e.target.deref())
t.compare(values, ['value2']) t.compare(values, ['value2'])
@ -381,7 +397,9 @@ export const testDeepObserveMap = tc => {
* @type {Array<any>} * @type {Array<any>}
*/ */
let events = [] let events = []
map.observeDeep((e) => events = e) map.observeDeep((e) => {
events = e
})
const nested = new Y.Map() const nested = new Y.Map()
array.insert(0, [nested]) array.insert(0, [nested])
@ -393,14 +411,14 @@ export const testDeepObserveMap = tc => {
nested.set('key', 'value') nested.set('key', 'value')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, nested) t.compare(events[0].target, nested)
t.compare(events[0].keys, new Map([['key', {action:'add', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['key', { action: 'add', oldValue: undefined }]]))
// delete entry in linked map // delete entry in linked map
events = [] events = []
nested.delete('key') nested.delete('key')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, nested) t.compare(events[0].target, nested)
t.compare(events[0].keys, new Map([['key', {action:'delete', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['key', { action: 'delete', oldValue: undefined }]]))
// delete linked map // delete linked map
array.delete(0) array.delete(0)
@ -411,7 +429,7 @@ export const testDeepObserveMap = tc => {
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testDeepObserveArray = tc => { //FIXME export const testDeepObserveArray = tc => { // FIXME
// test observers in a face of linked chains of values // test observers in a face of linked chains of values
const doc = new Y.Doc() const doc = new Y.Doc()
/* /*
@ -436,14 +454,14 @@ export const testDeepObserveArray = tc => { //FIXME
let events = [] let events = []
array.observeDeep((evts) => { array.observeDeep((evts) => {
events = [] events = []
for (let e of evts) { for (const e of evts) {
switch (e.constructor) { switch (e.constructor) {
case Y.YMapEvent: case Y.YMapEvent:
events.push({target: e.target, keys: e.keys}) events.push({ target: e.target, keys: e.keys })
break; break
case Y.YWeakLinkEvent: case Y.YWeakLinkEvent:
events.push({target: e.target}) events.push({ target: e.target })
break; break
default: throw new Error('unexpected event type ' + e.constructor) default: throw new Error('unexpected event type ' + e.constructor)
} }
} }
@ -454,18 +472,18 @@ export const testDeepObserveArray = tc => { //FIXME
nested.set('key', 'value') nested.set('key', 'value')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, nested) t.compare(events[0].target, nested)
t.compare(events[0].keys, new Map([['key', {action:'add', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['key', { action: 'add', oldValue: undefined }]]))
nested.set('key', 'value2') nested.set('key', 'value2')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, nested) t.compare(events[0].target, nested)
t.compare(events[0].keys, new Map([['key', {action:'update', oldValue: 'value'}]])) t.compare(events[0].keys, new Map([['key', { action: 'update', oldValue: 'value' }]]))
// delete entry in linked map // delete entry in linked map
nested.delete('key') nested.delete('key')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, nested) t.compare(events[0].target, nested)
t.compare(events[0].keys, new Map([['key', {action:'delete', oldValue: 'value2'}]])) t.compare(events[0].keys, new Map([['key', { action: 'delete', oldValue: 'value2' }]]))
// delete linked map // delete linked map
map.delete('nested') map.delete('nested')
@ -477,10 +495,10 @@ export const testDeepObserveArray = tc => { //FIXME
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testDeepObserveNewElementWithinQuotedRange = tc => { export const testDeepObserveNewElementWithinQuotedRange = tc => {
const { testConnector, users, array0, array1 } = init(tc, { users: 2 }) const { testConnector, array0, array1 } = init(tc, { users: 2 })
const m1 = new Y.Map() const m1 = new Y.Map()
const m3 = new Y.Map() const m3 = new Y.Map()
array0.insert(0, [1,m1,m3,2]) array0.insert(0, [1, m1, m3, 2])
const link0 = array0.quote(1, 2) const link0 = array0.quote(1, 2)
array0.insert(0, [link0]) array0.insert(0, [link0])
@ -492,14 +510,14 @@ export const testDeepObserveNewElementWithinQuotedRange = tc => {
let e0 = [] let e0 = []
link0.observeDeep((evts) => { link0.observeDeep((evts) => {
e0 = [] e0 = []
for (let e of evts) { for (const e of evts) {
switch (e.constructor) { switch (e.constructor) {
case Y.YMapEvent: case Y.YMapEvent:
e0.push({target: e.target, keys: e.keys}) e0.push({ target: e.target, keys: e.keys })
break; break
case Y.YWeakLinkEvent: case Y.YWeakLinkEvent:
e0.push({target: e.target}) e0.push({ target: e.target })
break; break
default: throw new Error('unexpected event type ' + e.constructor) default: throw new Error('unexpected event type ' + e.constructor)
} }
} }
@ -512,14 +530,14 @@ export const testDeepObserveNewElementWithinQuotedRange = tc => {
let e1 = [] let e1 = []
link1.observeDeep((evts) => { link1.observeDeep((evts) => {
e1 = [] e1 = []
for (let e of evts) { for (const e of evts) {
switch (e.constructor) { switch (e.constructor) {
case Y.YMapEvent: case Y.YMapEvent:
e1.push({target: e.target, keys: e.keys}) e1.push({ target: e.target, keys: e.keys })
break; break
case Y.YWeakLinkEvent: case Y.YWeakLinkEvent:
e1.push({target: e.target}) e1.push({ target: e.target })
break; break
default: throw new Error('unexpected event type ' + e.constructor) default: throw new Error('unexpected event type ' + e.constructor)
} }
} }
@ -531,20 +549,20 @@ export const testDeepObserveNewElementWithinQuotedRange = tc => {
m20.set('key', 'value') m20.set('key', 'value')
t.compare(e0.length, 1) t.compare(e0.length, 1)
t.compare(e0[0].target, m20) t.compare(e0[0].target, m20)
t.compare(e0[0].keys, new Map([['key', {action:'add', oldValue: undefined}]])) t.compare(e0[0].keys, new Map([['key', { action: 'add', oldValue: undefined }]]))
testConnector.flushAllMessages() testConnector.flushAllMessages()
const m21 = array1.get(3) const m21 = array1.get(3)
t.compare(e1.length, 1) t.compare(e1.length, 1)
t.compare(e1[0].target, m21) t.compare(e1[0].target, m21)
t.compare(e1[0].keys, new Map([['key', {action:'add', oldValue: undefined}]])) t.compare(e1[0].keys, new Map([['key', { action: 'add', oldValue: undefined }]]))
} }
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
export const testMapDeepObserve = tc => { //FIXME export const testMapDeepObserve = tc => { // FIXME
const doc = new Y.Doc() const doc = new Y.Doc()
const outer = doc.getMap('outer') const outer = doc.getMap('outer')
const inner = new Y.Map() const inner = new Y.Map()
@ -556,36 +574,35 @@ export const testMapDeepObserve = tc => { //FIXME
let events = [] let events = []
outer.observeDeep((evts) => { outer.observeDeep((evts) => {
events = [] events = []
for (let e of evts) { for (const e of evts) {
switch (e.constructor) { switch (e.constructor) {
case Y.YMapEvent: case Y.YMapEvent:
events.push({target: e.target, keys: e.keys}) events.push({ target: e.target, keys: e.keys })
break; break
case Y.YWeakLinkEvent: case Y.YWeakLinkEvent:
events.push({target: e.target}) events.push({ target: e.target })
break; break
default: throw new Error('unexpected event type ' + e.constructor) default: throw new Error('unexpected event type ' + e.constructor)
} }
} }
}) })
inner.set('key', 'value1') inner.set('key', 'value1')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, inner) t.compare(events[0].target, inner)
t.compare(events[0].keys, new Map([['key', {action:'add', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['key', { action: 'add', oldValue: undefined }]]))
events = [] events = []
inner.set('key', 'value2') inner.set('key', 'value2')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, inner) t.compare(events[0].target, inner)
t.compare(events[0].keys, new Map([['key', {action:'update', oldValue: 'value1'}]])) t.compare(events[0].keys, new Map([['key', { action: 'update', oldValue: 'value1' }]]))
events = [] events = []
inner.delete('key') inner.delete('key')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, inner) t.compare(events[0].target, inner)
t.compare(events[0].keys, new Map([['key', {action:'delete', oldValue: 'value2'}]])) t.compare(events[0].keys, new Map([['key', { action: 'delete', oldValue: 'value2' }]]))
} }
/** /**
@ -629,23 +646,25 @@ export const testDeepObserveRecursive = tc => {
* @type {Array<any>} * @type {Array<any>}
*/ */
let events = [] let events = []
m0.observeDeep((e) => events = e) m0.observeDeep((e) => {
events = e
})
m1.set('test-key1', 'value1') m1.set('test-key1', 'value1')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, m1) t.compare(events[0].target, m1)
t.compare(events[0].keys, new Map([['test-key1', {action:'add', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['test-key1', { action: 'add', oldValue: undefined }]]))
events = [] events = []
m2.set('test-key2', 'value2') m2.set('test-key2', 'value2')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, m2) t.compare(events[0].target, m2)
t.compare(events[0].keys, new Map([['test-key2', {action:'add', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['test-key2', { action: 'add', oldValue: undefined }]]))
m1.delete('test-key1') m1.delete('test-key1')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, m1) t.compare(events[0].target, m1)
t.compare(events[0].keys, new Map([['test-key1', {action:'delete', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['test-key1', { action: 'delete', oldValue: undefined }]]))
} }
/** /**
@ -733,8 +752,8 @@ export const testQuoteFormattedText = tc => {
const text2 = /** @type {Y.XmlText} */ (doc.get('text2', Y.XmlText)) const text2 = /** @type {Y.XmlText} */ (doc.get('text2', Y.XmlText))
text.insert(0, 'abcde') text.insert(0, 'abcde')
text.format(0, 1, {b:true}) text.format(0, 1, { b: true })
text.format(1, 3, {i:true}) // '<b>a</b><i>bcd</i>e' text.format(1, 3, { i: true }) // '<b>a</b><i>bcd</i>e'
const l1 = text.quote(0, 2) const l1 = text.quote(0, 2)
t.compare(l1.toString(), '<b>a</b><i>b</i>') t.compare(l1.toString(), '<b>a</b><i>b</i>')
const l2 = text.quote(2, 1) // '<i>c</i>' const l2 = text.quote(2, 1) // '<i>c</i>'
@ -748,8 +767,8 @@ export const testQuoteFormattedText = tc => {
const delta = text2.toDelta() const delta = text2.toDelta()
t.compare(delta, [ t.compare(delta, [
{insert: l1}, { insert: l1 },
{insert: l2}, { insert: l2 },
{insert: l3}, { insert: l3 }
]) ])
} }