@talend/react-cmf
Version:
A framework built on top of best react libraries
51 lines (49 loc) • 1.46 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.actions = exports.SAGA_COMPONENT_ACTIONS = void 0;
exports.handleSagaComponent = handleSagaComponent;
exports.sagaList = void 0;
var _effects = require("redux-saga/effects");
const SAGA_COMPONENT_ACTIONS = exports.SAGA_COMPONENT_ACTIONS = {
SAGA_COMPONENT_START: 'SAGA_COMPONENT_START',
SAGA_COMPONENT_STOP: 'SAGA_COMPONENT_STOP'
};
// This map of saga is used to keep the generator
// We can't pass it into redux as it's not serializable
const sagaList = exports.sagaList = {};
const startSaga = (sagaId, saga, sagaProps) => {
sagaList[sagaId] = saga;
return {
type: SAGA_COMPONENT_ACTIONS.SAGA_COMPONENT_START,
sagaId,
sagaProps
};
};
const stopSaga = sagaId => {
delete sagaList[sagaId];
return {
type: `${SAGA_COMPONENT_ACTIONS.SAGA_COMPONENT_STOP}-${sagaId}`
};
};
const actions = exports.actions = {
startSaga,
stopSaga
};
function* handleNewSaga({
sagaId,
sagaProps = {}
}) {
const saga = sagaList[sagaId];
if (!saga) {
throw new Error(`saga not found: ${sagaId}`);
}
const task = yield (0, _effects.spawn)(saga, sagaProps);
yield (0, _effects.take)(`${SAGA_COMPONENT_ACTIONS.SAGA_COMPONENT_STOP}-${sagaId}`);
yield (0, _effects.cancel)(task);
}
function* handleSagaComponent() {
yield (0, _effects.takeEvery)(SAGA_COMPONENT_ACTIONS.SAGA_COMPONENT_START, handleNewSaga);
}
//# sourceMappingURL=Saga.saga.js.map
;