pycrdt icon indicating copy to clipboard operation
pycrdt copied to clipboard

Updates does not trigger subscription

Open Faraphel opened this issue 2 months ago • 1 comments

I am trying to write an application using both Qt and pycrdt to allow for collaborative editing.

Many of my widgets wrap a pycrdt object to store its values or react to changes made by others.

The issue is that updates made with either Doc.apply_update or Map.update will not trigger any of the callback functions registered by the observe or observe_deep function.

import pycrdt


class Node(pycrdt.TypedMap):
    note: pycrdt.Text


class Nodes(pycrdt.TypedArray):
    type: Node


class Boards(pycrdt.TypedMap):
    note: pycrdt.Text


class Project(pycrdt.TypedMap):
    boards: Boards


class Document(pycrdt.TypedDoc):
    project: Project


class BoardsWrapper:
    def __init__(self, internal: Boards):
        self._internal = internal
        self._subscription = self._internal._.observe_deep(self.callback)

    def callback(self, event):
        print(event)


# -- Doc A

documentA = Document()

documentA.project.boards = Boards()
documentA.project.boards.note = pycrdt.Text("hello!")

state = documentA._.get_update()

# -- Doc B

documentB = Document()

documentB.project.boards = Boards()
wrapper = BoardsWrapper(documentB.project.boards)

documentB._.apply_update(state)

# ISSUE: BoardsWrapper.callback was never called when apply_update was used

Faraphel avatar Oct 09 '25 09:10 Faraphel

Both documentA and documentB are assigning a new Map:

documentA.project.boards = Boards()
...
documentB.project.boards = Boards()

There is a conflict so documentB's Map is replaced with documentA's, or the other way around. In any case your observer will not catch any event.

davidbrochart avatar Oct 09 '25 10:10 davidbrochart