8000 #4332 - Fix bug with 'onScrolled' event not firing in all cases by bryphe · Pull Request #2143 · onivim/oni · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.

#4332 - Fix bug with 'onScrolled' event not firing in all cases #2143

Merged
merged 5 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
10000
Diff view
Diff view
40 changes: 23 additions & 17 deletions browser/src/neovim/NeovimInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,12 @@ export class NeovimInstance extends EventEmitter implements INeovimInstance {
this._bufferUpdateManager.notifyModeChanged(newMode)
})

this._disposables = [s1]
const dispatchScroll = () => this._dispatchScrollEvent()

const s2 = this._autoCommands.onCursorMoved.subscribe(dispatchScroll)
const s3 = this._autoCommands.onCursorMovedI.subscribe(dispatchScroll)

this._disposables = [s1, s2, s3]
}

public dispose(): void {
Expand Down Expand Up @@ -656,22 +661,6 @@ export class NeovimInstance extends EventEmitter implements INeovimInstance {
return versionInfo[1].version as any
}

public dispatchScrollEvent(): void {
if (this._pendingScrollTimeout || this._isDisposed) {
return
}

this._pendingScrollTimeout = window.setTimeout(async () => {
if (this._isDisposed) {
return
}

const evt = await this.getContext()
this._onScroll.dispatch(evt)
this._pendingScrollTimeout = null
})
}

public async quit(): Promise<void> {
// This command won't resolve the promise (since it's quitting),
// so we're not awaiting..
Expand Down Expand Up @@ -701,6 +690,22 @@ export class NeovimInstance extends EventEmitter implements INeovimInstance {
}
}

private _dispatchScrollEvent(): void {
if (this._pendingScrollTimeout || this._isDisposed) {
return
}

this._pendingScrollTimeout = window.setTimeout(async () => {
if (this._isDisposed) {
return
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also logging here so I thought this bit being put into the the event loop rather than running synchronously was where things were going missing since if by some quirk of fate if the next scroll event came straight after and the pendingScrollTimeout still existed the function would just return so it might in effect be swallowing scroll events, didn't actually verify that this was happening

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ya, we actually want it to not dispatch too aggressively - the "redraw" place is an especially performance-critical path, so we want to defer dispatching the scroll event until we're done drawing. And there is no point in queuing up multiple calls if we don't need to - we just want to make sure that a scroll event gets out in response to this. It's essentially 'debouncing' here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Makes sense thanks for explaining

const evt = await this.getContext()
this._onScroll.dispatch(evt)
this._pendingScrollTimeout = null
})
}

private _resizeInternal(rows: number, columns: number): void {
if (this._configuration.hasValue("debug.fixedSize")) {
const fixedSize = this._configuration.getValue("debug.fixedSize")
Expand Down Expand Up @@ -760,6 +765,7 @@ export class NeovimInstance extends EventEmitter implements INeovimInstance {
break
case "scroll":
this.emit("action", Actions.scroll(a[0][0 10000 ]))
this._dispatchScrollEvent()
Copy link
Member
@akinsho akinsho Apr 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bryphe ah this was the line I was referring to which I though represented a scroll event from neovim, that was the what I was logging out which was providing a more accurate response to scrolling didnt realise it was from the redraw event

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha ya, I think the "bug" is that we weren't using it for our onScroll event 😄 So we were missing out on this info, which is the most reliable way to check for scroll events when the cursor doesn't move (it misses large scroll events, like G on a long file)

break
case "highlight_set":
const highlightInfo = a[a.length - 1][0]
Expand Down
1 change: 0 additions & 1 deletion browser/src/neovim/NeovimWindowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export class NeovimWindowManager extends Utility.Disposable {
})
.subscribe((tabState: NeovimTabPageState) => {
this._onWindowStateChangedEvent.dispatch(tabState)
this._neovimInstance.dispatchScrollEvent()
})
}

Expand Down
2 changes: 1 addition & 1 deletion test/CiTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as mkdirp from "mkdirp"
import { IFailedTest, Oni, runInProcTest } from "./common"

const LongTimeout = 5000

const CiTests = [
// Core functionality tests
"Api.Buffer.AddLayer",
Expand All @@ -27,6 +26,7 @@ const CiTests = [
"Editor.ExternalCommandLineTest",
"Editor.BufferModifiedState",
"Editor.OpenFile.PathWithSpacesTest",
"Editor.ScrollEventTest",
"Editor.TabModifiedState",
"Editor.CloseTabWithTabModesTabsTest",
"MarkdownPreviewTest",
Expand Down
79 changes: 79 additions & 0 deletions test/ci/Editor.ScrollEventTest.ts
5B9B
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Test script to validate the modified status for tabs.
*/

import * as assert from "assert"
import * as Oni from "oni-api"

import { createNewFile, getElementByClassName } from "./Common"

import * as os from "os"
const createLines = (num: number): string => {
const ret = []

for (let i = 0; i < num; i++) {
ret.push(i)
}

return ret.join(os.EOL)
}

const assertValue = (actual: number, expected: number, msg: string, oni: Oni.Plugin.Api) => {
const passed = actual === expected

const notification = oni.notifications.createItem()
const title = passed ? "Assertion Passed" : "Assertion Failed"
notification.setContents(title, `${msg}\nActual: ${actual}\nExpected:${expected}`)
;(notification as any).setLevel(passed ? "success" : "error")
notification.show()

assert.strictEqual(actual, expected, msg)
}

export const test = async (oni: Oni.Plugin.Api) => {
await oni.automation.waitForEditors()

await createNewFile("js", oni, createLines(500))

let scrollEventHitCount = 0

oni.editors.activeEditor.onBufferScrolled.subscribe(() => {
scrollEventHitCount++
})

await oni.automation.sendKeys("G")

await oni.automation.waitFor(() => scrollEventHitCount === 1)
assertValue(scrollEventHitCount, 1, "A single scroll event should've been triggered by G", oni)

await oni.automation.sendKeys("gg")
await oni.automation.waitFor(() => scrollEventHitCount === 2)
assertValue(scrollEventHitCount, 2, "Another scroll event should've been triggered by gg", oni)

await oni.automation.sendKeys(":50<cr>")
await oni.automation.waitFor(() => scrollEventHitCount === 3)
assertValue(
scrollEventHitCount,
3,
"Another scroll event should've been triggered by navigating to a line",
oni,
)

await oni.automation.sendKeys("<c-e>")
await oni.automation.waitFor(() => scrollEventHitCount === 4)
assertValue(
scrollEventHitCount,
4,
"Another scroll event should've been triggered by scrolling up one line",
oni,
)

await oni.automation.sendKeys("<c-d>")
await oni.automation.waitFor(() => scrollEventHitCount === 5)
assertValue(
scrollEventHitCount,
5,
"Another scroll event should've been triggered by scrolling down one line",
oni,
)
}
0