diff --git a/.changeset/angular-zoneless-enabled.md b/.changeset/angular-zoneless-enabled.md new file mode 100644 index 000000000..49978fc1b --- /dev/null +++ b/.changeset/angular-zoneless-enabled.md @@ -0,0 +1,5 @@ +--- +'@tanstack/angular-hotkeys': patch +--- + +In zoneless apps, a hotkey callback that changes a hotkey's or provider's `enabled` now takes effect before the browser sends the next key. Before, the change waited for Angular's next scheduled change detection, so a quick second press, such as from Playwright's `keyboard.press`, still matched the old value. Zoneless apps now run change detection in a microtask after a hotkey callback returns, and again when an async callback settles. Keys dispatched in one synchronous loop still see the value from before the loop. diff --git a/.intent/review-state.json b/.intent/review-state.json index 0952485f5..415b26792 100644 --- a/.intent/review-state.json +++ b/.intent/review-state.json @@ -25,10 +25,10 @@ ] }, "skill:packages/angular-hotkeys/skills/angular-hotkeys/SKILL.md": { - "fingerprint": "970de68e52a628bc43b599cd0aea2f74542afd0a0193678330f364b7bab08e44", + "fingerprint": "eaf619d827d363793dd2827684bdc1e5e0ac7fa31f895c04daa05255b9933962", "snapshot": { "docs/framework/angular/guides/provide-hotkeys-and-scopes.md": "eeb6082bcdb07cc21e1fe2c7e7849b260f7931d1c803a41333332e88f28b20b5", - "docs/framework/angular/quick-start.md": "759f4ea079af10bed94b9931ac73a05abd7b911a12e5bfe01fd42a674a7a9652", + "docs/framework/angular/quick-start.md": "d8f9a0d1723f9c64ad4b02ca11144279c0ff4f243f6e58d3fb777dd88781ccca", "packages/angular-hotkeys/skills/angular-hotkeys/SKILL.md": "2a623bf40bdffb6f1f2c64d7159d58ae25a6f8b4681f42e1b0274113ac2b0900", "packages/angular-hotkeys/skills/angular-hotkeys/references/app-hotkeys.md": "afe63a82c2e4b9269b0fece249317a1ad197481cb8d4ae434c5da48d64f0a2b5", "packages/angular-hotkeys/src/hotkeys-provider.ts": "9ba3e5c384a39ff3bbd3f163c61ee8f79e698d7f91c8cc35ff963cb80a12900d", @@ -37,11 +37,11 @@ "packages/angular-hotkeys/src/injectHotkeyRecorder.ts": "1f1ab7d1e41e5dc39721945c9d9152706689990ec57d6b4d6dbc262975a6a13c", "packages/angular-hotkeys/src/injectHotkeys.ts": "c923eba82e55ee9f0357ea0c355eca05719c0aea806a9acbb2b4de55d2d20d42" }, - "head": "1f0a2c627c4a18f005762600966a81e9b8daef3a", + "head": "bc1967b2e5f3b89c32b6e11ddbb6de5a7a117111", "outcome": "no-change", - "reason": "The change shares type and helper definitions across packages and keeps every public name and behavior, so the skill text stays accurate.", + "reason": "Zoneless apps now run change detection in a microtask after hotkey callbacks, so enabled changes apply before the next key. The API is unchanged and the skill's guidance on callbacks, OnPush, zoneless views, and root services still holds; the quick-start sentence documents the timing.", "evidence": [ - "Searched packages/*/skills for each replaced type spelling and changed API; none appear. Public export names and option shapes are unchanged." + "Changed: docs/framework/angular/quick-start.md at bc1967b2e5f3b89c32b6e11ddbb6de5a7a117111" ] }, "skill:packages/ember-hotkeys/skills/ember-hotkeys/SKILL.md": { @@ -277,7 +277,7 @@ ] }, "planning:_artifacts": { - "fingerprint": "7a3e410c78e12302953d78707d5e5549ebddccbffba71deba030ec184ec86a99", + "fingerprint": "6846876bec51eb827dcf5b19f19d1180763e727b52284d480e2273ecae71efb5", "snapshot": { "_artifacts/domain_map.yaml": "9020ca258eebcb3575acad17ed5fc62d53306d6e07d6dc6456c006fd8f05f94b", "_artifacts/skill_spec.md": "3dfe15cd5f88bff08bfe9492e7487f709fcec25af7576a7e81729433e6d1ebef", @@ -285,7 +285,7 @@ "docs/framework/alpine/guides/app-hotkeys.md": "ba8566f13ea2dfe6d63ca80aa5456bb4729bcb8db0c082980171ea7900025def", "docs/framework/alpine/quick-start.md": "e6e78df61dc758f7e934ff157ab0f0207f34ef0159902039405d773efadfc04d", "docs/framework/angular/guides/provide-hotkeys-and-scopes.md": "eeb6082bcdb07cc21e1fe2c7e7849b260f7931d1c803a41333332e88f28b20b5", - "docs/framework/angular/quick-start.md": "759f4ea079af10bed94b9931ac73a05abd7b911a12e5bfe01fd42a674a7a9652", + "docs/framework/angular/quick-start.md": "d8f9a0d1723f9c64ad4b02ca11144279c0ff4f243f6e58d3fb777dd88781ccca", "docs/framework/ember/guides/app-hotkeys.md": "c5dc085875b85b01dcab947f844ac3ddf48e260e01b27665c0b78d33ec4cff0c", "docs/framework/ember/quick-start.md": "7646cf9b51aa85d5085cc6bdf91015655e5937cca6e1a3a152cf354fbf3ab6da", "docs/framework/lit/guides/app-hotkeys.md": "ccaffd075053937eb5ab6d49354c0609c6a103a5d11f92c6844b605085d12e4c", @@ -408,11 +408,11 @@ "packages/vue-hotkeys/src/useHotkeyRecorder.ts": "20040667eec1bf86303fb6738985749c0db2f38db67fd9c34dd9ec1f52306899", "packages/vue-hotkeys/src/useHotkeys.ts": "a01bedb627cd6cad884cf59b2077c33b34289589e6a101fefb5ca1709dabb668" }, - "head": "0840d8fe7630b3b76ee4d19618224c3ef2c1d27b", + "head": "bc1967b2e5f3b89c32b6e11ddbb6de5a7a117111", "outcome": "no-change", - "reason": "Internal cleanup with no public behavior or guidance change.", + "reason": "Zoneless apps now run change detection in a microtask after hotkey callbacks, so enabled changes apply before the next key. The API is unchanged and the skill's guidance on callbacks, OnPush, zoneless views, and root services still holds; the quick-start sentence documents the timing.", "evidence": [ - "Changed: packages/hotkeys/src/hotkey-manager.ts, packages/hotkeys/src/hotkey-recorder.ts, packages/hotkeys/src/match.ts at 0840d8fe7630b3b76ee4d19618224c3ef2c1d27b" + "Changed: docs/framework/angular/quick-start.md at bc1967b2e5f3b89c32b6e11ddbb6de5a7a117111" ] }, "source:.changeset/intent-skills.md": { @@ -7904,14 +7904,16 @@ "evidence": ["packages/alpine-hotkeys/tests/reactive-options.test.ts"] }, "source:packages/angular-hotkeys/src/_zone.ts": { - "fingerprint": "32f40b85f7f4bcdf188e088e061cf4d99c49ed6450147a3dcbb1ad883fa79d27", + "fingerprint": "7f15a5156f166a7d330443156a94e868bec3b8f271b1dadd88446d06861ae527", "snapshot": { - "packages/angular-hotkeys/src/_zone.ts": "f19d61b093d6f0a20a0fc12c3dc95319d3df7317fa2cca8b9471dd80af7a9ea1" + "packages/angular-hotkeys/src/_zone.ts": "655a13f6673bd3869f103bb7ccaa6a6af761a832245031ced5ad8aec010eb777" }, - "head": "e56edd6b54a755f8bc9a34243d3a497c56560840", + "head": "afbafc9504561d910d209d0a02030b26b10be591", "outcome": "no-change", - "reason": "Covered by the skill reviews for the affected adapters in this PR.", - "evidence": ["packages/hotkeys/skills/hotkeys-core/SKILL.md"] + "reason": "Zoneless apps now run change detection in a microtask after hotkey callbacks return and after async callbacks settle, so enabled changes apply before the next key. The API is unchanged and the skill's guidance on callbacks, OnPush, zoneless views, and root services still holds; the quick-start sentence documents the timing.", + "evidence": [ + "Changed: packages/angular-hotkeys/src/_zone.ts at afbafc9504561d910d209d0a02030b26b10be591" + ] }, "source:packages/angular-hotkeys/tests/targets.test.ts": { "fingerprint": "25343857852fb8aa0acf481e9c83ff623b4c959dc0b84acd5d85088fc2168728", @@ -7924,14 +7926,16 @@ "evidence": ["packages/hotkeys/skills/hotkeys-core/SKILL.md"] }, "source:packages/angular-hotkeys/tests/zone.test.ts": { - "fingerprint": "26d06efb2fe0a664d7b1430f24c00cec752f3a4a845012c9a438ad337f69fb36", + "fingerprint": "9c73d17d4167ab16403eae476d02b0de24e81b4d1ad56438af39ec470d94b2ae", "snapshot": { - "packages/angular-hotkeys/tests/zone.test.ts": "393075c2e0de6fa4accc2cc1105762dfb7beebe3ee74a29fac8c454890141965" + "packages/angular-hotkeys/tests/zone.test.ts": "146f9a914b402949e2569a19aad320e95f7a028b901fede65072d357b7381591" }, - "head": "f993101815a9a769d0456da75313e68d89a19ff0", + "head": "bc1967b2e5f3b89c32b6e11ddbb6de5a7a117111", "outcome": "no-change", - "reason": "Tests for keyboard layout labels. Skills don't describe the test suites.", - "evidence": ["packages/angular-hotkeys/tests/zone.test.ts"] + "reason": "Zoneless apps now run change detection in a microtask after hotkey callbacks, so enabled changes apply before the next key. The API is unchanged and the skill's guidance on callbacks, OnPush, zoneless views, and root services still holds; the quick-start sentence documents the timing.", + "evidence": [ + "Changed: packages/angular-hotkeys/tests/zone.test.ts at bc1967b2e5f3b89c32b6e11ddbb6de5a7a117111" + ] }, "source:packages/hotkeys/src/index.ts": { "fingerprint": "1215ef18da2d3d7e657a7e0632ddd09e7d210e391da19f5ce8f7a7e5ca81cf53", @@ -20240,14 +20244,16 @@ "evidence": ["packages/alpine-hotkeys/tests/app-hotkeys.test.ts"] }, "source:packages/angular-hotkeys/tests/change-detection.test.ts": { - "fingerprint": "8888b8943b533c44b8383c0564717582f34c7515ccb32cfed7ef01938b5c0c79", + "fingerprint": "580d80a005e7c481251b5e634d8ea7a0bb3117ff90ba2883f9d19ae0788bac6b", "snapshot": { - "packages/angular-hotkeys/tests/change-detection.test.ts": "c2be32ae76dda391ce727822e886e1c0033667c58e60a06ab80400f6f635a6b8" + "packages/angular-hotkeys/tests/change-detection.test.ts": "62194ca35009d45f7ce09ff1faf53862e04d4f125a47103fbca4907eaf52edc9" }, - "head": "94358d54fd72a38952b6fb68560b48043a3f8277", + "head": "afbafc9504561d910d209d0a02030b26b10be591", "outcome": "no-change", - "reason": "Tests for the recorder's new default and the conflict check context. Skills don't describe the test suites.", - "evidence": ["packages/angular-hotkeys/tests/change-detection.test.ts"] + "reason": "Zoneless apps now run change detection in a microtask after hotkey callbacks return and after async callbacks settle, so enabled changes apply before the next key. The API is unchanged and the skill's guidance on callbacks, OnPush, zoneless views, and root services still holds; the quick-start sentence documents the timing.", + "evidence": [ + "Changed: packages/angular-hotkeys/tests/change-detection.test.ts at afbafc9504561d910d209d0a02030b26b10be591" + ] }, "source:packages/ember-hotkeys/tests/integration/app-hotkeys.test.gts": { "fingerprint": "c0a09eca64d87cbf7e135eda6018b41a81e9a9652e78db71d21e5c9c07fdcf94", diff --git a/docs/framework/angular/quick-start.md b/docs/framework/angular/quick-start.md index 3ad7b9c64..e4b2b5ae5 100644 --- a/docs/framework/angular/quick-start.md +++ b/docs/framework/angular/quick-start.md @@ -31,7 +31,7 @@ export class EditorComponent { Call it in an injection context, such as a constructor or a field initializer. -In apps that use zone.js, hotkeys listen outside the Angular zone, so typing doesn't run change detection on every key. When a hotkey fires, its callback runs inside the zone and marks the component for check, like Angular's own event listeners. The view updates in zoneless apps and OnPush components too. An `async` callback marks the component again when its promise settles, so changes after an `await` render as well. A hotkey registered in a service outside any component, such as a root service, has no component to mark, so its callback must write signals to update OnPush or zoneless views. +In apps that use zone.js, hotkeys listen outside the Angular zone, so typing doesn't run change detection on every key. When a hotkey fires, its callback runs inside the zone and marks the component for check, like Angular's own event listeners. The view updates in zoneless apps and OnPush components too. In zoneless apps, change detection runs in a microtask after the callback, so a change to a signal that feeds `enabled` applies before the browser sends the next key. An `async` callback marks the component again when its promise settles, so changes after an `await` render as well. A hotkey registered in a service outside any component, such as a root service, has no component to mark, so its callback must write signals to update OnPush or zoneless views. `Mod` is Command on macOS and Control on Windows and Linux, so `Mod+S` is the save shortcut each user already knows. The browser's Save dialog stays closed, and the shortcut still works while the user types in the textarea. [Sensible Defaults](../../guides/sensible-defaults) explains these defaults. diff --git a/packages/angular-hotkeys/src/_zone.ts b/packages/angular-hotkeys/src/_zone.ts index 3d7adba28..2869c7d70 100644 --- a/packages/angular-hotkeys/src/_zone.ts +++ b/packages/angular-hotkeys/src/_zone.ts @@ -1,4 +1,13 @@ -import { ChangeDetectorRef, NgZone, inject } from '@angular/core' +import { + ApplicationRef, + ChangeDetectorRef, + ErrorHandler, + NgZone, + inject, +} from '@angular/core' + +/** Apps with a change detection pass queued by `queueTick`. */ +const queuedTicks = new WeakSet() /** * Runs `fn` outside the Angular zone, so core listeners it attaches don't @@ -15,25 +24,62 @@ export function runOutsideZone(fn: () => T): T { * updates OnPush views and schedules change detection in zoneless apps. * A callback that returns a promise marks again when the promise settles, so * changes made after an `await` also reach the view. Outside a component, such - * as in a root service, there's no view to mark. Must be called in an - * injection context. + * as in a root service, there's no view to mark. + * + * Zoneless apps then run change detection in a microtask, after the callback + * returns and again after its promise settles, instead of on the next frame. + * Effects that apply options such as `enabled` run before the browser sends + * the next key. Must be called in an injection context. */ export function injectWrapCallback(): , TResult>( callback: (...args: TArgs) => TResult, ) => (...args: TArgs) => TResult { const zone = inject(NgZone) const changeDetector = inject(ChangeDetectorRef, { optional: true }) - const mark = () => changeDetector?.markForCheck() + // Zoneless apps provide a `NoopNgZone`, which isn't an `NgZone`. + const zoneless = !(zone instanceof NgZone) + const appRef = inject(ApplicationRef) + const update = () => { + changeDetector?.markForCheck() + if (zoneless) queueTick(appRef) + } return (callback) => (...args) => zone.run(() => { try { const result = callback(...args) // `finally` keeps a rejection unhandled, so it's still reported. - if (changeDetector && result instanceof Promise) result.finally(mark) + if ((changeDetector || zoneless) && result instanceof Promise) { + result.finally(update) + } return result } finally { - mark() + update() } }) } + +/** + * Runs change detection in a microtask, once for all the callbacks of a key + * press. Errors go to the `ErrorHandler`. + * + * After a tick it didn't start, Angular's zoneless scheduler runs the rest of + * the task's passes in microtasks, and dev mode throws NG0103 after 100 of + * them. Real key presses arrive in separate tasks, so only a test that sends + * over 100 matching keys or awaited writes in one task reaches the limit. + */ +function queueTick(appRef: ApplicationRef): void { + if (queuedTicks.has(appRef)) return + queuedTicks.add(appRef) + queueMicrotask(() => { + queuedTicks.delete(appRef) + if (appRef.destroyed) return + try { + appRef.tick() + } catch (error) { + // Resolved here, because an `ErrorHandler` may inject a service that + // registers hotkeys. + appRef.injector.get(ErrorHandler).handleError(error) + } + }) +} diff --git a/packages/angular-hotkeys/tests/change-detection.test.ts b/packages/angular-hotkeys/tests/change-detection.test.ts index a653741eb..4efc8044b 100644 --- a/packages/angular-hotkeys/tests/change-detection.test.ts +++ b/packages/angular-hotkeys/tests/change-detection.test.ts @@ -1,6 +1,7 @@ import { ChangeDetectionStrategy, Component, + ErrorHandler, Injectable, inject, provideZonelessChangeDetection, @@ -8,7 +9,8 @@ import { } from '@angular/core' import { TestBed } from '@angular/core/testing' import { HotkeyManager } from '@tanstack/hotkeys' -import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { provideHotkeys } from '../src/hotkeys-provider' import { injectHotkey } from '../src/injectHotkey' import { injectHotkeyRecorder } from '../src/injectHotkeyRecorder' import { injectHotkeys } from '../src/injectHotkeys' @@ -121,6 +123,136 @@ Component({ changeDetection: ChangeDetectionStrategy.OnPush, })(ShortcutsView) +/** + * Types each key a microtask apart: sooner than Angular's scheduled change + * detection, which waits for a timeout or the next frame. + */ +async function typeEach(keys: Array) { + for (const key of keys) { + type(key) + await Promise.resolve() + } +} + +const log: Array = [] + +// The inner scope moves down rows until the last one, then the outer scope +// takes the key. +class Rows { + row = signal(0) + constructor() { + injectHotkey( + 'ArrowDown', + () => { + log.push(`move from ${this.row()}`) + this.row.update((row) => row + 1) + }, + () => ({ enabled: this.row() < 2 }), + ) + } +} +Component({ + selector: 'app-rows', + template: '{{ row() }}', + providers: [provideHotkeys({ scope: 'rows' })], +})(Rows) + +class Grid { + constructor() { + injectHotkey('ArrowDown', () => log.push('cross')) + } +} +Component({ + selector: 'app-grid', + imports: [Rows], + template: '', + providers: [provideHotkeys({ scope: 'grid' })], +})(Grid) + +// The provider's own `enabled` turns the scope off after one key. +class Gate { + open = signal(true) +} +Injectable({ providedIn: 'root' })(Gate) + +class Once { + constructor() { + const gate = inject(Gate) + injectHotkey('K', () => { + log.push('once') + gate.open.set(false) + }) + } +} +Component({ + selector: 'app-once', + template: '', + providers: [ + provideHotkeys({ scope: 'once', enabled: () => inject(Gate).open() }), + ], +})(Once) + +// Two hotkeys on the same key. +class Pair { + count = signal(0) + checks = 0 + constructor() { + injectHotkey('K', () => this.count.update((n) => n + 1)) + injectHotkey('K', () => this.count.update((n) => n + 1)) + } + ngDoCheck() { + this.checks++ + } +} +Component({ selector: 'app-pair', template: '{{ count() }}' })(Pair) + +// An async callback turns its hotkey off once the save finishes. +class AsyncOnce { + enabled = signal(true) + calls = 0 + saved = Promise.withResolvers() + constructor() { + injectHotkey( + 'K', + async () => { + this.calls++ + await this.saved.promise + this.enabled.set(false) + }, + () => ({ enabled: this.enabled() }), + ) + } +} +Component({ selector: 'app-async-once', template: '' })(AsyncOnce) + +// Its template throws once a hotkey sets `broken`. +class BrokenView { + broken = signal(false) + otherCalls = 0 + constructor() { + injectHotkey('K', () => this.broken.set(true)) + injectHotkey('K', () => this.otherCalls++) + } + get label() { + if (this.broken()) throw new Error('broken view') + return '' + } +} +Component({ selector: 'app-broken-view', template: '{{ label }}' })(BrokenView) + +// An error handler that depends on a service with hotkeys. +class Toasts { + constructor() { + injectHotkey('Escape', () => {}) + } +} +Injectable({ providedIn: 'root' })(Toasts) + +class ToastErrorHandler { + toasts = inject(Toasts) + handleError() {} +} + describe('change detection in zoneless apps', () => { beforeEach(() => { TestBed.configureTestingModule({ @@ -131,6 +263,7 @@ describe('change detection in zoneless apps', () => { afterEach(() => { TestBed.resetTestingModule() HotkeyManager.resetInstance() + log.length = 0 }) for (const [name, strategy] of strategies) { @@ -195,4 +328,57 @@ describe('change detection in zoneless apps', () => { await fixture.whenStable() expect(fixture.nativeElement.textContent).toBe('1') }) + + it("applies a hotkey's enabled change before the next key", async () => { + const fixture = TestBed.createComponent(Grid) + await fixture.whenStable() + await typeEach(['ArrowDown', 'ArrowDown', 'ArrowDown', 'ArrowDown']) + expect(log).toEqual(['move from 0', 'move from 1', 'cross', 'cross']) + }) + + it("applies a provider's enabled change before the next key", async () => { + const fixture = TestBed.createComponent(Once) + await fixture.whenStable() + await typeEach(['k', 'k']) + expect(log).toEqual(['once']) + }) + + it("applies an async callback's enabled change before the next key", async () => { + const fixture = TestBed.createComponent(AsyncOnce) + await fixture.whenStable() + const component = fixture.componentInstance + await typeEach(['k']) + component.saved.resolve() + // Lets the callback finish, still sooner than scheduled change detection. + for (let i = 0; i < 5; i++) await Promise.resolve() + await typeEach(['k', 'k']) + expect(component.calls).toBe(1) + }) + + it('renders once for all the callbacks of a key', async () => { + const fixture = TestBed.createComponent(Pair) + await fixture.whenStable() + const before = fixture.componentInstance.checks + type('k') + await fixture.whenStable() + expect(fixture.nativeElement.textContent).toBe('2') + expect(fixture.componentInstance.checks - before).toBe(1) + }) + + it('reports a view error to the ErrorHandler and runs the other hotkeys', async () => { + const handleError = vi.fn() + TestBed.overrideProvider(ErrorHandler, { useValue: { handleError } }) + const fixture = TestBed.createComponent(BrokenView) + await fixture.whenStable() + await typeEach(['k']) + expect(handleError).toHaveBeenCalledWith( + expect.objectContaining({ message: 'broken view' }), + ) + expect(fixture.componentInstance.otherCalls).toBe(1) + }) + + it('works with an ErrorHandler that injects a service with hotkeys', () => { + TestBed.overrideProvider(ErrorHandler, { useClass: ToastErrorHandler }) + expect(() => TestBed.inject(ErrorHandler)).not.toThrow() + }) }) diff --git a/packages/angular-hotkeys/tests/zone.test.ts b/packages/angular-hotkeys/tests/zone.test.ts index ba4cd0a3a..60996687c 100644 --- a/packages/angular-hotkeys/tests/zone.test.ts +++ b/packages/angular-hotkeys/tests/zone.test.ts @@ -78,6 +78,17 @@ describe('zone.js apps', () => { expect(counter.turns).toBeGreaterThan(0) }) + it('leaves change detection after a callback to zone.js', async () => { + const { counter } = await setup() + // Zone.js calls Angular's internal `_tick`, so only the zoneless pass + // calls the public `tick`. + const tick = vi.spyOn(TestBed.inject(ApplicationRef), 'tick') + type('k') + await Promise.resolve() + expect(counter.turns).toBeGreaterThan(0) + expect(tick).not.toHaveBeenCalled() + }) + it('attaches the key state tracker outside the zone', async () => { // happy-dom's window listeners escape zone.js, so check where they attach. const attached: Array = []