2024-02-28 00:33:23 +03:00
|
|
|
import { ITlPlatform, TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime'
|
|
|
|
|
2024-03-01 01:52:17 +03:00
|
|
|
import { UploadFileLike } from './highlevel/types/files/utils.js'
|
2024-02-28 00:33:23 +03:00
|
|
|
import { MtUnsupportedError } from './types/errors.js'
|
2024-03-01 01:52:17 +03:00
|
|
|
import { MaybePromise } from './types/index.js'
|
2024-02-28 00:33:23 +03:00
|
|
|
|
|
|
|
export interface ICorePlatform extends ITlPlatform {
|
|
|
|
beforeExit(fn: () => void): () => void
|
|
|
|
log(color: number, level: number, tag: string, fmt: string, args: unknown[]): void
|
|
|
|
getDefaultLogLevel(): number | null
|
|
|
|
getDeviceModel(): string
|
2024-03-01 01:52:17 +03:00
|
|
|
normalizeFile?(file: UploadFileLike): MaybePromise<{
|
|
|
|
file?: UploadFileLike
|
|
|
|
fileSize?: number
|
|
|
|
fileName?: string
|
|
|
|
} | null>
|
2024-02-28 00:33:23 +03:00
|
|
|
}
|
|
|
|
|
2024-03-01 01:52:17 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
let globalObject: any
|
|
|
|
|
|
|
|
if (typeof globalThis !== 'undefined') {
|
|
|
|
globalObject = globalThis
|
|
|
|
} else if (typeof global !== 'undefined') {
|
|
|
|
globalObject = global
|
|
|
|
} else if (typeof self !== 'undefined') {
|
|
|
|
globalObject = self
|
|
|
|
} else if (typeof window !== 'undefined') {
|
|
|
|
globalObject = window
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: when using with some bundlers (e.g. vite) re-importing this module will not return the same object
|
|
|
|
// so we need to store the platform in a global object to be able to survive hot-reloads etc.
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
let _platform: ICorePlatform | null = globalObject?.__MTCUTE_PLATFORM__ ?? null
|
2024-02-28 00:33:23 +03:00
|
|
|
|
|
|
|
export function setPlatform(platform: ICorePlatform): void {
|
|
|
|
if (_platform) {
|
|
|
|
if (_platform.constructor !== platform.constructor) {
|
|
|
|
throw new MtUnsupportedError('Platform may not be changed at runtime!')
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_platform = platform
|
|
|
|
TlBinaryReader.platform = platform
|
|
|
|
TlBinaryWriter.platform = platform
|
2024-03-01 01:52:17 +03:00
|
|
|
|
|
|
|
if (globalObject) {
|
|
|
|
globalObject.__MTCUTE_PLATFORM__ = platform
|
|
|
|
}
|
2024-02-28 00:33:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getPlatform(): ICorePlatform {
|
|
|
|
if (!_platform) {
|
|
|
|
throw new MtUnsupportedError('Platform is not set! Have you instantiated the client?')
|
|
|
|
}
|
|
|
|
|
|
|
|
return _platform
|
|
|
|
}
|