-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add button to query from store in light-js #283
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,13 @@ <h2>Remote Peers</h2> | |
<br /> | ||
<div id="messages"></div> | ||
|
||
<div> | ||
<h2>Store</h2> | ||
</div> | ||
<button disabled id="queryStoreButton" type="button"> | ||
Retrieve past messages | ||
</button> | ||
|
||
<script src="https://unpkg.com/@multiformats/[email protected]/dist/index.min.js"></script> | ||
<script type="module"> | ||
import { | ||
|
@@ -69,11 +76,12 @@ <h2>Remote Peers</h2> | |
createDecoder, | ||
utf8ToBytes, | ||
bytesToUtf8, | ||
} from "https://unpkg.com/@waku/[email protected].18/bundle/index.js"; | ||
} from "https://unpkg.com/@waku/[email protected].20/bundle/index.js"; | ||
import { | ||
enrTree, | ||
DnsNodeDiscovery, | ||
} from "https://unpkg.com/@waku/[email protected]/bundle/index.js"; | ||
import { messageHash } from "https://unpkg.com/@waku/[email protected]/bundle/index.js"; | ||
|
||
const peerIdDiv = document.getElementById("peer-id"); | ||
const remotePeerIdDiv = document.getElementById("remote-peer-id"); | ||
|
@@ -82,6 +90,7 @@ <h2>Remote Peers</h2> | |
const dialButton = document.getElementById("dial"); | ||
const subscribeButton = document.getElementById("subscribe"); | ||
const unsubscribeButton = document.getElementById("unsubscribe"); | ||
const queryStoreButton = document.getElementById("queryStoreButton"); | ||
const messagesDiv = document.getElementById("messages"); | ||
const textInput = document.getElementById("textInput"); | ||
const sendButton = document.getElementById("sendButton"); | ||
|
@@ -91,12 +100,19 @@ <h2>Remote Peers</h2> | |
const ContentTopic = "/js-waku-examples/1/chat/utf8"; | ||
const decoder = createDecoder(ContentTopic); | ||
const encoder = createEncoder({ contentTopic: ContentTopic }); | ||
let messages = []; | ||
// Each key is a unique identifier for the message. Each value is an obj { text, timestamp } | ||
let messages = {}; | ||
let unsubscribe; | ||
|
||
const updateMessages = (msgs, div) => { | ||
div.innerHTML = "<ul>"; | ||
messages.forEach((msg) => (div.innerHTML += "<li>" + msg + "</li>")); | ||
Object.values(msgs) | ||
.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()) | ||
.forEach( | ||
(msg) => | ||
(div.innerHTML += | ||
"<li>" + `${msg.text} - ${msg.timestamp}` + "</li>") | ||
); | ||
div.innerHTML += "</ul>"; | ||
}; | ||
|
||
|
@@ -114,6 +130,11 @@ <h2>Remote Peers</h2> | |
statusDiv.innerHTML = "<p>Starting Waku node.</p>"; | ||
await node.start(); | ||
|
||
window.waku = node; | ||
console.info( | ||
"Use window.waku to access the waku node running in the browser directly through the console." | ||
); | ||
|
||
// Queries all peers from libp2p peer store and updates list of connected peers | ||
const updatePeersList = async () => { | ||
// Generate <p> element with connection string from each peer | ||
|
@@ -142,6 +163,7 @@ <h2>Remote Peers</h2> | |
textInput.disabled = false; | ||
sendButton.disabled = false; | ||
subscribeButton.disabled = false; | ||
queryStoreButton.disabled = false; | ||
}); | ||
|
||
statusDiv.innerHTML = "<p>Waku node started.</p>"; | ||
|
@@ -162,22 +184,40 @@ <h2>Remote Peers</h2> | |
statusDiv.innerHTML = "<p>Error: invalid multiaddr provided</p>"; | ||
throw err; | ||
} | ||
await node.dial(multiaddr, ["filter", "lightpush"]); | ||
await node.dial(multiaddr, ["filter", "lightpush", "store"]); | ||
}; | ||
|
||
const callback = (wakuMessage) => { | ||
const messageReceivedCallback = (wakuMessage) => { | ||
// create a unique key for the message | ||
const msgHash = | ||
bytesToUtf8(messageHash(ContentTopic, wakuMessage)) + | ||
wakuMessage.proto.timestamp; | ||
const text = bytesToUtf8(wakuMessage.payload); | ||
const timestamp = wakuMessage.timestamp.toString(); | ||
messages.push(text + " - " + timestamp); | ||
// store message by its key | ||
messages[msgHash + wakuMessage.proto.timestamp] = { | ||
text, | ||
timestamp: wakuMessage.timestamp, | ||
}; | ||
// call function to refresh display of messages | ||
updateMessages(messages, messagesDiv); | ||
}; | ||
|
||
subscribeButton.onclick = async () => { | ||
unsubscribe = await node.filter.subscribe([decoder], callback); | ||
unsubscribe = await node.filter.subscribe( | ||
[decoder], | ||
messageReceivedCallback | ||
); | ||
unsubscribeButton.disabled = false; | ||
subscribeButton.disabled = true; | ||
}; | ||
|
||
queryStoreButton.onclick = async () => { | ||
await node.store.queryWithOrderedCallback( | ||
[decoder], | ||
messageReceivedCallback | ||
); | ||
}; | ||
|
||
unsubscribeButton.onclick = async () => { | ||
await unsubscribe(); | ||
unsubscribe = undefined; | ||
|
@@ -203,7 +243,6 @@ <h2>Remote Peers</h2> | |
remoteMultiAddrDiv.value = event.target.value; | ||
}); | ||
|
||
// | ||
async function getPeers() { | ||
// Display status | ||
statusDiv.innerHTML = "<p>Discovering peers</p>"; | ||
|
@@ -232,7 +271,7 @@ <h2>Remote Peers</h2> | |
|
||
// Set first peer as selected | ||
peersSelector.options[0].selected = true; | ||
remoteMultiAddrDiv.value = selectElement.options[0].value; | ||
remoteMultiAddrDiv.value = peersSelector.options[0].value; | ||
|
||
statusDiv.innerHTML = "<p>Peers discovered</p>"; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using a
Map
might makes your life easier in terms of iterating through.