Skip to content

Commit

Permalink
throw error when event changes are computed after a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jun 27, 2023
1 parent 8586806 commit 90f2a06
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/utils/YEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>} T
Expand Down Expand Up @@ -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<string|null> */ (this.transaction.changed.get(target))
Expand Down Expand Up @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions tests/y-map.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 90f2a06

Please sign in to comment.