#667 - add sanity messages when data is read before type is added to a document.
This commit is contained in:
parent
8cd1a482bb
commit
3bf44b9850
@ -17,6 +17,12 @@ import * as map from 'lib0/map'
|
|||||||
import * as iterator from 'lib0/iterator'
|
import * as iterator from 'lib0/iterator'
|
||||||
import * as error from 'lib0/error'
|
import * as error from 'lib0/error'
|
||||||
import * as math from 'lib0/math'
|
import * as math from 'lib0/math'
|
||||||
|
import * as log from 'lib0/logging'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://docs.yjs.dev/getting-started/working-with-shared-types#caveats
|
||||||
|
*/
|
||||||
|
export const warnPrematureAccess = () => { log.warn('Invalid access: Add Yjs type to a document before reading data.') }
|
||||||
|
|
||||||
const maxSearchMarker = 80
|
const maxSearchMarker = 80
|
||||||
|
|
||||||
@ -215,6 +221,7 @@ export const updateMarkerChanges = (searchMarker, index, len) => {
|
|||||||
* @return {Array<Item>}
|
* @return {Array<Item>}
|
||||||
*/
|
*/
|
||||||
export const getTypeChildren = t => {
|
export const getTypeChildren = t => {
|
||||||
|
t.doc ?? warnPrematureAccess()
|
||||||
let s = t._start
|
let s = t._start
|
||||||
const arr = []
|
const arr = []
|
||||||
while (s) {
|
while (s) {
|
||||||
@ -408,6 +415,7 @@ export class AbstractType {
|
|||||||
* @function
|
* @function
|
||||||
*/
|
*/
|
||||||
export const typeListSlice = (type, start, end) => {
|
export const typeListSlice = (type, start, end) => {
|
||||||
|
type.doc ?? warnPrematureAccess()
|
||||||
if (start < 0) {
|
if (start < 0) {
|
||||||
start = type._length + start
|
start = type._length + start
|
||||||
}
|
}
|
||||||
@ -443,6 +451,7 @@ export const typeListSlice = (type, start, end) => {
|
|||||||
* @function
|
* @function
|
||||||
*/
|
*/
|
||||||
export const typeListToArray = type => {
|
export const typeListToArray = type => {
|
||||||
|
type.doc ?? warnPrematureAccess()
|
||||||
const cs = []
|
const cs = []
|
||||||
let n = type._start
|
let n = type._start
|
||||||
while (n !== null) {
|
while (n !== null) {
|
||||||
@ -492,6 +501,7 @@ export const typeListToArraySnapshot = (type, snapshot) => {
|
|||||||
export const typeListForEach = (type, f) => {
|
export const typeListForEach = (type, f) => {
|
||||||
let index = 0
|
let index = 0
|
||||||
let n = type._start
|
let n = type._start
|
||||||
|
type.doc ?? warnPrematureAccess()
|
||||||
while (n !== null) {
|
while (n !== null) {
|
||||||
if (n.countable && !n.deleted) {
|
if (n.countable && !n.deleted) {
|
||||||
const c = n.content.getContent()
|
const c = n.content.getContent()
|
||||||
@ -606,6 +616,7 @@ export const typeListForEachSnapshot = (type, f, snapshot) => {
|
|||||||
* @function
|
* @function
|
||||||
*/
|
*/
|
||||||
export const typeListGet = (type, index) => {
|
export const typeListGet = (type, index) => {
|
||||||
|
type.doc ?? warnPrematureAccess()
|
||||||
const marker = findMarker(type, index)
|
const marker = findMarker(type, index)
|
||||||
let n = type._start
|
let n = type._start
|
||||||
if (marker !== null) {
|
if (marker !== null) {
|
||||||
@ -874,6 +885,7 @@ export const typeMapSet = (transaction, parent, key, value) => {
|
|||||||
* @function
|
* @function
|
||||||
*/
|
*/
|
||||||
export const typeMapGet = (parent, key) => {
|
export const typeMapGet = (parent, key) => {
|
||||||
|
parent.doc ?? warnPrematureAccess()
|
||||||
const val = parent._map.get(key)
|
const val = parent._map.get(key)
|
||||||
return val !== undefined && !val.deleted ? val.content.getContent()[val.length - 1] : undefined
|
return val !== undefined && !val.deleted ? val.content.getContent()[val.length - 1] : undefined
|
||||||
}
|
}
|
||||||
@ -890,6 +902,7 @@ export const typeMapGetAll = (parent) => {
|
|||||||
* @type {Object<string,any>}
|
* @type {Object<string,any>}
|
||||||
*/
|
*/
|
||||||
const res = {}
|
const res = {}
|
||||||
|
parent.doc ?? warnPrematureAccess()
|
||||||
parent._map.forEach((value, key) => {
|
parent._map.forEach((value, key) => {
|
||||||
if (!value.deleted) {
|
if (!value.deleted) {
|
||||||
res[key] = value.content.getContent()[value.length - 1]
|
res[key] = value.content.getContent()[value.length - 1]
|
||||||
@ -907,6 +920,7 @@ export const typeMapGetAll = (parent) => {
|
|||||||
* @function
|
* @function
|
||||||
*/
|
*/
|
||||||
export const typeMapHas = (parent, key) => {
|
export const typeMapHas = (parent, key) => {
|
||||||
|
parent.doc ?? warnPrematureAccess()
|
||||||
const val = parent._map.get(key)
|
const val = parent._map.get(key)
|
||||||
return val !== undefined && !val.deleted
|
return val !== undefined && !val.deleted
|
||||||
}
|
}
|
||||||
@ -957,10 +971,13 @@ export const typeMapGetAllSnapshot = (parent, snapshot) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Map<string,Item>} map
|
* @param {AbstractType<any> & { _map: Map<string, Item> }} type
|
||||||
* @return {IterableIterator<Array<any>>}
|
* @return {IterableIterator<Array<any>>}
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @function
|
* @function
|
||||||
*/
|
*/
|
||||||
export const createMapIterator = map => iterator.iteratorFilter(map.entries(), /** @param {any} entry */ entry => !entry[1].deleted)
|
export const createMapIterator = type => {
|
||||||
|
type.doc ?? warnPrematureAccess()
|
||||||
|
return iterator.iteratorFilter(type._map.entries(), /** @param {any} entry */ entry => !entry[1].deleted)
|
||||||
|
}
|
||||||
|
@ -16,6 +16,7 @@ import {
|
|||||||
YArrayRefID,
|
YArrayRefID,
|
||||||
callTypeObservers,
|
callTypeObservers,
|
||||||
transact,
|
transact,
|
||||||
|
warnPrematureAccess,
|
||||||
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item // eslint-disable-line
|
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item // eslint-disable-line
|
||||||
} from '../internals.js'
|
} from '../internals.js'
|
||||||
import { typeListSlice } from './AbstractType.js'
|
import { typeListSlice } from './AbstractType.js'
|
||||||
@ -104,6 +105,7 @@ export class YArray extends AbstractType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get length () {
|
get length () {
|
||||||
|
this.doc ?? warnPrematureAccess()
|
||||||
return this._length
|
return this._length
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ import {
|
|||||||
YMapRefID,
|
YMapRefID,
|
||||||
callTypeObservers,
|
callTypeObservers,
|
||||||
transact,
|
transact,
|
||||||
|
warnPrematureAccess,
|
||||||
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item // eslint-disable-line
|
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Transaction, Item // eslint-disable-line
|
||||||
} from '../internals.js'
|
} from '../internals.js'
|
||||||
|
|
||||||
@ -121,6 +122,7 @@ export class YMap extends AbstractType {
|
|||||||
* @return {Object<string,any>}
|
* @return {Object<string,any>}
|
||||||
*/
|
*/
|
||||||
toJSON () {
|
toJSON () {
|
||||||
|
this.doc ?? warnPrematureAccess()
|
||||||
/**
|
/**
|
||||||
* @type {Object<string,MapType>}
|
* @type {Object<string,MapType>}
|
||||||
*/
|
*/
|
||||||
@ -140,7 +142,7 @@ export class YMap extends AbstractType {
|
|||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
get size () {
|
get size () {
|
||||||
return [...createMapIterator(this._map)].length
|
return [...createMapIterator(this)].length
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -149,7 +151,7 @@ export class YMap extends AbstractType {
|
|||||||
* @return {IterableIterator<string>}
|
* @return {IterableIterator<string>}
|
||||||
*/
|
*/
|
||||||
keys () {
|
keys () {
|
||||||
return iterator.iteratorMap(createMapIterator(this._map), /** @param {any} v */ v => v[0])
|
return iterator.iteratorMap(createMapIterator(this), /** @param {any} v */ v => v[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -158,7 +160,7 @@ export class YMap extends AbstractType {
|
|||||||
* @return {IterableIterator<MapType>}
|
* @return {IterableIterator<MapType>}
|
||||||
*/
|
*/
|
||||||
values () {
|
values () {
|
||||||
return iterator.iteratorMap(createMapIterator(this._map), /** @param {any} v */ v => v[1].content.getContent()[v[1].length - 1])
|
return iterator.iteratorMap(createMapIterator(this), /** @param {any} v */ v => v[1].content.getContent()[v[1].length - 1])
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -167,7 +169,7 @@ export class YMap extends AbstractType {
|
|||||||
* @return {IterableIterator<[string, MapType]>}
|
* @return {IterableIterator<[string, MapType]>}
|
||||||
*/
|
*/
|
||||||
entries () {
|
entries () {
|
||||||
return iterator.iteratorMap(createMapIterator(this._map), /** @param {any} v */ v => /** @type {any} */ ([v[0], v[1].content.getContent()[v[1].length - 1]]))
|
return iterator.iteratorMap(createMapIterator(this), /** @param {any} v */ v => /** @type {any} */ ([v[0], v[1].content.getContent()[v[1].length - 1]]))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -176,6 +178,7 @@ export class YMap extends AbstractType {
|
|||||||
* @param {function(MapType,string,YMap<MapType>):void} f A function to execute on every element of this YArray.
|
* @param {function(MapType,string,YMap<MapType>):void} f A function to execute on every element of this YArray.
|
||||||
*/
|
*/
|
||||||
forEach (f) {
|
forEach (f) {
|
||||||
|
this.doc ?? warnPrematureAccess()
|
||||||
this._map.forEach((item, key) => {
|
this._map.forEach((item, key) => {
|
||||||
if (!item.deleted) {
|
if (!item.deleted) {
|
||||||
f(item.content.getContent()[item.length - 1], key, this)
|
f(item.content.getContent()[item.length - 1], key, this)
|
||||||
|
@ -26,6 +26,7 @@ import {
|
|||||||
typeMapGetAll,
|
typeMapGetAll,
|
||||||
updateMarkerChanges,
|
updateMarkerChanges,
|
||||||
ContentType,
|
ContentType,
|
||||||
|
warnPrematureAccess,
|
||||||
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, ID, Doc, Item, Snapshot, Transaction // eslint-disable-line
|
ArraySearchMarker, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, ID, Doc, Item, Snapshot, Transaction // eslint-disable-line
|
||||||
} from '../internals.js'
|
} from '../internals.js'
|
||||||
|
|
||||||
@ -875,6 +876,7 @@ export class YText extends AbstractType {
|
|||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
get length () {
|
get length () {
|
||||||
|
this.doc ?? warnPrematureAccess()
|
||||||
return this._length
|
return this._length
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -931,6 +933,7 @@ export class YText extends AbstractType {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
toString () {
|
toString () {
|
||||||
|
this.doc ?? warnPrematureAccess()
|
||||||
let str = ''
|
let str = ''
|
||||||
/**
|
/**
|
||||||
* @type {Item|null}
|
* @type {Item|null}
|
||||||
@ -1004,6 +1007,7 @@ export class YText extends AbstractType {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
toDelta (snapshot, prevSnapshot, computeYChange) {
|
toDelta (snapshot, prevSnapshot, computeYChange) {
|
||||||
|
this.doc ?? warnPrematureAccess()
|
||||||
/**
|
/**
|
||||||
* @type{Array<any>}
|
* @type{Array<any>}
|
||||||
*/
|
*/
|
||||||
|
@ -17,6 +17,7 @@ import {
|
|||||||
transact,
|
transact,
|
||||||
typeListGet,
|
typeListGet,
|
||||||
typeListSlice,
|
typeListSlice,
|
||||||
|
warnPrematureAccess,
|
||||||
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, ContentType, Transaction, Item, YXmlText, YXmlHook // eslint-disable-line
|
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, ContentType, Transaction, Item, YXmlText, YXmlHook // eslint-disable-line
|
||||||
} from '../internals.js'
|
} from '../internals.js'
|
||||||
|
|
||||||
@ -66,6 +67,7 @@ export class YXmlTreeWalker {
|
|||||||
*/
|
*/
|
||||||
this._currentNode = /** @type {Item} */ (root._start)
|
this._currentNode = /** @type {Item} */ (root._start)
|
||||||
this._firstCall = true
|
this._firstCall = true
|
||||||
|
root.doc ?? warnPrematureAccess()
|
||||||
}
|
}
|
||||||
|
|
||||||
[Symbol.iterator] () {
|
[Symbol.iterator] () {
|
||||||
@ -177,6 +179,7 @@ export class YXmlFragment extends AbstractType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get length () {
|
get length () {
|
||||||
|
this.doc ?? warnPrematureAccess()
|
||||||
return this._prelimContent === null ? this._length : this._prelimContent.length
|
return this._prelimContent === null ? this._length : this._prelimContent.length
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user