diff --git a/packages/core/src/highlevel/types/stories/all-stories.ts b/packages/core/src/highlevel/types/stories/all-stories.ts index 24cf549b..bafc4fd7 100644 --- a/packages/core/src/highlevel/types/stories/all-stories.ts +++ b/packages/core/src/highlevel/types/stories/all-stories.ts @@ -12,13 +12,14 @@ import { StoriesStealthMode } from './stealth-mode.js' * Returned by {@link TelegramClient.getAllStories} */ export class AllStories { + /** Peers index */ + readonly _peers constructor( /** Raw TL object */ readonly raw: tl.stories.RawAllStories, - ) {} - - /** Peers index */ - readonly _peers = PeersIndex.from(this.raw) + ) { + this._peers = PeersIndex.from(this.raw) + } /** Whether there are more stories to fetch */ get hasMore(): boolean { diff --git a/packages/core/src/highlevel/types/stories/story-viewer.ts b/packages/core/src/highlevel/types/stories/story-viewer.ts index e20afddf..75e9e21d 100644 --- a/packages/core/src/highlevel/types/stories/story-viewer.ts +++ b/packages/core/src/highlevel/types/stories/story-viewer.ts @@ -129,9 +129,10 @@ export class StoryRepost { * List of story viewers. */ export class StoryViewersList { - constructor(readonly raw: tl.stories.RawStoryViewsList) {} - - readonly _peers = PeersIndex.from(this.raw) + readonly _peers: PeersIndex + constructor(readonly raw: tl.stories.RawStoryViewsList) { + this._peers = PeersIndex.from(this.raw) + } /** Next offset for pagination */ get next(): string | undefined { diff --git a/packages/core/src/highlevel/worker/app-config.ts b/packages/core/src/highlevel/worker/app-config.ts index 6f5dcbbc..cea8564a 100644 --- a/packages/core/src/highlevel/worker/app-config.ts +++ b/packages/core/src/highlevel/worker/app-config.ts @@ -3,10 +3,13 @@ import { AppConfigManager } from '../managers/app-config-manager.js' import { WorkerInvoker } from './invoker.js' export class AppConfigManagerProxy implements PublicPart { - constructor(readonly invoker: WorkerInvoker) {} + readonly get: AppConfigManager['get'] + readonly getField - private _bind = this.invoker.makeBinder('app-config') + constructor(readonly invoker: WorkerInvoker) { + const bind = invoker.makeBinder('app-config') - readonly get = this._bind('get') - readonly getField = this._bind('getField') + this.get = bind('get') + this.getField = bind('getField') + } } diff --git a/packages/core/src/highlevel/worker/port.ts b/packages/core/src/highlevel/worker/port.ts index 6078f775..1a24cec6 100644 --- a/packages/core/src/highlevel/worker/port.ts +++ b/packages/core/src/highlevel/worker/port.ts @@ -14,12 +14,68 @@ export interface TelegramWorkerPortOptions { } export abstract class TelegramWorkerPort implements ITelegramClient { - constructor(readonly options: TelegramWorkerPortOptions) {} + readonly log + + private _connection + private _invoker + + readonly storage + readonly appConfig + + // bound methods + readonly prepare + private _connect + readonly close + readonly notifyLoggedIn + readonly notifyLoggedOut + readonly notifyChannelOpened + readonly notifyChannelClosed + readonly call + readonly importSession + readonly exportSession + readonly handleClientUpdate + readonly getApiCrenetials + readonly getPoolSize + readonly getPrimaryDcId + readonly computeSrpParams + readonly computeNewPasswordHash + readonly startUpdatesLoop + readonly stopUpdatesLoop + + constructor(readonly options: TelegramWorkerPortOptions) { + this.log = new LogManager('worker') + + this._connection = this.connectToWorker(this.options.worker, this._onMessage) + this._invoker = new WorkerInvoker(this._connection[0]) + + this.storage = new TelegramStorageProxy(this._invoker) + this.appConfig = new AppConfigManagerProxy(this._invoker) + + const bind = this._invoker.makeBinder('client') + + this.prepare = bind('prepare') + this._connect = bind('connect') + + this.close = bind('close') + this.notifyLoggedIn = bind('notifyLoggedIn') + this.notifyLoggedOut = bind('notifyLoggedOut') + this.notifyChannelOpened = bind('notifyChannelOpened') + this.notifyChannelClosed = bind('notifyChannelClosed') + this.call = bind('call') + this.importSession = bind('importSession') + this.exportSession = bind('exportSession') + this.handleClientUpdate = bind('handleClientUpdate', true) + this.getApiCrenetials = bind('getApiCrenetials') + this.getPoolSize = bind('getPoolSize') + this.getPrimaryDcId = bind('getPrimaryDcId') + this.computeSrpParams = bind('computeSrpParams') + this.computeNewPasswordHash = bind('computeNewPasswordHash') + this.startUpdatesLoop = bind('startUpdatesLoop') + this.stopUpdatesLoop = bind('stopUpdatesLoop') + } abstract connectToWorker(worker: SomeWorker, handler: ClientMessageHandler): [SendFn, () => void] - readonly log = new LogManager('worker') - private _serverUpdatesHandler: (updates: tl.TypeUpdates) => void = () => {} onServerUpdate(handler: (updates: tl.TypeUpdates) => void): void { this._serverUpdatesHandler = handler @@ -69,13 +125,6 @@ export abstract class TelegramWorkerPort imp } } - private _connection = this.connectToWorker(this.options.worker, this._onMessage) - private _invoker = new WorkerInvoker(this._connection[0]) - private _bind = this._invoker.makeBinder('client') - - readonly storage = new TelegramStorageProxy(this._invoker) - readonly appConfig = new AppConfigManagerProxy(this._invoker) - private _destroyed = false destroy(terminate = false): void { if (this._destroyed) return @@ -91,26 +140,8 @@ export abstract class TelegramWorkerPort imp return this._invoker.invoke('custom', method as string, args) as Promise> } - readonly prepare = this._bind('prepare') - private _connect = this._bind('connect') async connect(): Promise { await this._connect() await this.storage.self.fetch() // force cache self locally } - readonly close = this._bind('close') - readonly notifyLoggedIn = this._bind('notifyLoggedIn') - readonly notifyLoggedOut = this._bind('notifyLoggedOut') - readonly notifyChannelOpened = this._bind('notifyChannelOpened') - readonly notifyChannelClosed = this._bind('notifyChannelClosed') - readonly call = this._bind('call') - readonly importSession = this._bind('importSession') - readonly exportSession = this._bind('exportSession') - readonly handleClientUpdate = this._bind('handleClientUpdate', true) - readonly getApiCrenetials = this._bind('getApiCrenetials') - readonly getPoolSize = this._bind('getPoolSize') - readonly getPrimaryDcId = this._bind('getPrimaryDcId') - readonly computeSrpParams = this._bind('computeSrpParams') - readonly computeNewPasswordHash = this._bind('computeNewPasswordHash') - readonly startUpdatesLoop = this._bind('startUpdatesLoop') - readonly stopUpdatesLoop = this._bind('stopUpdatesLoop') } diff --git a/packages/core/src/highlevel/worker/storage.ts b/packages/core/src/highlevel/worker/storage.ts index 49252410..4d07f754 100644 --- a/packages/core/src/highlevel/worker/storage.ts +++ b/packages/core/src/highlevel/worker/storage.ts @@ -8,25 +8,32 @@ import { TelegramStorageManager } from '../storage/storage.js' import { WorkerInvoker } from './invoker.js' class CurrentUserServiceProxy implements PublicPart { - constructor(private _invoker: WorkerInvoker) {} - private _bind = this._invoker.makeBinder('storage-self') + private _store + private _storeFrom + private _fetch + private _update + + constructor(invoker: WorkerInvoker) { + const bind = invoker.makeBinder('storage-self') + this._store = bind('store') + this._storeFrom = bind('storeFrom') + this._fetch = bind('fetch') + this._update = bind('update') + } private _cached?: CurrentUserInfo | null - private _store = this._bind('store') async store(info: CurrentUserInfo | null): Promise { await this._store(info) this._cached = info } - private _storeFrom = this._bind('storeFrom') async storeFrom(user: tl.TypeUser): Promise { this._cached = await this._storeFrom(user) return this._cached } - private _fetch = this._bind('fetch') async fetch(): Promise { if (this._cached) return this._cached @@ -45,7 +52,6 @@ class CurrentUserServiceProxy implements PublicPart { return this._cached } - private _update = this._bind('update') async update(params: Parameters[0]): Promise { await this._update(params) this._cached = await this._fetch() @@ -53,28 +59,41 @@ class CurrentUserServiceProxy implements PublicPart { } class PeersServiceProxy implements PublicPart { - constructor(private _invoker: WorkerInvoker) {} - private _bind = this._invoker.makeBinder('storage-peers') + readonly updatePeersFrom + readonly store + readonly getById + readonly getByPhone + readonly getByUsername + readonly getCompleteById - readonly updatePeersFrom = this._bind('updatePeersFrom') - readonly store = this._bind('store') - readonly getById = this._bind('getById') - readonly getByPhone = this._bind('getByPhone') - readonly getByUsername = this._bind('getByUsername') - readonly getCompleteById = this._bind('getCompleteById') + constructor(private _invoker: WorkerInvoker) { + const bind = this._invoker.makeBinder('storage-peers') + + this.updatePeersFrom = bind('updatePeersFrom') + this.store = bind('store') + this.getById = bind('getById') + this.getByPhone = bind('getByPhone') + this.getByUsername = bind('getByUsername') + this.getCompleteById = bind('getCompleteById') + } } export class TelegramStorageProxy implements PublicPart { - constructor(private _invoker: WorkerInvoker) {} + readonly self + readonly peers - private _bind = this._invoker.makeBinder('storage') + readonly clear + + constructor(private _invoker: WorkerInvoker) { + const bind = this._invoker.makeBinder('storage') + + this.self = new CurrentUserServiceProxy(this._invoker) + this.peers = new PeersServiceProxy(this._invoker) + + this.clear = bind('clear') + } // todo - remove once we move these to updates manager readonly updates = null as never readonly refMsgs = null as never - - readonly self = new CurrentUserServiceProxy(this._invoker) - readonly peers = new PeersServiceProxy(this._invoker) - - readonly clear = this._bind('clear') } diff --git a/packages/dispatcher/src/state/providers/memory.ts b/packages/dispatcher/src/state/providers/memory.ts index 65dd3928..d72a521c 100644 --- a/packages/dispatcher/src/state/providers/memory.ts +++ b/packages/dispatcher/src/state/providers/memory.ts @@ -14,9 +14,12 @@ interface RateLimitDto { } class MemoryStateRepository implements IStateRepository { - constructor(readonly _driver: MemoryStorageDriver) {} - - readonly state = this._driver.getState>('dispatcher_fsm', () => new Map()) + readonly state + readonly rl + constructor(readonly _driver: MemoryStorageDriver) { + this.state = this._driver.getState>('dispatcher_fsm', () => new Map()) + this.rl = this._driver.getState>('rl', () => new Map()) + } setState(key: string, state: string, ttl?: number | undefined): void { this.state.set(key, { @@ -56,8 +59,6 @@ class MemoryStateRepository implements IStateRepository { } } - readonly rl = this._driver.getState>('rl', () => new Map()) - getRateLimit(key: string, now: number, limit: number, window: number): [number, number] { // leaky bucket const item = this.rl.get(key) @@ -97,7 +98,9 @@ class MemoryStateRepository implements IStateRepository { } export class MemoryStateStorage implements IStateStorageProvider { - constructor(readonly driver: MemoryStorageDriver = new MemoryStorageDriver()) {} + readonly state - readonly state = new MemoryStateRepository(this.driver) + constructor(readonly driver: MemoryStorageDriver = new MemoryStorageDriver()) { + this.state = new MemoryStateRepository(this.driver) + } } diff --git a/packages/dispatcher/src/state/providers/sqlite.ts b/packages/dispatcher/src/state/providers/sqlite.ts index 5b3423db..1273c07e 100644 --- a/packages/dispatcher/src/state/providers/sqlite.ts +++ b/packages/dispatcher/src/state/providers/sqlite.ts @@ -116,11 +116,12 @@ class SqliteStateRepository implements IStateRepository { } export class SqliteStateStorage implements IStateStorageProvider { - constructor(readonly driver: BaseSqliteStorageDriver) {} + readonly state + constructor(readonly driver: BaseSqliteStorageDriver) { + this.state = new SqliteStateRepository(driver) + } static from(provider: BaseSqliteStorage) { return new SqliteStateStorage(provider.driver) } - - readonly state = new SqliteStateRepository(this.driver) }