Skip to content

Commit f8918bb

Browse files
committed
remove waitForREmotePEer
1 parent bdaa478 commit f8918bb

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

index.html

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@
475475

476476
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/protobuf.min.js"></script>
477477
<script type="module">
478-
import { createLightNode, waitForRemotePeer, Protocols } from 'https://unpkg.com/@waku/[email protected]/bundle/index.js';
478+
import { createLightNode, Protocols } from 'https://unpkg.com/@waku/[email protected]/bundle/index.js';
479479
import { MessageChannel, MessageChannelEvent, encodeMessage, decodeMessage } from 'https://unpkg.com/@waku/[email protected]/bundle/index.js';
480480

481481
// Define protobuf schema
@@ -503,6 +503,7 @@
503503
this.messageChannel = null; // SDS Message Channel
504504
this.displayedMessages = new Set(); // Track displayed SDS message IDs to prevent dupes
505505
this.displayedContent = new Set(); // Track displayed content (timestamp+text) to prevent content dupes
506+
this.isConnectedToStore = false; // Track store node connection
506507

507508
this.initProtobuf();
508509
this.initElements();
@@ -583,6 +584,12 @@
583584

584585
async loadMessageHistory() {
585586
try {
587+
// Only proceed if we're connected to a store node
588+
if (!this.isConnectedToStore) {
589+
console.log('Not connected to store node, skipping message history loading');
590+
return;
591+
}
592+
586593
console.log('Starting progressive message history loading...');
587594

588595
const now = new Date();
@@ -873,6 +880,13 @@
873880

874881
// Create Waku node
875882
this.node = await createLightNode({ defaultBootstrap: true });
883+
884+
// Listen for connection events using Waku event system
885+
this.node.events.addEventListener("waku:connection", (event) => {
886+
console.log('Waku connection event:', event.detail); // true if connected, false if disconnected
887+
this.checkStoreConnection();
888+
});
889+
876890
await this.node.start();
877891

878892
// Dial the specific node
@@ -886,31 +900,26 @@
886900

887901
this.updateStatus('Waiting for peers...', 'connecting');
888902

889-
// Wait for connection to remote peers
890-
await waitForRemotePeer(this.node, [Protocols.LightPush, Protocols.Filter]);
891-
892903
// Create encoder and decoder
893904
this.encoder = this.node.createEncoder({ contentTopic: this.contentTopic });
894905
this.decoder = this.node.createDecoder({ contentTopic: this.contentTopic });
895906

896-
// Subscribe to messages
907+
// Subscribe to messages (don't wait for specific protocol peers)
897908
await this.node.filter.subscribe([this.decoder], (message) => {
898909
this.handleIncomingWakuMessage(message);
899910
});
900911

901-
this.updateStatus('Loading history...', 'connecting');
902-
903-
// Query store for messages from the past 24 hours
904-
await this.loadMessageHistory();
905-
906-
this.updateStatus('Connected', 'connected');
912+
// Enable sending immediately
907913
this.messageInput.disabled = false;
908914
this.sendButton.disabled = false;
909915

916+
this.updateStatus('Connected', 'connected');
917+
910918
// Start monitoring node connections
911919
this.startNodeCountMonitoring();
920+
this.startStoreMonitoring();
912921

913-
this.addSystemMessage('Connected to Waku network with SDS reliability! You can now chat.');
922+
this.addSystemMessage('Connected to Waku network! Waiting for store nodes to load history...');
914923

915924
} catch (error) {
916925
console.error('Failed to initialize Waku:', error);
@@ -919,6 +928,51 @@
919928
}
920929
}
921930

931+
async checkStoreConnection() {
932+
if (!this.node) return;
933+
934+
try {
935+
// Get all connected peers with their protocol support
936+
const connectedPeers = await this.node.getConnectedPeers();
937+
938+
// Find peers that support the store protocol
939+
const storePeers = connectedPeers.filter(peerInfo =>
940+
peerInfo.protocols && peerInfo.protocols.includes('/vac/waku/store/2.0.0-beta4')
941+
);
942+
943+
const hasStore = storePeers.length > 0;
944+
945+
console.log(`Connected peers: ${connectedPeers.length}, Store peers: ${storePeers.length}`);
946+
if (storePeers.length > 0) {
947+
console.log('Store peers:', storePeers.map(p => p.id.toString()));
948+
}
949+
950+
// Track connection state changes
951+
const wasConnectedToStore = this.isConnectedToStore;
952+
this.isConnectedToStore = hasStore;
953+
954+
// Start store queries when we first connect to a store node
955+
if (!wasConnectedToStore && hasStore) {
956+
console.log('Connected to store node, starting message history loading...');
957+
this.addSystemMessage('Store node connected, loading message history...');
958+
await this.loadMessageHistory();
959+
} else if (wasConnectedToStore && !hasStore) {
960+
console.log('Lost connection to store nodes');
961+
this.addSystemMessage('Lost connection to store nodes - history loading unavailable');
962+
}
963+
964+
} catch (error) {
965+
console.error('Failed to check store connection:', error);
966+
}
967+
}
968+
969+
startStoreMonitoring() {
970+
// Check store connection every 5 seconds
971+
setInterval(() => {
972+
this.checkStoreConnection();
973+
}, 5000);
974+
}
975+
922976
startNodeCountMonitoring() {
923977
// Update node count immediately and then every 5 seconds
924978
this.updateNodeCountFromWaku();

0 commit comments

Comments
 (0)