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.true if the tracker has been disposed, false otherwise.
Methods
track()
Registers a cleanup function to be called when the tracker is disposed.cleanup: A function that will be called when the tracker is disposed
trackDisposable()
Tracks an object that has a.dispose() or .close() method.
disposable: An object with a.dispose()or.close()method
trackTimer(), trackTimeout(), trackInterval()
Track Node.js timers to ensure they are cleared when the tracker is disposed.timerId: The ID returned bysetTimeoutorsetIntervalisInterval: FortrackTimeronly - set totrueif the timer is an interval
setTimeout(), setInterval()
Convenience methods that create and automatically track timers.callback: The function to executems: The time in milliseconds to wait before execution
dispose()
Executes all registered cleanup functions and marks the tracker as disposed.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 standardStreamType 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.subscription: The stream identifier to parse (e.g., “transcription:en-US”)
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.language: The language code (e.g., “en-US”)
createTranslationStream()
Creates a language-specific stream identifier string for translation.sourceLanguage: The source language code (e.g., “es-ES”)targetLanguage: The target language code (e.g., “en-US”)
isValidStreamType()
Checks if a value is either a validStreamType enum member or a valid language-specific stream string.
subscription: The value to check
true if the subscription type is valid, false otherwise
Example:
getBaseStreamType()
Extracts the baseStreamType enum value from an ExtendedStreamType.
subscription: TheExtendedStreamTypevalue
StreamType, or null if invalid
Example:
isLanguageStream()
Checks if anExtendedStreamType represents a language-specific stream.
subscription: TheExtendedStreamTypevalue
true if it’s a language-specific stream format, false otherwise
Example:
getLanguageInfo()
Convenience function equivalent toparseLanguageStream().
subscription: TheExtendedStreamTypevalue
null

