TdRuntime class final

The process-wide TDLib runtime.

One background isolate calls td_receive for every client (TDLib forbids two concurrent receivers). The runtime routes responses to the futures returned by TdClient.invoke and updates to each client's streams, in the order TDLib produced them. Requests are sent directly with td_send, which is thread-safe.

Start it once, early, on the isolate that owns TDLib:

final runtime = await TdRuntime.start();
final client = runtime.createClient();
client.on<UpdateAuthorizationState>().listen(...);
final me = await client.invoke(const GetMe());

Properties

clients → Iterable<TdClient>
Clients created by this runtime that are not closed yet.
no setter
defaultTimeout → Duration
Default timeout for TdClient.invoke. A timeout only stops waiting; the TDLib request itself keeps running.
final
errors → Stream<Object>
Errors that aren't tied to one request: the receive isolate failing (TdReceiverException) or events that couldn't be decoded.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isReceiving → bool
Whether the receive isolate is running.
no setter
liveClientIds → List<int>
Identifiers of the live clients. Persist these, and pass them as staleClientIds on the next start, to close clients that survived a Dart restart in the same process (hot restart, a re-created engine).
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

close({Duration timeout = const Duration(seconds: 15)}) → Future<void>
Closes every client, waits for each to report it is closed (up to timeout), then stops the receive isolate. Call before the process exits when you can (e.g. on desktop quit).
createClient() → TdClient
Creates a TDLib client and sends it a first request, which makes TDLib start it: updateAuthorizationState with authorizationStateWaitTdlibParameters follows. Subscribe to TdClient.authorizationStates right after this call.
ensureReceiving() → Future<void>
Restarts the receive isolate if it stopped (e.g. after the OS froze a backgrounded Android app). Safe to call any time; call it on resume.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setCoalescedUpdates(List<String> updateTypes) → void
Update types whose older copies may be dropped when a newer copy arrives in the same small batch. Only for updates that carry the full current value. Defaults to file, user-status and connection updates.
setUpdateFilter(Set<String>? updateTypes) → void
Limits which updates are decoded at all. Updates of other types are dropped in the receive isolate without decoding, which saves CPU when an app needs only a few kinds. null (the default) delivers all.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

instance → TdRuntime?
The running runtime, if start has completed.
no setter

Static Methods

execute<R extends TdObject>(TdFunction<R> request) → R
Executes a request synchronously through td_execute, without a client. Only requests with TdFunction.canBeCalledSynchronously work. Loads libtdjson from its default place if start hasn't run yet.
start({String? libraryPath, int logVerbosityLevel = 1, bool checkVersion = true, Duration defaultTimeout = const Duration(seconds: 30), Iterable<int> staleClientIds = const []}) → Future<TdRuntime>
Starts the runtime, or returns the one already running.
verifyNativeVersion() → void
Throws TdVersionMismatchException if the native TDLib isn't the one this package's API was generated for.