Code optimization.
This commit is contained in:
1 parent
12c514aab0
commit
a121c26d63
11 files changed
+211
-37
No files matched your search
@@ -93,8 +93,10 @@ func (adapter *Adapter) CaptureDocumentSoundGeometry(ctx context.Context, object
|
||||
if !adapter.captureSound.Load() {
|
||||
return [2]SoundGeometry{}, nil
|
||||
}
|
||||
result, err := adapter.documentOperation(ctx, treeCaptureRequest{soundObjects: [2]ObjectReference{object, link}})
|
||||
return result.soundGeometry, err
|
||||
return retryDocumentSoundGeometry(ctx, link.Valid(), func(ctx context.Context) ([2]SoundGeometry, error) {
|
||||
result, err := adapter.documentOperation(ctx, treeCaptureRequest{soundObjects: [2]ObjectReference{object, link}})
|
||||
return result.soundGeometry, err
|
||||
})
|
||||
}
|
||||
|
||||
func (adapter *Adapter) captureNativeDocumentSoundGeometry(ctx context.Context, objects [2]ObjectReference) ([2]SoundGeometry, error) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package atspi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A successful ScrollTo can precede the browser's visibility/geometry update.
|
||||
// Retry missing geometry on the calling worker, leaving the native event owner
|
||||
// free to process those updates. Unsupported geometry gets at most 50ms of
|
||||
// extra waiting; native calls remain bounded by the caller's context.
|
||||
func retryDocumentSoundGeometry(ctx context.Context, linked bool, capture func(context.Context) ([2]SoundGeometry, error)) ([2]SoundGeometry, error) {
|
||||
for attempt := 0; ; attempt++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return [2]SoundGeometry{}, err
|
||||
}
|
||||
geometry, err := capture(ctx)
|
||||
if err != nil {
|
||||
return [2]SoundGeometry{}, err
|
||||
}
|
||||
ready := geometry[0].ObjectKnown && geometry[0].ReferenceKnown
|
||||
if linked {
|
||||
ready = ready && geometry[1].ObjectKnown && geometry[1].ReferenceKnown
|
||||
}
|
||||
if ready || attempt == 5 {
|
||||
return geometry, nil
|
||||
}
|
||||
timer := time.NewTimer(10 * time.Millisecond)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return [2]SoundGeometry{}, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package atspi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDocumentSoundGeometryWaitsForBrowserScroll(t *testing.T) {
|
||||
ready := SoundGeometry{ObjectKnown: true, ReferenceKnown: true,
|
||||
Object: Rectangle{X: 18, Y: 147, Width: 102, Height: 13},
|
||||
Reference: Rectangle{Width: 800, Height: 600}}
|
||||
for _, linked := range []bool{false, true} {
|
||||
calls := 0
|
||||
geometry, err := retryDocumentSoundGeometry(context.Background(), linked, func(context.Context) ([2]SoundGeometry, error) {
|
||||
calls++
|
||||
// Scroll completion can reach the heading and link at different times.
|
||||
if calls == 1 {
|
||||
return [2]SoundGeometry{}, nil
|
||||
}
|
||||
if calls == 2 {
|
||||
return [2]SoundGeometry{ready, {}}, nil
|
||||
}
|
||||
return [2]SoundGeometry{ready, ready}, nil
|
||||
})
|
||||
wantCalls := 2
|
||||
if linked {
|
||||
wantCalls = 3
|
||||
}
|
||||
if err != nil || calls != wantCalls || geometry[0] != ready || linked && geometry[1] != ready {
|
||||
t.Fatalf("linked=%t: calls=%d geometry=%+v err=%v", linked, calls, geometry, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentSoundGeometryRetryBoundsAndCancellation(t *testing.T) {
|
||||
for _, scenario := range []string{"ready", "unavailable", "stale", "cancelled", "already cancelled"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
if scenario == "already cancelled" {
|
||||
cancel()
|
||||
}
|
||||
calls := 0
|
||||
_, err := retryDocumentSoundGeometry(ctx, false, func(context.Context) ([2]SoundGeometry, error) {
|
||||
calls++
|
||||
switch scenario {
|
||||
case "ready":
|
||||
return [2]SoundGeometry{{ObjectKnown: true, ReferenceKnown: true}, {}}, nil
|
||||
case "stale":
|
||||
return [2]SoundGeometry{}, ErrStaleObject
|
||||
case "cancelled":
|
||||
cancel()
|
||||
}
|
||||
return [2]SoundGeometry{}, nil
|
||||
})
|
||||
wantCalls, wantErr := 1, error(nil)
|
||||
switch scenario {
|
||||
case "unavailable":
|
||||
wantCalls = 6
|
||||
case "stale":
|
||||
wantErr = ErrStaleObject
|
||||
case "cancelled":
|
||||
wantErr = context.Canceled
|
||||
case "already cancelled":
|
||||
wantCalls, wantErr = 0, context.Canceled
|
||||
}
|
||||
if calls != wantCalls || !errors.Is(err, wantErr) {
|
||||
t.Fatalf("calls=%d err=%v, want calls=%d err=%v", calls, err, wantCalls, wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user