import { nativeImage } from 'electron' ; export function isPromise ( val ) { return ( val && val . then && val . then instanceof Function && val . constructor && val . constructor . reject && val . constructor . reject instanceof Function && val . constructor . resolve && val . constructor . resolve instanceof Function ) ; } const serializableTypes = [ Boolean , Number , String , Date , Error , RegExp , ArrayBuffer ] ; export function isSerializableObject ( value ) { return value === null || ArrayBuffer . isView ( value ) || serializableTypes . some ( type => value instanceof type ) ; } const objectMap = function ( source , mapper ) { const sourceEntries = Object . entries ( source ) ; const targetEntries = sourceEntries . map ( ( [ key , val ] ) => [ key , mapper ( val ) ] ) ; return Object . fromEntries ( targetEntries ) ; } ; function serializeNativeImage ( image ) { const representations = [ ] ; const scaleFactors = image . getScaleFactors ( ) ; if ( scaleFactors . length === 1 ) { const scaleFactor = scaleFactors [ 0 ] ; const size = image . getSize ( scaleFactor ) ; const buffer = image . toBitmap ( { scaleFactor } ) ; representations . push ( { scaleFactor , size , buffer } ) ; } else { for ( const scaleFactor of scaleFactors ) { const size = image . getSize ( scaleFactor ) ; const dataURL = image . toDataURL ( { scaleFactor } ) ; representations . push ( { scaleFactor , size , dataURL } ) ; } } return { __ELECTRON_SERIALIZED_NativeImage__ : true , representations } ; } function deserializeNativeImage ( value ) { const image = nativeImage . createEmpty ( ) ; if ( value . representations . length === 1 ) { const { buffer , size , scaleFactor } = value . representations [ 0 ] ; const { width , height } = size ; image . addRepresentation ( { buffer , scaleFactor , width , height } ) ; } else { for ( const rep of value . representations ) { const { dataURL , size , scaleFactor } = rep ; const { width , height } = size ; image . addRepresentation ( { dataURL , scaleFactor , width , height } ) ; } } return image ; } export function serialize ( value ) { if ( value && value . constructor && value . constructor . name === 'NativeImage' ) { return serializeNativeImage ( value ) ; } if ( Array . isArray ( value ) ) { return value . map ( serialize ) ; } else if ( isSerializableObject ( value ) ) { return value ; } else if ( value instanceof Object ) { return objectMap ( value , serialize ) ; } else { return value ; } } export function deserialize ( value ) { if ( value && value . __ELECTRON_SERIALIZED_NativeImage__ ) { return deserializeNativeImage ( value ) ; } else if ( Array . isArray ( value ) ) { return value . map ( deserialize ) ; } else if ( isSerializableObject ( value ) ) { return value ; } else if ( value instanceof Object ) { return objectMap ( value , deserialize ) ; } else { return value ; } }