Browse Source

Pass langPack code in initConnection

master
Eduard Kuzmenko 4 years ago
parent
commit
39d296edeb
  1. 2
      src/lib/langPack.ts
  2. 2
      src/lib/mtproto/mtproto.service.ts
  3. 3
      src/lib/mtproto/mtproto.worker.ts
  4. 35
      src/lib/mtproto/mtprotoworker.ts
  5. 24
      src/lib/mtproto/networker.ts
  6. 16
      src/lib/mtproto/networkerFactory.ts
  7. 2
      src/lib/rootScope.ts

2
src/lib/langPack.ts

@ -217,7 +217,7 @@ namespace I18n { @@ -217,7 +217,7 @@ namespace I18n {
strings.set(string.key as LangPackKey, string);
}
rootScope.dispatchEvent('language_change');
rootScope.dispatchEvent('language_change', langPack.lang_code);
const elements = Array.from(document.querySelectorAll(`.i18n`)) as HTMLElement[];
elements.forEach(element => {

2
src/lib/mtproto/mtproto.service.ts

@ -57,7 +57,7 @@ function isCorrectResponse(response: Response) { @@ -57,7 +57,7 @@ function isCorrectResponse(response: Response) {
async function requestCache(event: FetchEvent) {
try {
const cache = await ctx.caches.open('cachedAssets');
const file = await cache.match(event.request);
const file = await cache.match(event.request, {ignoreVary: true});
if(file && isCorrectResponse(file)) {
return file;

3
src/lib/mtproto/mtproto.worker.ts

@ -131,10 +131,11 @@ const onMessage = async(e: any) => { @@ -131,10 +131,11 @@ const onMessage = async(e: any) => {
break;
}
case 'setLanguage':
case 'startAll':
case 'stopAll': {
// @ts-ignore
networkerFactory[task.task].apply(networkerFactory);
networkerFactory[task.task].apply(networkerFactory, task.args);
break;
}

35
src/lib/mtproto/mtprotoworker.ts

@ -179,6 +179,10 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -179,6 +179,10 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
}
});
rootScope.addEventListener('language_change', (language) => {
this.performTaskWorkerVoid('setLanguage', language);
});
/// #if !MTPROTO_SW
this.registerWorker();
/// #endif
@ -310,22 +314,25 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -310,22 +314,25 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
}
}
public performTaskWorkerVoid(task: string, ...args: any[]) {
const params = {
task,
taskId: this.taskId,
args
};
this.pending.push(params);
this.releasePending();
this.taskId++;
}
public performTaskWorker<T>(task: string, ...args: any[]) {
this.debug && this.log.debug('start', task, args);
return new Promise<T>((resolve, reject) => {
this.awaiting[this.taskId] = {resolve, reject, taskName: task};
const params = {
task,
taskId: this.taskId,
args
};
this.pending.push(params);
this.releasePending();
this.taskId++;
this.performTaskWorkerVoid(task, ...args);
});
}
@ -504,15 +511,15 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -504,15 +511,15 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
}
public toggleStorage(enabled: boolean) {
return this.performTaskWorker('toggleStorage', enabled);
return this.performTaskWorkerVoid('toggleStorage', enabled);
}
public stopAll() {
return this.performTaskWorker('stopAll');
return this.performTaskWorkerVoid('stopAll');
}
public startAll() {
return this.performTaskWorker('startAll');
return this.performTaskWorkerVoid('startAll');
}
}

24
src/lib/mtproto/networker.ts

@ -15,7 +15,7 @@ import CryptoWorker from '../crypto/cryptoworker'; @@ -15,7 +15,7 @@ import CryptoWorker from '../crypto/cryptoworker';
import sessionStorage from '../sessionStorage';
import Schema from './schema';
import timeManager from './timeManager';
import NetworkerFactory from './networkerFactory';
import networkerFactory from './networkerFactory';
import { logger, LogTypes } from '../logger';
import { InvokeApiOptions } from '../../types';
import { longToBytes } from '../crypto/crypto_utils';
@ -79,7 +79,7 @@ let invokeAfterMsgConstructor: number; @@ -79,7 +79,7 @@ let invokeAfterMsgConstructor: number;
export default class MTPNetworker {
private authKeyUint8: Uint8Array;
private isFileNetworker: boolean;
public isFileNetworker: boolean;
private isFileUpload: boolean;
private isFileDownload: boolean;
@ -92,7 +92,7 @@ export default class MTPNetworker { @@ -92,7 +92,7 @@ export default class MTPNetworker {
private pendingMessages: {[msgId: string]: number} = {};
private pendingAcks: Array<string> = [];
private pendingResends: Array<string> = [];
private connectionInited = false;
public connectionInited = false;
private nextReqTimeout: number;
private nextReq: number = 0;
@ -288,7 +288,7 @@ export default class MTPNetworker { @@ -288,7 +288,7 @@ export default class MTPNetworker {
if(!this.connectionInited) { // this will call once for each new session
///////this.log('Wrap api call !this.connectionInited');
const invokeWithLayer = Schema.API.methods.find(m => m.method === 'invokeWithLayer');
if(!invokeWithLayer) throw new Error('no invokeWithLayer!');
serializer.storeInt(+invokeWithLayer.id >>> 0, 'invokeWithLayer');
@ -307,7 +307,7 @@ export default class MTPNetworker { @@ -307,7 +307,7 @@ export default class MTPNetworker {
serializer.storeString(App.version + (App.isMainDomain ? ' ' + App.suffix : ''), 'app_version');
serializer.storeString(navigator.language || 'en', 'system_lang_code');
serializer.storeString(App.langPack, 'lang_pack');
serializer.storeString(navigator.language || 'en', 'lang_code');
serializer.storeString(networkerFactory.language, 'lang_code');
//serializer.storeInt(0x0, 'proxy');
/* serializer.storeMethod('initConnection', {
'flags': 0,
@ -698,8 +698,8 @@ export default class MTPNetworker { @@ -698,8 +698,8 @@ export default class MTPNetworker {
this.isOnline = online;
if(willChange) {
if(NetworkerFactory.onConnectionStatusChange) {
NetworkerFactory.onConnectionStatusChange({
if(networkerFactory.onConnectionStatusChange) {
networkerFactory.onConnectionStatusChange({
_: 'networkerStatus',
online: this.isOnline,
dcId: this.dcId,
@ -792,7 +792,7 @@ export default class MTPNetworker { @@ -792,7 +792,7 @@ export default class MTPNetworker {
}
public isStopped() {
return NetworkerFactory.akStopped && !this.isFileNetworker;
return networkerFactory.akStopped && !this.isFileNetworker;
}
private performScheduledRequest() {
@ -1544,8 +1544,8 @@ export default class MTPNetworker { @@ -1544,8 +1544,8 @@ export default class MTPNetworker {
this.applyServerSalt(message.server_salt);
sessionStorage.get('dc').then((baseDcId) => {
if(baseDcId === this.dcId && !this.isFileNetworker && NetworkerFactory.updatesProcessor) {
NetworkerFactory.updatesProcessor(message);
if(baseDcId === this.dcId && !this.isFileNetworker && networkerFactory.updatesProcessor) {
networkerFactory.updatesProcessor(message);
}
});
break;
@ -1649,8 +1649,8 @@ export default class MTPNetworker { @@ -1649,8 +1649,8 @@ export default class MTPNetworker {
this.log.debug('Update', message);
} */
if(NetworkerFactory.updatesProcessor !== null) {
NetworkerFactory.updatesProcessor(message);
if(networkerFactory.updatesProcessor !== null) {
networkerFactory.updatesProcessor(message);
}
break;
}

16
src/lib/mtproto/networkerFactory.ts

@ -12,9 +12,12 @@ @@ -12,9 +12,12 @@
import MTPNetworker from "./networker";
import { ConnectionStatusChange, InvokeApiOptions } from "../../types";
import MTTransport from "./transports/transport";
import App from "../../config/app";
import { MOUNT_CLASS_TO } from "../../config/debug";
export class NetworkerFactory {
private networkers: MTPNetworker[] = [];
public language = navigator.language || App.langPackCode;
public updatesProcessor: (obj: any) => void = null;
public onConnectionStatusChange: (info: ConnectionStatusChange) => void = null;
public akStopped = false;
@ -46,6 +49,17 @@ export class NetworkerFactory { @@ -46,6 +49,17 @@ export class NetworkerFactory {
public stopAll() {
this.akStopped = true;
}
public setLanguage(langCode: string) {
this.language = langCode;
for(const networker of this.networkers) {
if(!networker.isFileNetworker) {
networker.connectionInited = false;
}
}
}
}
export default new NetworkerFactory();
const networkerFactory = new NetworkerFactory();
MOUNT_CLASS_TO && (MOUNT_CLASS_TO.networkerFactory = networkerFactory);
export default networkerFactory;

2
src/lib/rootScope.ts

@ -109,7 +109,7 @@ export type BroadcastEvents = { @@ -109,7 +109,7 @@ export type BroadcastEvents = {
'notify_settings': Update.updateNotifySettings,
'notify_peer_type_settings': {key: Exclude<NotifyPeer['_'], 'notifyPeer'>, settings: PeerNotifySettings},
'language_change': void,
'language_change': string,
'theme_change': void,

Loading…
Cancel
Save