Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactional operations #1129

Open
adnumaro opened this issue Sep 9, 2023 · 1 comment
Open

Transactional operations #1129

adnumaro opened this issue Sep 9, 2023 · 1 comment
Labels
feature request Feature requested by the community

Comments

@adnumaro
Copy link

adnumaro commented Sep 9, 2023

Is your feature request related to a problem? Please describe.

A clear and concise description of what the problem is.

Given the following tree navigation store in the Storage:

const navigationTree = [
  {
    "id": "nav0",
    "order": 0,
    "name": "nav0",
    "parentId": null,
    "children": [
      {
        "id": "nav0.0",
        "order": 0,
        "name": "nav0.0",
        "parentId": "nav0",
        "children": [
          {
            "id": "nav0.0.0",
            "order": 0,
            "name": "nav0.0.0",
            "parentId": "nav0.0",
            "children": [
              // N elements
            ]
          },
          {
            "id": "nav0.0.1",
            "order": 1,
            "name": "nav0.0.1",
            "parentId": "nav0.0",
            "children": [
              // N elements
            ]
          },
          // N elements
        ]
      },
      // N elements
    ]
  }
  // N elements
]

The user can move each element to any position within the tree. When this happens, for the elements that need it, the order property must be updated.

This generates that N elements within the tree have to be updated at the same time.

The current behavior is that you can only update items one by one. This can lead to race conditions.

Describe the solution you'd like

Have the ability that no operations performed were saved in Storage until a commit of the operations was made, and be able to rollback all before operation if an error occur.

First solucion

async function transactionalFunction (roomId, ...args) {
    const room = this.client.getRoom(roomId)
    const storage = room.getStorage()
    const navigationTree = storage.root.get('navigationTree')

    try {
        // perform several operations (set, update, ...)
        storage.commit()
    } catch (e) {
        storage.roolback()
    }
}

Second solution

async function transactionalFunction (roomId, ...args) {
    const room = this.client.getRoom(roomId)
    const storage = room.getStorage()

     // Automatically commit all operation inside when the callback is finished or rollback if an error occur
    storage.transaction((root) => {
        const navigationTree = root.get('navigationTree')

        // perform several operations (set, update, ...)
    })
}
@adnumaro adnumaro added the feature request Feature requested by the community label Sep 9, 2023
@jrowny
Copy link
Contributor

jrowny commented Sep 18, 2023

Hi @adnumaro, we have a batch operation, room.batch that I think addresses your needs, here's a description from our docs:

  • Batches modifications made during the given function.
  • All the modifications are sent to other clients in a single message.
  • All the subscribers are called only after the batch is over.
  • All the modifications are merged in a single history item (undo/redo).

Let me know if there's a case this doesn't cover for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature requested by the community
Projects
None yet
Development

No branches or pull requests

2 participants