Skip to main content

Utilities

The MentraOS SDK provides several utility classes and functions that help with resource management and working with data streams.

ResourceTracker

ResourceTracker is a utility class used to manage the lifecycle of resources, ensuring they are properly cleaned up to prevent memory leaks.

Creating a ResourceTracker

Properties

disposed

A getter that checks if the tracker has been disposed.
Returns: true if the tracker has been disposed, false otherwise.

Methods

track()

Registers a cleanup function to be called when the tracker is disposed.
Parameters:
  • cleanup: A function that will be called when the tracker is disposed
Returns: The same cleanup function, allowing for chaining Example:

trackDisposable()

Tracks an object that has a .dispose() or .close() method.
Parameters:
  • disposable: An object with a .dispose() or .close() method
Returns: A cleanup function that will dispose or close the object when called Example:

trackTimer(), trackTimeout(), trackInterval()

Track Node.js timers to ensure they are cleared when the tracker is disposed.
Parameters:
  • timerId: The ID returned by setTimeout or setInterval
  • isInterval: For trackTimer only - set to true if the timer is an interval
Returns: A cleanup function that will clear the timer when called Example:

setTimeout(), setInterval()

Convenience methods that create and automatically track timers.
Parameters:
  • callback: The function to execute
  • ms: The time in milliseconds to wait before execution
Returns: The timer ID, which is already being tracked Example:

dispose()

Executes all registered cleanup functions and marks the tracker as disposed.
Example:

Example: Using ResourceTracker in a Resource Manager

Stream Utility Functions

These utility functions help work with language-specific streams and stream types.

Language Stream Types

ExtendedStreamType

Represents either a standard StreamType enum value or a language-specific stream string.

LanguageStreamInfo

Structure holding parsed information from a language-specific stream identifier string.

parseLanguageStream()

Parses a string to determine if it represents a language-specific stream and extracts its components.
Parameters:
  • subscription: The stream identifier to parse (e.g., “transcription:en-US”)
Returns: A parsed LanguageStreamInfo object, or null if the input is not a valid language-specific stream Example:

createTranscriptionStream()

Creates a language-specific stream identifier string for transcription.
Parameters:
  • language: The language code (e.g., “en-US”)
Returns: The formatted stream identifier string (e.g., “transcription:en-US”) Example:

createTranslationStream()

Creates a language-specific stream identifier string for translation.
Parameters:
  • sourceLanguage: The source language code (e.g., “es-ES”)
  • targetLanguage: The target language code (e.g., “en-US”)
Returns: The formatted stream identifier string (e.g., “translation:es-ES-to-en-US”) Example:

isValidStreamType()

Checks if a value is either a valid StreamType enum member or a valid language-specific stream string.
Parameters:
  • subscription: The value to check
Returns: true if the subscription type is valid, false otherwise Example:

getBaseStreamType()

Extracts the base StreamType enum value from an ExtendedStreamType.
Parameters: Returns: The base StreamType, or null if invalid Example:

isLanguageStream()

Checks if an ExtendedStreamType represents a language-specific stream.
Parameters: Returns: true if it’s a language-specific stream format, false otherwise Example:

getLanguageInfo()

Convenience function equivalent to parseLanguageStream().
Parameters: Returns: Parsed info or null

Usage Patterns

Managing Language-Specific Streams

Resource Management in App Sessions