@@ -17,6 +17,12 @@ import * as map from 'lib0/map'
17
17
import * as iterator from 'lib0/iterator'
18
18
import * as error from 'lib0/error'
19
19
import * as math from 'lib0/math'
20
+ import * as log from 'lib0/logging'
21
+
22
+ /**
23
+ * https://docs.yjs.dev/getting-started/working-with-shared-types#caveats
24
+ */
25
+ export const warnPrematureAccess = ( ) => { log . warn ( 'Invalid access: Add Yjs type to a document before reading data.' ) }
20
26
21
27
const maxSearchMarker = 80
22
28
@@ -215,6 +221,7 @@ export const updateMarkerChanges = (searchMarker, index, len) => {
215
221
* @return {Array<Item> }
216
222
*/
217
223
export const getTypeChildren = t => {
224
+ t . doc ?? warnPrematureAccess ( )
218
225
let s = t . _start
219
226
const arr = [ ]
220
227
while ( s ) {
@@ -408,6 +415,7 @@ export class AbstractType {
408
415
* @function
409
416
*/
410
417
export const typeListSlice = ( type , start , end ) => {
418
+ type . doc ?? warnPrematureAccess ( )
411
419
if ( start < 0 ) {
412
420
start = type . _length + start
413
421
}
@@ -443,6 +451,7 @@ export const typeListSlice = (type, start, end) => {
443
451
* @function
444
452
*/
445
453
export const typeListToArray = type => {
454
+ type . doc ?? warnPrematureAccess ( )
446
455
const cs = [ ]
447
456
let n = type . _start
448
457
while ( n !== null ) {
@@ -492,6 +501,7 @@ export const typeListToArraySnapshot = (type, snapshot) => {
492
501
export const typeListForEach = ( type , f ) => {
493
502
let index = 0
494
503
let n = type . _start
504
+ type . doc ?? warnPrematureAccess ( )
495
505
while ( n !== null ) {
496
506
if ( n . countable && ! n . deleted ) {
497
507
const c = n . content . getContent ( )
@@ -606,6 +616,7 @@ export const typeListForEachSnapshot = (type, f, snapshot) => {
606
616
* @function
607
617
*/
608
618
export const typeListGet = ( type , index ) => {
619
+ type . doc ?? warnPrematureAccess ( )
609
620
const marker = findMarker ( type , index )
610
621
let n = type . _start
611
622
if ( marker !== null ) {
@@ -874,6 +885,7 @@ export const typeMapSet = (transaction, parent, key, value) => {
874
885
* @function
875
886
*/
876
887
export const typeMapGet = ( parent , key ) => {
888
+ parent . doc ?? warnPrematureAccess ( )
877
889
const val = parent . _map . get ( key )
878
890
return val !== undefined && ! val . deleted ? val . content . getContent ( ) [ val . length - 1 ] : undefined
879
891
}
@@ -890,6 +902,7 @@ export const typeMapGetAll = (parent) => {
890
902
* @type {Object<string,any> }
891
903
*/
892
904
const res = { }
905
+ parent . doc ?? warnPrematureAccess ( )
893
906
parent . _map . forEach ( ( value , key ) => {
894
907
if ( ! value . deleted ) {
895
908
res [ key ] = value . content . getContent ( ) [ value . length - 1 ]
@@ -907,6 +920,7 @@ export const typeMapGetAll = (parent) => {
907
920
* @function
908
921
*/
909
922
export const typeMapHas = ( parent , key ) => {
923
+ parent . doc ?? warnPrematureAccess ( )
910
924
const val = parent . _map . get ( key )
911
925
return val !== undefined && ! val . deleted
912
926
}
@@ -957,10 +971,13 @@ export const typeMapGetAllSnapshot = (parent, snapshot) => {
957
971
}
958
972
959
973
/**
960
- * @param {Map<string,Item> } map
974
+ * @param {AbstractType<any> & { _map: Map<string, Item> } } type
961
975
* @return {IterableIterator<Array<any>> }
962
976
*
963
977
* @private
964
978
* @function
965
979
*/
966
- export const createMapIterator = map => iterator . iteratorFilter ( map . entries ( ) , /** @param {any } entry */ entry => ! entry [ 1 ] . deleted )
980
+ export const createMapIterator = type => {
981
+ type . doc ?? warnPrematureAccess ( )
982
+ return iterator . iteratorFilter ( type . _map . entries ( ) , /** @param {any } entry */ entry => ! entry [ 1 ] . deleted )
983
+ }
0 commit comments