fix(llc): channel.query not initializing state.
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -1339,26 +1339,6 @@ class Channel {
|
|||||||
return _client.markChannelRead(id!, type, messageId: messageId);
|
return _client.markChannelRead(id!, type, messageId: messageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads the initial channel state and watches for changes.
|
|
||||||
Future<ChannelState> watch({bool presence = false}) async {
|
|
||||||
ChannelState response;
|
|
||||||
|
|
||||||
try {
|
|
||||||
response = await query(watch: true, presence: presence);
|
|
||||||
} catch (error, stackTrace) {
|
|
||||||
if (!_initializedCompleter.isCompleted) {
|
|
||||||
_initializedCompleter.completeError(error, stackTrace);
|
|
||||||
}
|
|
||||||
rethrow;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == null) {
|
|
||||||
_initState(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
void _initState(ChannelState channelState) {
|
void _initState(ChannelState channelState) {
|
||||||
state = ChannelClientState(this, channelState);
|
state = ChannelClientState(this, channelState);
|
||||||
|
|
||||||
@@ -1370,6 +1350,22 @@ class Channel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Loads the initial channel state and watches for changes.
|
||||||
|
Future<ChannelState> watch({
|
||||||
|
bool presence = false,
|
||||||
|
PaginationParams? messagesPagination,
|
||||||
|
PaginationParams? membersPagination,
|
||||||
|
PaginationParams? watchersPagination,
|
||||||
|
}) {
|
||||||
|
return query(
|
||||||
|
watch: true,
|
||||||
|
presence: presence,
|
||||||
|
messagesPagination: messagesPagination,
|
||||||
|
membersPagination: membersPagination,
|
||||||
|
watchersPagination: watchersPagination,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// Stop watching the channel.
|
/// Stop watching the channel.
|
||||||
Future<EmptyResponse> stopWatching() async {
|
Future<EmptyResponse> stopWatching() async {
|
||||||
_checkInitialized();
|
_checkInitialized();
|
||||||
@@ -1435,7 +1431,7 @@ class Channel {
|
|||||||
);
|
);
|
||||||
|
|
||||||
/// Creates a new channel.
|
/// Creates a new channel.
|
||||||
Future<ChannelState> create() async => query(state: false);
|
Future<ChannelState> create() => query(state: false);
|
||||||
|
|
||||||
/// Query the API, get messages, members or other channel fields.
|
/// Query the API, get messages, members or other channel fields.
|
||||||
///
|
///
|
||||||
@@ -1450,23 +1446,28 @@ class Channel {
|
|||||||
PaginationParams? watchersPagination,
|
PaginationParams? watchersPagination,
|
||||||
bool preferOffline = false,
|
bool preferOffline = false,
|
||||||
}) async {
|
}) async {
|
||||||
if (preferOffline && cid != null) {
|
ChannelState? channelState;
|
||||||
final updatedState = await _client.chatPersistenceClient
|
|
||||||
?.getChannelStateByCid(cid!, messagePagination: messagesPagination);
|
|
||||||
if (updatedState != null &&
|
|
||||||
updatedState.messages != null &&
|
|
||||||
updatedState.messages!.isNotEmpty) {
|
|
||||||
if (this.state == null) {
|
|
||||||
_initState(updatedState);
|
|
||||||
} else {
|
|
||||||
this.state?.updateChannelState(updatedState);
|
|
||||||
}
|
|
||||||
return updatedState;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final updatedState = await _client.queryChannel(
|
// If we prefer offline, we first try to get the channel state from the
|
||||||
|
// offline storage.
|
||||||
|
if (preferOffline && !watch && cid != null) {
|
||||||
|
final persistenceClient = _client.chatPersistenceClient;
|
||||||
|
if (persistenceClient != null) {
|
||||||
|
final cachedState = await persistenceClient.getChannelStateByCid(
|
||||||
|
cid!,
|
||||||
|
messagePagination: messagesPagination,
|
||||||
|
);
|
||||||
|
|
||||||
|
// If the cached state contains messages, we can use it.
|
||||||
|
if (cachedState.messages?.isNotEmpty == true) {
|
||||||
|
channelState = cachedState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we still don't have the channelState, we try to get it from the API.
|
||||||
|
channelState ??= await _client.queryChannel(
|
||||||
type,
|
type,
|
||||||
channelId: id,
|
channelId: id,
|
||||||
channelData: _extraData,
|
channelData: _extraData,
|
||||||
@@ -1479,18 +1480,35 @@ class Channel {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (_id == null) {
|
if (_id == null) {
|
||||||
_id = updatedState.channel!.id;
|
_id = channelState.channel!.id;
|
||||||
_cid = updatedState.channel!.cid;
|
_cid = channelState.channel!.cid;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.state?.updateChannelState(updatedState);
|
// Initialize the channel state if it's not initialized yet.
|
||||||
return updatedState;
|
if (this.state == null) {
|
||||||
} catch (e) {
|
_initState(channelState);
|
||||||
if (_client.persistenceEnabled) {
|
} else {
|
||||||
return _client.chatPersistenceClient!.getChannelStateByCid(
|
// Otherwise, update the channel state.
|
||||||
cid!,
|
this.state?.updateChannelState(channelState);
|
||||||
messagePagination: messagesPagination,
|
}
|
||||||
);
|
|
||||||
|
return channelState;
|
||||||
|
} catch (e, stk) {
|
||||||
|
// If we failed to get the channel state from the API and we were not
|
||||||
|
// supposed to watch the channel, we will try to get the channel state
|
||||||
|
// from the offline storage.
|
||||||
|
if (watch == false) {
|
||||||
|
if (_client.persistenceEnabled) {
|
||||||
|
return _client.chatPersistenceClient!.getChannelStateByCid(
|
||||||
|
cid!,
|
||||||
|
messagePagination: messagesPagination,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, we will just rethrow the error.
|
||||||
|
if (!_initializedCompleter.isCompleted) {
|
||||||
|
_initializedCompleter.completeError(e, stk);
|
||||||
}
|
}
|
||||||
|
|
||||||
rethrow;
|
rethrow;
|
||||||
|
|||||||
Reference in New Issue
Block a user