Dispatching within Update on Plugin

I’m trying to update the current selection from within the update method of a Plugin using: view.dispatch(state.tr.setSelection(newSelection)); But the selection is not updated.

If I ensure the dispatch runs later using a timeout, it works.

The problem appears to be that without the timeout the dispatched transaction starts running before the previous transaction has finished.

The greater problem is that with a relatively short timeout e.g. 100ms, the selection is only changed about 50% of the time. There appears to be a race condition.

Digging into updateStateInner on the view, it appears that there is some domObserver logic that attempts to workaround some browser bugs and selections. I believe this logic is reverting selection and in effect undoing the selection change transaction.

Is it OK to dispatch transactions within update on a plugin? Is there a way to ensure a dispatched transaction runs after the current transaction ends without resorting to setTimeout? Is there any better workaround for the issue I’ve encountered here?

Thanks

Take this with a grain of salt because I’m pretty often wrong about this stuff, but I suspect you’ll have better luck using appendTransaction

Generally, no. Maybe look into appendTransaction instead.

Thanks. appendTransaction works for this use case.