2019-03-18 19:37:06 +00:00
|
|
|
const { nativeImage, NativeImage } = process.electronBinding('native_image')
|
2019-03-16 00:32:04 +00:00
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const objectMap = function (source: Object, mapper: (value: any) => any) {
|
2019-03-16 00:32:04 +00:00
|
|
|
const sourceEntries = Object.entries(source)
|
|
|
|
const targetEntries = sourceEntries.map(([key, val]) => [key, mapper(val)])
|
|
|
|
return Object.fromEntries(targetEntries)
|
|
|
|
}
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
export function serialize (value: any): any {
|
2019-03-16 00:32:04 +00:00
|
|
|
if (value instanceof NativeImage) {
|
|
|
|
return {
|
|
|
|
buffer: value.toBitmap(),
|
|
|
|
size: value.getSize(),
|
|
|
|
__ELECTRON_SERIALIZED_NativeImage__: true
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
return value.map(serialize)
|
|
|
|
} else if (value instanceof Buffer) {
|
|
|
|
return value
|
|
|
|
} else if (value instanceof Object) {
|
|
|
|
return objectMap(value, serialize)
|
|
|
|
} else {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
export function deserialize (value: any): any {
|
2019-03-16 00:32:04 +00:00
|
|
|
if (value && value.__ELECTRON_SERIALIZED_NativeImage__) {
|
|
|
|
return nativeImage.createFromBitmap(value.buffer, value.size)
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
return value.map(deserialize)
|
|
|
|
} else if (value instanceof Buffer) {
|
|
|
|
return value
|
|
|
|
} else if (value instanceof Object) {
|
|
|
|
return objectMap(value, deserialize)
|
|
|
|
} else {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|