UNPKG

@tillpos/rn-receipt-printer-utils

Version:

Fork of react-native-printer. A React Native Library to support USB/BLE/Net printer

72 lines (65 loc) 3.36 kB
import { NativeModules, NativeEventEmitter } from 'react-native'; import { EpsonUtil } from './epson'; const RNUSBPrinter = NativeModules.RNUSBPrinter; const RNBLEPrinter = NativeModules.RNBLEPrinter; const RNNetPrinter = NativeModules.RNNetPrinter; export let PrinterBrand; (function (PrinterBrand) { PrinterBrand["EPSON"] = "EPSON"; PrinterBrand["STAR"] = "STAR"; PrinterBrand["OTHER"] = "OTHER"; })(PrinterBrand || (PrinterBrand = {})); export let PrinterSeries; (function (PrinterSeries) { PrinterSeries["TM_M30"] = "TM_M30"; PrinterSeries["TM_M30II"] = "TM_M30II"; PrinterSeries["TM_U220"] = "TM_U220"; PrinterSeries["TM_T82"] = "TM_T82"; PrinterSeries["TM_L90"] = "TM_L90"; })(PrinterSeries || (PrinterSeries = {})); // Timeout for returning response to client const SDK_RESPONSE_TIMEOUT = 30000; // Promise with timeout const promiseWithTimeout = promise => { let timeoutId; const timeoutPromise = new Promise((_, reject) => { timeoutId = setTimeout(() => { reject(new Error('Request timed out')); }, SDK_RESPONSE_TIMEOUT); }); return { promiseOrTimeout: Promise.race([promise, timeoutPromise]), // @ts-ignore timeoutId }; }; export const USBPrinter = { connectAndSend: (vendorId, productId, data, brand) => { return new Promise((resolve, reject) => RNUSBPrinter.connectAndSend(vendorId, productId, data.toString('base64'), brand, printer => resolve(printer), error => reject(error))); } }; export const BLEPrinter = { connectAndSend: (bdAddress, data, brand, series = PrinterSeries.TM_M30) => { const { promiseOrTimeout, timeoutId } = promiseWithTimeout(new Promise((resolve, reject) => brand === PrinterBrand.EPSON ? EpsonUtil.connectAndSend(bdAddress, data, false, series, printer => resolve(printer), error => reject(error)) : RNBLEPrinter.connectAndSend(bdAddress, data.toString('base64'), brand, series, printer => resolve(printer), error => reject(error)))); return new Promise((resolve, reject) => promiseOrTimeout.then(printer => resolve(printer)).catch(error => reject(error)).finally(() => clearTimeout(timeoutId))); } }; export const NetPrinter = { connectAndSend: (host, port, data, brand, series = PrinterSeries.TM_M30) => { const { promiseOrTimeout, timeoutId } = promiseWithTimeout(new Promise((resolve, reject) => brand === PrinterBrand.EPSON ? EpsonUtil.connectAndSend(host, data, true, series, printer => resolve(printer), error => reject(error)) : RNNetPrinter.connectAndSend(host, port, data.toString('base64'), brand, series, printer => resolve(printer), error => reject(error)))); return new Promise((resolve, reject) => promiseOrTimeout.then(printer => resolve(printer)).catch(error => reject(error)).finally(() => clearTimeout(timeoutId))); } }; export const NetPrinterEventEmitter = new NativeEventEmitter(RNNetPrinter); export let RN_THERMAL_RECEIPT_PRINTER_EVENTS; (function (RN_THERMAL_RECEIPT_PRINTER_EVENTS) { RN_THERMAL_RECEIPT_PRINTER_EVENTS["EVENT_NET_PRINTER_SCANNED_SUCCESS"] = "scannerResolved"; RN_THERMAL_RECEIPT_PRINTER_EVENTS["EVENT_NET_PRINTER_SCANNING"] = "scannerRunning"; RN_THERMAL_RECEIPT_PRINTER_EVENTS["EVENT_NET_PRINTER_SCANNED_ERROR"] = "registerError"; })(RN_THERMAL_RECEIPT_PRINTER_EVENTS || (RN_THERMAL_RECEIPT_PRINTER_EVENTS = {})); //# sourceMappingURL=index.js.map