commit 1e6b291b294728550f1e519a8c1b10cf52d5377a
parent 5662b2f3d5ab04ee1834e8e28a7ee38eeff272d6
Author: Johan Lindskogen <johan.lindskogen@gmail.com>
Date: Sun, 1 Apr 2018 14:06:11 +0200
Add support for _buffer_line_added
Diffstat:
4 files changed, 50 insertions(+), 33 deletions(-)
diff --git a/index.js b/index.js
@@ -27,7 +27,7 @@ class WeechatNative extends React.Component {
"(buffers) hdata buffer:gui_buffers(*) local_variables,notify,number,full_name,short_name,title,hidden,type"
);
// conn.send("(nicklist) nicklist");
- // conn.send("sync");
+ conn.send("sync");
},
error => {
console.log(error);
diff --git a/src/lib/weechat/action_transformer.ts b/src/lib/weechat/action_transformer.ts
@@ -7,38 +7,47 @@ const reduceToObjectByKey = <T>(array: T[], keyFn: (t: T) => string): object =>
array.reduce((acc, elem) => ({ ...acc, [keyFn(elem)]: elem }), {});
export const transformToReduxAction = (data: WeechatResponse<any>) => {
- if (data.id.startsWith("_")) {
- // weechat builtin actions (resulting from sync)
- return { type: `WEECHAT${data.id}`, payload: data.objects };
- } else {
- switch (data.id) {
- case "buffers": {
- const object = data.objects[0] as WeechatObject<WeechatBuffer[]>;
+ switch (data.id) {
+ // Weechat internal events starts with "_"
+ case "_buffer_line_added": {
+ const object = data.objects[0] as WeechatObject<WeechatLine[]>;
+ const line = object.content[0];
- return {
- type: "FETCH_BUFFERS",
- payload: reduceToObjectByKey(
- object.content.map(o => ({ ...o, id: o.pointers[0] })),
- buffer => buffer.id
- )
- };
- }
- case "version": {
- const infolist = data.objects[0] as WeechatObject<WeechatInfoList>;
+ return {
+ type: "BUFFER_LINE_ADDED",
+ bufferId: line.buffer,
+ payload: line
+ };
+ }
+ case "buffers": {
+ const object = data.objects[0] as WeechatObject<WeechatBuffer[]>;
+
+ return {
+ type: "FETCH_BUFFERS",
+ payload: reduceToObjectByKey(
+ object.content.map(o => ({ ...o, id: o.pointers[0] })),
+ buffer => buffer.id
+ )
+ };
+ }
+ case "version": {
+ const infolist = data.objects[0] as WeechatObject<WeechatInfoList>;
- return {
- type: "FETCH_VERSION",
- payload: infolist.content.value
- };
- }
- case "lines": {
- const object = data.objects[0] as WeechatObject<WeechatLine[]>;
- return {
- type: "FETCH_LINES",
- bufferId: object.content[0].buffer,
- payload: object.content
- };
- }
+ return {
+ type: "FETCH_VERSION",
+ payload: infolist.content.value
+ };
+ }
+ case "lines": {
+ const object = data.objects[0] as WeechatObject<WeechatLine[]>;
+ return {
+ type: "FETCH_LINES",
+ bufferId: object.content[0].buffer,
+ payload: object.content
+ };
}
+ default:
+ console.log("unhandled event!", data.id, data);
+ return null;
}
};
diff --git a/src/lib/weechat/connection.ts b/src/lib/weechat/connection.ts
@@ -31,7 +31,10 @@ export default class WeechatConnection {
console.log("Parsed data:", parsed);
const action = transformToReduxAction(parsed);
- this.dispatch(action);
+
+ if (action) {
+ this.dispatch(action);
+ }
}
send(data) {
diff --git a/src/store/lines.ts b/src/store/lines.ts
@@ -1,4 +1,4 @@
-export type LineState = { [key: string]: WeechatLine };
+export type LineState = { [key: string]: WeechatLine[] };
const initialState: LineState = {};
@@ -9,6 +9,11 @@ export default (state: LineState = initialState, action): LineState => {
...state,
[action.bufferId]: action.payload
};
+ case "BUFFER_LINE_ADDED":
+ return {
+ ...state,
+ [action.bufferId]: [action.payload, ...(state[action.bufferId] || [])]
+ };
default:
return state;
}