chore(web-chat): bump js-waku to 0.29.0

This commit is contained in:
fryorcraken.eth 2022-09-21 16:36:41 +10:00
parent 8dac380050
commit 15dffc8ada
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 27 additions and 25 deletions

View File

@ -6,7 +6,7 @@
"dependencies": { "dependencies": {
"@livechat/ui-kit": "^0.5.0-20", "@livechat/ui-kit": "^0.5.0-20",
"@multiformats/multiaddr": "^10.4.0", "@multiformats/multiaddr": "^10.4.0",
"js-waku": "0.28.1", "js-waku": "0.29.0",
"process": "^0.11.10", "process": "^0.11.10",
"protons-runtime": "^3.1.0", "protons-runtime": "^3.1.0",
"react": "^17.0.2", "react": "^17.0.2",

View File

@ -9,7 +9,7 @@ specifiers:
'@types/react-dom': ^17.0.11 '@types/react-dom': ^17.0.11
cspell: ^6.0.0 cspell: ^6.0.0
gh-pages: ^4.0.0 gh-pages: ^4.0.0
js-waku: 0.28.1 js-waku: 0.29.0
npm-run-all: ^4.1.5 npm-run-all: ^4.1.5
prettier: ^2.6.2 prettier: ^2.6.2
process: ^0.11.10 process: ^0.11.10
@ -26,7 +26,7 @@ specifiers:
dependencies: dependencies:
'@livechat/ui-kit': 0.5.0-20_ibvs32p3vr2bbtbo3dwziny444 '@livechat/ui-kit': 0.5.0-20_ibvs32p3vr2bbtbo3dwziny444
'@multiformats/multiaddr': 10.4.0 '@multiformats/multiaddr': 10.4.0
js-waku: 0.28.1 js-waku: 0.29.0
process: 0.11.10 process: 0.11.10
protons-runtime: 3.1.0_uint8arraylist@2.3.2 protons-runtime: 3.1.0_uint8arraylist@2.3.2
react: 17.0.2 react: 17.0.2
@ -8382,8 +8382,8 @@ packages:
/js-tokens/4.0.0: /js-tokens/4.0.0:
resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}
/js-waku/0.28.1: /js-waku/0.29.0:
resolution: {integrity: sha512-0h9TpV6jywyjdes8hr9tFV/5iJh3LQN3sQFYHcXyi4cK+4htNiMrCRjBSqRBtfhs/j+4tOkrht8gRJRLHdA5RA==} resolution: {integrity: sha512-44GOpNbkFt/1/bDZ3tcaeemehaZaxw404QmTvHw7FUwY6dtvGsDEERLEw1TERUljDESFjvEOcJjYnLcNDY1MHg==}
engines: {node: '>=16'} engines: {node: '>=16'}
dependencies: dependencies:
'@chainsafe/libp2p-gossipsub': 4.1.1 '@chainsafe/libp2p-gossipsub': 4.1.1

View File

@ -1,6 +1,6 @@
import { useEffect, useReducer, useState } from "react"; import { useEffect, useReducer, useState } from "react";
import "./App.css"; import "./App.css";
import { PageDirection, Protocols, WakuMessage } from "js-waku"; import { PageDirection, Protocols } from "js-waku";
import handleCommand from "./command"; import handleCommand from "./command";
import Room from "./Room"; import Room from "./Room";
import { WakuContext } from "./WakuContext"; import { WakuContext } from "./WakuContext";
@ -16,6 +16,7 @@ import { PeerDiscoveryStaticPeers } from "js-waku/lib/peer_discovery_static_list
import type { WakuLight } from "js-waku/lib/interfaces"; import type { WakuLight } from "js-waku/lib/interfaces";
import process from "process"; import process from "process";
import { createLightNode } from "js-waku/lib/create_waku"; import { createLightNode } from "js-waku/lib/create_waku";
import { DecoderV0, MessageV0 } from "js-waku/lib/waku_message/version_0";
const themes = { const themes = {
AuthorName: { AuthorName: {
@ -47,6 +48,7 @@ const themes = {
}; };
export const ChatContentTopic = "/toy-chat/2/huilong/proto"; export const ChatContentTopic = "/toy-chat/2/huilong/proto";
const ChatDecoder = new DecoderV0(ChatContentTopic);
async function retrieveStoreMessages( async function retrieveStoreMessages(
waku: WakuLight, waku: WakuLight,
@ -60,7 +62,7 @@ async function retrieveStoreMessages(
try { try {
for await (const messagesPromises of waku.store.queryGenerator( for await (const messagesPromises of waku.store.queryGenerator(
[ChatContentTopic], [ChatDecoder],
{ {
pageSize: 5, pageSize: 5,
pageDirection: PageDirection.FORWARD, pageDirection: PageDirection.FORWARD,
@ -70,11 +72,11 @@ async function retrieveStoreMessages(
}, },
} }
)) { )) {
const messages: Message[] = [];
const wakuMessages = await Promise.all(messagesPromises); const wakuMessages = await Promise.all(messagesPromises);
const messages: Message[] = [];
wakuMessages wakuMessages
.filter(isWakuMessageDefined) .filter(isMessageDefined)
.map((wakuMsg) => Message.fromWakuMessage(wakuMsg)) .map((wakuMsg) => Message.fromWakuMessage(wakuMsg))
.forEach((message) => { .forEach((message) => {
if (message) { if (message) {
@ -113,7 +115,7 @@ export default function App() {
// Let's retrieve previous messages before listening to new messages // Let's retrieve previous messages before listening to new messages
if (!historicalMessagesRetrieved) return; if (!historicalMessagesRetrieved) return;
const handleIncomingMessage = (wakuMsg: WakuMessage) => { const handleIncomingMessage = (wakuMsg: MessageV0) => {
console.log("Message received: ", wakuMsg); console.log("Message received: ", wakuMsg);
const msg = Message.fromWakuMessage(wakuMsg); const msg = Message.fromWakuMessage(wakuMsg);
if (msg) { if (msg) {
@ -122,7 +124,7 @@ export default function App() {
}; };
let unsubscribe: undefined | (() => Promise<void>); let unsubscribe: undefined | (() => Promise<void>);
waku.filter.subscribe(handleIncomingMessage, [ChatContentTopic]).then( waku.filter.subscribe([ChatDecoder], handleIncomingMessage).then(
(_unsubscribe) => { (_unsubscribe) => {
console.log("subscribed to ", ChatContentTopic); console.log("subscribed to ", ChatContentTopic);
unsubscribe = _unsubscribe; unsubscribe = _unsubscribe;
@ -229,8 +231,6 @@ function reduceMessages(state: Message[], newMessages: Message[]) {
return state.concat(newMessages); return state.concat(newMessages);
} }
const isWakuMessageDefined = ( const isMessageDefined = (msg: MessageV0 | undefined): msg is MessageV0 => {
msg: WakuMessage | undefined
): msg is WakuMessage => {
return !!msg; return !!msg;
}; };

View File

@ -1,4 +1,4 @@
import { WakuMessage } from "js-waku"; import { MessageV0 } from "js-waku/lib/waku_message/version_0";
import { ChatMessage } from "./chat_message"; import { ChatMessage } from "./chat_message";
export class Message { export class Message {
@ -11,7 +11,7 @@ export class Message {
this.sentTimestamp = sentTimestamp; this.sentTimestamp = sentTimestamp;
} }
static fromWakuMessage(wakuMsg: WakuMessage): Message | undefined { static fromWakuMessage(wakuMsg: MessageV0): Message | undefined {
if (wakuMsg.payload) { if (wakuMsg.payload) {
try { try {
const chatMsg = ChatMessage.decode(wakuMsg.payload); const chatMsg = ChatMessage.decode(wakuMsg.payload);

View File

@ -1,4 +1,4 @@
import { PushResponse, WakuMessage } from "js-waku"; import type { Message as WakuMessage } from "js-waku/lib/interfaces";
import { ChatContentTopic } from "./App"; import { ChatContentTopic } from "./App";
import ChatList from "./ChatList"; import ChatList from "./ChatList";
import MessageInput from "./MessageInput"; import MessageInput from "./MessageInput";
@ -7,6 +7,7 @@ import { TitleBar } from "@livechat/ui-kit";
import { Message } from "./Message"; import { Message } from "./Message";
import { ChatMessage } from "./chat_message"; import { ChatMessage } from "./chat_message";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { EncoderV0 } from "js-waku/lib/waku_message/version_0";
interface Props { interface Props {
messages: Message[]; messages: Message[];
@ -21,6 +22,8 @@ export default function Room(props: Props) {
const [filterPeers, setFilterPeers] = useState(0); const [filterPeers, setFilterPeers] = useState(0);
const [lightPushPeers, setLightPushPeers] = useState(0); const [lightPushPeers, setLightPushPeers] = useState(0);
const ChatEncoder = new EncoderV0(ChatContentTopic);
useEffect(() => { useEffect(() => {
if (!waku) return; if (!waku) return;
@ -57,7 +60,9 @@ export default function Room(props: Props) {
messageToSend, messageToSend,
props.nick, props.nick,
props.commandHandler, props.commandHandler,
waku.lightPush.push.bind(waku.lightPush) async (msg) => {
await waku.lightPush.push(ChatEncoder, msg);
}
); );
} }
: undefined : undefined
@ -71,18 +76,15 @@ async function handleMessage(
message: string, message: string,
nick: string, nick: string,
commandHandler: (cmd: string) => void, commandHandler: (cmd: string) => void,
messageSender: (msg: WakuMessage) => Promise<PushResponse | null> sender: (wakuMsg: WakuMessage) => Promise<void>
) { ) {
if (message.startsWith("/")) { if (message.startsWith("/")) {
commandHandler(message); commandHandler(message);
} else { } else {
const timestamp = new Date(); const timestamp = new Date();
const chatMessage = ChatMessage.fromUtf8String(timestamp, nick, message); const chatMessage = ChatMessage.fromUtf8String(timestamp, nick, message);
const wakuMsg = await WakuMessage.fromBytes( const payload = chatMessage.encode();
chatMessage.encode(),
ChatContentTopic, await sender({ payload, timestamp });
{ timestamp }
);
await messageSender(wakuMsg);
} }
} }