import bindings from 'bindings' ; import { interval , Observable , Subject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; const PCSC = bindings ( 'pcsc.node' ) ; export const NativeCardServiceUpdateInterval = 250 ; interface Service { /**
     * Update native services.
     */ update ( ) : void ; param name param listener on ( name , listener ) : void ; } export interface NativeCardMetadata { /**
     * Name of reader.
     */ name : string ; /**
     * Card atr.
     */ atr : Buffer ; } export interface NativeCardInterface { open ( metadata ) : void ; transmit ( data ) : Buffer ; close ( ) : void ; } export class NativeCard { /**
     * Native card interface.
     */ private card : NativeCardInterface ; constructor ( metadata ) { this . card = new PCSC . card ( ) ; this . card . open ( metadata ) ; } param data returns transmit ( data ) { return this . card . transmit ( data ) ; } /**
     * Close native card interface.
     */ close ( ) { this . card . close ( ) ; } } class NativeCardServiceController { private serviceSubject : Subject < NativeCardMetadata > ; /**
     * Status subject for turning on/off service.
     */ private statusSubject : Subject < void > ; /**
     * Update subject.
     */ private updateSubject : Subject < number > ; /**
     * Native card service handler.
     */ private service : Service ; constructor ( ) { /**
         * Init service.
         */ this . statusSubject = new Subject < void > ( ) ; this . updateSubject = new Subject < number > ( ) ; this . service = new PCSC . service ( ) ; this . serviceSubject = new Subject < NativeCardMetadata > ( ) ; /**
         * Add service listeners.
         */ this . service . on ( 'card' , ( card ) => this . serviceSubject . next ( card ) ) ; this . service . on ( 'error' , ( e ) => this . serviceSubject . error ( e ) ) ; this . service . on ( 'update' , ( delta ) => this . updateSubject . next ( delta ) ) ; } /**
     * Emit consumed time (in ms) of last update.
     */ get update ( ) : Observable < number > { return this . updateSubject . asObservable ( ) ; } /**
     * Start native card service.
     */ start ( ) { interval ( NativeCardServiceUpdateInterval ) . pipe ( takeUntil ( this . statusSubject ) ) . subscribe ( ( ) => { try { this . service . update ( ) ; } catch ( e ) { this . serviceSubject . error ( e ) ; } } ) ; } /**
     * Stop native card service.
     */ stop ( ) { this . statusSubject . next ( ) ; } get observable ( ) { return this . serviceSubject . asObservable ( ) ; } } export const pcsc = new NativeCardServiceController ( ) ;