Skip to content

Commit

Permalink
⚠️ Removed updatesStreamController
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySreelal committed Nov 23, 2024
1 parent 22cfc51 commit 08a3f57
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
26 changes: 11 additions & 15 deletions lib/src/televerse/bot/bot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,6 @@ class Bot<CTX extends Context> {
fetcher._updateStreamController?.isClosed == true) {
fetcher._updateStreamController = StreamController<Update>.broadcast();
}
if (fetcher._updatesStreamController == null ||
fetcher._updatesStreamController?.isClosed == true) {
fetcher._updatesStreamController =
StreamController<List<Update>>.broadcast();
}
// Set instance variable
_instance = this;
}
Expand Down Expand Up @@ -360,7 +355,7 @@ class Bot<CTX extends Context> {
///
/// This method creates the Context instance for the update and resolves the
/// Handler Scopes and proceeds to process the update.
void _onUpdate(Update update) async {
Future<void> _onUpdate(Update update) async {
// Find matching scopes
bool matchScopes(HandlerScope<CTX> scope) {
return scope.types.contains(update.type);
Expand Down Expand Up @@ -389,8 +384,10 @@ class Bot<CTX extends Context> {
// Finds and processes the handler scopes.
for (int i = 0; i < sub.length; i++) {
final passing = sub[i].predicate(context);
if (!passing) continue;
print("Testing");

if (passing && sub[i].hasCustomPredicate) {
if (sub[i].hasCustomPredicate) {
try {
final customPass = await sub[i].options!.customPredicate!.call(
context,
Expand All @@ -403,20 +400,19 @@ class Bot<CTX extends Context> {
}
}

if (sub[i].isConversation && passing) {
if (sub[i].isConversation) {
print("Ah, conversation! Skipped!");
break;
}

if (sub[i].handler == null) continue;

_preProcess(sub[i], context);

if (passing) {
await _applyMiddlewares(context, () async {
await _processUpdate(sub[i], context);
});
break;
}
await _applyMiddlewares(context, () async {
await _processUpdate(sub[i], context);
});
break;
}
}

Expand Down Expand Up @@ -593,7 +589,7 @@ class Bot<CTX extends Context> {
/// This method is useful when you want to use a custom webhook server instead of the default one provided by Televerse,
/// or to use in a cloud function.
/// use Update.fromJson(json) to convert the json to an update.
void handleUpdate(Update update) => _onUpdate(update);
Future<void> handleUpdate(Update update) => _onUpdate(update);

/// Initialize the bot.
///
Expand Down
10 changes: 0 additions & 10 deletions lib/src/televerse/fetch/fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@ sealed class Fetcher<CTX extends Context> {
/// The stream controller that emits new updates.
StreamController<Update>? _updateStreamController;

/// The stream controller that emits new updates.
StreamController<List<Update>>? _updatesStreamController;

/// Creates a new fetcher.
Fetcher();

/// Emit new update into the stream.
void addUpdate(Update update) => _updateStreamController?.add(update);

/// Emit new update into the stream.
void addUpdates(List<Update> updates) =>
_updatesStreamController?.add(updates);

/// Handler for new updates.
Stream<Update> onUpdate() => _updateStreamController!.stream;

/// Handler for new updates.
Stream<List<Update>> onUpdates() => _updatesStreamController!.stream;

/// Starts fetching updates.
Future<void> start();

Expand Down
1 change: 0 additions & 1 deletion lib/src/televerse/fetch/long_polling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class LongPolling<CTX extends Context> extends Fetcher<CTX> {
timeout: timeout,
allowedUpdates: allowedUpdates?.map((e) => e.type).toList(),
);
addUpdates(updates);
int len = updates.length;
for (int i = 0; i < len; i++) {
if (_updateStreamController?.isClosed ?? false) {
Expand Down
7 changes: 1 addition & 6 deletions lib/src/televerse/fetch/webhook.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,7 @@ class Webhook extends Fetcher {
return;
}

final body = await request
.cast<List<int>>()
.transform(
utf8.decoder,
)
.join();
final body = await request.cast<List<int>>().transform(utf8.decoder).join();
try {
final update = Update.fromJson(jsonDecode(body));
addUpdate(update);
Expand Down

0 comments on commit 08a3f57

Please sign in to comment.