From 5fb2802e32ba089a150dd456edd7146ea1609e62 Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Sat, 13 Aug 2016 18:09:32 -0700 Subject: [PATCH] Chore(challenges): separate code storage sagas --- client/sagas/code-storage-saga.js | 32 +++++++++++-------- .../app/redux/load-current-challenge-saga.js | 2 +- .../routes/settings/redux/update-user-saga.js | 2 +- common/{app => }/utils/combine-sagas.js | 0 common/utils/get-actions-of-type.js | 17 ++++++++++ 5 files changed, 38 insertions(+), 15 deletions(-) rename common/{app => }/utils/combine-sagas.js (100%) diff --git a/client/sagas/code-storage-saga.js b/client/sagas/code-storage-saga.js index fad5eed49b..440cf0a830 100644 --- a/client/sagas/code-storage-saga.js +++ b/client/sagas/code-storage-saga.js @@ -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); diff --git a/common/app/redux/load-current-challenge-saga.js b/common/app/redux/load-current-challenge-saga.js index 2905516db9..c89c8f87cf 100644 --- a/common/app/redux/load-current-challenge-saga.js +++ b/common/app/redux/load-current-challenge-saga.js @@ -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'); diff --git a/common/app/routes/settings/redux/update-user-saga.js b/common/app/routes/settings/redux/update-user-saga.js index a004f96740..3e62f15128 100644 --- a/common/app/routes/settings/redux/update-user-saga.js +++ b/common/app/routes/settings/redux/update-user-saga.js @@ -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', diff --git a/common/app/utils/combine-sagas.js b/common/utils/combine-sagas.js similarity index 100% rename from common/app/utils/combine-sagas.js rename to common/utils/combine-sagas.js diff --git a/common/utils/get-actions-of-type.js b/common/utils/get-actions-of-type.js index 458e5d33ee..2a32d1617c 100644 --- a/common/utils/get-actions-of-type.js +++ b/common/utils/get-actions-of-type.js @@ -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