From 53ef23362c12487771417c53b9581cfc09977334 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 28 Jul 2021 11:42:42 +1000 Subject: [PATCH] Remove faulty fleet feature It does not work as it can lead to infinite loops due to the handling of the Waku instance. It should disconnect and reconnect to peers instead of starting a new waku instance. --- CHANGELOG.md | 3 +++ examples/web-chat/src/App.tsx | 22 ++++++---------- examples/web-chat/src/Room.tsx | 5 ++-- examples/web-chat/src/command.ts | 43 ++++---------------------------- 4 files changed, 17 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d558f83bb6..84ae53c1a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Removed +- Examples (web-chat): Remove broken `/fleet` command. + ## [0.9.0] - 2021-07-26 ### Changed diff --git a/examples/web-chat/src/App.tsx b/examples/web-chat/src/App.tsx index 70538fcb90..b55bea7f1e 100644 --- a/examples/web-chat/src/App.tsx +++ b/examples/web-chat/src/App.tsx @@ -2,8 +2,8 @@ import PeerId from 'peer-id'; import { useEffect, useState } from 'react'; import './App.css'; import { - getStatusFleetNodes, Environment, + getStatusFleetNodes, StoreCodec, Waku, WakuMessage, @@ -81,17 +81,16 @@ export default function App() { const persistedNick = window.localStorage.getItem('nick'); return persistedNick !== null ? persistedNick : generate(); }); - const [fleetEnv, setFleetEnv] = useState(defaultFleetEnv); useEffect(() => { localStorage.setItem('nick', nick); }, [nick]); useEffect(() => { - initWaku(fleetEnv, setWaku) + initWaku(setWaku) .then(() => console.log('Waku init done')) .catch((e) => console.log('Waku init failed ', e)); - }, [fleetEnv]); + }, []); useEffect(() => { if (!waku) return; @@ -160,15 +159,8 @@ export default function App() { nick={nick} newMessages={newMessages} archivedMessages={archivedMessages} - fleetEnv={fleetEnv} commandHandler={(input: string) => { - const { command, response } = handleCommand( - input, - waku, - setNick, - fleetEnv, - setFleetEnv - ); + const { command, response } = handleCommand(input, waku, setNick); const commandMessages = response.map((msg) => { return Message.fromUtf8String(command, msg); }); @@ -181,7 +173,7 @@ export default function App() { ); } -async function initWaku(fleetEnv: Environment, setter: (waku: Waku) => void) { +async function initWaku(setter: (waku: Waku) => void) { try { const waku = await Waku.create({ libp2p: { @@ -196,7 +188,7 @@ async function initWaku(fleetEnv: Environment, setter: (waku: Waku) => void) { setter(waku); - const nodes = await getStatusFleetNodes(fleetEnv); + const nodes = await getStatusFleetNodes(selectFleetEnv()); await Promise.all( nodes.map((addr) => { return waku.dial(addr); @@ -207,7 +199,7 @@ async function initWaku(fleetEnv: Environment, setter: (waku: Waku) => void) { } } -function defaultFleetEnv() { +function selectFleetEnv() { // Works with react-scripts if (process?.env?.NODE_ENV === 'development') { return Environment.Test; diff --git a/examples/web-chat/src/Room.tsx b/examples/web-chat/src/Room.tsx index 790c7171bb..70ade131df 100644 --- a/examples/web-chat/src/Room.tsx +++ b/examples/web-chat/src/Room.tsx @@ -1,4 +1,4 @@ -import { ChatMessage, Environment, WakuMessage } from 'js-waku'; +import { ChatMessage, WakuMessage } from 'js-waku'; import { ChatContentTopic } from './App'; import ChatList from './ChatList'; import MessageInput from './MessageInput'; @@ -11,7 +11,6 @@ interface Props { archivedMessages: Message[]; commandHandler: (cmd: string) => void; nick: string; - fleetEnv: Environment; } export default function Room(props: Props) { @@ -30,7 +29,7 @@ export default function Room(props: Props) { style={{ height: '98vh', display: 'flex', flexDirection: 'column' }} > : set a new nickname', '/info: some information about the node', '/connect : connect to the given peer', - '/fleet : connect to this fleet; beware it restarts waku node.', '/help: Display this help', ]; } @@ -23,14 +22,11 @@ function nick( return [`New nick: ${nick}`]; } -function info(waku: Waku | undefined, fleetEnv: Environment): string[] { +function info(waku: Waku | undefined): string[] { if (!waku) { return ['Waku node is starting']; } - return [ - `PeerId: ${waku.libp2p.peerId.toB58String()}`, - `Fleet environment: ${fleetEnv}`, - ]; + return [`PeerId: ${waku.libp2p.peerId.toB58String()}`]; } function connect(peer: string | undefined, waku: Waku | undefined): string[] { @@ -82,28 +78,6 @@ function peers(waku: Waku | undefined): string[] { return response; } -function fleet( - newFleetEnv: string | undefined, - currFleetEnv: Environment, - setFleetEnv: (fleetEnv: Environment) => void -): string[] { - switch (newFleetEnv) { - case Environment.Test: - setFleetEnv(newFleetEnv); - break; - case Environment.Prod: - setFleetEnv(newFleetEnv); - break; - default: - return [ - `Incorrect values, acceptable values are ${Environment.Test}, ${Environment.Prod}`, - `Current fleet environment is ${currFleetEnv}`, - ]; - } - - return [`New fleet Environment: ${newFleetEnv}`]; -} - function connections(waku: Waku | undefined): string[] { if (!waku) { return ['Waku node is starting']; @@ -133,9 +107,7 @@ function connections(waku: Waku | undefined): string[] { export default function handleCommand( input: string, waku: Waku | undefined, - setNick: (nick: string) => void, - currFleetEnv: Environment, - setFleetEnv: (fleetEnv: Environment) => void + setNick: (nick: string) => void ): { command: string; response: string[] } { let response: string[] = []; const args = parseInput(input); @@ -148,7 +120,7 @@ export default function handleCommand( nick(args.shift(), setNick).map((str) => response.push(str)); break; case '/info': - info(waku, currFleetEnv).map((str) => response.push(str)); + info(waku).map((str) => response.push(str)); break; case '/connect': connect(args.shift(), waku).map((str) => response.push(str)); @@ -159,11 +131,6 @@ export default function handleCommand( case '/connections': connections(waku).map((str) => response.push(str)); break; - case '/fleet': - fleet(args.shift(), currFleetEnv, setFleetEnv).map((str) => - response.push(str) - ); - break; default: response.push(`Unknown Command '${command}'`); }