summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/utils/sagaInjectors.js
diff options
context:
space:
mode:
authorgabrhr <[email protected]>2022-04-20 10:19:29 -0500
committergabrhr <[email protected]>2022-04-20 10:19:29 -0500
commite13e630cd6e4fc0b1ff92098a28a770794c7bb9a (patch)
treee68ad2f947d1b3ec454529b35f37ca2f223e5431 /front/odiparpack/app/utils/sagaInjectors.js
parent457816ac1129fcc6019d2fc795b6693ee6776d59 (diff)
downloadDP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.tar.gz
DP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.tar.bz2
DP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.zip
AƱadir plantilla
Base para front
Diffstat (limited to 'front/odiparpack/app/utils/sagaInjectors.js')
-rw-r--r--front/odiparpack/app/utils/sagaInjectors.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/front/odiparpack/app/utils/sagaInjectors.js b/front/odiparpack/app/utils/sagaInjectors.js
new file mode 100644
index 0000000..edb4626
--- /dev/null
+++ b/front/odiparpack/app/utils/sagaInjectors.js
@@ -0,0 +1,92 @@
+import invariant from 'invariant';
+import {
+ isEmpty, isFunction, isString, conformsTo
+} 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',
+);
+
+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',
+ );
+};
+
+export function injectSagaFactory(store, isValid) {
+ return function injectSaga(key, descriptor = {}, args) {
+ if (!isValid) checkStore(store);
+
+ const newDescriptor = {
+ ...descriptor,
+ mode: descriptor.mode || DAEMON,
+ };
+ const { saga, mode } = newDescriptor;
+
+ checkKey(key);
+ checkDescriptor(newDescriptor);
+
+ let hasSaga = Reflect.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 */
+ }
+ };
+}
+
+export function ejectSagaFactory(store, isValid) {
+ return function ejectSaga(key) {
+ if (!isValid) checkStore(store);
+
+ checkKey(key);
+
+ if (Reflect.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
+ }
+ }
+ }
+ };
+}
+
+export default function getInjectors(store) {
+ checkStore(store);
+
+ return {
+ injectSaga: injectSagaFactory(store, true),
+ ejectSaga: ejectSagaFactory(store, true),
+ };
+}