interceptWith method

DriftConnection interceptWith(
  1. QueryInterceptor interceptor, {
  2. bool sticky = true,
})

Returns a DriftConnection that will use the same stream queries as this, but replaces its executor by wrapping it with the interceptor.

When sticky is enabled (the default), child sessions (e.g. transactions) are also intercepted using the interceptor.

See also: ApplyInterceptor.interceptWith.

Implementation

DriftConnection interceptWith(
  QueryInterceptor interceptor, {
  bool sticky = true,
}) {
  return DriftConnection.withImplementation(
    dialect: dialect,
    implementation: (dialect) async {
      final old = await open(dialect);
      // When intercepting an entire connection, prefer to wrap the inner
      // session in a compat session so that e.g. calls to create transactions
      // can be intercepted as begin / commit invocations.
      // Since the outermost transaction is always wrapped in a compatibility
      // session and most native session implementations don't support
      // transactions, the interceptor would otherwise see [execute] calls to
      // `BEGIN` instead of a transaction.
      final session = DriftCompatibilitySession(
        inner: old.session,
        dialect: dialect,
      ).interceptWith(interceptor, sticky: sticky);
      return OpenedDriftConnection(session, old.streamQueries);
    },
  );
}