1818import * as input from '../../input' ;
1919
2020import type * as types from '../../types' ;
21- import type { WVSession } from './wvConnection' ;
2221import type { Progress } from '../../progress' ;
2322
2423function modifierFlags ( modifiers : Set < types . KeyboardModifier > ) {
@@ -51,11 +50,13 @@ function toButtonsMask(buttons: Set<types.MouseButton>): number {
5150 return mask ;
5251}
5352
53+ export type DispatchWebViewInput = ( progress : Progress , method : string , params : any ) => Promise < void > ;
54+
5455export class RawKeyboardImpl implements input . RawKeyboard {
55- private _session : WVSession | undefined ;
56+ private _dispatcher : DispatchWebViewInput ;
5657
57- setSession ( session : WVSession ) {
58- this . _session = session ;
58+ constructor ( dispatcher : DispatchWebViewInput ) {
59+ this . _dispatcher = dispatcher ;
5960 }
6061
6162 async keydown ( progress : Progress , modifiers : Set < types . KeyboardModifier > , keyName : string , description : input . KeyDescription , autoRepeat : boolean ) : Promise < void > {
@@ -65,29 +66,29 @@ export class RawKeyboardImpl implements input.RawKeyboard {
6566 ...modifierFlags ( modifiers ) ,
6667 ...( text ? { text } : { } ) ,
6768 } ;
68- await callWebViewInput ( progress , this . _session , 'keydown' , params ) ;
69+ await this . _dispatcher ( progress , 'keydown' , params ) ;
6970 }
7071
7172 async keyup ( progress : Progress , modifiers : Set < types . KeyboardModifier > , keyName : string , description : input . KeyDescription ) : Promise < void > {
7273 const { code, keyCode, key, location } = description ;
7374 const params = { code, key, keyCode, location, ...modifierFlags ( modifiers ) } ;
74- await callWebViewInput ( progress , this . _session , 'keyup' , params ) ;
75+ await this . _dispatcher ( progress , 'keyup' , params ) ;
7576 }
7677
7778 async sendText ( progress : Progress , text : string ) : Promise < void > {
78- await callWebViewInput ( progress , this . _session , 'insertText' , text ) ;
79+ await this . _dispatcher ( progress , 'insertText' , text ) ;
7980 }
8081}
8182
8283export class RawMouseImpl implements input . RawMouse {
83- private _session : WVSession | undefined ;
84+ private _dispatcher : DispatchWebViewInput ;
8485
85- setSession ( session : WVSession ) {
86- this . _session = session ;
86+ constructor ( dispatcher : DispatchWebViewInput ) {
87+ this . _dispatcher = dispatcher ;
8788 }
8889
8990 async move ( progress : Progress , x : number , y : number , button : types . MouseButton | 'none' , buttons : Set < types . MouseButton > , modifiers : Set < types . KeyboardModifier > , forClick : boolean ) : Promise < void > {
90- await callWebViewInput ( progress , this . _session , 'mouseMove' , {
91+ await this . _dispatcher ( progress , 'mouseMove' , {
9192 x, y, button : buttonToNumber ( button ) , buttons : toButtonsMask ( buttons ) , ...modifierFlags ( modifiers ) ,
9293 } ) ;
9394 }
@@ -114,43 +115,24 @@ export class RawMouseImpl implements input.RawMouse {
114115 }
115116
116117 async wheel ( progress : Progress , x : number , y : number , buttons : Set < types . MouseButton > , modifiers : Set < types . KeyboardModifier > , deltaX : number , deltaY : number ) : Promise < void > {
117- await callWebViewInput ( progress , this . _session , 'wheel' , { x, y, deltaX, deltaY, ...modifierFlags ( modifiers ) } ) ;
118+ await this . _dispatcher ( progress , 'wheel' , { x, y, deltaX, deltaY, ...modifierFlags ( modifiers ) } ) ;
118119 }
119120
120121 private async _mouseEvent ( progress : Progress , type : string , x : number , y : number , button : number , buttons : number , modifiers : Set < types . KeyboardModifier > , clickCount : number ) {
121- await callWebViewInput ( progress , this . _session , 'mouseEvent' , {
122+ await this . _dispatcher ( progress , 'mouseEvent' , {
122123 type, x, y, button, buttons, clickCount, ...modifierFlags ( modifiers ) ,
123124 } ) ;
124125 }
125126}
126127
127128export class RawTouchscreenImpl implements input . RawTouchscreen {
128- private _session : WVSession | undefined ;
129+ private _dispatcher : DispatchWebViewInput ;
129130
130- setSession ( session : WVSession ) {
131- this . _session = session ;
131+ constructor ( dispatcher : DispatchWebViewInput ) {
132+ this . _dispatcher = dispatcher ;
132133 }
133134
134135 async tap ( progress : Progress , x : number , y : number , modifiers : Set < types . KeyboardModifier > ) {
135- await callWebViewInput ( progress , this . _session , 'tap' , { x, y, ...modifierFlags ( modifiers ) } ) ;
136- }
137- }
138-
139- async function callWebViewInput ( progress : Progress , session : WVSession | undefined , method : string , arg : any ) : Promise < void > {
140- if ( ! session )
141- throw new Error ( 'Page is not initialized' ) ;
142- const expression = `window.__pwWebViewInput.${ method } (${ JSON . stringify ( arg ) } )` ;
143- // Some dispatchers are async — they spread events across event-loop tasks the
144- // way a real device does. Await the returned promise so the action only
145- // resolves once every event has been delivered. Stock WebKit's Runtime.evaluate
146- // has no awaitPromise option, so use the separate Runtime.awaitPromise command.
147- const { result } = await progress . race ( session . send ( 'Runtime.evaluate' , { expression, returnByValue : false } ) ) ;
148- // `result` is absent if evaluation failed (e.g. the frame navigated away).
149- // Only promises carry an objectId here — every __pwWebViewInput method returns
150- // void or a Promise — so this both awaits async dispatch and avoids leaking a
151- // handle for the synchronous (void) case.
152- if ( result ?. className === 'Promise' && result . objectId ) {
153- await progress . race ( session . send ( 'Runtime.awaitPromise' , { promiseObjectId : result . objectId , returnByValue : true } ) ) ;
154- session . sendMayFail ( 'Runtime.releaseObject' , { objectId : result . objectId } ) ;
136+ await this . _dispatcher ( progress , 'tap' , { x, y, ...modifierFlags ( modifiers ) } ) ;
155137 }
156138}
0 commit comments