Skip to content

Commit d4939cb

Browse files
committed
fix: replace channel with actionChannel for handling EV3 firmware replies
1 parent 3c27784 commit d4939cb

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

src/firmware/sagas.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import moveHubZip from '@pybricks/firmware/build/movehub.zip';
1515
import technicHubZip from '@pybricks/firmware/build/technichub.zip';
1616
import { WebDFU } from 'dfu';
1717
import { AnyAction } from 'redux';
18-
import { channel, eventChannel } from 'redux-saga';
18+
import { eventChannel } from 'redux-saga';
1919
import { ActionPattern } from 'redux-saga/effects';
2020
import {
2121
SagaGenerator,
22+
actionChannel,
2223
all,
2324
call,
2425
cancel,
@@ -1140,24 +1141,10 @@ function* handleFlashEV3(action: ReturnType<typeof firmwareFlashEV3>): Generator
11401141
payload?: Uint8Array,
11411142
): SagaGenerator<[DataView | undefined, Error | undefined]> {
11421143
// Create a channel that buffers the actions
1143-
const replyChannel = yield* call(
1144-
channel<ReturnType<typeof firmwareDidReceiveEV3Reply>>,
1144+
const replyChannel = yield* actionChannel(
1145+
firmwareDidReceiveEV3Reply.when((a) => a.replyCommand === command),
11451146
);
11461147

1147-
// Create a task that forwards matching actions to our channel
1148-
// Sending the command and setting up the listener are not atomic,
1149-
// so we might receive the reply before we start listening. To handle
1150-
// this, we keep listening until we find the matching reply.
1151-
const forwardTask = yield* fork(function* () {
1152-
while (true) {
1153-
const action = yield* take(firmwareDidReceiveEV3Reply);
1154-
if (action.replyCommand === command) {
1155-
yield* put(replyChannel, action);
1156-
break; // Stop after finding the matching reply
1157-
}
1158-
}
1159-
});
1160-
11611148
// Send the command
11621149
const dataBuffer = new Uint8Array((payload?.byteLength ?? 0) + 6);
11631150
const data = new DataView(dataBuffer.buffer);
@@ -1206,7 +1193,6 @@ function* handleFlashEV3(action: ReturnType<typeof firmwareFlashEV3>): Generator
12061193
}
12071194

12081195
// Clean up
1209-
yield* cancel(forwardTask);
12101196
yield* call(() => replyChannel.close());
12111197

12121198
return [new DataView(reply.payload), undefined];

0 commit comments

Comments
 (0)