fetchNext method
Future<List<Reaction> >
fetchNext({
- required dynamic onSuccess()?,
- required dynamic onError(
- CometChatException excep
- ReactionFetchListener? listener,
Asynchronously fetches the next set of reactions for a particular message.
Android Reference: ReactionsRequest.fetchNext()
Implementation
Future<List<Reaction>> fetchNext({
required Function(List<Reaction> message)? onSuccess,
required Function(CometChatException excep)? onError,
ReactionFetchListener? listener,
}) async {
try {
if (limit <= 0) {
final error = CometChatException(
ErrorCode.errorNonPositiveLimit,
'Limit must be a positive number',
'Limit must be a positive number',
);
if (onError != null) onError(error);
listener?.onError(error);
return [];
}
if (messageId <= 0) {
final error = CometChatException(
ErrorCode.errorInvalidMessageId,
'Invalid message ID',
'Invalid message ID',
);
if (onError != null) onError(error);
listener?.onError(error);
return [];
}
if (_inProgress) {
final error = CometChatException(
ErrorCode.errorRequestInProgress,
'Request already in progress',
'Request already in progress',
);
if (onError != null) onError(error);
listener?.onError(error);
return [];
}
// A read past the end, two-phase with offline running: the cursor is
// still on the last page, so the request re-sends it, and only what this
// request has not delivered goes out — usually nothing; nothing is
// recorded (BEH-READ-05, R120). A legacy caller, or offline off, keeps
// today's behaviour (BEH-AUTH-05).
final readsPastTheEnd =
_nextExhausted &&
listener != null &&
OfflineRuntime.instance.store != null;
// Set before the cache phase: a fetch called while this one is in
// flight fires no cache phase and leaves the replay index alone
// (BEH-SNAP-14).
_inProgress = true;
// Phase 1 — replay a recorded page. The replay index is independent of
// the request's pagination cursor, so a replay never advances it.
//
// `fetchNext` ONLY: the snapshot records a forward-paged sequence, and
// `fetchPrevious` walks the other way. Replaying forward pages backwards
// would hand the caller the wrong window, so backward reads go straight
// to the server.
if (listener != null) {
final cached = await _snapshots.cachedPhase(Reaction.fromMap);
if (cached != null) listener.onCachedReactions(cached);
}
final sdk = SdkRegistry.getInstance();
// Captured as the request goes out: a page answered after this
// session ended is never written (BEH-SYNC-06).
final issuedIn = OfflineRuntime.instance.readSession;
final result = await sdk.reactions.getReactions(
messageId.toString(),
limit: limit,
affix: 'append',
paginationReactionId: _nextReactionId,
reaction: reaction,
);
// **Android Reference:** hasPrevious set based on KEY_PAGINATION_PREVIOUS key existence
_hasPrevious = result.hasPreviousPage;
if (result.previousReactionId != null) {
_previousReactionId = result.previousReactionId;
}
if (result.nextReactionId != null) {
_nextReactionId = result.nextReactionId;
}
final reactions = result.reactions.map((reactionData) {
return Reaction(
id: reactionData.id,
messageId: reactionData.messageId ?? messageId,
reaction: reactionData.reaction,
uid: reactionData.uid,
reactedBy: User.fromMap(reactionData.reactedBy),
reactedAt: reactionData.reactedAt,
);
}).toList();
if (readsPastTheEnd) {
final fresh = reactions
.where((r) => !_deliveredKeys.contains(_keyOf(r)))
.toList(growable: false);
_deliveredKeys.addAll(fresh.map(_keyOf));
if (result.nextReactionId != null) _nextExhausted = false;
_inProgress = false;
if (onSuccess != null) onSuccess(fresh);
listener.onServerReactions(fresh);
return fresh;
}
_nextExhausted = result.nextReactionId == null;
_deliveredKeys.addAll(reactions.map(_keyOf));
// Record before delivering, so a caller that re-reads immediately sees
// the same page.
await _snapshots.recordEntities(
reactions,
hasNext: result.nextReactionId != null,
issuedIn: issuedIn,
toJson: (r) => (r as Reaction).toJson(),
);
// Held through the record step, so a fetch landing during it cannot
// replay the page about to be delivered (BEH-SNAP-14); cleared before
// the callbacks, so a fetch made from one of them is a new fetch.
_inProgress = false;
if (onSuccess != null) {
onSuccess(reactions);
}
listener?.onServerReactions(reactions);
return reactions;
} on sdk_errors.SdkException catch (sdkEx) {
_inProgress = false;
final cometChatEx = CometChatException(
sdkEx.code,
sdkEx.details ?? sdkEx.message,
sdkEx.message,
);
if (onError != null) {
onError(cometChatEx);
}
// A listener-only caller is told too, or it waits forever offline
// (BEH-READ-05).
listener?.onError(cometChatEx);
} catch (e) {
_inProgress = false;
final cometChatEx = CometChatException(
ErrorCode.errorUnhandledException,
e.toString(),
e.toString(),
);
if (onError != null) {
onError(cometChatEx);
}
// A listener-only caller is told too, or it waits forever offline
// (BEH-READ-05).
listener?.onError(cometChatEx);
}
return [];
}