Skip to content

Commit c41d5c9

Browse files
authored
feat: rerun ready event to prevent the non-existence of bot instance (#312)
* feat: rerun ready event to prevent the non-existence of bot instance * feat: remove duplicated getter
1 parent ac332c6 commit c41d5c9

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

lib/src/domains/container/ioc_container.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:collection';
2-
32
import 'package:mineral/src/domains/common/utils/helper.dart';
43

54
final class IocContainer {

lib/src/infrastructure/internals/packets/listeners/ready_packet.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:isolate';
2+
13
import 'package:mineral/src/api/common/bot/bot.dart';
24
import 'package:mineral/src/domains/commands/command_interaction_manager.dart';
35
import 'package:mineral/src/domains/container/ioc_container.dart';
@@ -6,6 +8,11 @@ import 'package:mineral/src/infrastructure/internals/packets/listenable_packet.d
68
import 'package:mineral/src/infrastructure/internals/packets/packet_type.dart';
79
import 'package:mineral/src/infrastructure/internals/wss/shard_message.dart';
810

11+
final class ReadyPacketMessage<T> {
12+
ShardMessage<T> message;
13+
ReadyPacketMessage(this.message);
14+
}
15+
916
final class ReadyPacket implements ListenablePacket {
1017
@override
1118
PacketType get packetType => PacketType.ready;

lib/src/infrastructure/internals/packets/packet_dispatcher.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import 'package:mineral/container.dart';
12
import 'package:mineral/contracts.dart';
23
import 'package:mineral/src/domains/common/kernel.dart';
34
import 'package:mineral/src/domains/services/packets/packet_dispatcher.dart';
45
import 'package:mineral/src/infrastructure/internals/packets/listenable_packet.dart';
6+
import 'package:mineral/src/infrastructure/internals/packets/listeners/ready_packet.dart';
7+
import 'package:mineral/src/infrastructure/internals/packets/packet_type.dart';
58
import 'package:mineral/src/infrastructure/internals/wss/shard_message.dart';
69
import 'package:rxdart/rxdart.dart';
710

@@ -17,6 +20,9 @@ final class PacketDispatcher implements PacketDispatcherContract {
1720
_packets.stream
1821
.where((event) => event.type == packet.name)
1922
.listen((ShardMessage message) {
23+
if (message.type == PacketType.ready.name) {
24+
ioc.bind(() => ReadyPacketMessage(message));
25+
}
2026
Function.apply(
2127
listener, [message, _kernel.eventListener.dispatcher.dispatch]);
2228
});

lib/src/infrastructure/internals/wss/running_strategies/hmr_running_strategy.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import 'package:mineral/container.dart';
99
import 'package:mineral/contracts.dart';
1010
import 'package:mineral/src/domains/services/packets/packet_dispatcher.dart';
1111
import 'package:mineral/src/domains/services/wss/running_strategy.dart';
12+
import 'package:mineral/src/infrastructure/internals/packets/listeners/ready_packet.dart';
13+
import 'package:mineral/src/infrastructure/internals/packets/packet_type.dart';
1214
import 'package:mineral/src/infrastructure/internals/wss/shard_message.dart';
1315
import 'package:mineral/src/infrastructure/internals/wss/websocket_isolate_message_transfert.dart';
1416
import 'package:mineral/src/infrastructure/services/wss/websocket_message.dart';
@@ -42,7 +44,8 @@ final class HmrRunningStrategy implements RunningStrategy {
4244
final dateTime = DateTime.now();
4345

4446
Watcher(middlewares: [
45-
IgnoreMiddleware(['~', '.dart_tool', '.git', '.idea', '.vscode']),
47+
IgnoreMiddleware(
48+
['~', '.dart_tool', '.git', '.idea', '.vscode', '.cursor']),
4649
IncludeMiddleware([Glob('**.dart'), ..._watchedFiles]),
4750
DebounceMiddleware(Duration(milliseconds: 50), dateTime),
4851
], onStart: handleStart, onFileChange: handleModify)
@@ -70,6 +73,12 @@ final class HmrRunningStrategy implements RunningStrategy {
7073
Future<void> dispatch(WebsocketMessage message) async {
7174
final strategy = _wss.config.encoding;
7275
final decoded = strategy.decode(message);
76+
final messageContent = decoded.content.serialize();
77+
78+
if (messageContent case final Map<String, dynamic> json
79+
when json['t'] == PacketType.ready.name) {
80+
ioc.bind(() => ReadyPacketMessage(decoded.content));
81+
}
7382

7483
await _runner.send(decoded.content.serialize());
7584
}
@@ -123,8 +132,13 @@ final class HmrRunningStrategy implements RunningStrategy {
123132
}
124133

125134
sequences.add(AsciiControl.lineFeed);
126-
127135
stdout.writeAnsiAll(sequences);
136+
128137
await _runner.reload();
138+
139+
if (ioc.resolveOrNull<ReadyPacketMessage>()
140+
case final ReadyPacketMessage packet) {
141+
await _runner.send(packet.message.serialize());
142+
}
129143
}
130144
}

0 commit comments

Comments
 (0)