fetchPrevious method
Future<List<Reaction> >
fetchPrevious({
- required dynamic onSuccess()?,
- required dynamic onError(
- CometChatException excep
- ReactionFetchListener? listener,
Asynchronously fetches the previous set of reactions for a specific message.
With a listener, the read is two-phase like fetchNext, and records
and replays its own snapshot (BEH-SNAP-08): the cache phase fires first
while offline runs, then the server page, and every error ends in
ReactionFetchListener.onError. With no listener, the single-delivery
contract is unchanged.
Android Reference: ReactionsRequest.fetchPrevious()
Implementation
Future<List<Reaction>> fetchPrevious({
required Function(List<Reaction> message)? onSuccess,
required Function(CometChatException excep)? onError,
ReactionFetchListener? listener,
}) async {
try {
// A read past the start, two-phase with offline running: the cursor is
// left 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 empty answer with no request (BEH-AUTH-05).
final readsPastTheEnd =
!_hasPrevious &&
listener != null &&
OfflineRuntime.instance.store != null;
if (!_hasPrevious && !readsPastTheEnd) {
if (onSuccess != null) onSuccess([]);
listener?.onServerReactions(const <Reaction>[]);
return [];
}
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 [];
}
// 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 — this direction's recorded page (BEH-SNAP-08).
final before = _position;
final snapshots = listener == null ? null : _snapshotsFor(_previous);
if (listener != null) await _cachePhase(snapshots, listener);
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: 'prepend',
paginationReactionId: _previousReactionId,
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));
_inProgress = false;
if (onSuccess != null) onSuccess(fresh);
listener.onServerReactions(fresh);
return fresh;
}
_deliveredKeys.addAll(reactions.map(_keyOf));
if (listener == null && _position != before) _snapshotsLost = true;
// Record before delivering (BEH-SNAP-05). `hasNext` for this direction
// is `meta.previous` present (BEH-SNAP-08).
await snapshots?.recordEntities(
reactions,
hasNext: result.hasPreviousPage,
issuedIn: issuedIn,
toJson: (r) => (r as Reaction).toJson(),
);
// Held through the record step (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);
}
// Phase 2 — the server page.
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);
}
listener?.onError(cometChatEx);
}
return [];
}