weechatRN

Weechat relay client for iOS using websockets https://github.com/mhoran/weechatRN
git clone http://git.hanabi.in/repos/weechatRN.git
Log | Files | Refs | README | LICENSE

commit 1aa1b5f32490101c77c6e8a512b90a8fc4cb14ef
parent 5ae2aaff180a63e7bd337f44ba599cdc438eb51d
Author: Johan Lindskogen <johan.lindskogen@gmail.com>
Date:   Mon,  2 Apr 2018 21:43:53 +0200

Connect from root component

Diffstat:
Msrc/lib/weechat/connection.ts | 20+++++++++-----------
Msrc/usecase/Root.tsx | 75+++++++++++++++++++++++++++++++++++++++++++++++----------------------------
2 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/src/lib/weechat/connection.ts b/src/lib/weechat/connection.ts @@ -10,22 +10,20 @@ export default class WeechatConnection { compressed: boolean; websocket: WebSocket; - constructor(dispatch, host, password = "", compressed = false) { + constructor(dispatch) { this.dispatch = dispatch; - this.host = host; - this.password = password; - this.compressed = compressed; this.websocket = null; } - connect(): Promise<WeechatConnection> { - return new Promise((resolve, reject) => { - this.websocket = new WebSocket(this.host); + connect(host, password = "", onSuccess, onError) { + this.host = host; + this.password = password; + + this.websocket = new WebSocket(this.host); - this.websocket.onopen = () => this.onopen(resolve); - this.websocket.onmessage = event => this.onmessage(event); - this.websocket.onerror = reject; - }); + this.websocket.onopen = () => this.onopen(onSuccess); + this.websocket.onmessage = event => this.onmessage(event); + this.websocket.onerror = onError; } onopen(callback) { diff --git a/src/usecase/Root.tsx b/src/usecase/Root.tsx @@ -3,45 +3,62 @@ import { StatusBar } from "react-native"; import { Provider } from "react-redux"; import WeechatConnection from "../lib/weechat/connection"; -import { HOSTNAME, PASSWORD } from "../../config"; - import store from "../store"; import App from "./App"; import ConnectionGate from "./ConnectionGate"; -const compressed = false; -const connection = new WeechatConnection( - store.dispatch, - HOSTNAME, - PASSWORD, - compressed -); +interface State { + connecting: boolean; +} -export default class WeechatNative extends React.Component { - componentWillMount() { - connection.connect().then( - conn => { - conn.send("(hotlist) hdata hotlist:gui_hotlist(*)"); - conn.send( - "(buffers) hdata buffer:gui_buffers(*) local_variables,notify,number,full_name,short_name,title,hidden,type" - ); - // conn.send("(nicklist) nicklist"); - conn.send("sync"); - }, - error => { - console.log(error); - } - ); +export default class WeechatNative extends React.Component<{}, State> { + state: State = { + connecting: false + }; + + connection: WeechatConnection; + + constructor(props) { + super(props); + this.connection = new WeechatConnection(store.dispatch); } + + onConnectionSuccess = connection => { + this.setState({ connecting: false }); + connection.send("(hotlist) hdata hotlist:gui_hotlist(*)"); + connection.send( + "(buffers) hdata buffer:gui_buffers(*) local_variables,notify,number,full_name,short_name,title,hidden,type" + ); + // connection.send("(nicklist) nicklist"); + connection.send("sync"); + }; + + onConnectionError = error => { + this.setState({ connecting: false }); + console.log(error); + }; + + onConnect = (hostname: string, password: string) => { + console.log(hostname, password); + this.setState({ connecting: true }); + this.connection.connect( + hostname, + password, + this.onConnectionSuccess, + this.onConnectionError + ); + }; + fetchLines = (bufferId: string, numLines: number = 50) => { - connection && - connection.send( + this.connection && + this.connection.send( `(lines) hdata buffer:0x${bufferId}/own_lines/last_line(-${numLines})/data` ); }; sendMessageToBuffer = (fullBufferName: string, message: string) => { - connection && connection.send(`(input) input ${fullBufferName} ${message}`); + this.connection && + this.connection.send(`(input) input ${fullBufferName} ${message}`); }; clearHotlistForBuffer = (fullBufferName: string) => { this.sendMessageToBuffer(fullBufferName, "/buffer set hotlist -1"); @@ -51,9 +68,11 @@ export default class WeechatNative extends React.Component { ); }; render() { + const { connecting } = this.state; + return ( <Provider store={store}> - <ConnectionGate> + <ConnectionGate connecting={connecting} onConnect={this.onConnect}> <StatusBar barStyle="light-content" /> <App clearHotlistForBuffer={this.clearHotlistForBuffer}