1- import { Command , Session } from "../session" ;
1+ import { ClientEvent , Command , Session } from "../session" ;
22import { CommandHandler } from "./base" ;
3- import { GetPinValue , PinChangeCallback , PinUpdate , WaitChange } from "../models/pin" ;
3+ import { GetPinValue , PinChangeCallback , PinUpdate , WaitChange , IsFileInput } from "../models/pin" ;
44import { state } from "../state" ;
5+ import { serialize_file , serialize_json } from "../utils" ;
56
67
78export class PinHandler implements CommandHandler {
@@ -15,22 +16,56 @@ export class PinHandler implements CommandHandler {
1516
1617 handle_message ( msg : Command ) {
1718 if ( msg . command === 'pin_value' ) {
18- let val = GetPinValue ( msg . spec . name ) ;
19- let data = val === undefined ? null : { value : val } ;
20- state . CurrentSession . send_message ( { event : "js_yield" , task_id : msg . task_id , data : data } ) ;
19+ let val = GetPinValue ( msg . spec . name ) ; // undefined or value
20+ let send_msg = {
21+ event : "js_yield" , task_id : msg . task_id ,
22+ data : val === undefined ? null : { value : val }
23+ } ;
24+ this . submit ( send_msg , IsFileInput ( msg . spec . name ) ) ;
2125 } else if ( msg . command === 'pin_update' ) {
2226 PinUpdate ( msg . spec . name , msg . spec . attributes ) ;
2327 } else if ( msg . command === 'pin_wait' ) {
2428 let p = WaitChange ( msg . spec . names , msg . spec . timeout ) ;
25- Promise . resolve ( p ) . then ( function ( value ) {
26- state . CurrentSession . send_message ( { event : "js_yield" , task_id : msg . task_id , data : value } ) ;
29+ Promise . resolve ( p ) . then ( ( change_info : ( null | { name : string , value : any } ) ) => {
30+ // change_info: null or {'name': name, 'value': value}
31+ let send_msg = { event : "js_yield" , task_id : msg . task_id , data : change_info }
32+ this . submit ( send_msg , IsFileInput ( change_info . name ) ) ;
2733 } ) . catch ( ( error ) => {
2834 console . error ( 'error in `pin_wait`: %s' , error ) ;
29- state . CurrentSession . send_message ( { event : "js_yield" , task_id : msg . task_id , data : null } ) ;
35+ this . submit ( { event : "js_yield" , task_id : msg . task_id , data : null } ) ;
3036 } ) ;
31- } else if ( msg . command === 'pin_onchange' ) {
32- PinChangeCallback ( msg . spec . name , msg . spec . callback_id , msg . spec . clear ) ;
37+ } else if ( msg . command === 'pin_onchange' ) {
38+ let onchange = ( val : any ) => {
39+ let send_msg = {
40+ event : "callback" ,
41+ task_id : msg . spec . callback_id ,
42+ data : { value : val }
43+ }
44+ this . submit ( send_msg , IsFileInput ( msg . spec . name ) ) ;
45+ }
46+ PinChangeCallback ( msg . spec . name , msg . spec . callback_id ? onchange : null , msg . spec . clear ) ;
3347 }
48+ }
3449
50+ /*
51+ * Send pin value to server.
52+ * `msg.data` may be null, or {value: any, ...}
53+ * `msg.data.value` stores the value of the pin.
54+ * when submit files, `msg.data.value` is {multiple: bool, files: File[] }
55+ * */
56+ submit ( msg : ClientEvent , is_file : boolean = false ) {
57+ if ( is_file && msg . data !== null ) {
58+ // msg.data.value: {multiple: bool, files: File[]}
59+ let { multiple, files} = msg . data . value ;
60+ msg . data . value = multiple ? [ ] : null ; // replace file value with initial value
61+ state . CurrentSession . send_buffer (
62+ new Blob ( [
63+ serialize_json ( msg ) ,
64+ ...files . map ( ( file : File ) => serialize_file ( file , 'value' ) )
65+ ] , { type : 'application/octet-stream' } )
66+ ) ;
67+ } else {
68+ state . CurrentSession . send_message ( msg ) ;
69+ }
3570 }
3671}
0 commit comments