Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize typing a single character #1233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/keyboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export async function keyboard(this: Instance, text: string): Promise<void> {
const actions: KeyboardAction[] = parseKeyDef(this.config.keyboardMap, text)

for (let i = 0; i < actions.length; i++) {
await wait(this.config)
if (i > 0) {
await wait(this.config)
}

await keyboardAction(this, actions[i])
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/misc/wait.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {type Instance} from '../../setup'

export function wait(config: Instance['config']) {
export async function wait(config: Instance['config']): Promise<void> {
const delay = config.delay
if (typeof delay !== 'number') {
return
}
return Promise.all([
await Promise.all([
Comment on lines +3 to +8
Copy link
Member

Choose a reason for hiding this comment

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

This makes the function return Promise<undefined> instead of undefined for delay: null and results in await wait() to add a new micro task.
Are we sure this has no effect?

Copy link
Author

@Janpot Janpot Jan 30, 2025

Choose a reason for hiding this comment

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

Doesn't await wait() always create a new microtask regardless? Doesn't matter what wait() returns, right?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure. I guess it depends on implementation.

Typescript reports 'await' has no effect on the type of this expression. ts(80007) if you try await undefined.

The following results in sync in node@10 and async in node@12:

async function waitAsync() {
    return undefined
}

function waitSync() {
    return undefined
}

(async function () {
    await Promise.race([
        (async () => {
            await waitAsync()
            return 'async'
        })(),
        (async () => {
            await waitSync()
            return 'sync'
        })(),
    ]).then(console.log)
})()

If we're not sure that this has no effect on any platform that is still supported, I wouldn't touch it if I don't have to.

new Promise<void>(resolve => globalThis.setTimeout(() => resolve(), delay)),
config.advanceTimers(delay),
])
Expand Down