@airgram/core
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -16,2 +16,3 @@ import { ApiMethods } from '@airgram/api/apiFactory'; | ||
private apiMiddleware; | ||
private bootstrapMiddleware; | ||
private callApi; | ||
@@ -18,0 +19,0 @@ private createContext; |
@@ -64,2 +64,11 @@ "use strict"; | ||
var components_1 = require("./components"); | ||
var helpers_1 = require("./helpers"); | ||
function createDeferred() { | ||
var deferred = {}; | ||
deferred.promise = new Promise(function (resolve, reject) { | ||
deferred.resolve = resolve; | ||
deferred.reject = reject; | ||
}); | ||
return deferred; | ||
} | ||
var DEFAULT_CONFIG = { | ||
@@ -110,3 +119,4 @@ applicationVersion: '0.1.0', | ||
} | ||
setTimeout(function () { return _this.api.getAuthorizationState(); }, 0); | ||
_this.bootstrapMiddleware(); | ||
setTimeout(_this.api.getAuthorizationState, 0); | ||
return _this; | ||
@@ -148,2 +158,66 @@ } | ||
}; | ||
Airgram.prototype.bootstrapMiddleware = function () { | ||
var _this = this; | ||
var deferred = createDeferred(); | ||
this.use(function (ctx, next) { return __awaiter(_this, void 0, void 0, function () { | ||
var update, keys; | ||
return __generator(this, function (_a) { | ||
if (!deferred) { | ||
return [2 /*return*/, next()]; | ||
} | ||
if (ctx.update) { | ||
if (ctx._ === 'updateAuthorizationState') { | ||
update = ctx.update; | ||
switch (update.authorizationState._) { | ||
case 'authorizationStateWaitTdlibParameters': { | ||
keys = [ | ||
'useTestDc', | ||
'databaseDirectory', | ||
'filesDirectory', | ||
'useFileDatabase', | ||
'useChatInfoDatabase', | ||
'useMessageDatabase', | ||
'useSecretChats', | ||
'apiId', | ||
'apiHash', | ||
'systemLanguageCode', | ||
'deviceModel', | ||
'systemVersion', | ||
'applicationVersion', | ||
'enableStorageOptimizer', | ||
'ignoreFileNames' | ||
]; | ||
this.api.setTdlibParameters({ | ||
parameters: __assign({ _: 'tdlibParameters' }, helpers_1.pick(this.config, keys)) | ||
}).catch(this.handleError); | ||
break; | ||
} | ||
case 'authorizationStateWaitEncryptionKey': { | ||
this.api.checkDatabaseEncryptionKey({ | ||
encryptionKey: this.config.databaseEncryptionKey | ||
}).catch(this.handleError); | ||
break; | ||
} | ||
default: { | ||
setTimeout(function () { | ||
if (deferred) { | ||
deferred.resolve(); | ||
deferred = null; | ||
} | ||
}, 0); | ||
} | ||
} | ||
} | ||
return [2 /*return*/, next()]; | ||
} | ||
else if (['setTdlibParameters', 'checkDatabaseEncryptionKey'].includes(ctx._)) { | ||
return [2 /*return*/, next()]; | ||
} | ||
else { | ||
return [2 /*return*/, deferred.promise.then(next)]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
}; | ||
Airgram.prototype.callApi = function (method, params, state) { | ||
@@ -150,0 +224,0 @@ var _this = this; |
{ | ||
"name": "@airgram/core", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"homepage": "https://airgram.io", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,5 +0,21 @@ | ||
import { apiFactory, ErrorUnion } from '@airgram/api' | ||
import { apiFactory, ErrorUnion, UpdateAuthorizationState } from '@airgram/api' | ||
import { ApiMethods } from '@airgram/api/apiFactory' | ||
import { Composer, createContext, Serializable, Updates } from './components' | ||
import { pick } from './helpers' | ||
interface Deferred { | ||
promise: Promise<void>, | ||
resolve: () => any | ||
reject: (error: Error) => any | ||
} | ||
function createDeferred (): Deferred { | ||
const deferred: Record<string, any> = {} | ||
deferred.promise = new Promise<void>((resolve, reject) => { | ||
deferred.resolve = resolve | ||
deferred.reject = reject | ||
}) | ||
return deferred as Deferred | ||
} | ||
const DEFAULT_CONFIG: Partial<Airgram.Config<any>> = { | ||
@@ -75,3 +91,4 @@ applicationVersion: '0.1.0', | ||
setTimeout(() => this.api.getAuthorizationState(), 0) | ||
this.bootstrapMiddleware() | ||
setTimeout(this.api.getAuthorizationState, 0) | ||
} | ||
@@ -108,2 +125,60 @@ | ||
private bootstrapMiddleware (): void { | ||
let deferred: Deferred | null = createDeferred() | ||
this.use(async (ctx, next) => { | ||
if (!deferred) { | ||
return next() | ||
} | ||
if (ctx.update) { | ||
if (ctx._ === 'updateAuthorizationState') { | ||
const update: UpdateAuthorizationState = ctx.update | ||
switch (update.authorizationState._) { | ||
case 'authorizationStateWaitTdlibParameters': { | ||
const keys: Array<keyof Airgram.TdLibConfig> = [ | ||
'useTestDc', | ||
'databaseDirectory', | ||
'filesDirectory', | ||
'useFileDatabase', | ||
'useChatInfoDatabase', | ||
'useMessageDatabase', | ||
'useSecretChats', | ||
'apiId', | ||
'apiHash', | ||
'systemLanguageCode', | ||
'deviceModel', | ||
'systemVersion', | ||
'applicationVersion', | ||
'enableStorageOptimizer', | ||
'ignoreFileNames' | ||
] | ||
this.api.setTdlibParameters({ | ||
parameters: { _: 'tdlibParameters', ...pick(this.config, keys) } | ||
}).catch(this.handleError) | ||
break | ||
} | ||
case 'authorizationStateWaitEncryptionKey': { | ||
this.api.checkDatabaseEncryptionKey({ | ||
encryptionKey: this.config.databaseEncryptionKey | ||
}).catch(this.handleError) | ||
break | ||
} | ||
default: { | ||
setTimeout(() => { | ||
if (deferred) { | ||
deferred.resolve() | ||
deferred = null | ||
} | ||
}, 0) | ||
} | ||
} | ||
} | ||
return next() | ||
} else if (['setTdlibParameters', 'checkDatabaseEncryptionKey'].includes(ctx._)) { | ||
return next() | ||
} else { | ||
return deferred.promise.then(next) | ||
} | ||
}) | ||
} | ||
private callApi<ParamsT, ResponseT> ( | ||
@@ -110,0 +185,0 @@ method: string, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107997
52
1489