Chore(challenges): separate code storage sagas

This commit is contained in:
Berkeley Martinez
2016-08-13 18:09:32 -07:00
parent c3d9d48b01
commit 5fb2802e32
5 changed files with 38 additions and 15 deletions

View File

@ -1,15 +1,16 @@
import { Observable } from 'rx';
import store from 'store';
import { ofType } from '../../common/utils/get-actions-of-type';
import { updateContents } from '../../common/utils/polyvinyl';
import combineSagas from '../../common/utils/combine-sagas';
import { makeToast } from '../../common/app/toasts/redux/actions';
import types from '../../common/app/routes/challenges/redux/types';
import {
savedCodeFound,
updateMain
} from '../../common/app/routes/challenges/redux/actions';
import {
updateContents
} from '../../common/utils/polyvinyl';
const legacyPrefixes = [
'Bonfire: ',
@ -47,13 +48,20 @@ function legacyToFile(code, files, key) {
return { [key]: updateContents(code, files[key]) };
}
export default function codeStorageSaga(actions$, getState) {
export function saveCodeSaga(actions, getState) {
return actions
::ofType(types.saveCode)
.map(() => {
const { challengesApp: { id = '', files = {} } } = getState();
store.set(id, files);
return null;
});
}
export function loadCodeSaga(actions$, getState) {
return actions$
.filter(({ type }) => (
type === types.saveCode ||
type === types.loadCode
))
.flatMap(({ type }) => {
::ofType(types.loadCode)
.flatMap(() => {
let finalFiles;
const {
challengesApp: {
@ -63,10 +71,6 @@ export default function codeStorageSaga(actions$, getState) {
key
}
} = getState();
if (type === types.saveCode) {
store.set(id, files);
return null;
}
const codeFound = getCode(id);
if (codeFound) {
@ -90,3 +94,5 @@ export default function codeStorageSaga(actions$, getState) {
return Observable.empty();
});
}
export default combineSagas(saveCodeSaga, loadCodeSaga);

View File

@ -13,7 +13,7 @@ import {
} from './selectors';
import { updateCurrentChallenge } from '../routes/challenges/redux/actions';
import getActionsOfType from '../../utils/get-actions-of-type';
import combineSagas from '../utils/combine-sagas';
import combineSagas from '../../utils/combine-sagas';
import { postJSON$ } from '../../utils/ajax-stream';
const log = debug('fcc:app/redux/load-current-challenge-saga');

View File

@ -2,7 +2,6 @@ import { Observable } from 'rx';
import { push } from 'react-router-redux';
import { types } from './actions';
import combineSagas from '../../../utils/combine-sagas';
import { makeToast } from '../../../toasts/redux/actions';
import { fetchChallenges } from '../../challenges/redux/actions';
import {
@ -14,6 +13,7 @@ import {
import { userSelector } from '../../../redux/selectors';
import { postJSON$ } from '../../../../utils/ajax-stream';
import langs from '../../../../utils/supported-languages';
import combineSagas from '../../../../utils/combine-sagas';
const urlMap = {
isLocked: 'lockdown',

View File

@ -1,3 +1,20 @@
// redux-observable compatible operator
export function ofType(...keys) {
return this.filter(({ type }) => {
const len = keys.length;
if (len === 1) {
return type === keys[0];
} else {
for (let i = 0; i < len; i++) {
if (keys[i] === type) {
return true;
}
}
}
return false;
});
}
export default function getActionsOfType(actions, ...types) {
const length = types.length;
return actions