import 'dart:async'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import '../persistence/app_settings_repositories.dart'; import 'projects.dart'; import 'timeline_session.dart'; import 'timeline_view_state.dart'; part 'timeline_provider.g.dart'; @riverpod FutureOr editorTimeline( Ref ref, String projectId, String timelineId, ) { final ids = (projectId, timelineId); final projects = ref.read(projectsProvider.notifier); final cached = projects.cachedTimelineSession(projectId, timelineId); if (cached != null) return _bindTimeline(ref, ids, cached); return projects.openTimelineSession(projectId, timelineId).then((session) { if (!ref.mounted) return session; return _bindTimeline(ref, ids, session); }); } FutureOr _bindTimeline( Ref ref, (String, String) ids, EditorTimelineSession session, ) { final binding = TimelineViewStateBinding.attach( projectId: ids.$1, timelineId: ids.$2, session: session, repository: ref.read(timelineViewSettingsRepositoryProvider), ); if (binding is TimelineViewStateBinding) { return _retainTimeline(ref, session, binding); } return binding.then((resolved) => _retainTimeline(ref, session, resolved)); } EditorTimelineSession _retainTimeline( Ref ref, EditorTimelineSession session, TimelineViewStateBinding binding, ) { if (ref.mounted) { final releaseViews = session.retainViews(); ref.onDispose(() { unawaited(binding.close()); releaseViews(); }); } else { unawaited(binding.close()); } return session; }