Skip to content

Commit

Permalink
Update line on _buffer_line_data_changed
Browse files Browse the repository at this point in the history
When a line is changed (e.g. the message is edited), WeeChat now sends a
_buffer_line_data_changed message. Update line state so that line
changes are reflected in WeechatRN.

Upgrade to Node v22 for toSpliced support.
  • Loading branch information
mhoran committed Nov 14, 2024
1 parent 2af2779 commit f7e1626
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Node and cache dependencies
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 22
cache: npm
- run: npm install
- run: npm test -- --testTimeout 10000
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 22
cache: npm
- name: Set up Expo
uses: expo/expo-github-action@v8
Expand Down
41 changes: 41 additions & 0 deletions __tests__/lib/weechat/action_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,47 @@ describe('transformToReduxAction', () => {
});
});

describe('on buffer_line_data_changed', () => {
it('updates the line', () => {
const preloadedState = {
lines: { '83d204d80': [{ id: 0 } as WeechatLine] }
};
const store = configureStore({
reducer,
preloadedState,
enhancers: (getDefaultEnhancers) =>
getDefaultEnhancers({ autoBatch: false })
});

const action = transformToReduxAction({
id: '_buffer_line_data_changed',
header: { compression: 0, length: 0 },
objects: [
{
type: 'hda',
content: [
{
id: 0,
buffer: '83d204d80',
pointers: ['83d204d80', '83c016280', '838a65900', '83c000420'],
date: new Date('2024-11-09T00:02:07.000Z'),
date_printed: new Date('2024-11-10T17:28:48.000Z'),
message: 'Beep boop'
}
]
}
]
});
expect(action).toBeDefined();

store.dispatch(action!);

expect(store.getState().lines).toHaveProperty('83d204d80');
const lines = store.getState().lines['83d204d80'];
expect(lines[0].message).toEqual('Beep boop');
});
});

describe('on version', () => {
it('stores the version', () => {
const store = configureStore({
Expand Down
27 changes: 22 additions & 5 deletions src/lib/weechat/action_transformer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { UnknownAction } from 'redux';
import { ThunkAction, ThunkDispatch } from 'redux-thunk';
import { StoreState } from '../../store';
import {
bufferClearedAction,
bufferClosedAction,
bufferLineAddedAction,
bufferLineDataChangedAction,
bufferLocalvarRemoveAction,
bufferLocalvarUpdateAction,
bufferOpenedAction,
Expand All @@ -13,14 +15,13 @@ import {
fetchHotlistsAction,
fetchLinesAction,
fetchNicklistAction,
fetchScriptsAction,
fetchVersionAction,
lastReadLinesAction,
nicklistUpdatedAction,
fetchScriptsAction,
upgradeAction,
pongAction
pongAction,
upgradeAction
} from '../../store/actions';
import { UnknownAction } from 'redux';

type KeyFn<T> = (t: T) => string;
type MapFn<A, B> = (a: A) => A | B;
Expand All @@ -32,7 +33,7 @@ const reduceToObjectByKey = <T, U>(
) => array.reduce((acc, elem) => ({ ...acc, [keyFn(elem)]: mapFn(elem) }), {});

const parseVersion = (version: string) => {
const parts = version.split('.').map((part) => parseInt(part) || 0);
const parts = version.split('.').map((part) => parseInt(part) ?? 0);
return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3];
};

Expand Down Expand Up @@ -112,6 +113,22 @@ export const transformToReduxAction = (
);
};
}
case '_buffer_line_data_changed': {
const object = data.objects[0] as WeechatObject<
Record<string, unknown>[]
>;
const line = object.content[0];
const { id, date, date_printed, ...restLine } = line;

if (id === undefined) return;

return bufferLineDataChangedAction({
...restLine,
id,
date: (date as Date).toISOString(),
date_printed: (date_printed as Date).toISOString()
} as WeechatLine);
}
case '_buffer_closing': {
const object = data.objects[0] as WeechatObject<WeechatBuffer[]>;
const buffer = object.content[0];
Expand Down
4 changes: 4 additions & 0 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export const bufferLineAddedAction = createAction(
'BUFFER_LINE_ADDED',
prepareAutoBatched<{ line: WeechatLine; currentBufferId: string | null }>()
);
export const bufferLineDataChangedAction = createAction(
'BUFFER_LINE_DATA_CHANGED',
prepareAutoBatched<WeechatLine>()
);
export const bufferClearedAction = createAction(
'BUFFER_CLEARED',
prepareAutoBatched<string>()
Expand Down
13 changes: 13 additions & 0 deletions src/store/lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
bufferClearedAction,
bufferClosedAction,
bufferLineAddedAction,
bufferLineDataChangedAction,
fetchBuffersRemovedAction,
fetchLinesAction,
upgradeAction
Expand Down Expand Up @@ -38,6 +39,18 @@ const linesReducer = createReducer(initialState, (builder) => {
]
};
});
builder.addCase(bufferLineDataChangedAction, (state, action) => {
const lines = state[action.payload.buffer];
if (lines === undefined) return state;

const lineIndex = lines.findIndex((line) => line.id === action.payload.id);
if (lineIndex < 0) return state;

return {
...state,
[action.payload.buffer]: lines.toSpliced(lineIndex, 1, action.payload)
};
});
builder.addCase(fetchBuffersRemovedAction, (state, action) => {
return Object.fromEntries(
Object.entries(state).filter(
Expand Down

0 comments on commit f7e1626

Please sign in to comment.