Browse Source

temp

master
morethanwords 4 years ago
parent
commit
86bb1e112c
  1. 4
      src/lib/mtproto/dcConfigurator.ts
  2. 15
      src/lib/mtproto/mtproto.service.ts
  3. 23
      src/lib/mtproto/mtprotoworker.ts

4
src/lib/mtproto/dcConfigurator.ts

@ -26,6 +26,7 @@ type Servers = {
let socketId = 0; let socketId = 0;
const TEST_SUFFIX = Modes.test ? '_test' : ''; const TEST_SUFFIX = Modes.test ? '_test' : '';
/// #if !MTPROTO_SW
class SocketProxied extends EventListenerBase<{ class SocketProxied extends EventListenerBase<{
open: () => void, open: () => void,
message: (buffer: ArrayBuffer) => any, message: (buffer: ArrayBuffer) => any,
@ -77,6 +78,7 @@ class SocketProxied extends EventListenerBase<{
notifyAll(task); notifyAll(task);
} }
} }
/// #endif
export const socketsProxied: Map<number, SocketProxied> = new Map(); export const socketsProxied: Map<number, SocketProxied> = new Map();
@ -108,7 +110,7 @@ export class DcConfigurator {
const retryTimeout = connectionType === 'client' ? 10000 : 10000; const retryTimeout = connectionType === 'client' ? 10000 : 10000;
const oooohLetMeLive: MTConnectionConstructable = (isSafari && isWebWorker) /* || true */ ? SocketProxied : Socket; const oooohLetMeLive: MTConnectionConstructable = (isSafari && isWebWorker && typeof(SocketProxied) !== 'undefined') /* || true */ ? SocketProxied : Socket;
return new TcpObfuscated(oooohLetMeLive, dcId, chosenServer, logSuffix, retryTimeout); return new TcpObfuscated(oooohLetMeLive, dcId, chosenServer, logSuffix, retryTimeout);
}; };

15
src/lib/mtproto/mtproto.service.ts

@ -222,6 +222,21 @@ function alignLimit(limit: number) {
return 2 ** Math.ceil(Math.log(limit) / Math.log(2)); return 2 ** Math.ceil(Math.log(limit) / Math.log(2));
} }
/* ctx.addEventListener('push', (event) => {
console.log('[Service Worker] Push Received.');
console.log(`[Service Worker] Push had this data: "${event.data.text()}"`, event, event.data);
const title = 'Push Push Push';
const options = {};
// const options = {
// body: 'Yay it works.',
// icon: 'images/icon.png',
// badge: 'images/badge.png'
// };
event.waitUntil(ctx.registration.showNotification(title, options));
}); */
//export default () => {}; //export default () => {};
//MOUNT_CLASS_TO && (MOUNT_CLASS_TO.onFetch = onFetch); //MOUNT_CLASS_TO && (MOUNT_CLASS_TO.onFetch = onFetch);

23
src/lib/mtproto/mtprotoworker.ts

@ -77,7 +77,8 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
private registerServiceWorker() { private registerServiceWorker() {
if(!('serviceWorker' in navigator)) return; if(!('serviceWorker' in navigator)) return;
navigator.serviceWorker.register('./sw.js', {scope: './'}).then(registration => { const worker = navigator.serviceWorker;
worker.register('./sw.js', {scope: './'}).then(registration => {
this.log('SW registered', registration); this.log('SW registered', registration);
this.isSWRegistered = true; this.isSWRegistered = true;
@ -86,33 +87,29 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
this.log('SW statechange', e); this.log('SW statechange', e);
}); });
const controller = navigator.serviceWorker.controller || registration.installing || registration.waiting || registration.active; /// #if MTPROTO_SW
const controller = worker.controller || registration.installing || registration.waiting || registration.active;
this.onWorkerFirstMessage(controller); this.onWorkerFirstMessage(controller);
/// #endif
}, (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) => { worker.addEventListener('controllerchange', () => {
this.log('set SW', navigator.serviceWorker);
//registration.update();
}); */
navigator.serviceWorker.addEventListener('controllerchange', () => {
this.log.warn('controllerchange'); this.log.warn('controllerchange');
this.releasePending(); this.releasePending();
navigator.serviceWorker.controller.addEventListener('error', (e) => { worker.controller.addEventListener('error', (e) => {
this.log.error('controller error:', e); this.log.error('controller error:', e);
}); });
}); });
/// #if MTPROTO_SW /// #if MTPROTO_SW
navigator.serviceWorker.addEventListener('message', this.onWorkerMessage); worker.addEventListener('message', this.onWorkerMessage);
/// #else /// #else
navigator.serviceWorker.addEventListener('message', (e) => { worker.addEventListener('message', (e) => {
const task: ServiceWorkerTask = e.data; const task: ServiceWorkerTask = e.data;
if(!isObject(task)) { if(!isObject(task)) {
return; return;
@ -122,7 +119,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
}); });
/// #endif /// #endif
navigator.serviceWorker.addEventListener('messageerror', (e) => { worker.addEventListener('messageerror', (e) => {
this.log.error('SW messageerror:', e); this.log.error('SW messageerror:', e);
}); });
} }

Loading…
Cancel
Save