Skip to content

Instantly share code, notes, and snippets.

@ourmaninamsterdam
Last active November 30, 2023 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ourmaninamsterdam/7a8e59d89e33961993e710ca85407528 to your computer and use it in GitHub Desktop.
Save ourmaninamsterdam/7a8e59d89e33961993e710ca85407528 to your computer and use it in GitHub Desktop.
WIP redux-saga/MQTT event channel
import { eventChannel } from 'redux-saga';
import { put, take } from 'redux-saga/effects';
const createMqttChannel = mqtt => {
return eventChannel(emitter => {
const mqttMsgHandler = (payload: MqttMessage) => {
recorder.mqtt(payload);
const { event } = payload;
switch (event) {
case 'buttonPressed': {
emitter(buttonRecallSlice.actions.buttonPressed(payload));
break;
}
// Add more cases as needed
}
};
mqtt.on('message', mqttMsgHandler);
return () => {
mqtt.removeListener('message', mqttMsgHandler);
};
});
};
function* mqttWatcher(mqtt) {
const channel = yield call(createMqttChannel, mqtt);
while (true) {
const action = yield take(channel);
yield put(action);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment