About lazyStructReaderGenerator

Hi, it is difficulties for me to understand the following code, why make a string as parent?? The type of parent is AbstructType or ID, not string, I really want to understand your intention。

        const struct = new Item(
          createID(client, clock),
          null, // left
          (info & binary.BIT8) === binary.BIT8 ? decoder.readLeftID() : null, // origin
          null, // right
          (info & binary.BIT7) === binary.BIT7 ? decoder.readRightID() : null, // right origin
          // @ts-ignore Force writing a string here.
          cantCopyParentInfo ? (decoder.readParentInfo() ? decoder.readString() : decoder.readLeftID()) : null, // parent
          cantCopyParentInfo && (info & binary.BIT6) === binary.BIT6 ? decoder.readString() : null, // parentSub
          readItemContent(decoder, info) // item content
        )

Before an Item is integrated, the parent can be a string (referring to the root-level type-name. E.g. ydoc.get('root-level-type-name')). When the item is integrated, we replace the string with the actual parent type instance.

I think I did this change when working on differential updates (also known as the Alternative Update API). We don’t need to integrate the item and lookup the parent. Hence we keep the parent as a string.

It might be confusing to change the representations of parent. My first draft had two versions of Item IntegratedItem and UnintegratedItem. The UnintegratedItem contains less information.

However, due to performance reasons, I merged them together. It is inefficient to create a bunch of UnintegratedItems and then generate the IntegratedItem objects once they are integrated (doubling the memory allocations for a short period of time and triggering a lot of gc’s).

1 Like