y-crdt/ypy

Pythonic API

davidbrochart opened this issue · 0 comments

Currently the API for the Y data structures (YText, YArray, YMap) differs from the API for the equivalent Python structures (str, list, dict, respectively).
For instance:

Ypy Python
ytext.extend(txn, "foo") text += "foo"
yarray.insert_range(txn, 3, [5, 2]) l =l[:3] + [5, 2] + l[3:]
ymap.set(txn, "key", "value") d["key"] = "value"

It seems to me that this is because the transaction object txn has to be passed around, and so I'm wondering if it has to be?
A transaction is tied to a document, and so are the root types. For instance in the following code:

doc = Y.YDoc()
ytext = doc.get_text("my_text")

with doc.begin_transaction() as txn:
    ytext.extend(txn, "foo")

ytext is a text root type of doc, and txn is the current transaction of doc. If txn were stored in doc, then ytext could have access to it, and there would be no need to pass it around, like so:

with doc.begin_transaction():  # stores txn in doc
    ytext.extend("foo")  # ytext, which is tied to doc, can access txn
    # then this becomes possible:
    # ytext += "foo"

I started experimenting with this idea in pycrdt, but maybe I'm missing something that will make this approach not viable.
Any feedback?