Skip to content

Commit

Permalink
Merge pull request #9 from keremcubuk/fix-hermes-injector
Browse files Browse the repository at this point in the history
Fix: Hermes Reflect
  • Loading branch information
keremcubuk authored Nov 19, 2019
2 parents 9babf36 + 8926f00 commit 5a67f61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
9 changes: 6 additions & 3 deletions app/utils/reducerInjectors.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import invariant from 'invariant';
import { isEmpty, isFunction, isString } from 'lodash';
import { isEmpty, isFunction, isString, has } from 'lodash';

import checkStore from './checkStore';
import createReducer from '../reducers';

export function injectReducerFactory(store, isValid) {
return function injectReducer(key, reducer) {
if (!isValid) checkStore(store);
if (!isValid) {
checkStore(store);
}

invariant(
isString(key) && !isEmpty(key) && isFunction(reducer),
'(app/utils...) injectReducer: Expected `reducer` to be a reducer function',
);

// Check `store.injectedReducers[key] === reducer` for hot reloading when a key is the same but a reducer is different
// eslint-disable-next-line prettier/prettier
if (
Reflect.has(store.injectedReducers, key) &&
has(store.injectedReducers, key) &&
store.injectedReducers[key] === reducer
)
return;
Expand Down
58 changes: 15 additions & 43 deletions app/utils/sagaInjectors.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,53 @@
import invariant from 'invariant';
import { isEmpty, isFunction, isString, conformsTo } from 'lodash';
import { isEmpty, isFunction, isString, conformsTo, has } from 'lodash';

import checkStore from './checkStore';
import { DAEMON, ONCE_TILL_UNMOUNT, RESTART_ON_REMOUNT } from './constants';

const allowedModes = [RESTART_ON_REMOUNT, DAEMON, ONCE_TILL_UNMOUNT];

const checkKey = key =>
invariant(
isString(key) && !isEmpty(key),
'(app/utils...) injectSaga: Expected `key` to be a non empty string',
);
invariant(isString(key) && !isEmpty(key), '(app/utils...) injectSaga: Expected `key` to be a non empty string');

const checkDescriptor = descriptor => {
const shape = {
saga: isFunction,
mode: mode => isString(mode) && allowedModes.includes(mode),
};
invariant(
conformsTo(descriptor, shape),
'(app/utils...) injectSaga: Expected a valid saga descriptor',
);
invariant(conformsTo(descriptor, shape), '(app/utils...) injectSaga: Expected a valid saga descriptor');
};

export function injectSagaFactory(store, isValid) {
return function injectSaga(key, descriptor = {}, args) {
if (!isValid) checkStore(store);
if (!isValid) {
checkStore(store);
}

const newDescriptor = {
...descriptor,
mode: descriptor.mode || DAEMON,
};
const { saga, mode } = newDescriptor;
const newDescriptor = { ...descriptor, mode: descriptor.mode || DAEMON };
const { saga } = newDescriptor;

checkKey(key);
checkDescriptor(newDescriptor);

let hasSaga = Reflect.has(store.injectedSagas, key);
let hasSaga = has(store.injectedSagas, key);

if (process.env.NODE_ENV !== 'production') {
const oldDescriptor = store.injectedSagas[key];
// enable hot reloading of daemon and once-till-unmount sagas
if (hasSaga && oldDescriptor.saga !== saga) {
oldDescriptor.task.cancel();
hasSaga = false;
}
}

if (
!hasSaga ||
(hasSaga && mode !== DAEMON && mode !== ONCE_TILL_UNMOUNT)
) {
/* eslint-disable no-param-reassign */
store.injectedSagas[key] = {
...newDescriptor,
task: store.runSaga(saga, args),
};
/* eslint-enable no-param-reassign */
if (!hasSaga) {
store.injectedSagas[key] = { ...newDescriptor, task: store.runSaga(saga, args) }; // eslint-disable-line no-param-reassign
}
};
}

export function ejectSagaFactory(store, isValid) {
return function ejectSaga(key) {
if (!isValid) checkStore(store);
if (!isValid) {
checkStore(store);
}

checkKey(key);

if (Reflect.has(store.injectedSagas, key)) {
if (has(store.injectedSagas, key)) {
const descriptor = store.injectedSagas[key];
if (descriptor.mode && descriptor.mode !== DAEMON) {
descriptor.task.cancel();
// Clean up in production; in development we need `descriptor.saga` for hot reloading
if (process.env.NODE_ENV === 'production') {
// Need some value to be able to detect `ONCE_TILL_UNMOUNT` sagas in `injectSaga`
store.injectedSagas[key] = 'done'; // eslint-disable-line no-param-reassign
}
}
}
};
Expand Down

0 comments on commit 5a67f61

Please sign in to comment.