Use dispatchTransactions option when creating EditorView (#14743)

Co-authored-by: Tim Down <158919+timdown@users.noreply.github.com>
GitOrigin-RevId: ccc43ead570bdf96e47d1d08fc114ddce32d1293
This commit is contained in:
Alf Eaton 2023-09-14 09:20:23 +01:00 committed by Copybot
parent b21e06952e
commit cf0dc6f132
2 changed files with 35 additions and 24 deletions

View file

@ -41,13 +41,13 @@ function CodeMirrorEditor() {
const view = new EditorView({ const view = new EditorView({
state, state,
dispatch: tr => { dispatchTransactions: trs => {
timer.start(tr) timer.start(trs)
view.update([tr]) view.update(trs)
if (isMounted.current) { if (isMounted.current) {
setState(view.state) setState(view.state)
} }
timer.end(tr, view) timer.end(trs, view)
}, },
}) })
viewRef.current = view viewRef.current = view

View file

@ -81,8 +81,8 @@ function isKeypress(userEventType: string | undefined) {
} }
export function dispatchTimer(): { export function dispatchTimer(): {
start: (tr: Transaction) => void start: (trs: readonly Transaction[]) => void
end: (tr: Transaction, view: EditorView) => void end: (trs: readonly Transaction[], view: EditorView) => void
} { } {
if (!performanceOptionsSupport) { if (!performanceOptionsSupport) {
return { start: () => {}, end: () => {} } return { start: () => {}, end: () => {} }
@ -92,34 +92,45 @@ export function dispatchTimer(): {
let keypressesSinceDomUpdateCount = 0 let keypressesSinceDomUpdateCount = 0
const unpaintedKeypressStartTimes: number[] = [] const unpaintedKeypressStartTimes: number[] = []
const start = (tr: Transaction) => { const start = (trs: readonly Transaction[]) => {
const userEventType = tr.annotation(Transaction.userEvent) const keypressStart = performance.now()
if (isKeypress(userEventType)) { trs.forEach(tr => {
unpaintedKeypressStartTimes.push(performance.now()) const userEventType = tr.annotation(Transaction.userEvent)
}
if (isKeypress(userEventType)) {
unpaintedKeypressStartTimes.push(keypressStart)
}
})
performance.mark(TIMER_START_NAME) performance.mark(TIMER_START_NAME)
} }
const end = (tr: Transaction, view: EditorView) => { const end = (trs: readonly Transaction[], view: EditorView) => {
performance.mark(TIMER_END_NAME) performance.mark(TIMER_END_NAME)
const userEventType = tr.annotation(Transaction.userEvent) let anyInputOrDelete = false
if (isInputOrDelete(userEventType)) { trs.forEach(tr => {
++userEventsSinceDomUpdateCount const userEventType = tr.annotation(Transaction.userEvent)
if (isKeypress(userEventType)) { if (isInputOrDelete(userEventType)) {
++keypressesSinceDomUpdateCount anyInputOrDelete = true
++userEventsSinceDomUpdateCount
if (isKeypress(userEventType)) {
++keypressesSinceDomUpdateCount
}
performance.measure(TIMER_MEASURE_NAME, {
start: TIMER_START_NAME,
end: TIMER_END_NAME,
detail: { userEventType, userEventsSinceDomUpdateCount },
})
} }
})
performance.measure(TIMER_MEASURE_NAME, { if (anyInputOrDelete) {
start: TIMER_START_NAME,
end: TIMER_END_NAME,
detail: { userEventType, userEventsSinceDomUpdateCount },
})
// The `key` property ensures that the measurement task is only run once // The `key` property ensures that the measurement task is only run once
// per measure phase // per measure phase
view.requestMeasure({ view.requestMeasure({
@ -144,7 +155,7 @@ export function dispatchTimer(): {
}) })
} }
latestDocLength = tr.state.doc.length latestDocLength = trs[trs.length - 1].state.doc.length
} }
return { start, end } return { start, end }