From 90f2a06b5e892ac08e989f5809e2e371ee101ec4 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Tue, 27 Jun 2023 13:20:53 +0200 Subject: [PATCH] throw error when event changes are computed after a transaction --- src/utils/YEvent.js | 9 +++++++++ tests/y-map.tests.js | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/utils/YEvent.js b/src/utils/YEvent.js index 8aafd94f..b47d8c9a 100644 --- a/src/utils/YEvent.js +++ b/src/utils/YEvent.js @@ -6,6 +6,9 @@ import { import * as set from 'lib0/set' import * as array from 'lib0/array' +import * as error from 'lib0/error' + +const errorComputeChanges = 'You must not compute changes after the event-handler fired.' /** * @template {AbstractType} T @@ -84,6 +87,9 @@ export class YEvent { */ get keys () { if (this._keys === null) { + if (this.transaction.doc._transactionCleanups.length === 0) { + throw error.create(errorComputeChanges) + } const keys = new Map() const target = this.target const changed = /** @type Set */ (this.transaction.changed.get(target)) @@ -167,6 +173,9 @@ export class YEvent { get changes () { let changes = this._changes if (changes === null) { + if (this.transaction.doc._transactionCleanups.length === 0) { + throw error.create(errorComputeChanges) + } const target = this.target const added = set.create() const deleted = set.create() diff --git a/tests/y-map.tests.js b/tests/y-map.tests.js index 3356afc1..346bd746 100644 --- a/tests/y-map.tests.js +++ b/tests/y-map.tests.js @@ -8,6 +8,29 @@ import * as Y from '../src/index.js' import * as t from 'lib0/testing' import * as prng from 'lib0/prng' +/** + * Computing event changes after transaction should result in an error. See yjs#539 + * + * @param {t.TestCase} _tc + */ +export const testMapEventError = _tc => { + const doc = new Y.Doc() + const ymap = doc.getMap() + /** + * @type {any} + */ + let event = null + ymap.observe((e) => { + event = e + }) + t.fails(() => { + t.info(event.keys) + }) + t.fails(() => { + t.info(event.keys) + }) +} + /** * @param {t.TestCase} tc */