Package 

Interface StreamTokenManager


  • 
    public interface StreamTokenManager
    
                        

    Manages the SDK’s authentication token lifecycle.

    A StreamTokenManager exposes the current token as a hot kotlinx.coroutines.flow.StateFlow and provides operations to supply a StreamTokenProvider, set a token directly, and invalidate the current token. Implementations are expected to cache the latest valid token and retrieve a new one via the provider on demand.

    • token emits the most recently known token, or null if no token is available.

    • Calling setProvider registers the source from which new tokens will be obtained.

    • Calling setToken sets the token immediately (useful when you already have one).

    • Calling invalidate clears the current token; the next access may trigger a refresh if a provider has been set.

    Implementations should be safe to use from any thread. The token flow is hot and never completes.

    val tokenManager = StreamTokenManager()
    tokenManager.setProvider(MyTokenProvider())
    tokenManager.token.value // null
    • Method Detail

      • setProvider

         abstract Result<Unit> setProvider(StreamTokenProvider provider)

        Registers the provider used to load tokens on demand.

        Supplying a provider does not necessarily trigger an immediate load; the implementation may fetch lazily or eagerly depending on its policy.

        Parameters:
        provider - The token provider to use for future refreshes.
      • setToken

         abstract Result<Unit> setToken(StreamToken token)

        Sets the current token explicitly.

        Use this when you already hold a freshly issued token and want to inject it directly. This overrides any previously stored token.

        Parameters:
        token - The token to store as current.
      • invalidate

         abstract Result<Unit> invalidate()

        Invalidates and clears the current token.

        After invalidation, token will emit null. If a provider is set, the next operation that requires a token may trigger a refresh.

      • loadIfAbsent

         abstract Result<StreamToken> loadIfAbsent()

        Ensures that a valid token is available.

        If a token is already cached, it is returned immediately. Otherwise, the provider is invoked to fetch a new token. This method does not block.

      • getToken

         abstract StateFlow<StreamToken> getToken()

        A hot stream of the current authentication token.

        Emits null when there is no token (e.g., before the first load or after invalidate is called) and updates with each successful set or refresh.