Telegram Web K with changes to work inside I2P
https://web.telegram.i2p/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.1 KiB
76 lines
2.1 KiB
/* |
|
* https://github.com/morethanwords/tweb |
|
* Copyright (C) 2019-2021 Eduard Kuzmenko |
|
* https://github.com/morethanwords/tweb/blob/master/LICENSE |
|
*/ |
|
|
|
import WebpWorker from 'worker-loader!./webp.worker'; |
|
import { MOUNT_CLASS_TO } from '../../config/debug'; |
|
import { CancellablePromise, deferredPromise } from '../../helpers/cancellablePromise'; |
|
import apiManagerProxy from '../mtproto/mtprotoworker'; |
|
|
|
export type WebpConvertTask = { |
|
type: 'convertWebp', |
|
payload: { |
|
fileName: string, |
|
bytes: Uint8Array |
|
} |
|
}; |
|
|
|
export class WebpWorkerController { |
|
private worker: Worker; |
|
private convertPromises: {[fileName: string]: CancellablePromise<Uint8Array>} = {}; |
|
private isWebpSupportedCache: boolean; |
|
|
|
init() { |
|
this.worker = new WebpWorker(); |
|
this.worker.addEventListener('message', (e) => { |
|
const payload = (e.data as WebpConvertTask).payload; |
|
|
|
if(payload.fileName.indexOf('main-') === 0) { |
|
const promise = this.convertPromises[payload.fileName]; |
|
if(promise) { |
|
payload.bytes ? promise.resolve(payload.bytes) : promise.reject(); |
|
delete this.convertPromises[payload.fileName]; |
|
} |
|
} else { |
|
apiManagerProxy.postMessage(e.data); |
|
} |
|
}); |
|
} |
|
|
|
postMessage(data: WebpConvertTask) { |
|
if(this.init) { |
|
this.init(); |
|
this.init = null; |
|
} |
|
|
|
this.worker.postMessage(data); |
|
} |
|
|
|
isWebpSupported() { |
|
if(this.isWebpSupportedCache === undefined) { |
|
this.isWebpSupportedCache = document.createElement('canvas').toDataURL('image/webp').startsWith('data:image/webp'); |
|
} |
|
|
|
return this.isWebpSupportedCache; |
|
} |
|
|
|
convert(fileName: string, bytes: Uint8Array) { |
|
fileName = 'main-' + fileName; |
|
|
|
if(this.convertPromises.hasOwnProperty(fileName)) { |
|
return this.convertPromises[fileName]; |
|
} |
|
|
|
const convertPromise = deferredPromise<Uint8Array>(); |
|
|
|
this.postMessage({type: 'convertWebp', payload: {fileName, bytes}}); |
|
|
|
return this.convertPromises[fileName] = convertPromise; |
|
} |
|
} |
|
|
|
const webpWorkerController = new WebpWorkerController(); |
|
MOUNT_CLASS_TO.webpWorkerController = webpWorkerController; |
|
export default webpWorkerController; |