-
Notifications
You must be signed in to change notification settings - Fork 13
fix: pass right type param, add session ttl handling #3653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ice-evander
wants to merge
1
commit into
master
Choose a base branch
from
bugfix/tc-filter-creator-tokens-by-type
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,11 @@ part 'category_tokens_provider.r.g.dart'; | |
| @riverpod | ||
| class CategoryTokensNotifier extends _$CategoryTokensNotifier { | ||
| static const int _limit = 10; | ||
| static const int _sessionRefreshBeforeExpirySec = 30; | ||
|
|
||
| NetworkSubscription<List<CommunityTokenBase>>? _realtimeSubscription; | ||
| Timer? _sessionExpiryTimer; | ||
| bool _disposed = false; | ||
| late final TokenCategoryType _type; | ||
| String? _tokenType; | ||
|
|
||
|
|
@@ -21,6 +25,9 @@ class CategoryTokensNotifier extends _$CategoryTokensNotifier { | |
| _type = type; | ||
|
|
||
| ref.onDispose(() async { | ||
| _disposed = true; | ||
| _sessionExpiryTimer?.cancel(); | ||
| _sessionExpiryTimer = null; | ||
| await _realtimeSubscription?.close(); | ||
| _realtimeSubscription = null; | ||
| }); | ||
|
|
@@ -30,6 +37,18 @@ class CategoryTokensNotifier extends _$CategoryTokensNotifier { | |
| return const CategoryTokensState(); | ||
| } | ||
|
|
||
| /// TTL from API: typically seconds (e.g. 300 = 5 min); | ||
| void _scheduleSessionRecreate(ViewingSession session) { | ||
| _sessionExpiryTimer?.cancel(); | ||
| _sessionExpiryTimer = null; | ||
| final ttlSec = session.ttl >= 1000 ? session.ttl ~/ 1000 : session.ttl; | ||
| final refreshInSec = (ttlSec - _sessionRefreshBeforeExpirySec).clamp(1, ttlSec); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason we refresh the token exactly 30 seconds before it expires? |
||
| _sessionExpiryTimer = Timer(Duration(seconds: refreshInSec), () { | ||
|
Comment on lines
+40
to
+46
|
||
| if (_disposed) return; | ||
| unawaited(refresh()); | ||
| }); | ||
| } | ||
|
|
||
| Future<void> _initialize() async { | ||
| if (state.sessionId != null) return; | ||
|
|
||
|
|
@@ -40,29 +59,31 @@ class CategoryTokensNotifier extends _$CategoryTokensNotifier { | |
| ); | ||
|
|
||
| state = state.copyWith(sessionId: session.id); | ||
| _scheduleSessionRecreate(session); | ||
|
|
||
| unawaited(_subscribeToRealtimeUpdates(session.id, _type)); | ||
| unawaited(_loadInitial()); | ||
| } | ||
|
|
||
| Future<void> _loadInitial() async { | ||
| Future<void> _loadInitial({String? sessionId}) async { | ||
| if (state.browsingIsLoading || state.browsingIsInitialLoading) return; | ||
|
|
||
| final effectiveSessionId = sessionId ?? state.sessionId; | ||
| if (effectiveSessionId == null) { | ||
| state = state.copyWith( | ||
| browsingIsLoading: false, | ||
| browsingIsInitialLoading: false, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| state = state.copyWith(browsingIsInitialLoading: true, browsingIsLoading: true); | ||
|
|
||
| try { | ||
| final client = await ref.read(ionTokenAnalyticsClientProvider.future); | ||
| final sessionId = state.sessionId; | ||
| if (sessionId == null) { | ||
| state = state.copyWith( | ||
| browsingIsLoading: false, | ||
| browsingIsInitialLoading: false, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| final page = await client.communityTokens.getCategoryTokens( | ||
| sessionId: sessionId, | ||
| sessionId: effectiveSessionId, | ||
| type: _type, | ||
| tokenType: _tokenType, | ||
| limit: _limit, | ||
|
|
@@ -236,6 +257,8 @@ class CategoryTokensNotifier extends _$CategoryTokensNotifier { | |
| } | ||
|
|
||
| Future<void> refresh() async { | ||
| _sessionExpiryTimer?.cancel(); | ||
| _sessionExpiryTimer = null; | ||
| await _realtimeSubscription?.close(); | ||
| _realtimeSubscription = null; | ||
| state = const CategoryTokensState(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have an issue with this
_disposed- u set it totruebut u never reset it.ref.onDisposeis called, for example, when a dependency is changed and the same instance is used, so the provider will remain in_disposed->truestate in this case.And why do we need this flag in the first place? The only place where it is used is in the Timer callback and u cancel all the timers in the same place u set this flag, so isn't it redundant?