Last active
November 30, 2023 12:49
-
-
Save ourmaninamsterdam/7a8e59d89e33961993e710ca85407528 to your computer and use it in GitHub Desktop.
WIP redux-saga/MQTT event channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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