Overview
TheCtx (Context) model is the central state management structure in Stremio Core. It maintains the user’s profile, library, streams, notifications, and other essential application data. All state updates flow through this model using the Update pattern.
Structure
Fields
User profile containing authentication, settings, and installed addons
User’s library of saved content items (movies, series, etc.)
Notifications for new episodes and content updates
Cached stream data and playback history
URLs for streaming server endpoints
User’s search history data
Events that have been dismissed by the user
Current status of the context (Loading or Ready)
Optional Trakt addon descriptor for syncing watch history
Catalogs from addons that support the
lastVideosIds resource for notificationsModal and notification events
CtxStatus
The context can be in one of two states:- Loading: Context is authenticating with the provided
AuthRequest - Ready: Context is ready for operations
Constructor
new()
Creates a newCtx instance with the provided buckets.
Update Implementation
TheCtx implements the Update<E> trait to handle state changes through messages.
Supported Messages
Action::Ctx::Authenticate
Action::Ctx::Authenticate
Initiates authentication with the provided
AuthRequest. Sets status to Loading and triggers authentication flow.Source: src/models/ctx/ctx.rs:113Action::Ctx::Logout
Action::Ctx::Logout
Logs out the current user. Triggers
Internal::Logout message.Source: src/models/ctx/ctx.rs:117Internal::Logout
Internal::Logout
Handles logout process:
- Deletes remote session if not already deleted
- Resets profile, library, streams, and all buckets
- Updates status to
Ready - Emits
Event::UserLoggedOut
src/models/ctx/ctx.rs:120Internal::CtxAuthResult
Internal::CtxAuthResult
Processes authentication result:
- Updates profile with auth credentials
- Loads user’s addons and library items
- Handles locked addons or missing library errors
- Emits
Event::UserAuthenticatedon success
src/models/ctx/ctx.rs:169All Other Messages
All Other Messages
Delegates to sub-update functions:
update_profile- Profile changesupdate_library- Library changesupdate_streams- Stream changesupdate_streaming_server_urls- Server URL changesupdate_trakt_addon- Trakt addon changesupdate_notifications- Notification changesupdate_search_history- Search history changesupdate_events- Event changes
src/models/ctx/ctx.rs:269Authentication Flow
API Requests
Concurrent requests are made to:
- Authenticate user and get auth key
- Fetch user’s addon collection
- Fetch user’s library items from datastore
Events
TheCtx model emits the following events:
Event::UserLoggedOut { uid }- User has logged outEvent::UserAuthenticated { auth_request }- User successfully authenticatedEvent::UserAddonsLocked { addons_locked }- User’s addons are locked/unlockedEvent::UserLibraryMissing { library_missing }- User’s library is missingEvent::SessionDeleted { auth_key }- Remote session was deletedEvent::Error- Various error events for locked addons or missing library
Error Handling
Usage Example
See Also
- Profile - User profile and settings
- LibraryBucket - Library data structure
- Update Pattern - State management pattern
