This commit is contained in:
Eduard Kuzmenko 2021-03-05 19:22:22 +04:00
parent 486a6ae509
commit 2874032b3a
5 changed files with 175 additions and 212 deletions

View File

@ -3,7 +3,12 @@ const Modes = {
debug: location.search.indexOf('debug=1') > 0, debug: location.search.indexOf('debug=1') > 0,
http: false, //location.search.indexOf('http=1') > 0, http: false, //location.search.indexOf('http=1') > 0,
ssl: true, // location.search.indexOf('ssl=1') > 0 || location.protocol === 'https:' && location.search.indexOf('ssl=0') === -1, ssl: true, // location.search.indexOf('ssl=1') > 0 || location.protocol === 'https:' && location.search.indexOf('ssl=0') === -1,
multipleConnections: true multipleConnections: true,
asServiceWorker: false
}; };
/// #if MTPROTO_SW
Modes.asServiceWorker = true;
/// #endif
export default Modes; export default Modes;

View File

@ -1,3 +1,6 @@
/// #if MTPROTO_SW
import './mtproto.worker';
/// #endif
import { isSafari } from '../../helpers/userAgent'; import { isSafari } from '../../helpers/userAgent';
import { logger, LogLevels } from '../logger'; import { logger, LogLevels } from '../logger';
import type { DownloadOptions } from './apiFileManager'; import type { DownloadOptions } from './apiFileManager';
@ -6,11 +9,12 @@ import { notifySomeone } from '../../helpers/context';
import type { InputFileLocation, FileLocation, UploadFile } from '../../layer'; import type { InputFileLocation, FileLocation, UploadFile } from '../../layer';
import { CancellablePromise, deferredPromise } from '../../helpers/cancellablePromise'; import { CancellablePromise, deferredPromise } from '../../helpers/cancellablePromise';
const log = logger('SW', LogLevels.error/* | LogLevels.debug | LogLevels.log */); const log = logger('SW', LogLevels.error | LogLevels.debug | LogLevels.log | LogLevels.warn);
const ctx = self as any as ServiceWorkerGlobalScope; const ctx = self as any as ServiceWorkerGlobalScope;
const deferredPromises: {[taskId: number]: CancellablePromise<any>} = {}; const deferredPromises: {[taskId: number]: CancellablePromise<any>} = {};
/// #if !MTPROTO_SW
ctx.addEventListener('message', (e) => { ctx.addEventListener('message', (e) => {
const task = e.data as ServiceWorkerTaskResponse; const task = e.data as ServiceWorkerTaskResponse;
const promise = deferredPromises[task.id]; const promise = deferredPromises[task.id];
@ -23,6 +27,7 @@ ctx.addEventListener('message', (e) => {
delete deferredPromises[task.id]; delete deferredPromises[task.id];
}); });
/// #endif
let taskId = 0; let taskId = 0;
@ -132,10 +137,7 @@ const onChangeState = () => {
ctx.onfetch = onFetch; ctx.onfetch = onFetch;
}; };
/** ctx.addEventListener('install', (event) => {
* Service Worker Installation
*/
ctx.addEventListener('install', (event: ExtendableEvent) => {
log('installing'); log('installing');
/* initCache(); /* initCache();
@ -146,9 +148,6 @@ ctx.addEventListener('install', (event: ExtendableEvent) => {
event.waitUntil(ctx.skipWaiting()); // Activate worker immediately event.waitUntil(ctx.skipWaiting()); // Activate worker immediately
}); });
/**
* Service Worker Activation
*/
ctx.addEventListener('activate', (event) => { ctx.addEventListener('activate', (event) => {
log('activating', ctx); log('activating', ctx);

View File

@ -5,51 +5,10 @@ import apiManager from "./apiManager";
import cryptoWorker from "../crypto/cryptoworker"; import cryptoWorker from "../crypto/cryptoworker";
import networkerFactory from "./networkerFactory"; import networkerFactory from "./networkerFactory";
import apiFileManager from './apiFileManager'; import apiFileManager from './apiFileManager';
//import { logger, LogLevels } from '../logger';
import type { ServiceWorkerTask, ServiceWorkerTaskResponse } from './mtproto.service'; import type { ServiceWorkerTask, ServiceWorkerTaskResponse } from './mtproto.service';
import { ctx } from '../../helpers/userAgent'; import { ctx } from '../../helpers/userAgent';
import { socketsProxied } from './dcConfigurator'; import { socketsProxied } from './dcConfigurator';
import { notifyAll } from '../../helpers/context';
//const log = logger('DW', LogLevels.error);
//console.error('INCLUDE !!!', new Error().stack);
/* function isObject(object: any) {
return typeof(object) === 'object' && object !== null;
} */
/* function fillTransfer(transfer: any, obj: any) {
if(!obj) return;
if(obj instanceof ArrayBuffer) {
transfer.add(obj);
} else if(obj.buffer && obj.buffer instanceof ArrayBuffer) {
transfer.add(obj.buffer);
} else if(isObject(obj)) {
for(var i in obj) {
fillTransfer(transfer, obj[i]);
}
} else if(Array.isArray(obj)) {
obj.forEach(value => {
fillTransfer(transfer, value);
});
}
} */
function respond(...args: any[]) {
// отключил для всего потому что не успел пофиксить transfer detached
//if(isSafari(self)/* || true */) {
// @ts-ignore
ctx.postMessage(...args);
/* } else {
var transfer = new Set();
fillTransfer(transfer, arguments);
//console.log('reply', transfer, [...transfer]);
ctx.postMessage(...arguments, [...transfer]);
//console.log('reply', transfer, [...transfer]);
} */
}
let webpSupported = false; let webpSupported = false;
export const isWebpSupported = () => { export const isWebpSupported = () => {
@ -57,30 +16,18 @@ export const isWebpSupported = () => {
}; };
networkerFactory.setUpdatesProcessor((obj) => { networkerFactory.setUpdatesProcessor((obj) => {
respond({update: obj}); notifyAll({update: obj});
}); });
networkerFactory.onConnectionStatusChange = (status) => { networkerFactory.onConnectionStatusChange = (status) => {
respond({type: 'connectionStatusChange', payload: status}); notifyAll({type: 'connectionStatusChange', payload: status});
}; };
/* ctx.onerror = (error) => {
console.error('error:', error);
};
ctx.onunhandledrejection = (error) => {
console.error('onunhandledrejection:', error);
}; */
const onMessage = async(e: any) => { const onMessage = async(e: any) => {
try { try {
const task = e.data; const task = e.data;
const taskId = task.taskId; const taskId = task.taskId;
//log.debug('got message:', taskId, task);
//debugger;
if(task.type === 'convertWebp') { if(task.type === 'convertWebp') {
const {fileName, bytes} = task.payload; const {fileName, bytes} = task.payload;
const deferred = apiFileManager.webpConvertPromises[fileName]; const deferred = apiFileManager.webpConvertPromises[fileName];
@ -105,7 +52,7 @@ const onMessage = async(e: any) => {
responseTask.error = err; responseTask.error = err;
} }
respond(responseTask); notifyAll(responseTask);
return; return;
} else if(task.type === 'webpSupport') { } else if(task.type === 'webpSupport') {
webpSupported = task.payload; webpSupported = task.payload;
@ -134,7 +81,7 @@ const onMessage = async(e: any) => {
case 'gzipUncompress': case 'gzipUncompress':
// @ts-ignore // @ts-ignore
return cryptoWorker[task.task].apply(cryptoWorker, task.args).then(result => { return cryptoWorker[task.task].apply(cryptoWorker, task.args).then(result => {
respond({taskId, result}); notifyAll({taskId, result});
}); });
case 'setQueueId': case 'setQueueId':
@ -152,9 +99,9 @@ const onMessage = async(e: any) => {
result = await result; result = await result;
} }
respond({taskId, result}); notifyAll({taskId, result});
} catch(error) { } catch(error) {
respond({taskId, error}); notifyAll({taskId, error});
} }
break; break;
@ -163,7 +110,7 @@ const onMessage = async(e: any) => {
case 'getNetworker': { case 'getNetworker': {
// @ts-ignore // @ts-ignore
apiManager[task.task].apply(apiManager, task.args).finally(() => { apiManager[task.task].apply(apiManager, task.args).finally(() => {
respond({taskId, result: null}); notifyAll({taskId, result: null});
}); });
break; break;
@ -178,9 +125,11 @@ const onMessage = async(e: any) => {
result = await result; result = await result;
} }
respond({taskId, result}); //console.log(notifyAll);
notifyAll({taskId, result});
} catch(error) { } catch(error) {
respond({taskId, error}); notifyAll({taskId, error});
} }
//throw new Error('Unknown task: ' + task.task); //throw new Error('Unknown task: ' + task.task);
@ -192,7 +141,6 @@ const onMessage = async(e: any) => {
} }
}; };
ctx.addEventListener('message', onMessage);
//console.log('[WORKER] Will send ready', Date.now() / 1000); //console.log('[WORKER] Will send ready', Date.now() / 1000);
ctx.postMessage('ready'); ctx.addEventListener('message', onMessage);
notifyAll('ready');

View File

@ -23,8 +23,6 @@ type Task = {
args: any[] args: any[]
}; };
const USE_WORKER_AS_WORKER = true;
type HashResult = { type HashResult = {
hash: number, hash: number,
result: any result: any
@ -66,7 +64,10 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
this.log('constructor'); this.log('constructor');
this.registerServiceWorker(); this.registerServiceWorker();
/// #if !MTPROTO_SW
this.registerWorker(); this.registerWorker();
/// #endif
} }
public isServiceWorkerOnline() { public isServiceWorkerOnline() {
@ -77,23 +78,27 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
if(!('serviceWorker' in navigator)) return; if(!('serviceWorker' in navigator)) return;
navigator.serviceWorker.register('./sw.js', {scope: './'}).then(registration => { navigator.serviceWorker.register('./sw.js', {scope: './'}).then(registration => {
this.log('SW registered', registration);
this.isSWRegistered = true; this.isSWRegistered = true;
const sw = registration.installing || registration.waiting || registration.active;
sw.addEventListener('statechange', (e) => {
this.log('SW statechange', e);
});
const controller = navigator.serviceWorker.controller || registration.installing || registration.waiting || registration.active;
this.onWorkerFirstMessage(controller);
}, (err) => { }, (err) => {
this.isSWRegistered = false; this.isSWRegistered = false;
this.log.error('SW registration failed!', err); this.log.error('SW registration failed!', err);
appDocsManager.onServiceWorkerFail(); appDocsManager.onServiceWorkerFail();
}); });
navigator.serviceWorker.ready.then((registration) => { /* navigator.serviceWorker.ready.then((registration) => {
this.log('set SW'); this.log('set SW', navigator.serviceWorker);
this.releasePending();
if(!USE_WORKER_AS_WORKER) {
this.postMessage = navigator.serviceWorker.controller.postMessage.bind(navigator.serviceWorker.controller);
}
//registration.update(); //registration.update();
}); }); */
navigator.serviceWorker.addEventListener('controllerchange', () => { navigator.serviceWorker.addEventListener('controllerchange', () => {
this.log.warn('controllerchange'); this.log.warn('controllerchange');
@ -104,9 +109,9 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
}); });
}); });
/** /// #if MTPROTO_SW
* Message resolver navigator.serviceWorker.addEventListener('message', this.onWorkerMessage);
*/ /// #else
navigator.serviceWorker.addEventListener('message', (e) => { navigator.serviceWorker.addEventListener('message', (e) => {
const task: ServiceWorkerTask = e.data; const task: ServiceWorkerTask = e.data;
if(!isObject(task)) { if(!isObject(task)) {
@ -115,25 +120,19 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
this.postMessage(task); this.postMessage(task);
}); });
/// #endif
navigator.serviceWorker.addEventListener('messageerror', (e) => { navigator.serviceWorker.addEventListener('messageerror', (e) => {
this.log.error('SW messageerror:', e); this.log.error('SW messageerror:', e);
}); });
} }
private registerWorker() { private onWorkerFirstMessage(worker: any) {
//return;
const worker = new MTProtoWorker();
//const worker = window;
worker.addEventListener('message', (e) => {
if(!this.worker) { if(!this.worker) {
this.worker = worker as any; this.worker = worker;
this.log('set webWorker'); this.log('set webWorker');
if(USE_WORKER_AS_WORKER) {
this.postMessage = this.worker.postMessage.bind(this.worker); this.postMessage = this.worker.postMessage.bind(this.worker);
}
const isWebpSupported = webpWorkerController.isWebpSupported(); const isWebpSupported = webpWorkerController.isWebpSupported();
this.log('WebP supported:', isWebpSupported); this.log('WebP supported:', isWebpSupported);
@ -141,8 +140,10 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
this.releasePending(); this.releasePending();
} }
}
//this.log('got message from worker:', e.data); private onWorkerMessage = (e: MessageEvent) => {
this.log('got message from worker:', e.data);
const task = e.data; const task = e.data;
@ -247,12 +248,22 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
} else if(task.hasOwnProperty('result') || task.hasOwnProperty('error')) { } else if(task.hasOwnProperty('result') || task.hasOwnProperty('error')) {
this.finalizeTask(task.taskId, task.result, task.error); this.finalizeTask(task.taskId, task.result, task.error);
} }
}); };
/// #if !MTPROTO_SW
private registerWorker() {
//return;
const worker = new MTProtoWorker();
//const worker = window;
worker.addEventListener('message', this.onWorkerFirstMessage.bind(this, worker), {once: true});
worker.addEventListener('message', this.onWorkerMessage);
worker.addEventListener('error', (err) => { worker.addEventListener('error', (err) => {
this.log.error('WORKER ERROR', err); this.log.error('WORKER ERROR', err);
}); });
} }
/// #endif
private finalizeTask(taskId: number, result: any, error: any) { private finalizeTask(taskId: number, result: any, error: any) {
const deferred = this.awaiting[taskId]; const deferred = this.awaiting[taskId];