Skip to content

Commit c6c21bd

Browse files
author
Yutaka "FMS_Cat" Obuchi
authored
Merge pull request #116 from FMS-Cat/fix-gui-sorted-items
fix (gui): the state of sorted items was broken, fixed this
2 parents 9b922de + cec8dc8 commit c6c21bd

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

packages/automaton-with-gui/src/view/states/Automaton.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -231,35 +231,46 @@ export const reducer: Reducer<State, ContextAction> = ( state = initialState, ac
231231
} else if ( action.type === 'Automaton/UpdateChannelStatus' ) {
232232
newState.channels[ action.channel ].status = action.status;
233233
} else if ( action.type === 'Automaton/UpdateChannelItem' ) {
234-
const prevTime = state.channels[ action.channel ].items[ action.id ]?.time;
235-
newState.channels[ action.channel ].items[ action.id ] = action.item;
234+
let shouldInsert = false;
235+
const timeExisting = newState.channels[ action.channel ].items[ action.id ]?.time;
236+
237+
if ( timeExisting == null ) {
238+
shouldInsert = true;
239+
} else {
240+
const indexExisting = binarySearch(
241+
newState.channels[ action.channel ].sortedItems,
242+
( item ) => item.time < timeExisting
243+
);
236244

237-
if ( prevTime !== action.item.time ) {
238-
if ( prevTime != null ) {
239-
const index = binarySearch(
240-
state.channels[ action.channel ].sortedItems,
241-
( item ) => item.time < prevTime
242-
);
243-
newState.channels[ action.channel ].sortedItems.splice( index, 1 );
245+
if ( timeExisting !== action.item.time ) {
246+
newState.channels[ action.channel ].sortedItems.splice( indexExisting, 1 );
247+
shouldInsert = true;
248+
} else {
249+
newState.channels[ action.channel ].sortedItems[ indexExisting ] = action.item;
244250
}
251+
}
245252

246-
const index = binarySearch(
247-
state.channels[ action.channel ].sortedItems,
253+
if ( shouldInsert ) {
254+
const indexToInsert = binarySearch(
255+
newState.channels[ action.channel ].sortedItems,
248256
( item ) => item.time < action.item.time
249257
);
250-
newState.channels[ action.channel ].sortedItems.splice( index, 0, action.item );
258+
newState.channels[ action.channel ].sortedItems.splice( indexToInsert, 0, action.item );
251259
}
260+
261+
newState.channels[ action.channel ].items[ action.id ] = action.item;
252262
} else if ( action.type === 'Automaton/RemoveChannelItem' ) {
253-
const prevTime = state.channels[ action.channel ].items[ action.id ]?.time;
254-
delete newState.channels[ action.channel ].items[ action.id ];
263+
const timeExisting = newState.channels[ action.channel ].items[ action.id ]?.time;
255264

256-
if ( prevTime != null ) {
257-
const index = binarySearch(
258-
state.channels[ action.channel ].sortedItems,
259-
( item ) => item.time < prevTime
265+
if ( timeExisting != null ) {
266+
const indexToRemove = binarySearch(
267+
newState.channels[ action.channel ].sortedItems,
268+
( item ) => item.time < timeExisting
260269
);
261-
newState.channels[ action.channel ].sortedItems.splice( index, 1 );
270+
newState.channels[ action.channel ].sortedItems.splice( indexToRemove, 1 );
262271
}
272+
273+
delete newState.channels[ action.channel ].items[ action.id ];
263274
} else if ( action.type === 'Automaton/CreateCurve' ) {
264275
newState.curves[ action.curveId ] = {
265276
status: null,

0 commit comments

Comments
 (0)